3 #define _INCLUDE_POSIX_SOURCE /* for HP-UX */
4 #define _INCLUDE_XOPEN_SOURCE /* for HP-UX */
14 #include "fmt.h" /* for struct syl */
16 /*global definitions*/
17 unit f__units
[MXUNIT
]; /*unit table */
18 int f__init
; /*bit 0: set after initializations;
19 bit 1: set during I/O involving returns to
20 caller of library (or calls to user code) */
21 cilist
*f__elist
; /*active external io list */
22 icilist
*f__svic
; /*active internal io list */
23 flag f__reading
; /*1 if reading, 0 if writing */
24 flag f__cplus
, f__cblank
;
27 flag f__external
; /*1 if external io, 0 if internal */
28 int (*f__getn
) (void); /* for formatted input */
29 void (*f__putn
) (int); /* for formatted output */
30 int (*f__doed
) (struct syl
*, char *, ftnlen
), (*f__doned
) (struct syl
*);
31 int (*f__dorevert
) (void), (*f__donewrec
) (void), (*f__doend
) (void);
32 flag f__sequential
; /*1 if sequential io, 0 if direct */
33 flag f__formatted
; /*1 if formatted io, 0 if unformatted */
34 FILE *f__cf
; /*current file */
35 unit
*f__curunit
; /*current unit */
36 int f__recpos
; /*place in current record */
37 int f__cursor
, f__hiwater
, f__scale
;
42 "error in format", /* 100 */
43 "illegal unit number", /* 101 */
44 "formatted io not allowed", /* 102 */
45 "unformatted io not allowed", /* 103 */
46 "direct io not allowed", /* 104 */
47 "sequential io not allowed", /* 105 */
48 "can't backspace file", /* 106 */
49 "null file name", /* 107 */
50 "can't stat file", /* 108 */
51 "unit not connected", /* 109 */
52 "off end of record", /* 110 */
53 "truncation failed in endfile", /* 111 */
54 "incomprehensible list input", /* 112 */
55 "out of free space", /* 113 */
56 "unit not connected", /* 114 */
57 "read unexpected character", /* 115 */
58 "bad logical input field", /* 116 */
59 "bad variable type", /* 117 */
60 "bad namelist name", /* 118 */
61 "variable not in namelist", /* 119 */
62 "no end record", /* 120 */
63 "variable count incorrect", /* 121 */
64 "subscript for scalar variable", /* 122 */
65 "invalid array section", /* 123 */
66 "substring out of bounds", /* 124 */
67 "subscript out of bounds", /* 125 */
68 "can't read file", /* 126 */
69 "can't write file", /* 127 */
70 "'new' file exists", /* 128 */
71 "can't append to file", /* 129 */
72 "non-positive record number", /* 130 */
73 "I/O started while already doing I/O", /* 131 */
74 "Temporary file name (TMPDIR?) too long" /* 132 */
76 #define MAXERR (sizeof(F_err)/sizeof(char *)+100)
79 f__canseek (FILE * f
) /*SYSDEP*/
82 return !isatty (fileno (f
));
86 if (fstat (fileno (f
), &x
) < 0)
89 switch (x
.st_mode
& S_IFMT
)
93 if (x
.st_nlink
> 0) /* !pipe */
98 if (isatty (fileno (f
)))
109 if (S_ISREG (x
.st_mode
) || S_ISDIR (x
.st_mode
))
111 if (x
.st_nlink
> 0) /* !pipe */
116 if (S_ISCHR (x
.st_mode
))
118 if (isatty (fileno (f
)))
122 if (S_ISBLK (x
.st_mode
))
125 Help
! How does fstat work on
this system
?
128 return (0); /* who knows what it is? */
133 f__fatal (int n
, char *s
)
137 if (n
< 100 && n
>= 0)
140 else if (n
>= (int) MAXERR
|| n
< -1)
142 fprintf (stderr
, "%s: illegal error number %d\n", s
, n
);
145 fprintf (stderr
, "%s: end of file\n", s
);
147 fprintf (stderr
, "%s: %s\n", s
, F_err
[n
- 100]);
150 fprintf (stderr
, "(libf2c f__fatal already called, aborting.)");
158 fprintf (stderr
, "apparent state: unit %d ",
159 (int) (f__curunit
- f__units
));
160 fprintf (stderr
, f__curunit
->ufnm
? "named %s\n" : "(unnamed)\n",
164 fprintf (stderr
, "apparent state: internal I/O\n");
166 fprintf (stderr
, "last format: %.*s\n", f__fmtlen
, f__fmtbuf
);
167 fprintf (stderr
, "lately %s %s %s %s",
168 f__reading
? "reading" : "writing",
169 f__sequential
? "sequential" : "direct",
170 f__formatted
? "formatted" : "unformatted",
171 f__external
? "external" : "internal");
173 f__init
&= ~2; /* No longer doing I/O (no more user code to be called). */
177 /*initialization routine*/
184 f__fatal (131, "I/O recursion");
188 p
->useek
= f__canseek (stderr
);
193 p
->useek
= f__canseek (stdin
);
198 p
->useek
= f__canseek (stdout
);
204 f__nowreading (unit
* x
)
208 extern char *f__r_mode
[], *f__w_mode
[];
214 ufmt
= x
->url
? 0 : x
->ufmt
;
215 loc
= FTELL (x
->ufd
);
217 if (!freopen (x
->ufnm
, f__w_mode
[ufmt
| 2], x
->ufd
))
220 if (!freopen (x
->ufnm
, f__r_mode
[ufmt
], x
->ufd
))
227 FSEEK (x
->ufd
, loc
, SEEK_SET
);
235 f__nowwriting (unit
* x
)
239 extern char *f__w_mode
[];
245 ufmt
= x
->url
? 0 : x
->ufmt
;
247 { /* just did write, rewind */
248 if (!(f__cf
= x
->ufd
= freopen (x
->ufnm
, f__w_mode
[ufmt
], x
->ufd
)))
254 loc
= FTELL (x
->ufd
);
255 if (!(f__cf
= x
->ufd
= freopen (x
->ufnm
, f__w_mode
[ufmt
|= 2], x
->ufd
)))
263 FSEEK (x
->ufd
, loc
, SEEK_SET
);
271 err__fl (int f
, int m
, char *s
)