Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

open.c File Reference

#include "stdlib.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "f2c.h"
#include "fio.h"
#include "string.h"
#include "rawio.h"
#include <unistd.h>

Go to the source code of this file.


Functions

int f__canseek (FILE *)
integer f_clos (cllist *)
int f__isdev (char *s)
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+"}

Function Documentation

int f__canseek FILE *   
 

Definition at line 86 of file err.c.

References isatty(), and S_ISDIR.

Referenced by f_init(), and f_open().

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

int f__isdev char *    s
 

Definition at line 37 of file open.c.

References close(), i, isatty(), and S_ISDIR.

Referenced by f_open().

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         /* POSIX version */
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 }

integer f_clos cllist  
 

Definition at line 27 of file close.c.

References a, free, MXUNIT, t_runc(), unit::uend, unit::ufd, unit::ufnm, unlink, unit::uscrtch, and unit::uwrt.

Referenced by f_exit(), f_open(), and MAIN__().

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

integer f_open olist   a
 

Definition at line 75 of file open.c.

References a, cllist::cerr, close(), cllist::csta, cllist::cunit, err, f__canseek(), f__inode(), f__isdev(), f__r_mode, f__w_mode, f_clos(), f_init(), fdopen(), g_char(), L, malloc, mktemp(), MXUNIT, O_WRONLY, unit::ublnk, unit::udev, unit::uend, unit::ufd, unit::ufmt, unit::ufnm, unit::uinode, unit::url, unit::uscrtch, unit::useek, and unit::uwrt.

Referenced by fk_open(), and MAIN__().

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                 /* no break */
00167         case 'r':       /* Fortran 90 replace option */
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 }

int fk_open int    seq,
int    fmt,
ftnint    n
 

Definition at line 228 of file open.c.

References a, DIR, f_open(), FMT, olist::oacc, olist::oblnk, olist::oerr, olist::ofm, olist::ofnm, olist::ofnmlen, olist::orl, olist::osta, and olist::ounit.

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

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 }

Variable Documentation

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

Definition at line 30 of file open.c.

Referenced by f_open(), and t_runc().

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

Definition at line 31 of file open.c.

Referenced by f_end(), f_open(), and t_runc().

 

Powered by Plone

This site conforms to the following standards: