* Clean up some function definitions to comply with strict
[alpine.git] / pico / fileio.c
blob236be93245380b318648860e38e1f3e6b042e9ea
1 /*
2 * ========================================================================
3 * Copyright 2006 University of Washington
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * ========================================================================
13 * Program: file reading routines
18 * The routines in this file read and write ASCII files from the disk. All of
19 * the knowledge about files are here. A better message writing scheme should
20 * be used.
22 #include "headers.h"
23 #include "../pith/charconv/filesys.h"
26 #if defined(bsd) || defined(lnx)
27 #ifndef errno
28 extern int errno;
29 #endif
30 #endif
34 FIOINFO g_pico_fio;
37 * Open a file for reading.
39 int
40 ffropen(char *fn)
42 int status;
44 if ((status = fexist(g_pico_fio.name = fn, "r", (off_t *)NULL)) == FIOSUC){
45 g_pico_fio.flags = FIOINFO_READ;
46 if((g_pico_fio.fp = our_fopen(g_pico_fio.name, "r")) == NULL)
47 status = FIOFNF;
50 return (status);
55 * Write a line to the already opened file. The "buf" points to the buffer,
56 * and the "nbuf" is its length, less the free newline. Return the status.
57 * Check only at the newline.
59 int
60 ffputline(CELL buf[], int nbuf)
62 register int i;
63 EML eml;
65 for(i = 0; i < nbuf; ++i)
66 if(write_a_wide_char((UCS) buf[i].c, g_pico_fio.fp) == EOF)
67 break;
69 if(i == nbuf)
70 write_a_wide_char((UCS) '\n', g_pico_fio.fp);
72 if(ferror(g_pico_fio.fp)){
73 eml.s = errstr(errno);
74 emlwwrite(_("Write error: %s"), &eml);
75 sleep(5);
76 return FIOERR;
79 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 * Translate the line from the user's locale charset to UCS-4.
91 int
92 ffgetline(UCS buf[], size_t nbuf, size_t *charsreturned, int msg)
94 size_t i;
95 UCS ucs;
96 static int displayed = 0;
98 if(charsreturned)
99 *charsreturned = 0;
101 i = 0;
103 while((ucs = read_a_wide_char(g_pico_fio.fp, input_cs)) != CCONV_EOF && ucs != '\n'){
105 * Don't blat the CR should the newline be CRLF and we're
106 * running on a unix system. NOTE: this takes care of itself
107 * under DOS since the non-binary open turns newlines into '\n'.
109 if(ucs == '\r'){
110 if((ucs = read_a_wide_char(g_pico_fio.fp, input_cs)) == CCONV_EOF || ucs == '\n')
111 break;
113 if(i < nbuf-2) /* Bare CR. Insert it and go on... */
114 buf[i++] = '\r'; /* else, we're up a creek */
117 if(i >= nbuf-2){
118 buf[nbuf - 2] = ucs; /* store last char read */
119 buf[nbuf - 1] = '\0'; /* and terminate it */
120 if(charsreturned)
121 *charsreturned = nbuf - 1;
123 if(msg && displayed == 0){
124 emlwrite("File has long line", NULL);
125 displayed = 1;
128 return FIOLNG;
131 buf[i++] = ucs;
134 if(ucs == CCONV_EOF){
135 if(ferror(g_pico_fio.fp)){
136 emlwrite("File read error", NULL);
137 if(charsreturned)
138 *charsreturned = i;
140 return FIOERR;
143 if(i != 0)
144 emlwrite("File doesn't end with newline. Adding one.", NULL);
145 else{
146 if(charsreturned)
147 *charsreturned = i;
148 return FIOEOF;
152 buf[i] = '\0';
154 if(charsreturned)
155 *charsreturned = i;
157 return FIOSUC;