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