home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / f2c / libi77 / err.c < prev    next >
C/C++ Source or Header  |  2000-01-15  |  7KB  |  275 lines

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