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