1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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
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
38 #ifndef HAVE_AMALGAMATION
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 n_MEMORY_DEBUG_ARGS
);
51 static bool_t
a_file_lock(int fd
, enum n_file_lock_type ft
, off_t off
, off_t len
);
54 _length_of_line(char const *line
, size_t linesize
)
59 /* Last character is always '\0' and was added by fgets() */
60 for (--linesize
, i
= 0; i
< linesize
; i
++)
63 i
= (i
< linesize
) ? i
+ 1 : linesize
;
69 _fgetline_byone(char **line
, size_t *linesize
, size_t *llen
, FILE *fp
,
70 int appendnl
, size_t n n_MEMORY_DEBUG_ARGS
)
76 assert(*linesize
== 0 || *line
!= NULL
);
77 pstate
&= ~PS_READLINE_NL
;
80 if (*linesize
<= LINESIZE
|| n
>= *linesize
- 128) {
81 *linesize
+= ((rv
== NULL
) ? LINESIZE
+ n
+ 1 : 256);
82 *line
= rv
= (n_realloc
)(rv
, *linesize n_MEMORY_DEBUG_ARGSCALL
);
89 pstate
|= PS_READLINE_NL
;
113 a_file_lock(int fd
, enum n_file_lock_type flt
, off_t off
, off_t len
)
119 memset(&flp
, 0, sizeof flp
);
123 case FLT_READ
: rv
= F_RDLCK
; break;
124 case FLT_WRITE
: rv
= F_WRLCK
; break;
128 flp
.l_whence
= SEEK_SET
;
131 if (!(rv
= (fcntl(fd
, F_SETLK
, &flp
) != -1)))
143 (fgetline
)(char **line
, size_t *linesize
, size_t *cnt
, size_t *llen
, FILE *fp
,
144 int appendnl n_MEMORY_DEBUG_ARGS
)
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 n_MEMORY_DEBUG_ARGSCALL
);
158 pstate
&= ~PS_READLINE_NL
;
160 if ((rv
= *line
) == NULL
|| *linesize
< LINESIZE
)
161 *line
= rv
= (n_realloc
)(rv
, *linesize
= LINESIZE
162 n_MEMORY_DEBUG_ARGSCALL
);
163 sz
= (*linesize
<= *cnt
) ? *linesize
: *cnt
+ 1;
164 if (sz
<= 1 || fgets(rv
, sz
, fp
) == NULL
) {
165 /* Leave llen untouched; it is used to determine whether the last line
166 * was \n-terminated in some callers */
171 i_llen
= _length_of_line(rv
, sz
);
173 while (rv
[i_llen
- 1] != '\n') {
174 *line
= rv
= (n_realloc
)(rv
, *linesize
+= 256 n_MEMORY_DEBUG_ARGSCALL
);
175 sz
= *linesize
- i_llen
;
176 sz
= (sz
<= *cnt
) ? sz
: *cnt
+ 1;
177 if (sz
<= 1 || fgets(rv
+ i_llen
, sz
, fp
) == NULL
) {
184 sz
= _length_of_line(rv
+ i_llen
, sz
);
196 (readline_restart
)(FILE *ibuf
, char **linebuf
, size_t *linesize
, size_t n
199 /* TODO readline_restart(): always *appends* LF just to strip it again;
200 * TODO should be configurable just as for fgetline(); ..or whatever..
208 /* Interrupts will cause trouble if we are inside a stdio call. As this is
209 * only relevant if input is from tty, bypass it by read(), then */
210 if ((options
& OPT_TTYIN
) && fileno(ibuf
) == 0) {
211 assert(*linesize
== 0 || *linebuf
!= NULL
);
212 pstate
&= ~PS_READLINE_NL
;
214 if (*linesize
<= LINESIZE
|| n
>= *linesize
- 128) {
215 *linesize
+= ((*linebuf
== NULL
) ? LINESIZE
+ n
+ 1 : 256);
216 *linebuf
= (n_realloc
)(*linebuf
, *linesize n_MEMORY_DEBUG_ARGSCALL
);
219 sz
= read(0, *linebuf
+ n
, *linesize
- n
- 1);
222 (*linebuf
)[n
] = '\0';
223 if ((*linebuf
)[n
- 1] == '\n') {
224 pstate
|= PS_READLINE_NL
;
228 if (sz
< 0 && errno
== EINTR
)
230 /* TODO eh. what is this? that now supposed to be a line?!? */
232 if ((*linebuf
)[n
- 1] != '\n') {
233 (*linebuf
)[n
++] = '\n';
234 (*linebuf
)[n
] = '\0';
236 pstate
|= PS_READLINE_NL
;
243 /* Not reading from standard input or standard input not a terminal. We
244 * read one char at a time as it is the only way to get lines with
245 * embedded NUL characters in standard stdio */
246 if (_fgetline_byone(linebuf
, linesize
, &n
, ibuf
, 1, n
247 n_MEMORY_DEBUG_ARGSCALL
) == NULL
)
250 if (n
> 0 && (*linebuf
)[n
- 1] == '\n')
251 (*linebuf
)[--n
] = '\0';
259 setptr(FILE *ibuf
, off_t offset
)
262 char *cp
, *linebuf
= NULL
;
265 bool_t maybe
, inhead
, rfc4155
;
266 size_t linesize
= 0, filesize
, cnt
;
269 memset(&self
, 0, sizeof self
);
270 self
.m_flag
= MUSED
| MNEW
| MNEWEST
;
271 filesize
= mailsize
- offset
;
272 offset
= ftell(mb
.mb_otf
);
274 rfc4155
= inhead
= FAL0
;
277 if (fgetline(&linebuf
, &linesize
, &filesize
, &cnt
, ibuf
, 0) == NULL
) {
278 self
.m_xsize
= self
.m_size
;
279 self
.m_xlines
= self
.m_lines
;
280 self
.m_content_info
= CI_HAVE_HEADER
| CI_HAVE_BODY
;
282 message_append(&self
);
283 message_append_null();
290 if (linebuf
[0] == '\0')
293 /* XXX Convert CRLF to LF; this should be rethought in that
294 * XXX CRLF input should possibly end as CRLF output? */
295 if (cnt
>= 2 && linebuf
[cnt
- 1] == '\n' && linebuf
[cnt
- 2] == '\r')
296 linebuf
[--cnt
- 1] = '\n';
297 fwrite(linebuf
, sizeof *linebuf
, cnt
, mb
.mb_otf
);
298 if (ferror(mb
.mb_otf
)) {
299 n_perr(_("/tmp"), 0);
302 if (linebuf
[cnt
- 1] == '\n')
303 linebuf
[cnt
- 1] = '\0';
304 if (maybe
&& linebuf
[0] == 'F' &&
305 (rfc4155
= is_head(linebuf
, cnt
, TRU1
))) {
306 /* TODO char date[FROM_DATEBUF];
307 * TODO extract_date_from_from_(linebuf, cnt, date);
308 * TODO self.m_time = 10000; */
309 if (rfc4155
== TRUM1
) {
310 if (options
& OPT_D_V
)
311 n_err(_("Invalid MBOX \"From_ line\": %.*s\n"),
313 else if (!(mb
.mb_active
& MB_FROM__WARNED
))
314 n_err(_("MBOX mailbox contains non-conforming From_ line(s)\n"));
315 mb
.mb_active
|= MB_FROM__WARNED
;
317 self
.m_xsize
= self
.m_size
;
318 self
.m_xlines
= self
.m_lines
;
319 self
.m_content_info
= CI_HAVE_HEADER
| CI_HAVE_BODY
;
321 message_append(&self
);
323 self
.m_flag
= MUSED
| MNEW
| MNEWEST
;
326 self
.m_block
= mailx_blockof(offset
);
327 self
.m_offset
= mailx_offsetof(offset
);
329 } else if (linebuf
[0] == 0) {
332 for (cp
= linebuf
, cp2
= "status";; ++cp
) {
333 if ((c
= *cp2
++) == 0) {
334 while (c
= *cp
++, whitechar(c
))
338 while ((c
= *cp
++) != '\0')
340 self
.m_flag
|= MREAD
;
342 self
.m_flag
&= ~MNEW
;
345 if (*cp
!= c
&& *cp
!= upperconv(c
))
348 for (cp
= linebuf
, cp2
= "x-status";; ++cp
) {
349 if ((c
= *cp2
++) == 0) {
350 while ((c
= *cp
++, whitechar(c
)))
354 while ((c
= *cp
++) != '\0')
356 self
.m_flag
|= MFLAGGED
;
358 self
.m_flag
|= MANSWERED
;
360 self
.m_flag
|= MDRAFTED
;
363 if (*cp
!= c
&& *cp
!= upperconv(c
))
370 maybe
= (linebuf
[0] == 0);
382 rv
= (fstat(fileno(iob
), &sbuf
) == -1) ? 0 : sbuf
.st_size
;
388 n_file_lock(int fd
, enum n_file_lock_type flt
, off_t off
, off_t len
,
395 if(pollmsecs
== UIZ_MAX
)
396 pollmsecs
= FILE_LOCK_MILLIS
;
399 for (didmsg
= FAL0
, tries
= 0; tries
<= FILE_LOCK_TRIES
; ++tries
) {
400 rv
= a_file_lock(fd
, flt
, off
, len
);
406 if (rv
|| pollmsecs
== 0)
410 n_err(_("Failed to create a file lock, waiting %lu milliseconds "),
415 n_msleep(pollmsecs
, FAL0
);
419 n_err(" %s\n", (rv
? _("ok") : _("failure")));