nail.h:ISQUOTE(): use n_WC_C() not L character constant prefix
[s-mailx.git] / fio.c
blob506e3907f0734f933ec5d80373ac3ef77b1cc810
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ File operations.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE fio
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 /* line is a buffer with the result of fgets(). Returns the first newline or
43 * the last character read */
44 static size_t _length_of_line(char const *line, size_t linesize);
46 /* Read a line, one character at a time */
47 static char * _fgetline_byone(char **line, size_t *linesize, size_t *llen,
48 FILE *fp, int appendnl, size_t n SMALLOC_DEBUG_ARGS);
50 /* Workhorse */
51 static bool_t a_file_lock(int fd, enum n_file_lock_type ft, off_t off, off_t len);
53 static size_t
54 _length_of_line(char const *line, size_t linesize)
56 size_t i;
57 NYD2_ENTER;
59 /* Last character is always '\0' and was added by fgets() */
60 for (--linesize, i = 0; i < linesize; i++)
61 if (line[i] == '\n')
62 break;
63 i = (i < linesize) ? i + 1 : linesize;
64 NYD2_LEAVE;
65 return i;
68 static char *
69 _fgetline_byone(char **line, size_t *linesize, size_t *llen, FILE *fp,
70 int appendnl, size_t n SMALLOC_DEBUG_ARGS)
72 char *rv;
73 int c;
74 NYD2_ENTER;
76 assert(*linesize == 0 || *line != NULL);
77 for (rv = *line;;) {
78 if (*linesize <= LINESIZE || n >= *linesize - 128) {
79 *linesize += ((rv == NULL) ? LINESIZE + n + 1 : 256);
80 *line = rv = (srealloc)(rv, *linesize SMALLOC_DEBUG_ARGSCALL);
82 c = getc(fp);
83 if (c != EOF) {
84 rv[n++] = c;
85 rv[n] = '\0';
86 if (c == '\n')
87 break;
88 } else {
89 if (n > 0) {
90 if (appendnl) {
91 rv[n++] = '\n';
92 rv[n] = '\0';
94 break;
95 } else {
96 rv = NULL;
97 goto jleave;
101 if (llen)
102 *llen = n;
103 jleave:
104 NYD2_LEAVE;
105 return rv;
108 static bool_t
109 a_file_lock(int fd, enum n_file_lock_type flt, off_t off, off_t len)
111 struct flock flp;
112 bool_t rv;
113 NYD2_ENTER;
115 memset(&flp, 0, sizeof flp);
117 switch (flt) {
118 default:
119 case FLT_READ: rv = F_RDLCK; break;
120 case FLT_WRITE: rv = F_WRLCK; break;
122 flp.l_type = rv;
123 flp.l_start = off;
124 flp.l_whence = SEEK_SET;
125 flp.l_len = len;
127 if (!(rv = (fcntl(fd, F_SETLK, &flp) != -1)))
128 switch (errno) {
129 case EBADF:
130 case EINVAL:
131 rv = TRUM1;
132 break;
134 NYD2_LEAVE;
135 return rv;
138 FL char *
139 (fgetline)(char **line, size_t *linesize, size_t *cnt, size_t *llen, FILE *fp,
140 int appendnl SMALLOC_DEBUG_ARGS)
142 size_t i_llen, sz;
143 char *rv;
144 NYD2_ENTER;
146 if (cnt == NULL) {
147 /* Without count, we can't determine where the chars returned by fgets()
148 * end if there's no newline. We have to read one character by one */
149 rv = _fgetline_byone(line, linesize, llen, fp, appendnl, 0
150 SMALLOC_DEBUG_ARGSCALL);
151 goto jleave;
154 if ((rv = *line) == NULL || *linesize < LINESIZE)
155 *line = rv = (srealloc)(rv, *linesize = LINESIZE SMALLOC_DEBUG_ARGSCALL);
156 sz = (*linesize <= *cnt) ? *linesize : *cnt + 1;
157 if (sz <= 1 || fgets(rv, sz, fp) == NULL) {
158 /* Leave llen untouched; it is used to determine whether the last line
159 * was \n-terminated in some callers */
160 rv = NULL;
161 goto jleave;
164 i_llen = _length_of_line(rv, sz);
165 *cnt -= i_llen;
166 while (rv[i_llen - 1] != '\n') {
167 *line = rv = (srealloc)(rv, *linesize += 256 SMALLOC_DEBUG_ARGSCALL);
168 sz = *linesize - i_llen;
169 sz = (sz <= *cnt) ? sz : *cnt + 1;
170 if (sz <= 1 || fgets(rv + i_llen, sz, fp) == NULL) {
171 if (appendnl) {
172 rv[i_llen++] = '\n';
173 rv[i_llen] = '\0';
175 break;
177 sz = _length_of_line(rv + i_llen, sz);
178 i_llen += sz;
179 *cnt -= sz;
181 if (llen)
182 *llen = i_llen;
183 jleave:
184 NYD2_LEAVE;
185 return rv;
188 FL int
189 (readline_restart)(FILE *ibuf, char **linebuf, size_t *linesize, size_t n
190 SMALLOC_DEBUG_ARGS)
192 /* TODO readline_restart(): always *appends* LF just to strip it again;
193 * TODO should be configurable just as for fgetline(); ..or whatever.. */
194 int rv = -1;
195 long sz;
196 NYD2_ENTER;
198 clearerr(ibuf);
200 /* Interrupts will cause trouble if we are inside a stdio call. As this is
201 * only relevant if input is from tty, bypass it by read(), then */
202 if (fileno(ibuf) == 0 && (options & OPT_TTYIN)) {
203 assert(*linesize == 0 || *linebuf != NULL);
204 for (;;) {
205 if (*linesize <= LINESIZE || n >= *linesize - 128) {
206 *linesize += ((*linebuf == NULL) ? LINESIZE + n + 1 : 256);
207 *linebuf = (srealloc)(*linebuf, *linesize SMALLOC_DEBUG_ARGSCALL);
209 jagain:
210 sz = read(0, *linebuf + n, *linesize - n - 1);
211 if (sz > 0) {
212 n += sz;
213 (*linebuf)[n] = '\0';
214 if (n > 0 && (*linebuf)[n - 1] == '\n')
215 break;
216 } else {
217 if (sz < 0 && errno == EINTR)
218 goto jagain;
219 if (n > 0) {
220 if ((*linebuf)[n - 1] != '\n') {
221 (*linebuf)[n++] = '\n';
222 (*linebuf)[n] = '\0';
224 break;
225 } else
226 goto jleave;
229 } else {
230 /* Not reading from standard input or standard input not a terminal. We
231 * read one char at a time as it is the only way to get lines with
232 * embedded NUL characters in standard stdio */
233 if (_fgetline_byone(linebuf, linesize, &n, ibuf, 1, n
234 SMALLOC_DEBUG_ARGSCALL) == NULL)
235 goto jleave;
237 if (n > 0 && (*linebuf)[n - 1] == '\n')
238 (*linebuf)[--n] = '\0';
239 rv = (int)n;
240 jleave:
241 NYD2_LEAVE;
242 return rv;
245 FL void
246 setptr(FILE *ibuf, off_t offset)
248 struct message self;
249 char *cp, *linebuf = NULL;
250 char const *cp2;
251 int c, maybe = 1, inhead = 0, selfcnt = 0;
252 size_t linesize = 0, filesize, cnt;
253 NYD_ENTER;
255 memset(&self, 0, sizeof self);
256 self.m_flag = MUSED | MNEW | MNEWEST;
257 filesize = mailsize - offset;
258 offset = ftell(mb.mb_otf);
260 for (;;) {
261 if (fgetline(&linebuf, &linesize, &filesize, &cnt, ibuf, 0) == NULL) {
262 self.m_xsize = self.m_size;
263 self.m_xlines = self.m_lines;
264 self.m_have = HAVE_HEADER | HAVE_BODY;
265 if (selfcnt > 0)
266 message_append(&self);
267 message_append_null();
268 if (linebuf != NULL)
269 free(linebuf);
270 break;
273 #ifdef notdef
274 if (linebuf[0] == '\0')
275 linebuf[0] = '.';
276 #endif
277 /* XXX Convert CRLF to LF; this should be rethought in that
278 * XXX CRLF input should possibly end as CRLF output? */
279 if (cnt >= 2 && linebuf[cnt - 1] == '\n' && linebuf[cnt - 2] == '\r')
280 linebuf[--cnt - 1] = '\n';
281 fwrite(linebuf, sizeof *linebuf, cnt, mb.mb_otf);
282 if (ferror(mb.mb_otf)) {
283 n_perr(_("/tmp"), 0);
284 exit(EXIT_ERR);
286 if (linebuf[cnt - 1] == '\n')
287 linebuf[cnt - 1] = '\0';
288 if (maybe && linebuf[0] == 'F' && is_head(linebuf, cnt, FAL0)) {
289 /* TODO char date[FROM_DATEBUF];
290 * TODO extract_date_from_from_(linebuf, cnt, date);
291 * TODO self.m_time = 10000; */
292 self.m_xsize = self.m_size;
293 self.m_xlines = self.m_lines;
294 self.m_have = HAVE_HEADER | HAVE_BODY;
295 if (selfcnt++ > 0)
296 message_append(&self);
297 msgCount++;
298 self.m_flag = MUSED | MNEW | MNEWEST;
299 self.m_size = 0;
300 self.m_lines = 0;
301 self.m_block = mailx_blockof(offset);
302 self.m_offset = mailx_offsetof(offset);
303 inhead = 1;
304 } else if (linebuf[0] == 0) {
305 inhead = 0;
306 } else if (inhead) {
307 for (cp = linebuf, cp2 = "status";; ++cp) {
308 if ((c = *cp2++) == 0) {
309 while (c = *cp++, whitechar(c))
311 if (cp[-1] != ':')
312 break;
313 while ((c = *cp++) != '\0')
314 if (c == 'R')
315 self.m_flag |= MREAD;
316 else if (c == 'O')
317 self.m_flag &= ~MNEW;
318 break;
320 if (*cp != c && *cp != upperconv(c))
321 break;
323 for (cp = linebuf, cp2 = "x-status";; ++cp) {
324 if ((c = *cp2++) == 0) {
325 while ((c = *cp++, whitechar(c)))
327 if (cp[-1] != ':')
328 break;
329 while ((c = *cp++) != '\0')
330 if (c == 'F')
331 self.m_flag |= MFLAGGED;
332 else if (c == 'A')
333 self.m_flag |= MANSWERED;
334 else if (c == 'T')
335 self.m_flag |= MDRAFTED;
336 break;
338 if (*cp != c && *cp != upperconv(c))
339 break;
342 offset += cnt;
343 self.m_size += cnt;
344 ++self.m_lines;
345 maybe = linebuf[0] == 0;
347 NYD_LEAVE;
350 FL int
351 putline(FILE *obuf, char *linebuf, size_t cnt)
353 int rv = -1;
354 NYD_ENTER;
356 fwrite(linebuf, sizeof *linebuf, cnt, obuf);
357 putc('\n', obuf);
358 if (!ferror(obuf))
359 rv = (int)(cnt + 1);
360 NYD_LEAVE;
361 return rv;
364 FL off_t
365 fsize(FILE *iob)
367 struct stat sbuf;
368 off_t rv;
369 NYD_ENTER;
371 rv = (fstat(fileno(iob), &sbuf) == -1) ? 0 : sbuf.st_size;
372 NYD_LEAVE;
373 return rv;
376 FL char const *
377 getdeadletter(void) /* XXX should that be in auxlily.c? */
379 char const *cp_base, *cp;
380 NYD_ENTER;
382 cp = fexpand(cp_base = ok_vlook(DEAD), FEXP_LOCAL | FEXP_NSHELL);
383 if (cp == NULL)
384 cp = cp_base;
385 NYD_LEAVE;
386 return cp;
389 FL bool_t
390 n_file_lock(int fd, enum n_file_lock_type flt, off_t off, off_t len,
391 size_t pollmsecs)
393 size_t tries;
394 bool_t didmsg, rv;
395 NYD_ENTER;
397 for (didmsg = FAL0, tries = 0; tries <= FILE_LOCK_TRIES; ++tries) {
398 rv = a_file_lock(fd, flt, off, len);
400 if (rv == TRUM1) {
401 rv = FAL0;
402 break;
404 if (rv || pollmsecs == 0)
405 break;
406 else {
407 if(!didmsg){
408 n_err(_("Failed to create a file lock, waiting %lu milliseconds "),
409 pollmsecs);
410 didmsg = TRU1;
411 }else
412 n_err(".");
413 n_msleep(pollmsecs, FAL0);
416 if(didmsg)
417 n_err(" %s\n", (rv ? _("ok") : _("failure")));
418 NYD_LEAVE;
419 return rv;
422 /* s-it-mode */