Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
open.c
Go to the documentation of this file.00001 #include "stdlib.h"
00002 #ifndef NON_UNIX_STDIO
00003 #include "sys/types.h"
00004 #include "sys/stat.h"
00005 #endif
00006 #include "f2c.h"
00007 #include "fio.h"
00008 #include "string.h"
00009 #include "rawio.h"
00010
00011 #ifdef KR_headers
00012 extern char *malloc(), *mktemp();
00013 extern integer f_clos();
00014 #else
00015 #undef abs
00016 #undef min
00017 #undef max
00018 #include "stdlib.h"
00019 extern int f__canseek(FILE*);
00020 extern integer f_clos(cllist*);
00021 #endif
00022
00023 #include <string.h>
00024 #include <unistd.h>
00025
00026 #ifdef NON_ANSI_RW_MODES
00027 char *f__r_mode[2] = {"r", "r"};
00028 char *f__w_mode[4] = {"w", "w", "r+w", "r+w"};
00029 #else
00030 char *f__r_mode[2] = {"rb", "r"};
00031 char *f__w_mode[4] = {"wb", "w", "r+b", "r+"};
00032 #endif
00033
00034 #ifdef KR_headers
00035 int f__isdev(s) char *s;
00036 #else
00037 int f__isdev(char *s)
00038 #endif
00039 {
00040 #ifdef NON_UNIX_STDIO
00041 int i, j;
00042
00043 i = open(s,O_RDONLY);
00044 if (i == -1)
00045 return 0;
00046 j = isatty(i);
00047 close(i);
00048 return j;
00049 #else
00050 struct stat x;
00051
00052 if(stat(s, &x) == -1) return(0);
00053 #ifdef S_IFMT
00054 switch(x.st_mode&S_IFMT) {
00055 case S_IFREG:
00056 case S_IFDIR:
00057 return(0);
00058 }
00059 #else
00060 #ifdef S_ISREG
00061
00062 if(S_ISREG(x.st_mode) || S_ISDIR(x.st_mode))
00063 return(0);
00064 else
00065 #else
00066 Help! How does stat work on this system?
00067 #endif
00068 #endif
00069 return(1);
00070 #endif
00071 }
00072 #ifdef KR_headers
00073 integer f_open(a) olist *a;
00074 #else
00075 integer f_open(olist *a)
00076 #endif
00077 { unit *b;
00078 integer rv;
00079 char buf[256], *s;
00080 cllist x;
00081 int ufmt;
00082 #ifdef NON_UNIX_STDIO
00083 FILE *tf;
00084 #else
00085 int n;
00086 struct stat stb;
00087 #endif
00088 if(a->ounit>=MXUNIT || a->ounit<0)
00089 err(a->oerr,101,"open")
00090 if (!f__init)
00091 f_init();
00092 f__curunit = b = &f__units[a->ounit];
00093 if(b->ufd) {
00094 if(a->ofnm==0)
00095 {
00096 same: if (a->oblnk)
00097 b->ublnk = *a->oblnk == 'z' || *a->oblnk == 'Z';
00098 return(0);
00099 }
00100 #ifdef NON_UNIX_STDIO
00101 if (b->ufnm
00102 && strlen(b->ufnm) == a->ofnmlen
00103 && !strncmp(b->ufnm, b->ufnm, (unsigned)a->ofnmlen))
00104 goto same;
00105 #else
00106 g_char(a->ofnm,a->ofnmlen,buf);
00107 if (f__inode(buf,&n) == b->uinode && n == b->udev)
00108 goto same;
00109 #endif
00110 x.cunit=a->ounit;
00111 x.csta=0;
00112 x.cerr=a->oerr;
00113 if ((rv = f_clos(&x)) != 0)
00114 return rv;
00115 }
00116 b->url = (int)a->orl;
00117 b->ublnk = a->oblnk && (*a->oblnk == 'z' || *a->oblnk == 'Z');
00118 if(a->ofm==0)
00119 { if(b->url>0) b->ufmt=0;
00120 else b->ufmt=1;
00121 }
00122 else if(*a->ofm=='f' || *a->ofm == 'F') b->ufmt=1;
00123 else b->ufmt=0;
00124 ufmt = b->ufmt;
00125 #ifdef url_Adjust
00126 if (b->url && !ufmt)
00127 url_Adjust(b->url);
00128 #endif
00129 if (a->ofnm) {
00130 g_char(a->ofnm,a->ofnmlen,buf);
00131 if (!buf[0])
00132 err(a->oerr,107,"open")
00133 }
00134 else
00135 sprintf(buf, "fort.%ld", a->ounit);
00136 b->uscrtch = 0;
00137 switch(a->osta ? *a->osta : 'u')
00138 {
00139 case 'o':
00140 case 'O':
00141 #ifdef NON_UNIX_STDIO
00142 if(access(buf,0))
00143 #else
00144 if(stat(buf,&stb))
00145 #endif
00146 err(a->oerr,errno,"open")
00147 break;
00148 case 's':
00149 case 'S':
00150 b->uscrtch=1;
00151 #ifdef _POSIX_SOURCE
00152 tmpnam(buf);
00153 #else
00154 (void) strcpy(buf,"tmp.FXXXXXX");
00155 (void) mktemp(buf);
00156 #endif
00157 goto replace;
00158 case 'n':
00159 case 'N':
00160 #ifdef NON_UNIX_STDIO
00161 if(!access(buf,0))
00162 #else
00163 if(!stat(buf,&stb))
00164 #endif
00165 err(a->oerr,128,"open")
00166
00167 case 'r':
00168 case 'R':
00169 replace:
00170 #ifdef NON_UNIX_STDIO
00171 if (tf = fopen(buf,f__w_mode[0]))
00172 fclose(tf);
00173 #else
00174 (void) close(creat(buf, 0666));
00175 #endif
00176 }
00177
00178 b->ufnm=(char *) malloc((unsigned int)(strlen(buf)+1));
00179 if(b->ufnm==NULL) err(a->oerr,113,"no space");
00180 (void) strcpy(b->ufnm,buf);
00181 b->uend=0;
00182 b->uwrt = 0;
00183 #ifdef NON_UNIX_STDIO
00184 if ((s = a->oacc) && (*s == 'd' || *s == 'D'))
00185 ufmt = 0;
00186 #endif
00187 if(f__isdev(buf))
00188 { b->ufd = fopen(buf,f__r_mode[ufmt]);
00189 if(b->ufd==NULL) err(a->oerr,errno,buf)
00190 }
00191 else {
00192 if(!(b->ufd = fopen(buf, f__r_mode[ufmt]))) {
00193 #ifdef NON_UNIX_STDIO
00194 if (b->ufd = fopen(buf, f__w_mode[ufmt|2]))
00195 b->uwrt = 2;
00196 else if (b->ufd = fopen(buf, f__w_mode[ufmt]))
00197 b->uwrt = 1;
00198 else
00199 #else
00200 if ((n = open(buf,O_WRONLY)) >= 0)
00201 b->uwrt = 2;
00202 else {
00203 n = creat(buf, 0666);
00204 b->uwrt = 1;
00205 }
00206 if (n < 0
00207 || (b->ufd = fdopen(n, f__w_mode[ufmt])) == NULL)
00208 #endif
00209 err(a->oerr, errno, "open");
00210 }
00211 }
00212 b->useek=f__canseek(b->ufd);
00213 #ifndef NON_UNIX_STDIO
00214 if((b->uinode=f__inode(buf,&b->udev))==-1)
00215 err(a->oerr,108,"open")
00216 #endif
00217 if(b->useek)
00218 if (a->orl)
00219 rewind(b->ufd);
00220 else if ((s = a->oacc) && (*s == 'a' || *s == 'A')
00221 && fseek(b->ufd, 0L, SEEK_END))
00222 err(a->oerr,129,"open");
00223 return(0);
00224 }
00225 #ifdef KR_headers
00226 int fk_open(seq,fmt,n) ftnint n;
00227 #else
00228 int fk_open(int seq, int fmt, ftnint n)
00229 #endif
00230 { char nbuf[10];
00231 olist a;
00232 (void) sprintf(nbuf,"fort.%ld",n);
00233 a.oerr=1;
00234 a.ounit=n;
00235 a.ofnm=nbuf;
00236 a.ofnmlen=strlen(nbuf);
00237 a.osta=NULL;
00238 a.oacc= seq==SEQ?"s":"d";
00239 a.ofm = fmt==FMT?"f":"u";
00240 a.orl = seq==DIR?1:0;
00241 a.oblnk=NULL;
00242 return(f_open(&a));
00243 }