10 extern FILE *tmpfile();
19 extern char *f__r_mode[], *f__w_mode[];
22 integer f_end(a) alist *a;
24 integer f_end(alist *a)
31 f__fatal (131, "I/O recursion");
32 if(a->aunit>=MXUNIT || a->aunit<0) err(a->aerr,101,"endfile");
33 b = &f__units[a->aunit];
36 sprintf(nbuf,"fort.%ld",(long)a->aunit);
37 if (tf = fopen(nbuf, f__w_mode[0]))
42 return(b->useek ? t_runc(a) : 0);
45 #ifndef HAVE_FTRUNCATE
48 copy(from, len, to) FILE *from, *to; register long len;
50 copy(FILE *from, register long len, FILE *to)
56 while(fread(buf, len1 = len > BUFSIZ ? BUFSIZ : (int)len, 1, from)) {
57 if (!fwrite(buf, len1, 1, to))
59 if ((len -= len1) <= 0)
64 #endif /* !defined(HAVE_FTRUNCATE) */
77 #ifndef HAVE_FTRUNCATE
79 #endif /* !defined(HAVE_FTRUNCATE) */
81 b = &f__units[a->aunit];
83 return(0); /*don't truncate direct files*/
84 loc=ftell(bf = b->ufd);
85 fseek(bf,0L,SEEK_END);
87 if (loc >= len || b->useek == 0 || b->ufnm == NULL)
89 #ifndef HAVE_FTRUNCATE
93 if (!(bf = fopen(b->ufnm, f__w_mode[b->ufmt])))
99 if (!(bf = fopen(b->ufnm, f__r_mode[0]))
100 || !(tf = tmpfile())) {
101 #ifdef NON_UNIX_STDIO
107 if (copy(bf, loc, tf)) {
112 if (!(bf = freopen(b->ufnm, f__w_mode[0], bf)))
115 if (copy(tf, loc, bf))
119 #ifdef NON_UNIX_STDIO
122 if (!(bf = fopen(b->ufnm, f__w_mode[3])))
124 fseek(bf,0L,SEEK_END);
132 #else /* !defined(HAVE_FTRUNCATE) */
134 /* The cast of loc is helpful on FreeBSD. It helps
135 in any case where ftruncate() prototype is somehow missing
136 even though autoconf test found it properly. */
137 rc = ftruncate(fileno(b->ufd), (off_t)loc);
138 #endif /* !defined(HAVE_FTRUNCATE) */
140 err(a->aerr,111,"endfile");