home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / f2c / libi77 / open.c < prev    next >
C/C++ Source or Header  |  2000-01-15  |  5KB  |  232 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 "string.h"
  8. #include "rawio.h"
  9.  
  10. #ifdef KR_headers
  11. extern char *malloc(), *mktemp();
  12. extern integer f_clos();
  13. #else
  14. #undef abs
  15. #undef min
  16. #undef max
  17. #include "stdlib.h"
  18. extern int f__canseek(FILE*);
  19. extern integer f_clos(cllist*);
  20. #endif
  21.  
  22. #ifdef NON_ANSI_RW_MODES
  23. char *f__r_mode[2] = {"r", "r"};
  24. char *f__w_mode[4] = {"w", "w", "r+w", "r+w"};
  25. #else
  26. char *f__r_mode[2] = {"rb", "r"};
  27. char *f__w_mode[4] = {"wb", "w", "r+b", "r+"};
  28. #endif
  29.  
  30. #ifdef KR_headers
  31. f__isdev(s) char *s;
  32. #else
  33. f__isdev(char *s)
  34. #endif
  35. {
  36. #ifdef NON_UNIX_STDIO
  37.     int i, j;
  38.  
  39.     i = open(s,O_RDONLY);
  40.     if (i == -1)
  41.         return 0;
  42.     j = isatty(i);
  43.     close(i);
  44.     return j;
  45. #else
  46.     struct stat x;
  47.  
  48.     if(stat(s, &x) == -1) return(0);
  49. #ifdef S_IFMT
  50.     switch(x.st_mode&S_IFMT) {
  51.         case S_IFREG:
  52.         case S_IFDIR:
  53.             return(0);
  54.         }
  55. #else
  56. #ifdef S_ISREG
  57.     /* POSIX version */
  58.     if(S_ISREG(x.st_mode) || S_ISDIR(x.st_mode))
  59.         return(0);
  60.     else
  61. #else
  62.     Help! How does stat work on this system?
  63. #endif
  64. #endif
  65.         return(1);
  66. #endif
  67. }
  68. #ifdef KR_headers
  69. integer f_open(a) olist *a;
  70. #else
  71. integer f_open(olist *a)
  72. #endif
  73. {    unit *b;
  74.     integer rv;
  75.     char buf[256], *s;
  76.     cllist x;
  77. #ifdef NON_UNIX_STDIO
  78.     FILE *tf;
  79. #else
  80.     int n;
  81.     struct stat stb;
  82. #endif
  83.     if(a->ounit>=MXUNIT || a->ounit<0)
  84.         err(a->oerr,101,"open")
  85.     f__curunit = b = &f__units[a->ounit];
  86.     if(b->ufd) {
  87.         if(a->ofnm==0)
  88.         {
  89.         same:    if (a->oblnk)
  90.                 b->ublnk = *a->oblnk == 'z' || *a->oblnk == 'Z';
  91.             return(0);
  92.         }
  93. #ifdef NON_UNIX_STDIO
  94.         if (b->ufnm
  95.          && strlen(b->ufnm) == a->ofnmlen
  96.          && !strncmp(b->ufnm, b->ufnm, (unsigned)a->ofnmlen))
  97.             goto same;
  98. #else
  99.         g_char(a->ofnm,a->ofnmlen,buf);
  100.         if (f__inode(buf,&n) == b->uinode && n == b->udev)
  101.             goto same;
  102. #endif
  103.         x.cunit=a->ounit;
  104.         x.csta=0;
  105.         x.cerr=a->oerr;
  106.         if ((rv = f_clos(&x)) != 0)
  107.             return rv;
  108.         }
  109.     b->url = (int)a->orl;
  110.     b->ublnk = a->oblnk && (*a->oblnk == 'z' || *a->oblnk == 'Z');
  111.     if(a->ofm==0)
  112.     {    if(b->url>0) b->ufmt=0;
  113.         else b->ufmt=1;
  114.     }
  115.     else if(*a->ofm=='f' || *a->ofm == 'F') b->ufmt=1;
  116.     else b->ufmt=0;
  117. #ifdef url_Adjust
  118.     if (b->url && !b->ufmt)
  119.         url_Adjust(b->url);
  120. #endif
  121.     if (a->ofnm) {
  122.         g_char(a->ofnm,a->ofnmlen,buf);
  123.         if (!buf[0])
  124.             err(a->oerr,107,"open")
  125.         }
  126.     else
  127.         sprintf(buf, "fort.%ld", a->ounit);
  128.     b->uscrtch = 0;
  129.     switch(a->osta ? *a->osta : 'u')
  130.     {
  131.     case 'o':
  132.     case 'O':
  133. #ifdef NON_UNIX_STDIO
  134.         if(access(buf,0))
  135. #else
  136.         if(stat(buf,&stb))
  137. #endif
  138.             err(a->oerr,errno,"open")
  139.         break;
  140.      case 's':
  141.      case 'S':
  142.         b->uscrtch=1;
  143. #ifdef _POSIX_SOURCE
  144.         tmpnam(buf);
  145. #else
  146.         (void) strcpy(buf,"tmp.FXXXXXX");
  147.         (void) mktemp(buf);
  148. #endif
  149.         goto replace;
  150.     case 'n':
  151.     case 'N':
  152. #ifdef NON_UNIX_STDIO
  153.         if(!access(buf,0))
  154. #else
  155.         if(!stat(buf,&stb))
  156. #endif
  157.             err(a->oerr,128,"open")
  158.         /* no break */
  159.     case 'r':    /* Fortran 90 replace option */
  160.     case 'R':
  161.  replace:
  162. #ifdef NON_UNIX_STDIO
  163.         if (tf = fopen(buf,f__w_mode[0]))
  164.             fclose(tf);
  165. #else
  166.         (void) close(creat(buf, 0666));
  167. #endif
  168.     }
  169.  
  170.     b->ufnm=(char *) malloc((unsigned int)(strlen(buf)+1));
  171.     if(b->ufnm==NULL) err(a->oerr,113,"no space");
  172.     (void) strcpy(b->ufnm,buf);
  173.     b->uend=0;
  174.     b->uwrt = 0;
  175.     if(f__isdev(buf))
  176.     {    b->ufd = fopen(buf,f__r_mode[b->ufmt]);
  177.         if(b->ufd==NULL) err(a->oerr,errno,buf)
  178.     }
  179.     else {
  180.         if(!(b->ufd = fopen(buf, f__r_mode[b->ufmt]))) {
  181. #ifdef NON_UNIX_STDIO
  182.             if (b->ufd = fopen(buf, f__w_mode[b->ufmt+2]))
  183.                 b->uwrt = 2;
  184.             else if (b->ufd = fopen(buf, f__w_mode[b->ufmt]))
  185.                 b->uwrt = 1;
  186.             else
  187. #else
  188.             if ((n = open(buf,O_WRONLY)) >= 0)
  189.                 b->uwrt = 2;
  190.             else {
  191.                 n = creat(buf, 0666);
  192.                 b->uwrt = 1;
  193.                 }
  194.             if (n < 0
  195.             || (b->ufd = fdopen(n, f__w_mode[b->ufmt])) == NULL)
  196. #endif
  197.                 err(a->oerr, errno, "open");
  198.             }
  199.     }
  200.     b->useek=f__canseek(b->ufd);
  201. #ifndef NON_UNIX_STDIO
  202.     if((b->uinode=f__inode(buf,&b->udev))==-1)
  203.         err(a->oerr,108,"open")
  204. #endif
  205.     if(b->useek)
  206.         if (a->orl)
  207.             rewind(b->ufd);
  208.         else if ((s = a->oacc) && (*s == 'a' || *s == 'A')
  209.             && fseek(b->ufd, 0L, SEEK_END))
  210.                 err(a->oerr,129,"open");
  211.     return(0);
  212. }
  213. #ifdef KR_headers
  214. fk_open(seq,fmt,n) ftnint n;
  215. #else
  216. fk_open(int seq, int fmt, ftnint n)
  217. #endif
  218. {    char nbuf[10];
  219.     olist a;
  220.     (void) sprintf(nbuf,"fort.%ld",n);
  221.     a.oerr=1;
  222.     a.ounit=n;
  223.     a.ofnm=nbuf;
  224.     a.ofnmlen=strlen(nbuf);
  225.     a.osta=NULL;
  226.     a.oacc= seq==SEQ?"s":"d";
  227.     a.ofm = fmt==FMT?"f":"u";
  228.     a.orl = seq==DIR?1:0;
  229.     a.oblnk=NULL;
  230.     return(f_open(&a));
  231. }
  232.