Commit | Line | Data |
---|---|---|
bae7117f WH |
1 | #ifndef NON_UNIX_STDIO |
2 | #include "sys/types.h" | |
3 | #include "sys/stat.h" | |
4 | #endif | |
5 | #include "f2c.h" | |
6 | #include "fio.h" | |
7 | #include "fmt.h" /* for struct syl */ | |
8 | #include "rawio.h" /* for fcntl.h, fdopen */ | |
fd3fa61c | 9 | #include "local.h" |
bae7117f WH |
10 | #ifdef NON_UNIX_STDIO |
11 | #ifdef KR_headers | |
12 | extern char *malloc(); | |
13 | #else | |
14 | #undef abs | |
15 | #undef min | |
16 | #undef max | |
17 | #include "stdlib.h" | |
18 | #endif | |
19 | #endif | |
20 | ||
21 | /*global definitions*/ | |
22 | unit f__units[MXUNIT]; /*unit table*/ | |
23 | flag f__init; /*0 on entry, 1 after initializations*/ | |
24 | cilist *f__elist; /*active external io list*/ | |
25 | flag f__reading; /*1 if reading, 0 if writing*/ | |
26 | flag f__cplus,f__cblank; | |
27 | char *f__fmtbuf; | |
28 | flag f__external; /*1 if external io, 0 if internal */ | |
29 | #ifdef KR_headers | |
30 | int (*f__doed)(),(*f__doned)(); | |
31 | int (*f__doend)(),(*f__donewrec)(),(*f__dorevert)(); | |
32 | int (*f__getn)(),(*f__putn)(); /*for formatted io*/ | |
33 | #else | |
34 | int (*f__getn)(void),(*f__putn)(int); /*for formatted io*/ | |
35 | int (*f__doed)(struct syl*, char*, ftnlen),(*f__doned)(struct syl*); | |
36 | int (*f__dorevert)(void),(*f__donewrec)(void),(*f__doend)(void); | |
37 | #endif | |
38 | flag f__sequential; /*1 if sequential io, 0 if direct*/ | |
39 | flag f__formatted; /*1 if formatted io, 0 if unformatted*/ | |
40 | FILE *f__cf; /*current file*/ | |
41 | unit *f__curunit; /*current unit*/ | |
42 | int f__recpos; /*place in current record*/ | |
43 | int f__cursor,f__scale; | |
44 | ||
45 | /*error messages*/ | |
46 | char *F_err[] = | |
47 | { | |
48 | "error in format", /* 100 */ | |
49 | "illegal unit number", /* 101 */ | |
50 | "formatted io not allowed", /* 102 */ | |
51 | "unformatted io not allowed", /* 103 */ | |
52 | "direct io not allowed", /* 104 */ | |
53 | "sequential io not allowed", /* 105 */ | |
54 | "can't backspace file", /* 106 */ | |
55 | "null file name", /* 107 */ | |
56 | "can't stat file", /* 108 */ | |
57 | "unit not connected", /* 109 */ | |
58 | "off end of record", /* 110 */ | |
59 | "truncation failed in endfile", /* 111 */ | |
60 | "incomprehensible list input", /* 112 */ | |
61 | "out of free space", /* 113 */ | |
62 | "unit not connected", /* 114 */ | |
63 | "read unexpected character", /* 115 */ | |
64 | "bad logical input field", /* 116 */ | |
65 | "bad variable type", /* 117 */ | |
66 | "bad namelist name", /* 118 */ | |
67 | "variable not in namelist", /* 119 */ | |
68 | "no end record", /* 120 */ | |
69 | "variable count incorrect", /* 121 */ | |
70 | "subscript for scalar variable", /* 122 */ | |
71 | "invalid array section", /* 123 */ | |
72 | "substring out of bounds", /* 124 */ | |
73 | "subscript out of bounds", /* 125 */ | |
74 | "can't read file", /* 126 */ | |
75 | "can't write file", /* 127 */ | |
76 | "'new' file exists", /* 128 */ | |
77 | "can't append to file" /* 129 */ | |
78 | }; | |
79 | #define MAXERR (sizeof(F_err)/sizeof(char *)+100) | |
80 | ||
81 | #ifdef KR_headers | |
82 | f__canseek(f) FILE *f; /*SYSDEP*/ | |
83 | #else | |
84 | f__canseek(FILE *f) /*SYSDEP*/ | |
85 | #endif | |
86 | { | |
87 | #ifdef NON_UNIX_STDIO | |
88 | return !isatty(fileno(f)); | |
89 | #else | |
90 | struct stat x; | |
91 | ||
92 | if (fstat(fileno(f),&x) < 0) | |
93 | return(0); | |
94 | #ifdef S_IFMT | |
95 | switch(x.st_mode & S_IFMT) { | |
96 | case S_IFDIR: | |
97 | case S_IFREG: | |
98 | if(x.st_nlink > 0) /* !pipe */ | |
99 | return(1); | |
100 | else | |
101 | return(0); | |
102 | case S_IFCHR: | |
103 | if(isatty(fileno(f))) | |
104 | return(0); | |
105 | return(1); | |
106 | #ifdef S_IFBLK | |
107 | case S_IFBLK: | |
108 | return(1); | |
109 | #endif | |
110 | } | |
111 | #else | |
112 | #ifdef S_ISDIR | |
113 | /* POSIX version */ | |
114 | if (S_ISREG(x.st_mode) || S_ISDIR(x.st_mode)) { | |
115 | if(x.st_nlink > 0) /* !pipe */ | |
116 | return(1); | |
117 | else | |
118 | return(0); | |
119 | } | |
120 | if (S_ISCHR(x.st_mode)) { | |
121 | if(isatty(fileno(f))) | |
122 | return(0); | |
123 | return(1); | |
124 | } | |
125 | if (S_ISBLK(x.st_mode)) | |
126 | return(1); | |
127 | #else | |
128 | Help! How does fstat work on this system? | |
129 | #endif | |
130 | #endif | |
131 | return(0); /* who knows what it is? */ | |
132 | #endif | |
133 | } | |
134 | ||
135 | void | |
136 | #ifdef KR_headers | |
137 | f__fatal(n,s) char *s; | |
138 | #else | |
139 | f__fatal(int n, char *s) | |
140 | #endif | |
141 | { | |
142 | if(n<100 && n>=0) perror(s); /*SYSDEP*/ | |
143 | else if(n >= (int)MAXERR || n < -1) | |
144 | { fprintf(stderr,"%s: illegal error number %d\n",s,n); | |
145 | } | |
146 | else if(n == -1) fprintf(stderr,"%s: end of file\n",s); | |
147 | else | |
148 | fprintf(stderr,"%s: %s\n",s,F_err[n-100]); | |
149 | if (f__curunit) { | |
150 | fprintf(stderr,"apparent state: unit %d ",f__curunit-f__units); | |
151 | fprintf(stderr, f__curunit->ufnm ? "named %s\n" : "(unnamed)\n", | |
152 | f__curunit->ufnm); | |
153 | } | |
154 | else | |
155 | fprintf(stderr,"apparent state: internal I/O\n"); | |
156 | if (f__fmtbuf) | |
157 | fprintf(stderr,"last format: %s\n",f__fmtbuf); | |
158 | fprintf(stderr,"lately %s %s %s %s",f__reading?"reading":"writing", | |
159 | f__sequential?"sequential":"direct",f__formatted?"formatted":"unformatted", | |
160 | f__external?"external":"internal"); | |
161 | sig_die(" IO", 1); | |
162 | } | |
163 | /*initialization routine*/ | |
164 | VOID | |
165 | f_init(Void) | |
166 | { unit *p; | |
167 | ||
168 | f__init=1; | |
169 | p= &f__units[0]; | |
170 | p->ufd=stderr; | |
171 | p->useek=f__canseek(stderr); | |
172 | #ifdef NON_UNIX_STDIO | |
173 | setbuf(stderr, (char *)malloc(BUFSIZ)); | |
174 | #else | |
175 | stderr->_flag &= ~_IONBF; | |
176 | #endif | |
177 | p->ufmt=1; | |
178 | p->uwrt=1; | |
179 | p = &f__units[5]; | |
180 | p->ufd=stdin; | |
181 | p->useek=f__canseek(stdin); | |
182 | p->ufmt=1; | |
183 | p->uwrt=0; | |
184 | p= &f__units[6]; | |
185 | p->ufd=stdout; | |
186 | p->useek=f__canseek(stdout); | |
187 | /* IOLBUF and setvbuf only in system 5+ */ | |
188 | #ifdef COMMENTED_OUT | |
189 | if(isatty(fileno(stdout))) { | |
190 | extern char _sobuf[]; | |
191 | setbuf(stdout, _sobuf); | |
192 | /* setvbuf(stdout, _IOLBF, 0, 0); /* the buf arg in setvbuf? */ | |
193 | p->useek = 1; /* only within a record no bigger than BUFSIZ */ | |
194 | } | |
195 | #endif | |
196 | p->ufmt=1; | |
197 | p->uwrt=1; | |
198 | } | |
199 | #ifdef KR_headers | |
200 | f__nowreading(x) unit *x; | |
201 | #else | |
202 | f__nowreading(unit *x) | |
203 | #endif | |
204 | { | |
205 | long loc; | |
206 | extern char *f__r_mode[]; | |
207 | if (!x->ufnm) | |
208 | goto cantread; | |
209 | loc=ftell(x->ufd); | |
210 | if(freopen(x->ufnm,f__r_mode[x->ufmt],x->ufd) == NULL) { | |
211 | cantread: | |
212 | errno = 126; | |
213 | return(1); | |
214 | } | |
215 | x->uwrt=0; | |
216 | (void) fseek(x->ufd,loc,SEEK_SET); | |
217 | return(0); | |
218 | } | |
219 | #ifdef KR_headers | |
220 | f__nowwriting(x) unit *x; | |
221 | #else | |
222 | f__nowwriting(unit *x) | |
223 | #endif | |
224 | { | |
225 | long loc; | |
226 | int k; | |
227 | extern char *f__w_mode[]; | |
228 | ||
229 | if (!x->ufnm) | |
230 | goto cantwrite; | |
231 | if (x->uwrt == 3) { /* just did write, rewind */ | |
232 | #ifdef NON_UNIX_STDIO | |
233 | if (!(f__cf = x->ufd = | |
234 | freopen(x->ufnm,f__w_mode[x->ufmt],x->ufd))) | |
235 | #else | |
236 | if (close(creat(x->ufnm,0666))) | |
237 | #endif | |
238 | goto cantwrite; | |
239 | } | |
240 | else { | |
241 | loc=ftell(x->ufd); | |
242 | #ifdef NON_UNIX_STDIO | |
243 | if (!(f__cf = x->ufd = | |
244 | freopen(x->ufnm, f__w_mode[x->ufmt+2], x->ufd))) | |
245 | #else | |
246 | if (fclose(x->ufd) < 0 | |
247 | || (k = x->uwrt == 2 ? creat(x->ufnm,0666) | |
248 | : open(x->ufnm,O_WRONLY)) < 0 | |
249 | || (f__cf = x->ufd = fdopen(k,f__w_mode[x->ufmt])) == NULL) | |
250 | #endif | |
251 | { | |
252 | x->ufd = NULL; | |
253 | cantwrite: | |
254 | errno = 127; | |
255 | return(1); | |
256 | } | |
257 | (void) fseek(x->ufd,loc,SEEK_SET); | |
258 | } | |
259 | x->uwrt = 1; | |
260 | return(0); | |
261 | } | |
262 | ||
263 | int | |
264 | #ifdef KR_headers | |
265 | err__fl(f, m, s) int f, m; char *s; | |
266 | #else | |
267 | err__fl(int f, int m, char *s) | |
268 | #endif | |
269 | { | |
270 | if (!f) | |
271 | f__fatal(m, s); | |
272 | if (f__doend) | |
273 | (*f__doend)(); | |
274 | return errno = m; | |
275 | } |