|
|
1.1 root 1: /*
2: * The routines in this file read and write ASCII files from the
3: * disk. All of the knowledge about files are here. A better message
4: * writing scheme should be used.
5: */
6: #include <stdio.h>
7: #include "ed.h"
8: #include <access.h>
9:
10: FILE *ffp; /* File pointer, all functions. */
11:
12: /*
13: * Open a file for reading.
14: */
15: ffropen(fn)
16: uchar *fn;
17: {
18: if ((ffp=fopen(fn, "r")) == NULL)
19: if (access(fn, AEXISTS)) /* access returns 0 on success */
20: return (FIOFNF); /* new file */
21: else
22: return (FIOERR); /* error */
23: return (FIOSUC);
24: }
25:
26: /*
27: * Open a file for writing.
28: * Return TRUE if all is well, and FALSE on error (cannot create).
29: */
30: ffwopen(fn, w)
31: uchar *fn, *w;
32: {
33: #if VMS
34: register int fd;
35:
36: if ((fd=creat(fn, 0666, "rfm=var", "rat=cr")) < 0
37: || (ffp=fdopen(fd, w)) == NULL) {
38: #else
39: if ((ffp=fopen(fn, w)) == NULL) {
40: #endif
41: mlwrite("Cannot open file for writing");
42: return (FIOERR);
43: }
44: return (FIOSUC);
45: }
46:
47: /*
48: * Close a file.
49: * Should look at the status in all systems.
50: */
51: ffclose()
52: {
53: #if V7
54: if (fclose(ffp) != FALSE) {
55: mlwrite("Error closing file");
56: return (FIOERR);
57: }
58: return (FIOSUC);
59: #endif
60: fclose(ffp);
61: return (FIOSUC);
62: }
63:
64: /*
65: * Write a line to the already opened file. The "buf" points to the
66: * buffer, and the "nbuf" is its length, less the free newline.
67: * Return the status.
68: * Check only at the newline.
69: */
70: ffputline(buf, nbuf)
71: #ifdef SLOW
72: register uchar buf[];
73: #else
74: uchar buf[];
75: #endif
76: {
77: register int i;
78:
79: #ifdef SLOW
80: for (i=0; i<nbuf; ++i)
81: putc(buf[i]&0xFF, ffp);
82: #else
83: fwrite(buf, 1, nbuf, ffp);
84: #endif
85: putc('\n', ffp);
86: if (ferror(ffp) != FALSE) {
87: mlwrite("Write I/O error");
88: return (FIOERR);
89: }
90: return (FIOSUC);
91: }
92:
93: /*
94: * Read a line from a file and store the bytes in the supplied buffer.
95: * The "nbuf" is the length of the buffer. Complain about long lines
96: * and lines at the end of the file that don't have a newline present.
97: * Check for I/O errors too. Return status.
98: */
99: ffgetline(buf, nbuf)
100: register uchar buf[];
101: {
102: register int c;
103: register int i;
104:
105: i = 0;
106: #ifdef SLOW
107: while ((c=getc(ffp))!=EOF && c!='\n') {
108: if (i >= nbuf-1) {
109: mlwrite("File has long line");
110: buf[i] = 0;
111: ungetc(c, ffp);
112: return (FIOSUC); /* Make long lines load */
113: }
114: buf[i++] = c;
115: }
116: #else
117: if (fgets(buf, nbuf, ffp) != NULL) {
118: for (i=0; i<nbuf-1; i++) {
119: if ((c = buf[i]) == '\n') {
120: buf[i] = 0;
121: goto lineread;
122: }
123: }
124: mlwrite("File has long line");
125: buf[i] = 0;
126: return FIOSUC;
127: } else
128: c = EOF;
129: lineread:
130: #endif
131: if (c == EOF) {
132: if (ferror(ffp) != FALSE) {
133: mlwrite("File read error");
134: return (FIOERR);
135: }
136: if (i != 0) {
137: mlwrite("File has funny line at EOF");
138: buf[i]=0; /* Make funny line show up. */
139: return (FIOSUC);
140: }
141: return (FIOEOF);
142: }
143: buf[i] = 0;
144: return (FIOSUC);
145: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.