home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / f2c / i77lib / err.c < prev    next >
C/C++ Source or Header  |  1998-10-28  |  7KB  |  295 lines

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