]> git.ipfire.org Git - pakfire.git/commitdiff
ui: Refactor confirmation using getline()
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 7 Jul 2021 09:45:34 +0000 (09:45 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 7 Jul 2021 09:45:34 +0000 (09:45 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/ui.c

index 4f3331e1c3a5d35c4b208b458936b6e34afd9106..02db59989630c90387a17c17f26faf0e24bf28a7 100644 (file)
@@ -36,24 +36,35 @@ int pakfire_ui_confirm(Pakfire pakfire, const char* message, const char* questio
        if (pakfire_has_flag(pakfire, PAKFIRE_FLAGS_NON_INTERACTIVE))
                return 0;
 
+       char* line = NULL;
+       size_t length = 0;
+       int r = 1;
+
        while (1) {
                // Print question
                printf("%s ", question);
 
                // Wait for user to enter something
-               char p = getchar();
+               ssize_t bytes_read = getline(&line, &length, stdin);
+               if (bytes_read < 0)
+                       goto END;
 
-               switch (p) {
+               // Must have one character and newline
+               if (!line || strlen(line) != 2)
+                       continue;
+
+               switch (*line) {
                        // Positive response
                        case 'Y':
                        case 'y':
-                               return 0;
+                               r = 0;
+                               goto END;
 
                        // Negative response
-                       case EOF:
                        case 'N':
                        case 'n':
-                               return 1;
+                               r = 1;
+                               goto END;
 
                        // Unknown input, repeat
                        default:
@@ -61,7 +72,11 @@ int pakfire_ui_confirm(Pakfire pakfire, const char* message, const char* questio
                }
        }
 
-       return 1;
+END:
+       if (line)
+               free(line);
+
+       return r;
 }
 
 static int pakfire_ui_enter_number(Pakfire pakfire, const char* question,