diff options
author | Kenneth R Westerback <krw@cvs.openbsd.org> | 2021-08-15 13:45:43 +0000 |
---|---|---|
committer | Kenneth R Westerback <krw@cvs.openbsd.org> | 2021-08-15 13:45:43 +0000 |
commit | 90f34b7bdb2b3a178327f687283a9505dcae55ee (patch) | |
tree | 0d8e1387978394cb580f26dcea43de478829e0c5 | |
parent | dc50d5f325502522f2e3925d6328a43d51a3f4b0 (diff) |
Don't check & errx() after each and every invocation of
string_from_line(). Just errx() inside string_from_line() if
getline() fails.
Use strcspn() idiom to nuke '\n' returned by getline().
No functional change.
-rw-r--r-- | sbin/fdisk/cmd.c | 13 | ||||
-rw-r--r-- | sbin/fdisk/misc.c | 15 | ||||
-rw-r--r-- | sbin/fdisk/misc.h | 4 | ||||
-rw-r--r-- | sbin/fdisk/user.c | 5 |
4 files changed, 13 insertions, 24 deletions
diff --git a/sbin/fdisk/cmd.c b/sbin/fdisk/cmd.c index af2fd613b31..11ee896a798 100644 --- a/sbin/fdisk/cmd.c +++ b/sbin/fdisk/cmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd.c,v 1.138 2021/08/12 12:31:16 krw Exp $ */ +/* $OpenBSD: cmd.c,v 1.139 2021/08/15 13:45:42 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -586,9 +586,7 @@ ask_num(const char *str, int dflt, int low, int high) do { printf("%s [%d - %d]: [%d] ", str, low, high, dflt); - - if (string_from_line(lbuf, sizeof(lbuf))) - errx(1, "eof"); + string_from_line(lbuf, sizeof(lbuf)); if (lbuf[0] == '\0') { num = dflt; @@ -612,9 +610,7 @@ ask_pid(const int dflt, struct uuid *guid) do { printf("Partition id ('0' to disable) [01 - FF]: [%X] ", dflt); printf("(? for help) "); - - if (string_from_line(lbuf, sizeof(lbuf))) - errx(1, "eof"); + string_from_line(lbuf, sizeof(lbuf)); if (lbuf[0] == '?') { PRT_printall(); @@ -654,8 +650,7 @@ ask_string(const char *prompt, const char *oval) buf[0] = '\0'; printf("%s: [%s] ", prompt, oval ? oval : ""); - if (string_from_line(buf, sizeof(buf))) - errx(1, "eof"); + string_from_line(buf, sizeof(buf)); if (buf[0] == '\0' && oval) strlcpy(buf, oval, sizeof(buf)); diff --git a/sbin/fdisk/misc.c b/sbin/fdisk/misc.c index e387856c736..2c058fea652 100644 --- a/sbin/fdisk/misc.c +++ b/sbin/fdisk/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.79 2021/08/07 13:33:12 krw Exp $ */ +/* $OpenBSD: misc.c,v 1.80 2021/08/15 13:45:42 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -59,7 +59,7 @@ unit_lookup(const char *units) return i; } -int +void string_from_line(char *buf, const size_t buflen) { static char *line; @@ -68,14 +68,11 @@ string_from_line(char *buf, const size_t buflen) len = getline(&line, &sz, stdin); if (len == -1) - return -1; + errx(1, "eof"); - if (line[len - 1] == '\n') - line[len - 1] = '\0'; + line[strcspn(line, "\n")] = '\0'; strlcpy(buf, line, buflen); - - return 0; } int @@ -125,9 +122,7 @@ getuint64(const char *prompt, uint64_t oval, const uint64_t minval, do { printf("%s [%llu - %llu]: [%llu] ", prompt, minval, maxval, oval); - - if (string_from_line(buf, sizeof(buf))) - errx(1, "eof"); + string_from_line(buf, sizeof(buf)); if (buf[0] == '\0') { rslt = snprintf(buf, sizeof(buf), "%llu", oval); diff --git a/sbin/fdisk/misc.h b/sbin/fdisk/misc.h index 1b95e853716..14b6c2f2683 100644 --- a/sbin/fdisk/misc.h +++ b/sbin/fdisk/misc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.h,v 1.40 2021/08/12 12:31:16 krw Exp $ */ +/* $OpenBSD: misc.h,v 1.41 2021/08/15 13:45:42 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -33,7 +33,7 @@ extern struct unit_type unit_types[]; /* Prototypes */ int unit_lookup(const char *); -int string_from_line(char *, const size_t); +void string_from_line(char *, const size_t); int ask_yn(const char *); uint64_t getuint64(const char *, uint64_t, const uint64_t, const uint64_t); char *utf16le_to_string(const uint16_t *); diff --git a/sbin/fdisk/user.c b/sbin/fdisk/user.c index 5a9c49ad7f9..4d751b9155c 100644 --- a/sbin/fdisk/user.c +++ b/sbin/fdisk/user.c @@ -1,4 +1,4 @@ -/* $OpenBSD: user.c,v 1.72 2021/08/12 17:30:52 krw Exp $ */ +/* $OpenBSD: user.c,v 1.73 2021/08/15 13:45:42 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -208,8 +208,7 @@ ask_cmd(char **cmd, char **arg) static char lbuf[100]; size_t cmdstart, cmdend, argstart; - if (string_from_line(lbuf, sizeof(lbuf))) - errx(1, "eof"); + string_from_line(lbuf, sizeof(lbuf)); cmdstart = strspn(lbuf, " \t"); cmdend = cmdstart + strcspn(&lbuf[cmdstart], " \t"); |