12 extern FILE *fdopen();
14 /*global definitions*/
15 unit units[MXUNIT]; /*unit table*/
16 flag init; /*0 on entry, 1 after initializations*/
17 cilist *elist; /*active external io list*/
18 flag reading; /*1 if reading, 0 if writing*/
21 flag external; /*1 if external io, 0 if internal */
22 int (*doed)(),(*doned)();
23 int (*doend)(),(*donewrec)(),(*dorevert)();
24 flag sequential; /*1 if sequential io, 0 if direct*/
25 flag formatted; /*1 if formatted io, 0 if unformatted*/
26 int (*getn)(),(*putn)(); /*for formatted io*/
27 FILE *cf; /*current file*/
28 unit *curunit; /*current unit*/
29 int recpos; /*place in current record*/
35 "error in format", /* 100 */
36 "illegal unit number", /* 101 */
37 "formatted io not allowed", /* 102 */
38 "unformatted io not allowed", /* 103 */
39 "direct io not allowed", /* 104 */
40 "sequential io not allowed", /* 105 */
41 "can't backspace file", /* 106 */
42 "null file name", /* 107 */
43 "can't stat file", /* 108 */
44 "unit not connected", /* 109 */
45 "off end of record", /* 110 */
46 "truncation failed in endfile", /* 111 */
47 "incomprehensible list input", /* 112 */
48 "out of free space", /* 113 */
49 "unit not connected", /* 114 */
50 "read unexpected character", /* 115 */
51 "bad logical input field", /* 116 */
52 "bad variable type", /* 117 */
53 "bad namelist name", /* 118 */
54 "variable not in namelist", /* 119 */
55 "no end record", /* 120 */
56 "variable count incorrect", /* 121 */
57 "subscript for scalar variable", /* 122 */
58 "invalid array section", /* 123 */
59 "substring out of bounds", /* 124 */
60 "subscript out of bounds", /* 125 */
61 "can't read file", /* 126 */
62 "can't write file", /* 127 */
63 "'new' file exists" /* 128 */
65 #define MAXERR (sizeof(F_err)/sizeof(char *)+100)
68 if(n<100 && n>=0) perror(s); /*SYSDEP*/
69 else if(n >= (int)MAXERR || n < -1)
70 { fprintf(stderr,"%s: illegal error number %d\n",s,n);
72 else if(n == -1) fprintf(stderr,"%s: end of file\n",s);
74 fprintf(stderr,"%s: %s\n",s,F_err[n-100]);
76 fprintf(stderr,"apparent state: unit %d ",curunit-units);
77 fprintf(stderr, curunit->ufnm ? "named %s\n" : "(unnamed)\n",
81 fprintf(stderr,"apparent state: internal I/O\n");
83 fprintf(stderr,"last format: %s\n",fmtbuf);
84 fprintf(stderr,"lately %s %s %s %s",reading?"reading":"writing",
85 sequential?"sequential":"direct",formatted?"formatted":"unformatted",
86 external?"external":"internal");
89 /*initialization routine*/
96 p->useek=canseek(stderr);
98 if(isatty(fileno(stderr))) {
99 extern char *malloc();
100 setbuf(stderr, malloc(BUFSIZ));
101 /* setvbuf(stderr, _IOLBF, 0, 0); */
102 } /* wastes space, but win for debugging in windows */
104 #ifdef NON_UNIX_STDIO
105 {extern char *malloc(); setbuf(stderr, malloc(BUFSIZ));}
107 stderr->_flag &= ~_IONBF;
113 p->useek=canseek(stdin);
118 p->useek=canseek(stdout);
119 /* IOLBUF and setvbuf only in system 5+ */
121 if(isatty(fileno(stdout))) {
122 extern char _sobuf[];
123 setbuf(stdout, _sobuf);
124 /* setvbuf(stdout, _IOLBF, 0, 0); /* the buf arg in setvbuf? */
125 p->useek = 1; /* only within a record no bigger than BUFSIZ */
131 canseek(f) FILE *f; /*SYSDEP*/
134 return !isatty(fileno(f));
138 if (fstat(fileno(f),&x) < 0)
141 switch(x.st_mode & S_IFMT) {
144 if(x.st_nlink > 0) /* !pipe */
149 if(isatty(fileno(f)))
160 if (S_ISREG(x.st_mode) || S_ISDIR(x.st_mode)) {
161 if(x.st_nlink > 0) /* !pipe */
166 if (S_ISCHR(x.st_mode)) {
167 if(isatty(fileno(f)))
171 if (S_ISBLK(x.st_mode))
174 Help! How does fstat work on this system?
177 return(0); /* who knows what it is? */
180 nowreading(x) unit *x;
183 extern char *r_mode[];
187 if(freopen(x->ufnm,r_mode[x->ufmt],x->ufd) == NULL) {
193 (void) fseek(x->ufd,loc,SEEK_SET);
196 nowwriting(x) unit *x;
200 extern char *w_mode[];
204 if (x->uwrt == 3) { /* just did write, rewind */
205 if (close(creat(x->ufnm,0666)))
210 if (fclose(x->ufd) < 0
211 || (k = x->uwrt == 2 ? creat(x->ufnm,0666)
212 : open(x->ufnm,O_WRONLY)) < 0
213 || (cf = x->ufd = fdopen(k,w_mode[x->ufmt])) == NULL) {
219 (void) fseek(x->ufd,loc,SEEK_SET);