home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / sharew / f_2_c / libi77 / open.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-11  |  3.5 KB  |  184 lines

  1. #include "sys/types.h"
  2. #ifndef MSDOS
  3. #include "sys/stat.h"
  4. #endif
  5. #include "f2c.h"
  6. #include "fio.h"
  7. #include "string.h"
  8. #include "fcntl.h"
  9. #ifndef O_WRONLY
  10. #define O_RDONLY 0
  11. #define O_WRONLY 1
  12. #endif
  13.  
  14. extern char *malloc(), *mktemp();
  15. extern FILE *fdopen();
  16. extern integer f_clos();
  17.  
  18. integer f_open(a) olist *a;
  19. {    unit *b;
  20.     int n;
  21.     char buf[256];
  22.     cllist x;
  23. #ifndef MSDOS
  24.     struct stat stb;
  25. #endif
  26.     if(a->ounit>=MXUNIT || a->ounit<0)
  27.         err(a->oerr,101,"open")
  28.     curunit = b = &units[a->ounit];
  29.     if(b->ufd) {
  30.         if(a->ofnm==0)
  31.         {
  32.         same:    if (a->oblnk)
  33.                 b->ublnk = *a->oblnk == 'z' || *a->oblnk == 'Z';
  34.             return(0);
  35.         }
  36. #ifdef MSDOS
  37.         if (b->ufnm
  38.          && strlen(b->ufnm) == a->ofnmlen
  39.          && !strncmp(b->ufnm, b->ufnm, (unsigned)a->ofnmlen))
  40.             goto same;
  41. #else
  42.         g_char(a->ofnm,a->ofnmlen,buf);
  43.         if (inode(buf,&n) == b->uinode && n == b->udev)
  44.             goto same;
  45. #endif
  46.         x.cunit=a->ounit;
  47.         x.csta=0;
  48.         x.cerr=a->oerr;
  49.         if((n=f_clos(&x))!=0) return(n);
  50.         }
  51.     b->url=a->orl;
  52.     b->ublnk = a->oblnk && (*a->oblnk == 'z' || *a->oblnk == 'Z');
  53.     if(a->ofm==0)
  54.     {    if(b->url>0) b->ufmt=0;
  55.         else b->ufmt=1;
  56.     }
  57.     else if(*a->ofm=='f' || *a->ofm == 'F') b->ufmt=1;
  58.     else b->ufmt=0;
  59. #ifdef url_Adjust
  60.     if (b->url && !b->ufmt)
  61.         url_Adjust(b->url);
  62. #endif
  63.     if (a->ofnm) {
  64.         g_char(a->ofnm,a->ofnmlen,buf);
  65.         if (!buf[0])
  66.             err(a->oerr,107,"open")
  67.         }
  68.     else
  69.         sprintf(buf, "fort.%ld", a->ounit);
  70.     b->uscrtch = 0;
  71.     switch(a->osta ? *a->osta : 'u')
  72.     {
  73.     case 'o':
  74.     case 'O':
  75. #ifdef MSDOS
  76.         if(access(buf,0))
  77. #else
  78.         if(stat(buf,&stb))
  79. #endif
  80.             err(a->oerr,errno,"open")
  81.         break;
  82.      case 's':
  83.      case 'S':
  84.         b->uscrtch=1;
  85.         (void) strcpy(buf,"FXXXXXX.tmp");
  86.         (void) mktemp(buf);
  87.         (void) close(creat(buf, 0666));
  88.         break;
  89.     case 'n':
  90.     case 'N':
  91. #ifdef MSDOS
  92.         if(!access(buf,0))
  93. #else
  94.         if(!stat(buf,&stb))
  95. #endif
  96.             err(a->oerr,128,"open")
  97.         /* no break */
  98.     case 'r':    /* Fortran 90 replace option */
  99.     case 'R':
  100.         (void) close(creat(buf, 0666));
  101.         break;
  102.     }
  103.  
  104.     b->ufnm=(char *) malloc((unsigned int)(strlen(buf)+1));
  105.     if(b->ufnm==NULL) err(a->oerr,113,"no space");
  106.     (void) strcpy(b->ufnm,buf);
  107.     b->uend=0;
  108.     b->uwrt = 0;
  109.     if(isdev(buf))
  110.     {    b->ufd = fopen(buf,"r");
  111.         if(b->ufd==NULL) err(a->oerr,errno,buf)
  112.     }
  113.     else {
  114.         if((b->ufd = fopen(buf, "r")) == NULL) {
  115.             if ((n = open(buf,O_WRONLY)) >= 0) {
  116.                 b->uwrt = 2;
  117.                 }
  118.             else {
  119.                 n = creat(buf, 0666);
  120.                 b->uwrt = 1;
  121.                 }
  122.             if (n < 0
  123.             || (b->ufd = fdopen(n, "w")) == NULL)
  124.                 err(a->oerr, errno, "open");
  125.             }
  126.     }
  127.     b->useek=canseek(b->ufd);
  128. #ifndef MSDOS
  129.     if((b->uinode=inode(buf,&b->udev))==-1)
  130.         err(a->oerr,108,"open")
  131. #endif
  132.     if(a->orl && b->useek) rewind(b->ufd);
  133.     return(0);
  134. }
  135. fk_open(seq,fmt,n) ftnint n;
  136. {    char nbuf[10];
  137.     olist a;
  138.     (void) sprintf(nbuf,"fort.%ld",n);
  139.     a.oerr=1;
  140.     a.ounit=n;
  141.     a.ofnm=nbuf;
  142.     a.ofnmlen=strlen(nbuf);
  143.     a.osta=NULL;
  144.     a.oacc= seq==SEQ?"s":"d";
  145.     a.ofm = fmt==FMT?"f":"u";
  146.     a.orl = seq==DIR?1:0;
  147.     a.oblnk=NULL;
  148.     return(f_open(&a));
  149. }
  150. isdev(s) char *s;
  151. {
  152. #ifdef MSDOS
  153.     int i, j;
  154.  
  155.     i = open(s,O_RDONLY);
  156.     if (i == -1)
  157.         return 0;
  158.     j = isatty(i);
  159.     close(i);
  160.     return j;
  161. #else
  162.     struct stat x;
  163.  
  164.     if(stat(s, &x) == -1) return(0);
  165. #ifdef S_IFMT
  166.     switch(x.st_mode&S_IFMT) {
  167.         case S_IFREG:
  168.         case S_IFDIR:
  169.             return(0);
  170.         }
  171. #else
  172. #ifdef S_ISREG
  173.     /* POSIX version */
  174.     if(S_ISREG(x.st_mode) || S_ISDIR(x.st_mode))
  175.         return(0);
  176.     else
  177. #else
  178.     Help! How does stat work on this system?
  179. #endif
  180. #endif
  181.         return(1);
  182. #endif
  183. }
  184.