err.c 6.3 KB

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