-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfileio.c
157 lines (135 loc) · 3.16 KB
/
fileio.c
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
#include <stdio.h>
#include "estruct.h"
#include "etype.h"
#include "edef.h"
#include "english.h"
FILE *ffp; /* File pointer, all functions. */
static int eofflag; /* end-of-file flag */
/* Open a file for reading. */
ffropen(char *fn)
{
if ((ffp = fopen(fn, "r")) == (FILE *) NULL)
return (FIOFNF);
eofflag = FALSE;
return (FIOSUC);
}
/*
* Open a file for writing. Return TRUE if all is well, and FALSE on error
* (cannot create).
*/
ffwopen(char *fn)
{
if ((ffp = fopen(fn, "w")) == (FILE *) NULL)
{
mlwrite(TEXT155);
/* "Cannot open file for writing" */
return (FIOERR);
}
return (FIOSUC);
}
/* Close a file. Should look at the status in all systems. */
ffclose()
{
/* free this since we do not need it anymore */
if (fline)
{
free(fline);
fline = (char *) NULL;
}
if (fclose(ffp) != FALSE)
{
mlwrite(TEXT156);
/* "Error closing file" */
return (FIOERR);
}
return (FIOSUC);
}
/*
* 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.
*/
ffputline(char buf[], int nbuf)
{
register int i;
for (i = 0; i < nbuf; ++i)
putc(buf[i], ffp);
putc('\n', ffp);
if (ferror(ffp))
{
mlwrite(TEXT157);
/* "Write I/O error" */
return (FIOERR);
}
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.
*/
ffgetline()
{
register int c; /* current character read */
register int i; /* current index into fline */
register char *tmpline; /* temp storage for expanding line */
/* if we are at the end...return it */
if (eofflag)
return (FIOEOF);
/* dump fline if it ended up too big */
if (flen > NSTRING && fline != (char *) NULL)
{
free(fline);
fline = (char *) NULL;
}
/* if we don't have an fline, allocate one */
if (fline == (char *) NULL)
if ((fline = malloc(flen = NSTRING)) == (char *) 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 ((tmpline = malloc(flen + NSTRING)) == (char *) NULL)
return (FIOMEM);
bytecopy(tmpline, fline, flen);
flen += NSTRING;
free(fline);
fline = tmpline;
}
}
/* 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;
return (FIOSUC);
}
int fexist(char *fname) /* does <fname> exist on disk? */
{
FILE *fp;
/* try to open the file for reading */
fp = fopen(fname, "r");
/* if it fails, just return false! */
if (fp == (FILE *) NULL)
return (FALSE);
/* otherwise, close it and report true */
fclose(fp);
return (TRUE);
}