home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / f2c / i77lib / open.c < prev    next >
C/C++ Source or Header  |  1998-08-16  |  6KB  |  301 lines

  1. #include "sys/types.h"
  2. #include "sys/stat.h"
  3. #include "f2c.h"
  4. #include "fio.h"
  5. #include "string.h"
  6. #include "fcntl.h"
  7. #include "rawio.h"
  8. #ifndef O_WRONLY
  9. #define O_RDONLY 0
  10. #define O_WRONLY 1
  11. #endif
  12. #ifdef _AMIGA
  13. #include <dos/dos.h>
  14. #include <dos/dosextens.h>
  15. #include <proto/dos.h>
  16. #endif
  17.  
  18. #ifdef KR_headers
  19. extern char *malloc(), *mktemp();
  20. extern FILE *fdopen();
  21. extern integer f_clos();
  22. #else
  23. #undef abs
  24. #undef min
  25. #undef max
  26. #include "stdlib.h"
  27. extern int f__canseek(FILE*);
  28. extern integer f_clos(cllist*);
  29. #endif
  30.  
  31. #ifdef NON_ANSI_RW_MODES
  32. char *r_mode[2] = {"r", "r"};
  33. char *w_mode[2] = {"w", "w"};
  34. #else
  35. char *r_mode[2] = {"rb", "r"};
  36. char *w_mode[2] = {"wb", "w"};
  37. #endif
  38.  
  39. #ifdef KR_headers
  40. f__isdev(s) char *s;
  41. #else
  42. f__isdev(char *s)
  43. #endif
  44. {
  45. #ifdef MSDOS
  46.     int i, j;
  47.  
  48.     i = open(s,O_RDONLY);
  49.     if (i == -1)
  50.         return 0;
  51.     j = isatty(i);
  52.     close(i);
  53.     return j;
  54. #else
  55. #ifdef _AMIGA
  56. /*
  57.  *    For the Amiga, try to get the DeviceProc.  If it succeeds
  58.  *      and the dvp->Lock is 0, then it is a device.
  59.  */
  60.     BPTR devlock;
  61.     struct DevProc *devproc;
  62.  
  63.     devproc = GetDeviceProc(s, NULL);
  64.     if(devproc == NULL)
  65.         return(0);
  66.     else {
  67.         devlock = devproc->dvp_Lock;
  68.         FreeDeviceProc(devproc);
  69.         return(devlock == 0);
  70.     }
  71. #else /* AMIGA */
  72.     struct stat x;
  73.  
  74.     if(stat(s, &x) == -1) return(0);
  75. #ifdef S_IFMT
  76.     switch(x.st_mode&S_IFMT) {
  77.         case S_IFREG:
  78.         case S_IFDIR:
  79.             return(0);
  80.         }
  81. #else /* S_IFMT */
  82. #ifdef S_ISREG
  83.     /* POSIX version */
  84.     if(S_ISREG(x.st_mode) || S_ISDIR(x.st_mode))
  85.         return(0);
  86.     else
  87. #else /* S_ISREG */
  88.     Help! How does stat work on this system?
  89. #endif /* S_ISREG */
  90. #endif /* S_IFMT */
  91. #endif /* AMIGA */
  92.         return(1);
  93. #endif /* MSDOS */
  94. }
  95. #ifdef KR_headers
  96. integer f_open(a) olist *a;
  97. #else
  98. integer f_open(olist *a)
  99. #endif
  100. {    unit *b;
  101.     int n;
  102.     char buf[256];
  103.     cllist x;
  104. #ifdef _AMIGA
  105.     extern int amiga_access(char *);
  106. #endif
  107. #ifndef MSDOS
  108. #ifndef _AMIGA
  109.     struct stat stb;
  110. #endif
  111. #endif
  112.     if(a->ounit>=MXUNIT || a->ounit<0)
  113.         err(a->oerr,101,"open")
  114.     f__curunit = b = &f__units[a->ounit];
  115.     if(b->ufd) {
  116.         if(a->ofnm==0)
  117.         {
  118.         same:    if (a->oblnk)
  119.                 b->ublnk = *a->oblnk == 'z' || *a->oblnk == 'Z';
  120.             return(0);
  121.         }
  122. #ifdef MSDOS
  123.         if (b->ufnm
  124.          && strlen(b->ufnm) == a->ofnmlen
  125.          && !strncmp(b->ufnm, b->ufnm, (unsigned)a->ofnmlen))
  126.             goto same;
  127. #else /* MSDOS */
  128. #ifdef _AMIGA
  129.         if (b->ufnm
  130.          && strlen(b->ufnm) == a->ofnmlen
  131.          && !strncmp(b->ufnm, b->ufnm, (unsigned)a->ofnmlen))
  132.             goto same;
  133. #else /* AMIGA */
  134.         g_char(a->ofnm,a->ofnmlen,buf);
  135.         if (f__inode(buf,&n) == b->uinodev && n == b->udev)
  136.             goto same;
  137. #endif /* AMIGA */
  138. #endif /* MSDOS */
  139.         x.cunit=a->ounit;
  140.         x.csta=0;
  141.         x.cerr=a->oerr;
  142.         if((n=f_clos(&x))!=0) return(n);
  143.         }
  144.     b->url=a->orl;
  145.     b->ublnk = a->oblnk && (*a->oblnk == 'z' || *a->oblnk == 'Z');
  146.     if(a->ofm==0)
  147.     {    if(b->url>0) b->ufmt=0;
  148.         else b->ufmt=1;
  149.     }
  150.     else if(*a->ofm=='f' || *a->ofm == 'F') b->ufmt=1;
  151.     else b->ufmt=0;
  152. #ifdef url_Adjust
  153.     if (b->url && !b->ufmt)
  154.         url_Adjust(b->url);
  155. #endif
  156.     if (a->ofnm) {
  157.         g_char(a->ofnm,a->ofnmlen,buf);
  158.         if (!buf[0])
  159.             err(a->oerr,107,"open")
  160.         }
  161.     else
  162.         sprintf(buf, "fort.%ld", a->ounit);
  163.     b->uscrtch = 0;
  164.     switch(a->osta ? *a->osta : 'u')
  165.     {
  166.     case 'o':
  167.     case 'O':
  168. #ifdef MSDOS
  169.         if(access(buf,0))
  170. #else
  171. #ifdef _AMIGA
  172.         if(amiga_access(buf))
  173. #else /* AMIGA */
  174.         if(stat(buf,&stb))
  175. #endif /* AMIGA */
  176. #endif /* MSDOS */
  177.             err(a->oerr,errno,"open")
  178.         break;
  179.      case 's':
  180.      case 'S':
  181.         b->uscrtch=1;
  182. #ifdef _POSIX_SOURCE
  183.         tmpnam(buf);
  184. #else
  185. #ifdef _AMIGA
  186.         tmpnam(buf);
  187. #else
  188.         (void) strcpy(buf,"tmp.FXXXXXX");
  189.         (void) mktemp(buf);
  190. #endif
  191. #endif
  192.         (void) close(creat(buf, 0666));
  193.         break;
  194.     case 'n':
  195.     case 'N':
  196. #ifdef MSDOS
  197.         if(!access(buf,0))
  198. #else
  199. #ifdef _AMIGA
  200.         if(!amiga_access(buf))
  201. #else /* AMIGA */
  202.         if(!stat(buf,&stb))
  203. #endif
  204. #endif
  205.             err(a->oerr,128,"open")
  206.         /* no break */
  207.     case 'r':    /* Fortran 90 replace option */
  208.     case 'R':
  209.         (void) close(creat(buf, 0666));
  210.         break;
  211.     }
  212.  
  213.     b->ufnm=(char *) malloc((unsigned int)(strlen(buf)+1));
  214.     if(b->ufnm==NULL) err(a->oerr,113,"no space");
  215.     (void) strcpy(b->ufnm,buf);
  216.     b->uend=0;
  217.     b->uwrt = 0;
  218.     if(f__isdev(buf))
  219.     {    b->ufd = fopen(buf,r_mode[b->ufmt]);
  220.         if(b->ufd==NULL) err(a->oerr,errno,buf)
  221.     }
  222.     else {
  223.         if((b->ufd = fopen(buf, r_mode[b->ufmt])) == NULL) {
  224.             if ((n = open(buf,O_WRONLY)) >= 0) {
  225.                 b->uwrt = 2;
  226.                 }
  227.             else {
  228.                 n = creat(buf, 0666);
  229.                 b->uwrt = 1;
  230.                 }
  231.             if (n < 0
  232.             || (b->ufd = fdopen(n, w_mode[b->ufmt])) == NULL)
  233.                 err(a->oerr, errno, "open");
  234.             }
  235.     }
  236.     b->useek=f__canseek(b->ufd);
  237. #ifndef MSDOS
  238. #ifndef _AMIGA
  239.     if((b->uinode=f__inode(buf,&b->udev))==-1)
  240.         err(a->oerr,108,"open")
  241. #endif
  242. #endif
  243.     if(a->orl && b->useek) rewind(b->ufd);
  244.     return(0);
  245. }
  246. #ifdef KR_headers
  247. fk_open(seq,fmt,n) ftnint n;
  248. #else
  249. fk_open(int seq, int fmt, ftnint n)
  250. #endif
  251. {    char nbuf[10];
  252.     olist a;
  253.     (void) sprintf(nbuf,"fort.%ld",n);
  254.     a.oerr=1;
  255.     a.ounit=n;
  256.     a.ofnm=nbuf;
  257.     a.ofnmlen=strlen(nbuf);
  258.     a.osta=NULL;
  259.     a.oacc= seq==SEQ?"s":"d";
  260.     a.ofm = fmt==FMT?"f":"u";
  261.     a.orl = seq==DIR?1:0;
  262.     a.oblnk=NULL;
  263.     return(f_open(&a));
  264. }
  265.  
  266. #ifdef _AMIGA
  267. /*
  268.  *  Try to get a device process
  269.  */
  270. int amiga_access(char *fnm)
  271. {
  272.     struct DevProc *devproc;
  273.     BPTR dlock;
  274.     BPTR flk;
  275.  
  276.     devproc = GetDeviceProc(fnm, NULL);
  277.     if(devproc == NULL) {
  278.         /* Couldn't find it.  Not accessable */
  279.         return(1);
  280.     } else {
  281.         /* Could find a device */
  282.         dlock = devproc -> dvp_Lock;
  283.         FreeDeviceProc(devproc);
  284.         if(dlock == 0) {
  285.             /* File is a device and is accessible */
  286.             return(0);
  287.         } else {
  288.             /* a file on file system. try to lock it. */
  289.             if((flk=Lock(fnm, ACCESS_READ)) == 0) {
  290.                 /* File does not exist, not accessable */
  291.                 return(1);
  292.             } else {
  293.                 /* File exists and is accessable */
  294.                 UnLock(flk);
  295.                 return(0);
  296.             }
  297.         }
  298.     }
  299. }
  300. #endif
  301.