diff options
author | Omar Polo <op@cvs.openbsd.org> | 2023-06-05 08:07:19 +0000 |
---|---|---|
committer | Omar Polo <op@cvs.openbsd.org> | 2023-06-05 08:07:19 +0000 |
commit | 0eb7def501a0e7a2b204ed981bccaf68ba234ab1 (patch) | |
tree | afb1865698d045cf8e2c14c05ebee0e2c3f145b3 /libexec | |
parent | f146e0cbe26c3130bff8fc646a4b490891ad872b (diff) |
use getline(3) instead of fgetln(3)
while here simplify the "From " check too.
ok millert@
Diffstat (limited to 'libexec')
-rw-r--r-- | libexec/mail.local/mail.local.c | 29 |
1 files changed, 10 insertions, 19 deletions
diff --git a/libexec/mail.local/mail.local.c b/libexec/mail.local/mail.local.c index 815fe58323c..7d0dfb4abb1 100644 --- a/libexec/mail.local/mail.local.c +++ b/libexec/mail.local/mail.local.c @@ -1,4 +1,4 @@ -/* $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> @@ -111,9 +111,10 @@ storemail(char *from) { 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"); @@ -125,23 +126,13 @@ storemail(char *from) (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; } @@ -149,7 +140,7 @@ storemail(char *from) if (ferror(fp)) break; } - free(tbuf); + free(line); /* Output a newline; note, empty messages are allowed. */ (void)putc('\n', fp); |