[RFC][PATCH v3 1/2] fdisk: move read_line() to libbb
Bartosz Golaszewski
bartekgola at gmail.com
Tue Dec 16 10:21:44 UTC 2014
Move read_line() to libbb in order to make it available for other applets.
While we're at it: implement a simple function which displays a prompt message
and waits for user confirmation.
function old new delta
read_int 449 472 +23
read_nonempty 30 49 +19
.rodata 152682 152693 +11
read_line 101 90 -11
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/1 up/down: 53/-11) Total: 42 bytes
Signed-off-by: Bartosz Golaszewski <bartekgola at gmail.com>
---
include/libbb.h | 2 ++
libbb/lineedit.c | 30 ++++++++++++++++++++++++++++++
util-linux/fdisk.c | 23 ++---------------------
3 files changed, 34 insertions(+), 21 deletions(-)
diff --git a/include/libbb.h b/include/libbb.h
index 8e8b9ca..21018e5 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1522,6 +1522,8 @@ int read_line_input(const char* prompt, char* command, int maxsize) FAST_FUNC;
read_line_input(prompt, command, maxsize)
#endif
+int read_line(const char *prompt, char *line_buffer, size_t buflen, char **line_ptr);
+int user_confirm(const char *prompt);
#ifndef COMM_LEN
# ifdef TASK_COMM_LEN
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index 8564307..a27771d 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -2833,6 +2833,36 @@ int FAST_FUNC read_line_input(const char* prompt, char* command, int maxsize)
#endif /* !FEATURE_EDITING */
+/* Read line; return 0 or first printable char */
+int read_line(const char *prompt, char *line_buffer, size_t buflen, char **line_ptr)
+{
+ int sz;
+
+ sz = read_line_input(NULL, prompt, line_buffer, buflen, /*timeout*/ -1);
+ if (sz <= 0)
+ exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */
+
+ if (line_buffer[sz-1] == '\n')
+ line_buffer[--sz] = '\0';
+
+ *line_ptr = line_buffer;
+ while (**line_ptr != '\0' && (unsigned char)**line_ptr <= ' ')
+ (*line_ptr)++;
+ return **line_ptr;
+}
+
+/*
+ * Display the prompt message followed by a '[y/N]' and wait for user
+ * confirmation. Return 1 if user entered 'y' or 'Y', or 0 otherwise.
+ */
+int user_confirm(const char *prompt)
+{
+ char *line_ptr, buf[3], p[64];
+
+ snprintf(p, sizeof(p), "%s [y/N]: ", prompt);
+
+ return tolower(read_line(p, buf, sizeof(buf), &line_ptr)) == 'y' ? 1 : 0;
+}
/*
* Testing
diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c
index 39eb27b..4ba1e90 100644
--- a/util-linux/fdisk.c
+++ b/util-linux/fdisk.c
@@ -563,25 +563,6 @@ seek_sector(sector_t secno)
}
#if ENABLE_FEATURE_FDISK_WRITABLE
-/* Read line; return 0 or first printable char */
-static int
-read_line(const char *prompt)
-{
- int sz;
-
- sz = read_line_input(NULL, prompt, line_buffer, sizeof(line_buffer), /*timeout*/ -1);
- if (sz <= 0)
- exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */
-
- if (line_buffer[sz-1] == '\n')
- line_buffer[--sz] = '\0';
-
- line_ptr = line_buffer;
- while (*line_ptr != '\0' && (unsigned char)*line_ptr <= ' ')
- line_ptr++;
- return *line_ptr;
-}
-
static void
set_all_unchanged(void)
{
@@ -607,7 +588,7 @@ write_part_table_flag(char *b)
static char
read_nonempty(const char *mesg)
{
- while (!read_line(mesg))
+ while (!read_line(mesg, line_buffer, sizeof(line_buffer), &line_ptr))
continue;
return *line_ptr;
}
@@ -615,7 +596,7 @@ read_nonempty(const char *mesg)
static char
read_maybe_empty(const char *mesg)
{
- if (!read_line(mesg)) {
+ if (!read_line(mesg, line_buffer, sizeof(line_buffer), &line_ptr)) {
line_ptr = line_buffer;
line_ptr[0] = '\n';
line_ptr[1] = '\0';
--
2.1.3
More information about the busybox
mailing list