Fix for a crash which happened when a document couldn't be opened.
[AROS-Contrib.git] / fish / microemacs / fileio.c
blobb8537f7b4cbbc54ccac6fc85f95763d777eec1f5
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 "ed.h"
9 FILE *ffp; /* File pointer, all functions. */
12 * Open a file for reading.
14 int ffropen(fn)
15 const char *fn;
17 if ((ffp=fopen(fn, "r")) == NULL)
18 return (FIOFNF);
19 return (FIOSUC);
23 * Open a file for writing. Return TRUE if all is well, and FALSE on error
24 * (cannot create).
26 int ffwopen(fn)
27 const char *fn;
29 #if VMS
30 register int fd;
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);
40 return (FIOSUC);
44 * Close a file. Should look at the status in all systems.
46 int ffclose()
48 #if V7
49 if (fclose(ffp) != FALSE) {
50 mlwrite("Error closing file");
51 return(FIOERR);
53 return(FIOSUC);
54 #endif
55 fclose(ffp);
56 return (FIOSUC);
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.
64 int ffputline(buf, nbuf)
65 char buf[];
66 int nbuf;
68 register int i;
70 for (i = 0; i < nbuf; ++i)
71 fputc(buf[i]&0xFF, ffp);
73 fputc('\n', ffp);
75 if (ferror(ffp)) {
76 mlwrite("Write I/O error");
77 return (FIOERR);
80 return (FIOSUC);
84 * Read a line from a file, and store the bytes in the supplied buffer. The
85 * "nbuf" is the length of the buffer. Complain about long lines and lines
86 * at the end of the file that don't have a newline present. Check for I/O
87 * errors too. Return status.
89 int ffgetline(buf, nbuf)
90 register char buf[];
91 int nbuf;
93 register int c;
94 register int i;
96 i = 0;
98 while ((c = fgetc(ffp)) != EOF && c != '\n') {
99 if (i >= nbuf-1) {
100 mlwrite("File has long line");
101 return (FIOERR);
103 buf[i++] = c;
106 if (c == EOF) {
107 if (ferror(ffp)) {
108 mlwrite("File read error");
109 return (FIOERR);
112 if (i != 0) {
113 mlwrite("File has funny line at EOF");
114 return (FIOERR);
116 return (FIOEOF);
119 buf[i] = 0;
120 return (FIOSUC);