]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fdisk: add wrap_fgets() for getting user input
authorVaclav Dolezal <vdolezal@redhat.com>
Tue, 15 Aug 2017 11:08:49 +0000 (13:08 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 24 Aug 2017 09:27:18 +0000 (11:27 +0200)
make function wrapping rl_fgets() and fputs()&fgets() to remove
code duplication in get_user_reply().

Signed-off-by: Vaclav Dolezal <vdolezal@redhat.com>
disk-utils/fdisk.c

index e8311bc016618300c4e97c7284618cc10ac2081a..d0d58fd67bbbc34988b0188c3151fc0e111b5264 100644 (file)
@@ -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 */