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