-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileio.c.bak
276 lines (232 loc) · 5.78 KB
/
fileio.c.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* FILEIO.C: Low level file i/o routines
MicroEMACS 3.12
* The routines in this file read and write ASCII files from the disk. All of
* the knowledge about files are here.
*/
#include <stdio.h>
#include "estruct.h"
#include "eproto.h"
#include "edef.h"
#include "elang.h"
#if AOSVS
#define fopen xxfopen
#endif
NOSHARE FILE *ffp; /* File pointer, all functions. */
static int eofflag; /* end-of-file flag */
#if (MSC || TURBO || IC) && MSDOS
#define FILE_BUFSIZE 4096
char file_buffer[FILE_BUFSIZE];
#endif
/*
* Open a file for reading.
*/
#if !(VMS & RMSIO) /* if using RMS under VMS, the code */
/* is in VMS.C */
PASCAL NEAR ffropen(fn)
char *fn;
{
if ((ffp=fopen(fn, "r")) == NULL)
return(FIOFNF);
#if (MSC || TURBO || IC) && MSDOS
/* tell the library to give us a LARGE buffer to speed I/O */
setvbuf(ffp, file_buffer, _IOFBF, FILE_BUFSIZE);
#endif
#if WINDOW_MSWIN
fbusy = FREADING;
#endif
eofflag = FALSE;
return(FIOSUC);
}
/*
* Open a file for writing. Return TRUE if all is well, and FALSE on error
* (cannot create).
*/
#if AOSVS == 0
PASCAL NEAR ffwopen(fn, mode)
char *fn;
char *mode; /* mode to open file for */
{
char xmode[6]; /* extended file open mode */
/* nonstandard line terminators? */
if (*lterm) {
/* open in binary mode */
strcpy(xmode, mode);
strcat(xmode, "b");
ffp = fopen(fn, xmode);
} else {
/* open in ascii(text) mode */
ffp = fopen(fn, mode);
}
if (ffp == NULL) {
mlwrite(TEXT155);
/* "Cannot open file for writing" */
return(FIOERR);
}
#if (MSC || TURBO || IC) && MSDOS
/* tell the library to give us a LARGE buffer to speed I/O */
setvbuf(ffp, file_buffer, _IOFBF, FILE_BUFSIZE);
#endif
#if WINDOW_MSWIN
fbusy = FWRITING;
#endif
return(FIOSUC);
}
#endif
/*
* Close a file. Should look at the status in all systems.
*/
PASCAL NEAR ffclose()
{
/* free this since we do not need it anymore */
if (fline) {
free(fline);
fline = NULL;
}
#if WINDOW_MSWIN
fbusy = FALSE;
#endif
#if MSDOS & CTRLZ
putc(26, ffp); /* add a ^Z at the end of the file */
#endif
#if USG | AIX | AUX | SMOS | HPUX8 | HPUX9 | SUN | XENIX | BSD || FREEBSD | WMCS | VMS | (MSDOS & (LATTICE | MSC | TURBO | IC | ZTC)) | WINNT | OS2 | (TOS & MWC) | AVIION
if (fclose(ffp) != FALSE) {
mlwrite(TEXT156);
/* "Error closing file" */
return(FIOERR);
}
return(FIOSUC);
#else
fclose(ffp);
return(FIOSUC);
#endif
}
/*
* Write a line to the already opened file. The "buf" points to the buffer,
* and the "nbuf" is its length, less the free newline. Return the status.
* Check only at the newline.
*/
PASCAL NEAR ffputline(buf, nbuf)
char buf[];
int nbuf;
{
register int i; /* index into line to write */
register char *lptr; /* ptr into the line terminator */
#if CRYPT
char c; /* character to translate */
if (cryptflag) {
for (i = 0; i < nbuf; ++i) {
c = buf[i];
ecrypt(&c, 1);
putc(c, ffp);
}
} else
for (i = 0; i < nbuf; ++i)
putc(buf[i], ffp);
#else
for (i = 0; i < nbuf; ++i)
putc(buf[i], ffp);
#endif
/* write out the appropriate line terminator(s) */
if (*lterm) {
lptr = <erm[0];
while (*lptr)
putc(*lptr++, ffp);
} else {
putc('\n', ffp);
}
/* check for write errors */
if (ferror(ffp)) {
mlwrite(TEXT157);
/* "Write I/O error" */
return(FIOERR);
}
#if WINDOW_MSWIN
{
static int o = 0;
if (--o < 0) {
longop(TRUE);
o = 10; /* to lower overhead, only 10% calls to longop */
}
}
#endif
return(FIOSUC);
}
/*
* Read a line from a file, and store the bytes in the supplied buffer. The
* "nbuf" is the length of the buffer. Complain about long lines and lines
* at the end of the file that don't have a newline present. Check for I/O
* errors too. Return status.
*/
PASCAL NEAR ffgetline(nbytes)
int *nbytes;
{
register int c; /* current character read */
register int i; /* current index into fline */
/* if we are at the end...return it */
if (eofflag)
return(FIOEOF);
/* dump fline if it ended up too big */
if (flen > NSTRING && fline != NULL) {
free(fline);
fline = NULL;
}
/* if we don't have an fline, allocate one */
if (fline == NULL)
if ((fline = room(flen = NSTRING)) == NULL)
return(FIOMEM);
/* read the line in */
i = 0;
while ((c = getc(ffp)) != EOF && c != '\n') {
fline[i++] = c;
/* if it's longer, get more room */
if (i >= flen) {
#if MSDOS
if (flen >= 16636)
return(FIOMEM);
#endif
flen *= 2;
if ((fline = reroom(fline, flen)) == NULL) {
return(FIOMEM);
}
}
}
/* dump any extra line terminators at the end */
while (i > 0 && (fline[i-1] == 10 || fline[i-1] == 13))
i--;
/* and save the length for our caller... */
*nbytes = i;
/* we should be ready to dump leading terminators too - ADD THIS DAN */
/* test for any errors that may have occured */
if (c == EOF) {
if (ferror(ffp)) {
mlwrite(TEXT158);
/* "File read error" */
return(FIOERR);
}
if (i != 0)
eofflag = TRUE;
else
return(FIOEOF);
}
/* terminate and decrypt the string */
fline[i] = 0;
#if CRYPT
if (cryptflag)
ecrypt(fline, strlen(fline));
#endif
return(FIOSUC);
}
#endif
int PASCAL NEAR fexist(fname) /* does <fname> exist on disk? */
char *fname; /* file to check for existance */
{
FILE *fp;
/* try to open the file for reading */
fp = fopen(fname, "r");
/* if it fails, just return false! */
if (fp == NULL)
return(FALSE);
/* otherwise, close it and report true */
fclose(fp);
return(TRUE);
}