3 #define _INCLUDE_POSIX_SOURCE /* for HP-UX */
4 #define _INCLUDE_XOPEN_SOURCE /* for HP-UX */
10 extern char *malloc();
18 #include "fmt.h" /* for struct syl */
20 /*global definitions*/
21 unit f__units[MXUNIT]; /*unit table*/
22 int f__init; /*bit 0: set after initializations;
23 bit 1: set during I/O involving returns to
24 caller of library (or calls to user code)*/
25 cilist *f__elist; /*active external io list*/
26 icilist *f__svic; /*active internal io list*/
27 flag f__reading; /*1 if reading, 0 if writing*/
28 flag f__cplus,f__cblank;
31 flag f__external; /*1 if external io, 0 if internal */
33 int (*f__doed)(),(*f__doned)();
34 int (*f__doend)(),(*f__donewrec)(),(*f__dorevert)();
35 int (*f__getn)(); /* for formatted input */
36 void (*f__putn)(); /* for formatted output */
38 int (*f__getn)(void); /* for formatted input */
39 void (*f__putn)(int); /* for formatted output */
40 int (*f__doed)(struct syl*, char*, ftnlen),(*f__doned)(struct syl*);
41 int (*f__dorevert)(void),(*f__donewrec)(void),(*f__doend)(void);
43 flag f__sequential; /*1 if sequential io, 0 if direct*/
44 flag f__formatted; /*1 if formatted io, 0 if unformatted*/
45 FILE *f__cf; /*current file*/
46 unit *f__curunit; /*current unit*/
47 int f__recpos; /*place in current record*/
48 int f__cursor, f__hiwater, f__scale;
54 "error in format", /* 100 */
55 "illegal unit number", /* 101 */
56 "formatted io not allowed", /* 102 */
57 "unformatted io not allowed", /* 103 */
58 "direct io not allowed", /* 104 */
59 "sequential io not allowed", /* 105 */
60 "can't backspace file", /* 106 */
61 "null file name", /* 107 */
62 "can't stat file", /* 108 */
63 "unit not connected", /* 109 */
64 "off end of record", /* 110 */
65 "truncation failed in endfile", /* 111 */
66 "incomprehensible list input", /* 112 */
67 "out of free space", /* 113 */
68 "unit not connected", /* 114 */
69 "read unexpected character", /* 115 */
70 "bad logical input field", /* 116 */
71 "bad variable type", /* 117 */
72 "bad namelist name", /* 118 */
73 "variable not in namelist", /* 119 */
74 "no end record", /* 120 */
75 "variable count incorrect", /* 121 */
76 "subscript for scalar variable", /* 122 */
77 "invalid array section", /* 123 */
78 "substring out of bounds", /* 124 */
79 "subscript out of bounds", /* 125 */
80 "can't read file", /* 126 */
81 "can't write file", /* 127 */
82 "'new' file exists", /* 128 */
83 "can't append to file", /* 129 */
84 "non-positive record number", /* 130 */
85 "I/O started while already doing I/O", /* 131 */
86 "Temporary file name (TMPDIR?) too long" /* 132 */
88 #define MAXERR (sizeof(F_err)/sizeof(char *)+100)
91 f__canseek(f) FILE *f; /*SYSDEP*/
93 f__canseek(FILE *f) /*SYSDEP*/
97 return !isatty(fileno(f));
101 if (fstat(fileno(f),&x) < 0)
104 switch(x.st_mode & S_IFMT) {
107 if(x.st_nlink > 0) /* !pipe */
112 if(isatty(fileno(f)))
123 if (S_ISREG(x.st_mode) || S_ISDIR(x.st_mode)) {
124 if(x.st_nlink > 0) /* !pipe */
129 if (S_ISCHR(x.st_mode)) {
130 if(isatty(fileno(f)))
134 if (S_ISBLK(x.st_mode))
137 Help! How does fstat work on this system?
140 return(0); /* who knows what it is? */
146 f__fatal(n,s) char *s;
148 f__fatal(int n, char *s)
153 if(n<100 && n>=0) perror(s); /*SYSDEP*/
154 else if(n >= (int)MAXERR || n < -1)
155 { fprintf(stderr,"%s: illegal error number %d\n",s,n);
157 else if(n == -1) fprintf(stderr,"%s: end of file\n",s);
159 fprintf(stderr,"%s: %s\n",s,F_err[n-100]);
161 fprintf (stderr, "(libf2c f__fatal already called, aborting.)");
167 fprintf(stderr,"apparent state: unit %d ",
168 (int)(f__curunit-f__units));
169 fprintf(stderr, f__curunit->ufnm ? "named %s\n" : "(unnamed)\n",
173 fprintf(stderr,"apparent state: internal I/O\n");
175 fprintf(stderr,"last format: %.*s\n",f__fmtlen,f__fmtbuf);
176 fprintf(stderr,"lately %s %s %s %s",f__reading?"reading":"writing",
177 f__sequential?"sequential":"direct",f__formatted?"formatted":"unformatted",
178 f__external?"external":"internal");
180 f__init &= ~2; /* No longer doing I/O (no more user code to be called). */
183 /*initialization routine*/
189 f__fatal (131, "I/O recursion");
193 p->useek=f__canseek(stderr);
198 p->useek=f__canseek(stdin);
203 p->useek=f__canseek(stdout);
208 f__nowreading(x) unit *x;
210 f__nowreading(unit *x)
215 extern char *f__r_mode[], *f__w_mode[];
221 ufmt = x->url ? 0 : x->ufmt;
224 if (!freopen(x->ufnm, f__w_mode[ufmt|2], x->ufd)) {
226 if(!freopen(x->ufnm, f__r_mode[ufmt], x->ufd)) {
232 FSEEK(x->ufd,loc,SEEK_SET);
239 f__nowwriting(x) unit *x;
241 f__nowwriting(unit *x)
246 extern char *f__w_mode[];
252 ufmt = x->url ? 0 : x->ufmt;
253 if (x->uwrt == 3) { /* just did write, rewind */
254 if (!(f__cf = x->ufd =
255 freopen(x->ufnm,f__w_mode[ufmt],x->ufd)))
261 if (!(f__cf = x->ufd =
262 freopen(x->ufnm, f__w_mode[ufmt |= 2], x->ufd)))
270 FSEEK(x->ufd,loc,SEEK_SET);
279 err__fl(f, m, s) int f, m; char *s;
281 err__fl(int f, int m, char *s)