|
|
1.1 root 1: #include "f2c.h"
2: #include "fio.h"
3: #include "sys/types.h"
4: #include "fcntl.h"
5: #include "rawio.h"
6: #ifndef O_RDONLY
7: #define O_RDONLY 0
8: #endif
9:
10: #ifdef KR_headers
11: extern char *strcpy();
12: #else
13: #undef abs
14: #undef min
15: #undef max
16: #include "stdlib.h"
17: #include "string.h"
18: #endif
19:
20:
21: #ifdef KR_headers
22: integer f_end(a) alist *a;
23: #else
24: integer f_end(alist *a)
25: #endif
26: {
27: unit *b;
28: if(a->aunit>=MXUNIT || a->aunit<0) err(a->aerr,101,"endfile");
29: b = &f__units[a->aunit];
30: if(b->ufd==NULL) {
31: char nbuf[10];
32: (void) sprintf(nbuf,"fort.%ld",a->aunit);
33: close(creat(nbuf, 0666));
34: return(0);
35: }
36: b->uend=1;
37: return(b->useek ? t_runc(a) : 0);
38: }
39:
40: static int
41: #ifdef KR_headers
42: copy(from, len, to) char *from, *to; register long len;
43: #else
44: copy(char *from, register long len, char *to)
45: #endif
46: {
47: register int n;
48: int k, rc = 0, tmp;
49: char buf[BUFSIZ];
50:
51: if ((k = open(from, O_RDONLY)) < 0)
52: return 1;
53: if ((tmp = creat(to,0666)) < 0)
54: return 1;
55: while((n = read(k, buf, len > BUFSIZ ? BUFSIZ : (int)len)) > 0) {
56: if (write(tmp, buf, n) != n)
57: { rc = 1; break; }
58: if ((len -= n) <= 0)
59: break;
60: }
61: close(k);
62: close(tmp);
63: return n < 0 ? 1 : rc;
64: }
65:
66: #ifndef L_tmpnam
67: #define L_tmpnam 16
68: #endif
69:
70: int
71: #ifdef KR_headers
72: t_runc(a) alist *a;
73: #else
74: t_runc(alist *a)
75: #endif
76: {
77: char nm[L_tmpnam+12]; /* extra space in case L_tmpnam is tiny */
78: long loc, len;
79: unit *b;
80: int rc = 0;
81:
82: b = &f__units[a->aunit];
83: if(b->url) return(0); /*don't truncate direct files*/
84: loc=ftell(b->ufd);
85: (void) fseek(b->ufd,0L,SEEK_END);
86: len=ftell(b->ufd);
87: if (loc >= len || b->useek == 0 || b->ufnm == NULL)
88: return(0);
89: #ifdef MSDOS
90: fclose(b->ufd);
91: #else
92: rewind(b->ufd); /* empty buffer */
93: #endif
94: if (!loc) {
95: if (close(creat(b->ufnm,0666)))
96: #ifdef MSDOS
97: rc = 1;
98: goto done;
99: #else
100: { rc = 1; goto done; }
101: if (b->uwrt)
102: b->uwrt = 1;
103: return 0;
104: #endif
105: }
106: #ifdef _POSIX_SOURCE
107: tmpnam(nm);
108: #else
109: (void) strcpy(nm,"tmp.FXXXXXX");
110: (void) mktemp(nm);
111: #endif
112: if (copy(b->ufnm, loc, nm)
113: || copy(nm, loc, b->ufnm))
114: rc = 1;
115: unlink(nm);
116: done:
117: #ifdef MSDOS
118: {
119: extern char *f__r_mode[];
120: b->uwrt = 0;
121: if (!(b->ufd = fopen(b->ufnm, f__r_mode[b->ufmt])))
122: rc = 1;
123: }
124: if (loc)
125: #endif
126: fseek(b->ufd, loc, SEEK_SET);
127: if (rc)
128: err(a->aerr,111,"endfile");
129: return 0;
130: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.