open.c

Go to the documentation of this file.
00001 #include "f2c.h"
00002 #include "fio.h"
00003 #include "string.h"
00004 #ifndef NON_POSIX_STDIO
00005 #ifdef MSDOS
00006 #include "io.h"
00007 #else
00008 #include "unistd.h"     /* for access */
00009 #endif
00010 #endif
00011 
00012 #ifdef KR_headers
00013 extern char *malloc();
00014 #ifdef NON_ANSI_STDIO
00015 extern char *mktemp();
00016 #endif
00017 extern integer f_clos();
00018 #else
00019 #undef abs
00020 #undef min
00021 #undef max
00022 #include "stdlib.h"
00023 #ifdef __cplusplus
00024 extern "C" {
00025 #endif
00026 extern int f__canseek(FILE*);
00027 extern integer f_clos(cllist*);
00028 #endif
00029 
00030 #ifdef NON_ANSI_RW_MODES
00031 char *f__r_mode[2] = {"r", "r"};
00032 char *f__w_mode[4] = {"w", "w", "r+w", "r+w"};
00033 #else
00034 char *f__r_mode[2] = {"rb", "r"};
00035 char *f__w_mode[4] = {"wb", "w", "r+b", "r+"};
00036 #endif
00037 
00038  static char f__buf0[400], *f__buf = f__buf0;
00039  int f__buflen = (int)sizeof(f__buf0);
00040 
00041  static void
00042 #ifdef KR_headers
00043 f__bufadj(n, c) int n, c;
00044 #else
00045 f__bufadj(int n, int c)
00046 #endif
00047 {
00048         unsigned int len;
00049         char *nbuf, *s, *t, *te;
00050 
00051         if (f__buf == f__buf0)
00052                 f__buflen = 1024;
00053         while(f__buflen <= n)
00054                 f__buflen <<= 1;
00055         len = (unsigned int)f__buflen;
00056         if (len != f__buflen || !(nbuf = (char*)malloc(len)))
00057                 f__fatal(113, "malloc failure");
00058         s = nbuf;
00059         t = f__buf;
00060         te = t + c;
00061         while(t < te)
00062                 *s++ = *t++;
00063         if (f__buf != f__buf0)
00064                 free(f__buf);
00065         f__buf = nbuf;
00066         }
00067 
00068  int
00069 #ifdef KR_headers
00070 f__putbuf(c) int c;
00071 #else
00072 f__putbuf(int c)
00073 #endif
00074 {
00075         char *s, *se;
00076         int n;
00077 
00078         if (f__hiwater > f__recpos)
00079                 f__recpos = f__hiwater;
00080         n = f__recpos + 1;
00081         if (n >= f__buflen)
00082                 f__bufadj(n, f__recpos);
00083         s = f__buf;
00084         se = s + f__recpos;
00085         if (c)
00086                 *se++ = c;
00087         *se = 0;
00088         for(;;) {
00089                 fputs(s, f__cf);
00090                 s += strlen(s);
00091                 if (s >= se)
00092                         break;  /* normally happens the first time */
00093                 putc(*s++, f__cf);
00094                 }
00095         return 0;
00096         }
00097 
00098  void
00099 #ifdef KR_headers
00100 x_putc(c)
00101 #else
00102 x_putc(int c)
00103 #endif
00104 {
00105         if (f__recpos >= f__buflen)
00106                 f__bufadj(f__recpos, f__buflen);
00107         f__buf[f__recpos++] = c;
00108         }
00109 
00110 #define opnerr(f,m,s) {if(f) errno= m; else opn_err(m,s,a); return(m);}
00111 
00112  static void
00113 #ifdef KR_headers
00114 opn_err(m, s, a) int m; char *s; olist *a;
00115 #else
00116 opn_err(int m, char *s, olist *a)
00117 #endif
00118 {
00119         if (a->ofnm) {
00120                 /* supply file name to error message */
00121                 if (a->ofnmlen >= f__buflen)
00122                         f__bufadj((int)a->ofnmlen, 0);
00123                 g_char(a->ofnm, a->ofnmlen, f__curunit->ufnm = f__buf);
00124                 }
00125         f__fatal(m, s);
00126         }
00127 
00128 #ifdef _MSC_VER
00129 #define  access _access
00130 #endif
00131 
00132 #ifdef KR_headers
00133 integer f_open(a) olist *a;
00134 #else
00135 integer f_open(olist *a)
00136 #endif
00137 {       unit *b;
00138         integer rv;
00139         char buf[256], *s;
00140         cllist x;
00141         int ufmt;
00142         FILE *tf;
00143 #ifndef NON_UNIX_STDIO
00144         int n;
00145 #endif
00146         f__external = 1;
00147         if(a->ounit>=MXUNIT || a->ounit<0)
00148                 err(a->oerr,101,"open")
00149         if (!f__init)
00150                 f_init();
00151         f__curunit = b = &f__units[a->ounit];
00152         if(b->ufd) {
00153                 if(a->ofnm==0)
00154                 {
00155                 same:   if (a->oblnk)
00156                                 b->ublnk = *a->oblnk == 'z' || *a->oblnk == 'Z';
00157                         return(0);
00158                 }
00159 #ifdef NON_UNIX_STDIO
00160                 if (b->ufnm
00161                  && strlen(b->ufnm) == a->ofnmlen
00162                  && !strncmp(b->ufnm, a->ofnm, (unsigned)a->ofnmlen))
00163                         goto same;
00164 #else
00165                 g_char(a->ofnm,a->ofnmlen,buf);
00166                 if (f__inode(buf,&n) == b->uinode && n == b->udev)
00167                         goto same;
00168 #endif
00169                 x.cunit=a->ounit;
00170                 x.csta=0;
00171                 x.cerr=a->oerr;
00172                 if ((rv = f_clos(&x)) != 0)
00173                         return rv;
00174                 }
00175         b->url = (int)a->orl;
00176         b->ublnk = a->oblnk && (*a->oblnk == 'z' || *a->oblnk == 'Z');
00177         if(a->ofm==0)
00178         {       if(b->url>0) b->ufmt=0;
00179                 else b->ufmt=1;
00180         }
00181         else if(*a->ofm=='f' || *a->ofm == 'F') b->ufmt=1;
00182         else b->ufmt=0;
00183         ufmt = b->ufmt;
00184 #ifdef url_Adjust
00185         if (b->url && !ufmt)
00186                 url_Adjust(b->url);
00187 #endif
00188         if (a->ofnm) {
00189                 g_char(a->ofnm,a->ofnmlen,buf);
00190                 if (!buf[0])
00191                         opnerr(a->oerr,107,"open")
00192                 }
00193         else
00194                 sprintf(buf, "fort.%ld", (long)a->ounit);
00195         b->uscrtch = 0;
00196         b->uend=0;
00197         b->uwrt = 0;
00198         b->ufd = 0;
00199         b->urw = 3;
00200         switch(a->osta ? *a->osta : 'u')
00201         {
00202         case 'o':
00203         case 'O':
00204 #ifdef NON_POSIX_STDIO
00205                 if (!(tf = FOPEN(buf,"r")))
00206                         opnerr(a->oerr,errno,"open")
00207                 fclose(tf);
00208 #else
00209                 if (access(buf,0))
00210                         opnerr(a->oerr,errno,"open")
00211 #endif
00212                 break;
00213          case 's':
00214          case 'S':
00215                 b->uscrtch=1;
00216 #ifdef NON_ANSI_STDIO
00217                 (void) strcpy(buf,"tmp.FXXXXXX");
00218                 (void) mktemp(buf);
00219                 goto replace;
00220 #else
00221                 if (!(b->ufd = tmpfile()))
00222                         opnerr(a->oerr,errno,"open")
00223                 b->ufnm = 0;
00224 #ifndef NON_UNIX_STDIO
00225                 b->uinode = b->udev = -1;
00226 #endif
00227                 b->useek = 1;
00228                 return 0;
00229 #endif
00230 
00231         case 'n':
00232         case 'N':
00233 #ifdef NON_POSIX_STDIO
00234                 if ((tf = FOPEN(buf,"r")) || (tf = FOPEN(buf,"a"))) {
00235                         fclose(tf);
00236                         opnerr(a->oerr,128,"open")
00237                         }
00238 #else
00239                 if (!access(buf,0))
00240                         opnerr(a->oerr,128,"open")
00241 #endif
00242                 /* no break */
00243         case 'r':       /* Fortran 90 replace option */
00244         case 'R':
00245 #ifdef NON_ANSI_STDIO
00246  replace:
00247 #endif
00248                 if (tf = FOPEN(buf,f__w_mode[0]))
00249                         fclose(tf);
00250         }
00251 
00252         b->ufnm=(char *) malloc((unsigned int)(strlen(buf)+1));
00253         if(b->ufnm==NULL) opnerr(a->oerr,113,"no space");
00254         (void) strcpy(b->ufnm,buf);
00255         if ((s = a->oacc) && b->url)
00256                 ufmt = 0;
00257         if(!(tf = FOPEN(buf, f__w_mode[ufmt|2]))) {
00258                 if (tf = FOPEN(buf, f__r_mode[ufmt]))
00259                         b->urw = 1;
00260                 else if (tf = FOPEN(buf, f__w_mode[ufmt])) {
00261                         b->uwrt = 1;
00262                         b->urw = 2;
00263                         }
00264                 else
00265                         err(a->oerr, errno, "open");
00266                 }
00267         b->useek = f__canseek(b->ufd = tf);
00268 #ifndef NON_UNIX_STDIO
00269         if((b->uinode = f__inode(buf,&b->udev)) == -1)
00270                 opnerr(a->oerr,108,"open")
00271 #endif
00272         if(b->useek)
00273                 if (a->orl)
00274                         rewind(b->ufd);
00275                 else if ((s = a->oacc) && (*s == 'a' || *s == 'A')
00276                         && FSEEK(b->ufd, 0L, SEEK_END))
00277                                 opnerr(a->oerr,129,"open");
00278         return(0);
00279 }
00280 
00281  int
00282 #ifdef KR_headers
00283 fk_open(seq,fmt,n) ftnint n;
00284 #else
00285 fk_open(int seq, int fmt, ftnint n)
00286 #endif
00287 {       char nbuf[10];
00288         olist a;
00289         (void) sprintf(nbuf,"fort.%ld",(long)n);
00290         a.oerr=1;
00291         a.ounit=n;
00292         a.ofnm=nbuf;
00293         a.ofnmlen=strlen(nbuf);
00294         a.osta=NULL;
00295         a.oacc= (char*)(seq==SEQ?"s":"d");
00296         a.ofm = (char*)(fmt==FMT?"f":"u");
00297         a.orl = seq==DIR?1:0;
00298         a.oblnk=NULL;
00299         return(f_open(&a));
00300 }
00301 #ifdef __cplusplus
00302 }
00303 #endif

Generated on Sun Mar 4 15:03:43 2007 for Scilab [trunk] by  doxygen 1.5.1