-/* $OpenBSD: mail.local.c,v 1.41 2023/05/15 12:02:40 op Exp $ */
+/* $OpenBSD: mail.local.c,v 1.42 2023/06/05 08:07:18 op Exp $ */
/*-
* Copyright (c) 1996-1998 Theo de Raadt <deraadt@theos.com>
{
FILE *fp = NULL;
time_t tval;
- int fd, eline;
- size_t len;
- char *line, *tbuf;
+ int fd, eline = 1;
+ char *tbuf, *line = NULL;
+ size_t linesize = 0;
+ ssize_t linelen;
if ((tbuf = strdup(_PATH_LOCTMP)) == NULL)
merr(EX_OSERR, "unable to allocate memory");
(void)time(&tval);
(void)fprintf(fp, "From %s %s", from, ctime(&tval));
- for (eline = 1, tbuf = NULL; (line = fgetln(stdin, &len));) {
- /* We have to NUL-terminate the line since fgetln does not */
- if (line[len - 1] == '\n')
- line[len - 1] = '\0';
- else {
- /* No trailing newline, so alloc space and copy */
- if ((tbuf = malloc(len + 1)) == NULL)
- merr(EX_OSERR, "unable to allocate memory");
- memcpy(tbuf, line, len);
- tbuf[len] = '\0';
- line = tbuf;
- }
+ while ((linelen = getline(&line, &linesize, stdin)) != -1) {
+ if (line[linelen - 1] == '\n')
+ line[linelen - 1] = '\0';
if (line[0] == '\0')
eline = 1;
else {
- if (eline && line[0] == 'F' && len > 5 &&
- !memcmp(line, "From ", 5))
+ if (eline && !strncmp(line, "From ", 5))
(void)putc('>', fp);
eline = 0;
}
if (ferror(fp))
break;
}
- free(tbuf);
+ free(line);
/* Output a newline; note, empty messages are allowed. */
(void)putc('\n', fp);