2 #define _INCLUDE_POSIX_SOURCE /* for HP-UX */
3 #define _INCLUDE_XOPEN_SOURCE /* for HP-UX */
17 #include "fmt.h" /* for struct syl */
19 /*global definitions*/
20 unit f__units[MXUNIT]; /*unit table*/
21 int f__init; /*bit 0: set after initializations;
22 bit 1: set during I/O involving returns to
23 caller of library (or calls to user code)*/
24 cilist *f__elist; /*active external io list*/
25 icilist *f__svic; /*active internal io list*/
26 flag f__reading; /*1 if reading, 0 if writing*/
27 flag f__cplus,f__cblank;
30 flag f__external; /*1 if external io, 0 if internal */
32 int (*f__doed)(),(*f__doned)();
33 int (*f__doend)(),(*f__donewrec)(),(*f__dorevert)();
34 int (*f__getn)(); /* for formatted input */
35 void (*f__putn)(); /* for formatted output */
37 int (*f__getn)(void); /* for formatted input */
38 void (*f__putn)(int); /* for formatted output */
39 int (*f__doed)(struct syl*, char*, ftnlen),(*f__doned)(struct syl*);
40 int (*f__dorevert)(void),(*f__donewrec)(void),(*f__doend)(void);
42 flag f__sequential; /*1 if sequential io, 0 if direct*/
43 flag f__formatted; /*1 if formatted io, 0 if unformatted*/
44 FILE *f__cf; /*current file*/
45 unit *f__curunit; /*current unit*/
46 int f__recpos; /*place in current record*/
47 int f__cursor, f__hiwater, f__scale;
53 "error in format", /* 100 */
54 "illegal unit number", /* 101 */
55 "formatted io not allowed", /* 102 */
56 "unformatted io not allowed", /* 103 */
57 "direct io not allowed", /* 104 */
58 "sequential io not allowed", /* 105 */
59 "can't backspace file", /* 106 */
60 "null file name", /* 107 */
61 "can't stat file", /* 108 */
62 "unit not connected", /* 109 */
63 "off end of record", /* 110 */
64 "truncation failed in endfile", /* 111 */
65 "incomprehensible list input", /* 112 */
66 "out of free space", /* 113 */
67 "unit not connected", /* 114 */
68 "read unexpected character", /* 115 */
69 "bad logical input field", /* 116 */
70 "bad variable type", /* 117 */
71 "bad namelist name", /* 118 */
72 "variable not in namelist", /* 119 */
73 "no end record", /* 120 */
74 "variable count incorrect", /* 121 */
75 "subscript for scalar variable", /* 122 */
76 "invalid array section", /* 123 */
77 "substring out of bounds", /* 124 */
78 "subscript out of bounds", /* 125 */
79 "can't read file", /* 126 */
80 "can't write file", /* 127 */
81 "'new' file exists", /* 128 */
82 "can't append to file", /* 129 */
83 "non-positive record number", /* 130 */
84 "I/O started while already doing I/O", /* 131 */
85 "Temporary file name (TMPDIR?) too long" /* 132 */
87 #define MAXERR (sizeof(F_err)/sizeof(char *)+100)
90 f__canseek(f) FILE *f; /*SYSDEP*/
92 f__canseek(FILE *f) /*SYSDEP*/
96 return !isatty(fileno(f));
100 if (fstat(fileno(f),&x) < 0)
103 switch(x.st_mode & S_IFMT) {
106 if(x.st_nlink > 0) /* !pipe */
111 if(isatty(fileno(f)))
122 if (S_ISREG(x.st_mode) || S_ISDIR(x.st_mode)) {
123 if(x.st_nlink > 0) /* !pipe */
128 if (S_ISCHR(x.st_mode)) {
129 if(isatty(fileno(f)))
133 if (S_ISBLK(x.st_mode))
136 Help! How does fstat work on this system?
139 return(0); /* who knows what it is? */
145 f__fatal(n,s) char *s;
147 f__fatal(int n, char *s)
152 if(n<100 && n>=0) perror(s); /*SYSDEP*/
153 else if(n >= (int)MAXERR || n < -1)
154 { fprintf(stderr,"%s: illegal error number %d\n",s,n);
156 else if(n == -1) fprintf(stderr,"%s: end of file\n",s);
158 fprintf(stderr,"%s: %s\n",s,F_err[n-100]);
160 fprintf (stderr, "(libf2c f__fatal already called, aborting.)");
166 fprintf(stderr,"apparent state: unit %d ",
167 (int)(f__curunit-f__units));
168 fprintf(stderr, f__curunit->ufnm ? "named %s\n" : "(unnamed)\n",
172 fprintf(stderr,"apparent state: internal I/O\n");
174 fprintf(stderr,"last format: %.*s\n",f__fmtlen,f__fmtbuf);
175 fprintf(stderr,"lately %s %s %s %s",f__reading?"reading":"writing",
176 f__sequential?"sequential":"direct",f__formatted?"formatted":"unformatted",
177 f__external?"external":"internal");
179 f__init &= ~2; /* No longer doing I/O (no more user code to be called). */
182 /*initialization routine*/
188 f__fatal (131, "I/O recursion");
192 p->useek=f__canseek(stderr);
197 p->useek=f__canseek(stdin);
202 p->useek=f__canseek(stdout);
207 f__nowreading(x) unit *x;
209 f__nowreading(unit *x)
214 extern char *f__r_mode[], *f__w_mode[];
220 ufmt = x->url ? 0 : x->ufmt;
223 if (!freopen(x->ufnm, f__w_mode[ufmt|2], x->ufd)) {
225 if(!freopen(x->ufnm, f__r_mode[ufmt], x->ufd)) {
231 fseek(x->ufd,loc,SEEK_SET);
238 f__nowwriting(x) unit *x;
240 f__nowwriting(unit *x)
245 extern char *f__w_mode[];
251 ufmt = x->url ? 0 : x->ufmt;
252 if (x->uwrt == 3) { /* just did write, rewind */
253 if (!(f__cf = x->ufd =
254 freopen(x->ufnm,f__w_mode[ufmt],x->ufd)))
260 if (!(f__cf = x->ufd =
261 freopen(x->ufnm, f__w_mode[ufmt |= 2], x->ufd)))
269 fseek(x->ufd,loc,SEEK_SET);
278 err__fl(f, m, s) int f, m; char *s;
280 err__fl(int f, int m, char *s)