open.c File Reference

#include "f2c.h"
#include "fio.h"
#include "string.h"
#include "unistd.h"
#include "stdlib.h"

Include dependency graph for open.c:

Go to the source code of this file.

Defines

#define opnerr(f, m, s)   {if(f) errno= m; else opn_err(m,s,a); return(m);}

Functions

int f__canseek (FILE *)
integer f_clos (cllist *)
static void f__bufadj (int n, int c)
int f__putbuf (int c)
void x_putc (int c)
static void opn_err (int m, char *s, olist *a)
integer f_open (olist *a)
int fk_open (int seq, int fmt, ftnint n)

Variables

char * f__r_mode [2] = {"rb", "r"}
char * f__w_mode [4] = {"wb", "w", "r+b", "r+"}
static char f__buf0 [400]
static char * f__buf = f__buf0
int f__buflen = (int)sizeof(f__buf0)


Define Documentation

#define opnerr ( f,
m,
s   )     {if(f) errno= m; else opn_err(m,s,a); return(m);}

Definition at line 110 of file open.c.

Referenced by f_open().


Function Documentation

static void f__bufadj ( int  n,
int  c 
) [static]

Definition at line 45 of file open.c.

References f__fatal(), free(), int, len, malloc(), s, and t.

Referenced by f__putbuf(), opn_err(), and x_putc().

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         }

Here is the call graph for this function:

Here is the caller graph for this function:

int f__canseek ( FILE *   ) 

Definition at line 91 of file err.c.

References FSTAT, isatty(), STAT_ST, and work.

Referenced by f_init(), and f_open().

00093 {
00094 #ifdef NON_UNIX_STDIO
00095 #ifdef _MSC_VER
00096 #define fileno _fileno
00097 #define isatty _isatty
00098 #endif
00099         return !isatty(fileno(f));
00100 #else
00101         struct STAT_ST x;
00102 
00103         if (FSTAT(fileno(f),&x) < 0)
00104                 return(0);
00105 #ifdef S_IFMT
00106         switch(x.st_mode & S_IFMT) {
00107         case S_IFDIR:
00108         case S_IFREG:
00109                 if(x.st_nlink > 0)      /* !pipe */
00110                         return(1);
00111                 else
00112                         return(0);
00113         case S_IFCHR:
00114                 if(isatty(fileno(f)))
00115                         return(0);
00116                 return(1);
00117 #ifdef S_IFBLK
00118         case S_IFBLK:
00119                 return(1);
00120 #endif
00121         }
00122 #else
00123 #ifdef S_ISDIR
00124         /* POSIX version */
00125         if (S_ISREG(x.st_mode) || S_ISDIR(x.st_mode)) {
00126                 if(x.st_nlink > 0)      /* !pipe */
00127                         return(1);
00128                 else
00129                         return(0);
00130                 }
00131         if (S_ISCHR(x.st_mode)) {
00132                 if(isatty(fileno(f)))
00133                         return(0);
00134                 return(1);
00135                 }
00136         if (S_ISBLK(x.st_mode))
00137                 return(1);
00138 #else
00139         Help! How does fstat work on this system?
00140 #endif
00141 #endif
00142         return(0);      /* who knows what it is? */
00143 #endif
00144 }

Here is the call graph for this function:

Here is the caller graph for this function:

int f__putbuf ( int  c  ) 

Definition at line 72 of file open.c.

References f__bufadj(), f__cf, f__hiwater, f__recpos, and s.

Referenced by e_wsle(), x_wSL(), xw_end(), xw_rev(), and y_rev().

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         }

Here is the call graph for this function:

Here is the caller graph for this function:

integer f_clos ( cllist  ) 

Definition at line 30 of file close.c.

References a, b, Delete(), done, f__units, free(), MXUNIT, NULL, t_runc(), and unlink().

Referenced by f_exit(), and f_open().

00032 {       unit *b;
00033 
00034         if(a->cunit >= MXUNIT) return(0);
00035         b= &f__units[a->cunit];
00036         if(b->ufd==NULL)
00037                 goto done;
00038         if (b->uscrtch == 1)
00039                 goto Delete;
00040         if (!a->csta)
00041                 goto Keep;
00042         switch(*a->csta) {
00043                 default:
00044                 Keep:
00045                 case 'k':
00046                 case 'K':
00047                         if(b->uwrt == 1)
00048                                 t_runc((alist *)a);
00049                         if(b->ufnm) {
00050                                 fclose(b->ufd);
00051                                 free(b->ufnm);
00052                                 }
00053                         break;
00054                 case 'd':
00055                 case 'D':
00056                 Delete:
00057                         fclose(b->ufd);
00058                         if(b->ufnm) {
00059                                 unlink(b->ufnm); /*SYSDEP*/
00060                                 free(b->ufnm);
00061                                 }
00062                 }
00063         b->ufd=NULL;
00064  done:
00065         b->uend=0;
00066         b->ufnm=NULL;
00067         return(0);
00068         }

Here is the call graph for this function:

Here is the caller graph for this function:

integer f_open ( olist a  ) 

Definition at line 135 of file open.c.

References a, b, buf, cllist::cerr, cllist::csta, cllist::cunit, err, errno, f__canseek(), f__curunit, f__external, f__init, f__inode(), f__units, f_clos(), f_init(), FOPEN, FSEEK, g_char(), int, L, malloc(), mktemp(), MXUNIT, NULL, opnerr, SEEK_END, tf, void(), and x.

Referenced by fk_open().

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 }

Here is the call graph for this function:

Here is the caller graph for this function:

int fk_open ( int  seq,
int  fmt,
ftnint  n 
)

Definition at line 285 of file open.c.

References a, DIR, f_open(), FMT, NULL, SEQ, and void().

Referenced by c_dfe(), c_due(), c_le(), c_sfe(), c_sue(), and f_back().

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 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void opn_err ( int  m,
char *  s,
olist a 
) [static]

Definition at line 116 of file open.c.

References a, f__bufadj(), f__curunit, f__fatal(), g_char(), and unit::ufnm.

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         }

Here is the call graph for this function:

void x_putc ( int  c  ) 

Definition at line 102 of file open.c.

References f__bufadj(), and f__recpos.

Referenced by s_wdfe(), s_wsfe(), s_wsle(), and s_wsne().

00104 {
00105         if (f__recpos >= f__buflen)
00106                 f__bufadj(f__recpos, f__buflen);
00107         f__buf[f__recpos++] = c;
00108         }

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

char * f__buf = f__buf0 [static]

Definition at line 38 of file open.c.

char f__buf0[400] [static]

Definition at line 38 of file open.c.

int f__buflen = (int)sizeof(f__buf0)

Definition at line 39 of file open.c.

char* f__r_mode[2] = {"rb", "r"}

Definition at line 34 of file open.c.

Referenced by f__nowreading().

char* f__w_mode[4] = {"wb", "w", "r+b", "r+"}

Definition at line 35 of file open.c.

Referenced by f__nowreading(), and f__nowwriting().


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