Commit | Line | Data |
---|---|---|
bae7117f WH |
1 | #include "f2c.h" |
2 | #include "fio.h" | |
3 | #ifndef NON_UNIX_STDIO | |
4 | #include "sys/types.h" | |
5 | #endif | |
6 | #include "rawio.h" | |
7 | ||
8 | #ifdef KR_headers | |
9 | extern char *strcpy(); | |
10 | #else | |
11 | #undef abs | |
12 | #undef min | |
13 | #undef max | |
14 | #include "stdlib.h" | |
15 | #include "string.h" | |
16 | #endif | |
17 | ||
18 | #ifdef NON_UNIX_STDIO | |
19 | #ifndef unlink | |
20 | #define unlink remove | |
21 | #endif | |
22 | #else | |
23 | #ifdef MSDOS | |
24 | #include "io.h" | |
25 | #endif | |
26 | #endif | |
27 | ||
28 | #ifdef NON_UNIX_STDIO | |
29 | extern char *f__r_mode[], *f__w_mode[]; | |
30 | #endif | |
31 | ||
32 | #ifdef KR_headers | |
33 | integer f_end(a) alist *a; | |
34 | #else | |
35 | integer f_end(alist *a) | |
36 | #endif | |
37 | { | |
38 | unit *b; | |
39 | if(a->aunit>=MXUNIT || a->aunit<0) err(a->aerr,101,"endfile"); | |
40 | b = &f__units[a->aunit]; | |
41 | if(b->ufd==NULL) { | |
42 | char nbuf[10]; | |
43 | (void) sprintf(nbuf,"fort.%ld",a->aunit); | |
44 | #ifdef NON_UNIX_STDIO | |
45 | { FILE *tf; | |
46 | if (tf = fopen(nbuf, f__w_mode[0])) | |
47 | fclose(tf); | |
48 | } | |
49 | #else | |
50 | close(creat(nbuf, 0666)); | |
51 | #endif | |
52 | return(0); | |
53 | } | |
54 | b->uend=1; | |
55 | return(b->useek ? t_runc(a) : 0); | |
56 | } | |
57 | ||
58 | static int | |
59 | #ifdef NON_UNIX_STDIO | |
60 | #ifdef KR_headers | |
61 | copy(from, len, to) char *from, *to; register long len; | |
62 | #else | |
63 | copy(FILE *from, register long len, FILE *to) | |
64 | #endif | |
65 | { | |
66 | int k, len1; | |
67 | char buf[BUFSIZ]; | |
68 | ||
69 | while(fread(buf, len1 = len > BUFSIZ ? BUFSIZ : (int)len, 1, from)) { | |
70 | if (!fwrite(buf, len1, 1, to)) | |
71 | return 1; | |
72 | if ((len -= len1) <= 0) | |
73 | break; | |
74 | } | |
75 | return 0; | |
76 | } | |
77 | #else | |
78 | #ifdef KR_headers | |
79 | copy(from, len, to) char *from, *to; register long len; | |
80 | #else | |
81 | copy(char *from, register long len, char *to) | |
82 | #endif | |
83 | { | |
84 | register int n; | |
85 | int k, rc = 0, tmp; | |
86 | char buf[BUFSIZ]; | |
87 | ||
88 | if ((k = open(from, O_RDONLY)) < 0) | |
89 | return 1; | |
90 | if ((tmp = creat(to,0666)) < 0) | |
91 | return 1; | |
92 | while((n = read(k, buf, len > BUFSIZ ? BUFSIZ : (int)len)) > 0) { | |
93 | if (write(tmp, buf, n) != n) | |
94 | { rc = 1; break; } | |
95 | if ((len -= n) <= 0) | |
96 | break; | |
97 | } | |
98 | close(k); | |
99 | close(tmp); | |
100 | return n < 0 ? 1 : rc; | |
101 | } | |
102 | #endif | |
103 | ||
104 | #ifndef L_tmpnam | |
105 | #define L_tmpnam 16 | |
106 | #endif | |
107 | ||
108 | int | |
109 | #ifdef KR_headers | |
110 | t_runc(a) alist *a; | |
111 | #else | |
112 | t_runc(alist *a) | |
113 | #endif | |
114 | { | |
115 | char nm[L_tmpnam+12]; /* extra space in case L_tmpnam is tiny */ | |
116 | long loc, len; | |
117 | unit *b; | |
118 | #ifdef NON_UNIX_STDIO | |
119 | FILE *bf, *tf; | |
120 | #else | |
121 | FILE *bf; | |
122 | #endif | |
123 | int rc = 0; | |
124 | ||
125 | b = &f__units[a->aunit]; | |
126 | if(b->url) | |
127 | return(0); /*don't truncate direct files*/ | |
128 | loc=ftell(bf = b->ufd); | |
129 | fseek(bf,0L,SEEK_END); | |
130 | len=ftell(bf); | |
131 | if (loc >= len || b->useek == 0 || b->ufnm == NULL) | |
132 | return(0); | |
133 | #ifdef NON_UNIX_STDIO | |
134 | fclose(b->ufd); | |
135 | #else | |
136 | rewind(b->ufd); /* empty buffer */ | |
137 | #endif | |
138 | if (!loc) { | |
139 | #ifdef NON_UNIX_STDIO | |
140 | if (!(bf = fopen(b->ufnm, f__w_mode[b->ufmt]))) | |
141 | #else | |
142 | if (close(creat(b->ufnm,0666))) | |
143 | #endif | |
144 | rc = 1; | |
145 | if (b->uwrt) | |
146 | b->uwrt = 1; | |
147 | goto done; | |
148 | } | |
149 | #ifdef _POSIX_SOURCE | |
150 | tmpnam(nm); | |
151 | #else | |
152 | strcpy(nm,"tmp.FXXXXXX"); | |
153 | mktemp(nm); | |
154 | #endif | |
155 | #ifdef NON_UNIX_STDIO | |
156 | if (!(bf = fopen(b->ufnm, f__r_mode[0]))) { | |
157 | bad: | |
158 | rc = 1; | |
159 | goto done; | |
160 | } | |
161 | if (!(tf = fopen(nm, f__w_mode[0]))) | |
162 | goto bad; | |
163 | if (copy(bf, loc, tf)) { | |
164 | bad1: | |
165 | rc = 1; | |
166 | goto done1; | |
167 | } | |
168 | if (!(bf = freopen(b->ufnm, f__w_mode[0], bf))) | |
169 | goto bad1; | |
170 | if (!(tf = freopen(nm, f__r_mode[0], tf))) | |
171 | goto bad1; | |
172 | if (copy(tf, loc, bf)) | |
173 | goto bad1; | |
174 | if (f__w_mode[0] != f__w_mode[b->ufmt]) { | |
175 | if (!(bf = freopen(b->ufnm, f__w_mode[b->ufmt+2], bf))) | |
176 | goto bad1; | |
177 | fseek(bf, loc, SEEK_SET); | |
178 | } | |
179 | done1: | |
180 | fclose(tf); | |
181 | unlink(nm); | |
182 | done: | |
183 | f__cf = b->ufd = bf; | |
184 | #else | |
185 | if (copy(b->ufnm, loc, nm) | |
186 | || copy(nm, loc, b->ufnm)) | |
187 | rc = 1; | |
188 | unlink(nm); | |
189 | fseek(b->ufd, loc, SEEK_SET); | |
190 | done: | |
191 | #endif | |
192 | if (rc) | |
193 | err(a->aerr,111,"endfile"); | |
194 | return 0; | |
195 | } |