]> gcc.gnu.org Git - gcc.git/blame - libf2c/libI77/endfile.c
endfile.c (t_runc): Add cast to help case where ftruncate() prototype is somehow...
[gcc.git] / libf2c / libI77 / endfile.c
CommitLineData
81fea2b1
JL
1#include "f2c.h"
2#include "fio.h"
81fea2b1 3
97897047
TM
4#include <unistd.h>
5
81fea2b1
JL
6#ifdef KR_headers
7extern char *strcpy();
a843efa0 8extern FILE *tmpfile();
81fea2b1
JL
9#else
10#undef abs
11#undef min
12#undef max
13#include <stdlib.h>
14#include <string.h>
15#endif
16
81fea2b1 17extern char *f__r_mode[], *f__w_mode[];
81fea2b1
JL
18
19#ifdef KR_headers
20integer f_end(a) alist *a;
21#else
22integer f_end(alist *a)
23#endif
24{
25 unit *b;
a843efa0
CB
26 FILE *tf;
27
81fea2b1
JL
28 if (f__init & 2)
29 f__fatal (131, "I/O recursion");
30 if(a->aunit>=MXUNIT || a->aunit<0) err(a->aerr,101,"endfile");
31 b = &f__units[a->aunit];
32 if(b->ufd==NULL) {
33 char nbuf[10];
6973bf54 34 sprintf(nbuf,"fort.%ld",(long)a->aunit);
a843efa0
CB
35 if (tf = fopen(nbuf, f__w_mode[0]))
36 fclose(tf);
81fea2b1
JL
37 return(0);
38 }
39 b->uend=1;
40 return(b->useek ? t_runc(a) : 0);
41}
42
97897047 43#ifndef HAVE_FTRUNCATE
81fea2b1 44 static int
81fea2b1 45#ifdef KR_headers
a843efa0 46copy(from, len, to) FILE *from, *to; register long len;
81fea2b1
JL
47#else
48copy(FILE *from, register long len, FILE *to)
49#endif
50{
a843efa0 51 int len1;
81fea2b1
JL
52 char buf[BUFSIZ];
53
54 while(fread(buf, len1 = len > BUFSIZ ? BUFSIZ : (int)len, 1, from)) {
55 if (!fwrite(buf, len1, 1, to))
56 return 1;
57 if ((len -= len1) <= 0)
58 break;
59 }
60 return 0;
61 }
97897047 62#endif /* !defined(HAVE_FTRUNCATE) */
81fea2b1
JL
63
64 int
65#ifdef KR_headers
66t_runc(a) alist *a;
67#else
68t_runc(alist *a)
69#endif
70{
81fea2b1
JL
71 long loc, len;
72 unit *b;
97897047
TM
73 int rc;
74 FILE *bf;
75#ifndef HAVE_FTRUNCATE
76 FILE *tf;
77#endif /* !defined(HAVE_FTRUNCATE) */
81fea2b1
JL
78
79 b = &f__units[a->aunit];
80 if(b->url)
81 return(0); /*don't truncate direct files*/
82 loc=ftell(bf = b->ufd);
83 fseek(bf,0L,SEEK_END);
84 len=ftell(bf);
85 if (loc >= len || b->useek == 0 || b->ufnm == NULL)
86 return(0);
97897047
TM
87#ifndef HAVE_FTRUNCATE
88 rc = 0;
81fea2b1 89 fclose(b->ufd);
81fea2b1 90 if (!loc) {
81fea2b1 91 if (!(bf = fopen(b->ufnm, f__w_mode[b->ufmt])))
81fea2b1
JL
92 rc = 1;
93 if (b->uwrt)
94 b->uwrt = 1;
95 goto done;
96 }
a843efa0
CB
97 if (!(bf = fopen(b->ufnm, f__r_mode[0]))
98 || !(tf = tmpfile())) {
dbf18916 99#ifdef NON_UNIX_STDIO
81fea2b1 100 bad:
dbf18916 101#endif
81fea2b1
JL
102 rc = 1;
103 goto done;
104 }
81fea2b1
JL
105 if (copy(bf, loc, tf)) {
106 bad1:
107 rc = 1;
108 goto done1;
109 }
110 if (!(bf = freopen(b->ufnm, f__w_mode[0], bf)))
111 goto bad1;
a843efa0 112 rewind(tf);
81fea2b1
JL
113 if (copy(tf, loc, bf))
114 goto bad1;
6973bf54 115 b->uwrt = 1;
a843efa0
CB
116 b->urw = 2;
117#ifdef NON_UNIX_STDIO
118 if (b->ufmt) {
119 fclose(bf);
120 if (!(bf = fopen(b->ufnm, f__w_mode[3])))
121 goto bad;
122 fseek(bf,0L,SEEK_END);
123 b->urw = 3;
81fea2b1 124 }
a843efa0 125#endif
81fea2b1
JL
126done1:
127 fclose(tf);
81fea2b1
JL
128done:
129 f__cf = b->ufd = bf;
97897047
TM
130#else /* !defined(HAVE_FTRUNCATE) */
131 fflush(b->ufd);
d2c43675
LR
132 /* The cast of loc is helpful on FreeBSD. It helps
133 in any case where ftruncate() prototype is somehow missing
134 even though autoconf test found it properly. */
135 rc = ftruncate(fileno(b->ufd), (off_t)loc);
97897047 136#endif /* !defined(HAVE_FTRUNCATE) */
81fea2b1
JL
137 if (rc)
138 err(a->aerr,111,"endfile");
139 return 0;
140 }
This page took 0.224463 seconds and 5 git commands to generate.