-
Notifications
You must be signed in to change notification settings - Fork 2
/
fileio.c
119 lines (104 loc) · 2.3 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
/*
* The routines in this file read and write ASCII files from the disk. All of
* the knowledge about files is here. A better message writing scheme should
* be used
*/
#include <stdio.h> /* fopen(3), et.al. */
#include "estruct.h"
extern void mlwrite ();
int ffropen (char *fn);
int ffwopen (char *fn);
int ffclose ();
int ffputline (char buf[], int nbuf);
int ffgetline (char buf[], int nbuf);
FILE *ffp; /* File pointer, all functions */
/*
* Open a file for reading.
*/
int ffropen (char *fn)
{
if ((ffp = fopen (fn, "r")) == NULL)
return (FIOFNF);
return (FIOSUC);
}
/*
* Open a file for writing. Return TRUE if all is well, and FALSE on error
* (cannot create).
*/
int ffwopen (char *fn)
{
if ((ffp = fopen (fn, "w")) == NULL)
{
mlwrite ("Cannot open file for writing");
return (FIOERR);
}
return (FIOSUC);
}
/*
* Close a file. Should look at the status in all systems.
*/
int ffclose ()
{
if (fclose (ffp) != FALSE)
{
mlwrite ("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.
*/
int ffputline (char buf[], int nbuf)
{
int i;
for (i = 0; i < nbuf; ++i)
fputc (buf[i] & 0xFF, ffp);
fputc ('\n', ffp);
if (ferror (ffp))
{
mlwrite ("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.
*/
int ffgetline (char buf[], int nbuf)
{
int c, i;
i = 0;
while ((c = fgetc (ffp)) != EOF && c != '\n')
{
if (i >= nbuf - 2)
{
buf[nbuf - 2] = c; /* store last char read */
buf[nbuf - 1] = 0; /* and terminate it */
mlwrite ("File has long line");
return (FIOLNG);
}
buf[i++] = c;
}
if (c == EOF)
{
if (ferror (ffp))
{
mlwrite ("File read error");
return (FIOERR);
}
if (i != 0)
{
mlwrite ("File has funny line at EOF");
return (FIOERR);
}
return (FIOEOF);
}
buf[i] = 0;
return (FIOSUC);
}