mk-release.inc: update
[s-mailx.git] / fio.c
blob28bfb9f5bf18cdee5e06fe8da794bfe676059f37
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 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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 pstate &= ~PS_READLINE_NL;
79 for (rv = *line;;) {
80 if (*linesize <= LINESIZE || n >= *linesize - 128) {
81 *linesize += ((rv == NULL) ? LINESIZE + n + 1 : 256);
82 *line = rv = (srealloc)(rv, *linesize SMALLOC_DEBUG_ARGSCALL);
84 c = getc(fp);
85 if (c != EOF) {
86 rv[n++] = c;
87 rv[n] = '\0';
88 if (c == '\n') {
89 pstate |= PS_READLINE_NL;
90 break;
92 } else {
93 if (n > 0) {
94 if (appendnl) {
95 rv[n++] = '\n';
96 rv[n] = '\0';
98 break;
99 } else {
100 rv = NULL;
101 goto jleave;
105 if (llen)
106 *llen = n;
107 jleave:
108 NYD2_LEAVE;
109 return rv;
112 static bool_t
113 a_file_lock(int fd, enum n_file_lock_type flt, off_t off, off_t len)
115 struct flock flp;
116 bool_t rv;
117 NYD2_ENTER;
119 memset(&flp, 0, sizeof flp);
121 switch (flt) {
122 default:
123 case FLT_READ: rv = F_RDLCK; break;
124 case FLT_WRITE: rv = F_WRLCK; break;
126 flp.l_type = rv;
127 flp.l_start = off;
128 flp.l_whence = SEEK_SET;
129 flp.l_len = len;
131 if (!(rv = (fcntl(fd, F_SETLK, &flp) != -1)))
132 switch (errno) {
133 case EBADF:
134 case EINVAL:
135 rv = TRUM1;
136 break;
138 NYD2_LEAVE;
139 return rv;
142 FL char *
143 (fgetline)(char **line, size_t *linesize, size_t *cnt, size_t *llen, FILE *fp,
144 int appendnl SMALLOC_DEBUG_ARGS)
146 size_t i_llen, sz;
147 char *rv;
148 NYD2_ENTER;
150 if (cnt == NULL) {
151 /* Without count, we can't determine where the chars returned by fgets()
152 * end if there's no newline. We have to read one character by one */
153 rv = _fgetline_byone(line, linesize, llen, fp, appendnl, 0
154 SMALLOC_DEBUG_ARGSCALL);
155 goto jleave;
158 pstate &= ~PS_READLINE_NL;
160 if ((rv = *line) == NULL || *linesize < LINESIZE)
161 *line = rv = (srealloc)(rv, *linesize = LINESIZE SMALLOC_DEBUG_ARGSCALL);
162 sz = (*linesize <= *cnt) ? *linesize : *cnt + 1;
163 if (sz <= 1 || fgets(rv, sz, fp) == NULL) {
164 /* Leave llen untouched; it is used to determine whether the last line
165 * was \n-terminated in some callers */
166 rv = NULL;
167 goto jleave;
170 i_llen = _length_of_line(rv, sz);
171 *cnt -= i_llen;
172 while (rv[i_llen - 1] != '\n') {
173 *line = rv = (srealloc)(rv, *linesize += 256 SMALLOC_DEBUG_ARGSCALL);
174 sz = *linesize - i_llen;
175 sz = (sz <= *cnt) ? sz : *cnt + 1;
176 if (sz <= 1 || fgets(rv + i_llen, sz, fp) == NULL) {
177 if (appendnl) {
178 rv[i_llen++] = '\n';
179 rv[i_llen] = '\0';
181 break;
183 sz = _length_of_line(rv + i_llen, sz);
184 i_llen += sz;
185 *cnt -= sz;
187 if (llen)
188 *llen = i_llen;
189 jleave:
190 NYD2_LEAVE;
191 return rv;
194 FL int
195 (readline_restart)(FILE *ibuf, char **linebuf, size_t *linesize, size_t n
196 SMALLOC_DEBUG_ARGS)
198 /* TODO readline_restart(): always *appends* LF just to strip it again;
199 * TODO should be configurable just as for fgetline(); ..or whatever..
200 * TODO intwrap */
201 int rv = -1;
202 long sz;
203 NYD2_ENTER;
205 clearerr(ibuf);
207 /* Interrupts will cause trouble if we are inside a stdio call. As this is
208 * only relevant if input is from tty, bypass it by read(), then */
209 if (fileno(ibuf) == 0 && (options & OPT_TTYIN)) {
210 assert(*linesize == 0 || *linebuf != NULL);
211 pstate &= ~PS_READLINE_NL;
212 for (;;) {
213 if (*linesize <= LINESIZE || n >= *linesize - 128) {
214 *linesize += ((*linebuf == NULL) ? LINESIZE + n + 1 : 256);
215 *linebuf = (srealloc)(*linebuf, *linesize SMALLOC_DEBUG_ARGSCALL);
217 jagain:
218 sz = read(0, *linebuf + n, *linesize - n - 1);
219 if (sz > 0) {
220 n += sz;
221 (*linebuf)[n] = '\0';
222 if ((*linebuf)[n - 1] == '\n') {
223 pstate |= PS_READLINE_NL;
224 break;
226 } else {
227 if (sz < 0 && errno == EINTR)
228 goto jagain;
229 /* TODO eh. what is this? that now supposed to be a line?!? */
230 if (n > 0) {
231 if ((*linebuf)[n - 1] != '\n') {
232 (*linebuf)[n++] = '\n';
233 (*linebuf)[n] = '\0';
234 } else
235 pstate |= PS_READLINE_NL;
236 break;
237 } else
238 goto jleave;
241 } else {
242 /* Not reading from standard input or standard input not a terminal. We
243 * read one char at a time as it is the only way to get lines with
244 * embedded NUL characters in standard stdio */
245 if (_fgetline_byone(linebuf, linesize, &n, ibuf, 1, n
246 SMALLOC_DEBUG_ARGSCALL) == NULL)
247 goto jleave;
249 if (n > 0 && (*linebuf)[n - 1] == '\n')
250 (*linebuf)[--n] = '\0';
251 rv = (int)n;
252 jleave:
253 NYD2_LEAVE;
254 return rv;
257 FL void
258 setptr(FILE *ibuf, off_t offset)
260 struct message self;
261 char *cp, *linebuf = NULL;
262 char const *cp2;
263 int c, maybe = 1, inhead = 0, selfcnt = 0;
264 size_t linesize = 0, filesize, cnt;
265 NYD_ENTER;
267 memset(&self, 0, sizeof self);
268 self.m_flag = MUSED | MNEW | MNEWEST;
269 filesize = mailsize - offset;
270 offset = ftell(mb.mb_otf);
272 for (;;) {
273 if (fgetline(&linebuf, &linesize, &filesize, &cnt, ibuf, 0) == NULL) {
274 self.m_xsize = self.m_size;
275 self.m_xlines = self.m_lines;
276 self.m_have = HAVE_HEADER | HAVE_BODY;
277 if (selfcnt > 0)
278 message_append(&self);
279 message_append_null();
280 if (linebuf != NULL)
281 free(linebuf);
282 break;
285 #ifdef notdef
286 if (linebuf[0] == '\0')
287 linebuf[0] = '.';
288 #endif
289 /* XXX Convert CRLF to LF; this should be rethought in that
290 * XXX CRLF input should possibly end as CRLF output? */
291 if (cnt >= 2 && linebuf[cnt - 1] == '\n' && linebuf[cnt - 2] == '\r')
292 linebuf[--cnt - 1] = '\n';
293 fwrite(linebuf, sizeof *linebuf, cnt, mb.mb_otf);
294 if (ferror(mb.mb_otf)) {
295 n_perr(_("/tmp"), 0);
296 exit(EXIT_ERR);
298 if (linebuf[cnt - 1] == '\n')
299 linebuf[cnt - 1] = '\0';
300 if (maybe && linebuf[0] == 'F' && is_head(linebuf, cnt, FAL0)) {
301 /* TODO char date[FROM_DATEBUF];
302 * TODO extract_date_from_from_(linebuf, cnt, date);
303 * TODO self.m_time = 10000; */
304 self.m_xsize = self.m_size;
305 self.m_xlines = self.m_lines;
306 self.m_have = HAVE_HEADER | HAVE_BODY;
307 if (selfcnt++ > 0)
308 message_append(&self);
309 msgCount++;
310 self.m_flag = MUSED | MNEW | MNEWEST;
311 self.m_size = 0;
312 self.m_lines = 0;
313 self.m_block = mailx_blockof(offset);
314 self.m_offset = mailx_offsetof(offset);
315 inhead = 1;
316 } else if (linebuf[0] == 0) {
317 inhead = 0;
318 } else if (inhead) {
319 for (cp = linebuf, cp2 = "status";; ++cp) {
320 if ((c = *cp2++) == 0) {
321 while (c = *cp++, whitechar(c))
323 if (cp[-1] != ':')
324 break;
325 while ((c = *cp++) != '\0')
326 if (c == 'R')
327 self.m_flag |= MREAD;
328 else if (c == 'O')
329 self.m_flag &= ~MNEW;
330 break;
332 if (*cp != c && *cp != upperconv(c))
333 break;
335 for (cp = linebuf, cp2 = "x-status";; ++cp) {
336 if ((c = *cp2++) == 0) {
337 while ((c = *cp++, whitechar(c)))
339 if (cp[-1] != ':')
340 break;
341 while ((c = *cp++) != '\0')
342 if (c == 'F')
343 self.m_flag |= MFLAGGED;
344 else if (c == 'A')
345 self.m_flag |= MANSWERED;
346 else if (c == 'T')
347 self.m_flag |= MDRAFTED;
348 break;
350 if (*cp != c && *cp != upperconv(c))
351 break;
354 offset += cnt;
355 self.m_size += cnt;
356 ++self.m_lines;
357 maybe = linebuf[0] == 0;
359 NYD_LEAVE;
362 FL off_t
363 fsize(FILE *iob)
365 struct stat sbuf;
366 off_t rv;
367 NYD_ENTER;
369 rv = (fstat(fileno(iob), &sbuf) == -1) ? 0 : sbuf.st_size;
370 NYD_LEAVE;
371 return rv;
374 FL bool_t
375 n_file_lock(int fd, enum n_file_lock_type flt, off_t off, off_t len,
376 size_t pollmsecs)
378 size_t tries;
379 bool_t didmsg, rv;
380 NYD_ENTER;
382 if(pollmsecs == UIZ_MAX)
383 pollmsecs = FILE_LOCK_MILLIS;
385 UNINIT(rv, 0);
386 for (didmsg = FAL0, tries = 0; tries <= FILE_LOCK_TRIES; ++tries) {
387 rv = a_file_lock(fd, flt, off, len);
389 if (rv == TRUM1) {
390 rv = FAL0;
391 break;
393 if (rv || pollmsecs == 0)
394 break;
395 else {
396 if(!didmsg){
397 n_err(_("Failed to create a file lock, waiting %lu milliseconds "),
398 pollmsecs);
399 didmsg = TRU1;
400 }else
401 n_err(".");
402 n_msleep(pollmsecs, FAL0);
405 if(didmsg)
406 n_err(" %s\n", (rv ? _("ok") : _("failure")));
407 NYD_LEAVE;
408 return rv;
411 /* s-it-mode */