endfile.c
Go to the documentation of this file.
00001 #include "f2c.h"
00002 #include "fio.h"
00003 
00004 /* Compile this with -DNO_TRUNCATE if unistd.h does not exist or */
00005 /* if it does not define int truncate(const char *name, off_t). */
00006 #define NO_TRUNCATE
00007 
00008 #ifdef MSDOS
00009 #undef NO_TRUNCATE
00010 #define NO_TRUNCATE
00011 #endif
00012 
00013 #ifndef NO_TRUNCATE
00014 #include "unistd.h"
00015 #endif
00016 
00017 #ifdef KR_headers
00018 extern char *strcpy();
00019 extern FILE *tmpfile();
00020 #else
00021 #undef abs
00022 #undef min
00023 #undef max
00024 #include "stdlib.h"
00025 #include "string.h"
00026 #ifdef __cplusplus
00027 extern "C" {
00028 #endif
00029 #endif
00030 
00031 extern char *f__r_mode[], *f__w_mode[];
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         FILE *tf;
00041 
00042         if(a->aunit>=MXUNIT || a->aunit<0) err(a->aerr,101,"endfile");
00043         b = &f__units[a->aunit];
00044         if(b->ufd==NULL) {
00045                 char nbuf[10];
00046                 sprintf(nbuf,"fort.%ld",(long)a->aunit);
00047                 if (tf = FOPEN(nbuf, f__w_mode[0]))
00048                         fclose(tf);
00049                 return(0);
00050                 }
00051         b->uend=1;
00052         return(b->useek ? t_runc(a) : 0);
00053 }
00054 
00055 #ifdef NO_TRUNCATE
00056  static int
00057 #ifdef KR_headers
00058 copy(from, len, to) FILE *from, *to; register long len;
00059 #else
00060 copy(FILE *from, register long len, FILE *to)
00061 #endif
00062 {
00063         int len1;
00064         char buf[BUFSIZ];
00065 
00066         while(fread(buf, len1 = len > BUFSIZ ? BUFSIZ : (int)len, 1, from)) {
00067                 if (!fwrite(buf, len1, 1, to))
00068                         return 1;
00069                 if ((len -= len1) <= 0)
00070                         break;
00071                 }
00072         return 0;
00073         }
00074 #endif /* NO_TRUNCATE */
00075 
00076  int
00077 #ifdef KR_headers
00078 t_runc(a) alist *a;
00079 #else
00080 t_runc(alist *a)
00081 #endif
00082 {
00083         OFF_T loc, len;
00084         unit *b;
00085         int rc;
00086         FILE *bf;
00087 #ifdef NO_TRUNCATE
00088         FILE *tf;
00089 #endif
00090 
00091         b = &f__units[a->aunit];
00092         if(b->url)
00093                 return(0);      /*don't truncate direct files*/
00094         loc=FTELL(bf = b->ufd);
00095         FSEEK(bf,(OFF_T)0,SEEK_END);
00096         len=FTELL(bf);
00097         if (loc >= len || b->useek == 0)
00098                 return(0);
00099 #ifdef NO_TRUNCATE
00100         if (b->ufnm == NULL)
00101                 return 0;
00102         rc = 0;
00103         fclose(b->ufd);
00104         if (!loc) {
00105                 if (!(bf = FOPEN(b->ufnm, f__w_mode[b->ufmt])))
00106                         rc = 1;
00107                 if (b->uwrt)
00108                         b->uwrt = 1;
00109                 goto done;
00110                 }
00111         if (!(bf = FOPEN(b->ufnm, f__r_mode[0]))
00112          || !(tf = tmpfile())) {
00113 #ifdef NON_UNIX_STDIO
00114  bad:
00115 #endif
00116                 rc = 1;
00117                 goto done;
00118                 }
00119         if (copy(bf, (long)loc, tf)) {
00120  bad1:
00121                 rc = 1;
00122                 goto done1;
00123                 }
00124         if (!(bf = FREOPEN(b->ufnm, f__w_mode[0], bf)))
00125                 goto bad1;
00126         rewind(tf);
00127         if (copy(tf, (long)loc, bf))
00128                 goto bad1;
00129         b->uwrt = 1;
00130         b->urw = 2;
00131 #ifdef NON_UNIX_STDIO
00132         if (b->ufmt) {
00133                 fclose(bf);
00134                 if (!(bf = FOPEN(b->ufnm, f__w_mode[3])))
00135                         goto bad;
00136                 FSEEK(bf,(OFF_T)0,SEEK_END);
00137                 b->urw = 3;
00138                 }
00139 #endif
00140 done1:
00141         fclose(tf);
00142 done:
00143         f__cf = b->ufd = bf;
00144 #else /* NO_TRUNCATE */
00145         if (b->urw & 2)
00146                 fflush(b->ufd); /* necessary on some Linux systems */
00147 #ifndef FTRUNCATE
00148 #define FTRUNCATE ftruncate
00149 #endif
00150         rc = FTRUNCATE(fileno(b->ufd), loc);
00151         /* The following FSEEK is unnecessary on some systems, */
00152         /* but should be harmless. */
00153         FSEEK(b->ufd, (OFF_T)0, SEEK_END);
00154 #endif /* NO_TRUNCATE */
00155         if (rc)
00156                 err(a->aerr,111,"endfile");
00157         return 0;
00158         }
00159 #ifdef __cplusplus
00160 }
00161 #endif


swiftnav
Author(s):
autogenerated on Sat Jun 8 2019 18:55:50