int get_user_reply(struct fdisk_context *cxt, const char *prompt, char *buf, size_t bufsz) { char *p; 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; } } while (1); for (p = buf; *p && !isgraph(*p); p++); /* get first non-blank */ if (p > buf) memmove(buf, p, p - buf); /* remove blank space */ sz = strlen(buf); if (sz && *(buf + sz - 1) == '\n') *(buf + sz - 1) = '\0'; DBG(ASK, ul_debug("user's reply: >>>%s<<<", buf)); return 0; }
static int get_user_reply(struct fdisk_context *cxt, char *prompt, char *buf, size_t bufsz) { char *p; size_t sz; do { fputs(prompt, stdout); fflush(stdout); if (!fgets(buf, bufsz, stdin)) { if (fdisk_label_is_changed(cxt->label)) { fprintf(stderr, _("Do you really want to quit? ")); if (fgets(buf, bufsz, stdin) && !rpmatch(buf)) continue; } fdisk_free_context(cxt); exit(EXIT_FAILURE); } else break; } while (1); for (p = buf; *p && !isgraph(*p); p++); /* get first non-blank */ if (p > buf) memmove(buf, p, p - buf); /* remove blank space */ sz = strlen(buf); if (sz && *(buf + sz - 1) == '\n') *(buf + sz - 1) = '\0'; DBG(ASK, dbgprint("user's reply: >>>%s<<<", buf)); return 0; }