diff options
author | Vaclav Dolezal | 2017-08-15 13:08:49 +0200 |
---|---|---|
committer | Karel Zak | 2017-08-24 11:27:18 +0200 |
commit | 55cf3716098e1c5977940b0508a740a2d133aede (patch) | |
tree | 6410f6b27fb5f4451827811a31ece7aaf01c52a1 /disk-utils | |
parent | libfdisk: fix fdisk_reset_partition() leaking *_chs strings. (diff) | |
download | kernel-qcow2-util-linux-55cf3716098e1c5977940b0508a740a2d133aede.tar.gz kernel-qcow2-util-linux-55cf3716098e1c5977940b0508a740a2d133aede.tar.xz kernel-qcow2-util-linux-55cf3716098e1c5977940b0508a740a2d133aede.zip |
fdisk: add wrap_fgets() for getting user input
make function wrapping rl_fgets() and fputs()&fgets() to remove
code duplication in get_user_reply().
Signed-off-by: Vaclav Dolezal <vdolezal@redhat.com>
Diffstat (limited to 'disk-utils')
-rw-r--r-- | disk-utils/fdisk.c | 57 |
1 files changed, 26 insertions, 31 deletions
diff --git a/disk-utils/fdisk.c b/disk-utils/fdisk.c index e8311bc01..d0d58fd67 100644 --- a/disk-utils/fdisk.c +++ b/disk-utils/fdisk.c @@ -85,6 +85,21 @@ static char *rl_fgets(char *s, int n, FILE *stream, const char *prompt) } #endif +static char *wrap_fgets(char *s, int n, FILE *stream, const char *prompt) +{ +#ifdef HAVE_LIBREADLINE + if (isatty(STDIN_FILENO)) { + return rl_fgets(s, n, stream, prompt); + } + else +#endif + { + fputs(prompt, stream); + fflush(stream); + return fgets(s, n, stdin); + } +} + int get_user_reply(struct fdisk_context *cxt, const char *prompt, char *buf, size_t bufsz) { @@ -92,37 +107,17 @@ int get_user_reply(struct fdisk_context *cxt, const char *prompt, size_t sz; do { -#ifdef HAVE_LIBREADLINE - if (isatty(STDIN_FILENO)) { - if (!rl_fgets(buf, bufsz, stdout, prompt)) { - if (fdisk_label_is_changed(fdisk_get_label(cxt, NULL))) { - if (rl_fgets(buf, bufsz, stderr, - _("\nDo you really want to quit? ")) - && !rpmatch(buf)) - continue; - } - fdisk_unref_context(cxt); - exit(EXIT_FAILURE); - } else - break; - } - else -#endif - { - fputs(prompt, stdout); - fflush(stdout); - if (!fgets(buf, bufsz, stdin)) { - if (fdisk_label_is_changed(fdisk_get_label(cxt, NULL))) { - fprintf(stderr, _("\nDo you really want to quit? ")); - - if (fgets(buf, bufsz, stdin) && !rpmatch(buf)) - continue; - } - fdisk_unref_context(cxt); - exit(EXIT_FAILURE); - } else - break; - } + if (!wrap_fgets(buf, bufsz, stdout, prompt)) { + if (fdisk_label_is_changed(fdisk_get_label(cxt, NULL))) { + if (wrap_fgets(buf, bufsz, stderr, + _("\nDo you really want to quit? ")) + && !rpmatch(buf)) + continue; + } + fdisk_unref_context(cxt); + exit(EXIT_FAILURE); + } else + break; } while (1); for (p = buf; *p && !isgraph(*p); p++); /* get first non-blank */ |