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  

endfile.c

Go to the documentation of this file.
00001 #include "stdlib.h"
00002 #include "f2c.h"
00003 #include "fio.h"
00004 #ifndef NON_UNIX_STDIO
00005 #include "sys/types.h"
00006 #endif
00007 #include "rawio.h"
00008 
00009 #ifdef KR_headers
00010 extern char *strcpy();
00011 #else
00012 #undef abs
00013 #undef min
00014 #undef max
00015 #include "stdlib.h"
00016 #include "string.h"
00017 #endif
00018 
00019 #ifdef NON_UNIX_STDIO
00020 #ifndef unlink
00021 #define unlink remove
00022 #endif
00023 #else
00024 #ifdef MSDOS
00025 #include "io.h"
00026 #endif
00027 #endif
00028 
00029 #ifdef NON_UNIX_STDIO
00030 extern char *f__r_mode[], *f__w_mode[];
00031 #endif
00032 
00033 #ifdef KR_headers
00034 integer f_end(a) alist *a;
00035 #else
00036 integer f_end(alist *a)
00037 #endif
00038 {
00039         unit *b;
00040         if(a->aunit>=MXUNIT || a->aunit<0) err(a->aerr,101,"endfile");
00041         b = &f__units[a->aunit];
00042         if(b->ufd==NULL) {
00043                 char nbuf[10];
00044                 (void) sprintf(nbuf,"fort.%ld",a->aunit);
00045 #ifdef NON_UNIX_STDIO
00046                 { FILE *tf;
00047                         if (tf = fopen(nbuf, f__w_mode[0]))
00048                                 fclose(tf);
00049                         }
00050 #else
00051                 close(creat(nbuf, 0666));
00052 #endif
00053                 return(0);
00054                 }
00055         b->uend=1;
00056         return(b->useek ? t_runc(a) : 0);
00057 }
00058 
00059  static int
00060 #ifdef NON_UNIX_STDIO
00061 #ifdef KR_headers
00062 copy(from, len, to) char *from, *to; register long len;
00063 #else
00064 copy(FILE *from, register long len, FILE *to)
00065 #endif
00066 {
00067         int k, len1;
00068         char buf[BUFSIZ];
00069 
00070         while(fread(buf, len1 = len > BUFSIZ ? BUFSIZ : (int)len, 1, from)) {
00071                 if (!fwrite(buf, len1, 1, to))
00072                         return 1;
00073                 if ((len -= len1) <= 0)
00074                         break;
00075                 }
00076         return 0;
00077         }
00078 #else
00079 #ifdef KR_headers
00080 copy(from, len, to) char *from, *to; register long len;
00081 #else
00082 copy(char *from, register long len, char *to)
00083 #endif
00084 {
00085         register int n;
00086         int k, rc = 0, tmp;
00087         char buf[BUFSIZ];
00088 
00089         if ((k = open(from, O_RDONLY)) < 0)
00090                 return 1;
00091         if ((tmp = creat(to,0666)) < 0)
00092                 return 1;
00093         while((n = read(k, buf, len > BUFSIZ ? BUFSIZ : (int)len)) > 0) {
00094                 if (write(tmp, buf, n) != n)
00095                         { rc = 1; break; }
00096                 if ((len -= n) <= 0)
00097                         break;
00098                 }
00099         close(k);
00100         close(tmp);
00101         return n < 0 ? 1 : rc;
00102         }
00103 #endif
00104 
00105 #ifndef L_tmpnam
00106 #define L_tmpnam 16
00107 #endif
00108 
00109  int
00110 #ifdef KR_headers
00111 t_runc(a) alist *a;
00112 #else
00113 t_runc(alist *a)
00114 #endif
00115 {
00116         char nm[L_tmpnam+12];   /* extra space in case L_tmpnam is tiny */
00117         long loc, len;
00118         unit *b;
00119 #ifdef NON_UNIX_STDIO
00120         FILE *bf, *tf;
00121 #else
00122         FILE *bf;
00123 #endif
00124         int rc = 0;
00125 
00126         b = &f__units[a->aunit];
00127         if(b->url)
00128                 return(0);      /*don't truncate direct files*/
00129         loc=ftell(bf = b->ufd);
00130         fseek(bf,0L,SEEK_END);
00131         len=ftell(bf);
00132         if (loc >= len || b->useek == 0 || b->ufnm == NULL)
00133                 return(0);
00134 #ifdef NON_UNIX_STDIO
00135         fclose(b->ufd);
00136 #else
00137         rewind(b->ufd); /* empty buffer */
00138 #endif
00139         if (!loc) {
00140 #ifdef NON_UNIX_STDIO
00141                 if (!(bf = fopen(b->ufnm, f__w_mode[b->ufmt])))
00142 #else
00143                 if (close(creat(b->ufnm,0666)))
00144 #endif
00145                         rc = 1;
00146                 if (b->uwrt)
00147                         b->uwrt = 1;
00148                 goto done;
00149                 }
00150 #ifdef _POSIX_SOURCE
00151         tmpnam(nm);
00152 #else
00153         strcpy(nm,"tmp.FXXXXXX");
00154         mktemp(nm);
00155 #endif
00156 #ifdef NON_UNIX_STDIO
00157         if (!(bf = fopen(b->ufnm, f__r_mode[0]))) {
00158  bad:
00159                 rc = 1;
00160                 goto done;
00161                 }
00162         if (!(tf = fopen(nm, f__w_mode[0])))
00163                 goto bad;
00164         if (copy(bf, loc, tf)) {
00165  bad1:
00166                 rc = 1;
00167                 goto done1;
00168                 }
00169         if (!(bf = freopen(b->ufnm, f__w_mode[0], bf)))
00170                 goto bad1;
00171         if (!(tf = freopen(nm, f__r_mode[0], tf)))
00172                 goto bad1;
00173         if (copy(tf, loc, bf))
00174                 goto bad1;
00175         if (f__w_mode[0] != f__w_mode[b->ufmt]) {
00176                 if (!(bf = freopen(b->ufnm, f__w_mode[b->ufmt|2], bf)))
00177                         goto bad1;
00178                 fseek(bf, loc, SEEK_SET);
00179                 }
00180 done1:
00181         fclose(tf);
00182         unlink(nm);
00183 done:
00184         f__cf = b->ufd = bf;
00185 #else
00186         if (copy(b->ufnm, loc, nm)
00187          || copy(nm, loc, b->ufnm))
00188                 rc = 1;
00189         unlink(nm);
00190         fseek(b->ufd, loc, SEEK_SET);
00191 done:
00192 #endif
00193         if (rc)
00194                 err(a->aerr,111,"endfile");
00195         return 0;
00196         }
 

Powered by Plone

This site conforms to the following standards: