diff options
-rw-r--r-- | sbin/pdisk/io.c | 67 | ||||
-rw-r--r-- | sbin/pdisk/io.h | 4 | ||||
-rw-r--r-- | sbin/pdisk/pdisk.c | 102 |
3 files changed, 78 insertions, 95 deletions
diff --git a/sbin/pdisk/io.c b/sbin/pdisk/io.c index 63571ce203c..ef130c3ebc1 100644 --- a/sbin/pdisk/io.c +++ b/sbin/pdisk/io.c @@ -1,4 +1,4 @@ -/* $OpenBSD: io.c,v 1.23 2016/01/26 16:39:00 krw Exp $ */ +/* $OpenBSD: io.c,v 1.24 2016/01/26 23:41:48 krw Exp $ */ /* * io.c - simple io and input parsing routines @@ -33,18 +33,18 @@ #include <string.h> #include <stdarg.h> +#include "dpme.h" #include "io.h" #define BAD_DIGIT 17 /* must be greater than any base */ -#define STRING_CHUNK 16 #define UNGET_MAX_COUNT 10 short unget_buf[UNGET_MAX_COUNT + 1]; int unget_count; -long get_number(int); -char *get_string(int); -int my_getch (void); +static long get_number(int); +static char *get_string(int); +static int my_getch (void); int my_getch() @@ -215,11 +215,10 @@ get_number(int first_char) return (ret_value); } -int -get_string_argument(const char *prompt, char **string) +char * +get_dpistr_argument(const char *prompt) { int c; - int result = 0; for (;;) { c = my_getch(); @@ -231,68 +230,46 @@ get_string_argument(const char *prompt, char **string) } else if (c == '\n') { printf(prompt); } else if (c == '"' || c == '\'') { - *string = get_string(c); - result = 1; - break; + return get_string(c); } else if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || (c == '-' || c == '/' || c == '.' || c == ':')) { my_ungetch(c); - *string = get_string(' '); - result = 1; - break; + return get_string(' '); } else { my_ungetch(c); - *string = NULL; - break; + return NULL; } } - return result; + return NULL; } char * get_string(int eos) { - char *s, *ret_value, *limit; - int c, length; + char buf[DPISTRLEN]; + char *s, *limit; + int c; - ret_value = malloc(STRING_CHUNK); - if (ret_value == NULL) { - warn("can't allocate memory for string buffer"); - return NULL; - } - length = STRING_CHUNK; - limit = ret_value + length; + memset(buf, 0, sizeof(buf)); + limit = buf + sizeof(buf); c = my_getch(); - for (s = ret_value;; c = my_getch()) { - if (s >= limit) { - /* expand string */ - limit = malloc(length + STRING_CHUNK); - if (limit == NULL) { - warn("can't allocate memory for string buffer"); - ret_value[length - 1] = 0; - break; - } - strncpy(limit, ret_value, length); - free(ret_value); - s = limit + (s - ret_value); - ret_value = limit; - length += STRING_CHUNK; - limit = ret_value + length; - } + for (s = buf;; c = my_getch()) { if (c <= 0 || c == eos || (eos == ' ' && c == '\t')) { - *s++ = 0; + *s = 0; break; } else if (c == '\n') { - *s++ = 0; + *s = 0; my_ungetch(c); break; } else { *s++ = c; + if (s >= limit) + return NULL; } } - return (ret_value); + return (strdup(buf)); } diff --git a/sbin/pdisk/io.h b/sbin/pdisk/io.h index f09dbaf5d41..1188fb716f1 100644 --- a/sbin/pdisk/io.h +++ b/sbin/pdisk/io.h @@ -1,4 +1,4 @@ -/* $OpenBSD: io.h,v 1.11 2016/01/24 01:38:32 krw Exp $ */ +/* $OpenBSD: io.h,v 1.12 2016/01/26 23:41:48 krw Exp $ */ /* * io.h - simple io and input parsing routines @@ -38,7 +38,7 @@ int get_command(const char *, int, int *); int get_number_argument(const char *, long *); int get_okay(const char *, int); int get_partition_modifier(void); -int get_string_argument(const char *, char **); +char *get_dpistr_argument(const char *); int number_of_digits(unsigned long); #endif /* __io__ */ diff --git a/sbin/pdisk/pdisk.c b/sbin/pdisk/pdisk.c index 2649e889a91..56927b1aed8 100644 --- a/sbin/pdisk/pdisk.c +++ b/sbin/pdisk/pdisk.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pdisk.c,v 1.71 2016/01/26 21:07:54 krw Exp $ */ +/* $OpenBSD: pdisk.c,v 1.72 2016/01/26 23:41:48 krw Exp $ */ /* * pdisk - an editor for Apple format partition tables @@ -242,41 +242,43 @@ do_create_partition(struct partition_map_header *map, int get_type) { long base, length; char *name = NULL; - char *type_name = NULL; + char *type = NULL; - if (get_base_argument(&base, map) == 0) { + if (get_base_argument(&base, map) == 0) return; - } - if (get_size_argument(&length, map) == 0) { + if (get_size_argument(&length, map) == 0) return; - } - if (get_string_argument("Name of partition: ", &name) == 0) { + + name = get_dpistr_argument("Name of partition: "); + if (name == NULL) { bad_input("Bad name"); - return; + goto out; } - if (get_type == 0) { - add_partition_to_map(name, kUnixType, base, length, map); - } else if (get_string_argument("Type of partition: ", &type_name) == - 0) { + + if (get_type == 0) + type = strdup(kUnixType); + else + type = get_dpistr_argument("Type of partition: "); + if (type == NULL) { bad_input("Bad type"); - goto xit1; - } else { - if (strncasecmp(type_name, kFreeType, DPISTRLEN) == 0) { - bad_input("Can't create a partition with the Free " - "type"); - goto xit2; - } - if (strncasecmp(type_name, kMapType, DPISTRLEN) == 0) { - bad_input("Can't create a partition with the Map " - "type"); - goto xit2; - } - add_partition_to_map(name, type_name, base, length, map); + goto out; + } + + if (strncasecmp(type, kFreeType, DPISTRLEN) == 0) { + bad_input("Can't create a partition with the Free type"); + goto out; + } + if (strncasecmp(type, kMapType, DPISTRLEN) == 0) { + bad_input("Can't create a partition with the Map type"); + goto out; } -xit2: - free(type_name); -xit1: + + add_partition_to_map(name, type, base, length, map); + +out: + free(type); free(name); + return; } @@ -347,24 +349,27 @@ do_rename_partition(struct partition_map_header *map) bad_input("Bad partition number"); return; } - if (get_string_argument("New name of partition: ", &name) == 0) { - bad_input("Bad name"); - return; - } - /* find partition and change it */ entry = find_entry_by_disk_address(ix, map); if (entry == NULL) { printf("No such partition\n"); - } else { - /* - * Since dpme_name is supposed to be NUL-filled, make sure - * current contents are zapped before copying in new name! - */ - memset(entry->dpme->dpme_name, 0, - sizeof(entry->dpme->dpme_name)); - strlcpy(entry->dpme->dpme_name, name, DPISTRLEN); - map->changed = 1; + return; } + + printf("Existing partition name ``%s''.\n", entry->dpme->dpme_name); + name = get_dpistr_argument("New name of partition: "); + if (name == NULL) { + bad_input("Bad name"); + return; + } + + /* + * Since dpme_name is supposed to be NUL-filled, make sure + * current contents are zapped before copying in new name! + */ + memset(entry->dpme->dpme_name, 0, sizeof(entry->dpme->dpme_name)); + strlcpy(entry->dpme->dpme_name, name, DPISTRLEN); + map->changed = 1; + free(name); return; } @@ -373,7 +378,7 @@ void do_change_type(struct partition_map_header *map) { struct partition_map *entry; - char *type = NULL; + char *type; long ix; if (get_number_argument("Partition number: ", &ix) == 0) { @@ -381,16 +386,18 @@ do_change_type(struct partition_map_header *map) return; } entry = find_entry_by_disk_address(ix, map); - if (entry == NULL) { printf("No such partition\n"); - goto out; + return; } + printf("Existing partition type ``%s''.\n", entry->dpme->dpme_type); - if (get_string_argument("New type of partition: ", &type) == 0) { + type = get_dpistr_argument("New type of partition: "); + if (type == NULL) { bad_input("Bad type"); - goto out; + return; } + /* * Since dpme_type is supposed to be NUL-filled, make sure * current contents are zapped before copying in new type! @@ -399,7 +406,6 @@ do_change_type(struct partition_map_header *map) strncpy(entry->dpme->dpme_type, type, DPISTRLEN); map->changed = 1; -out: free(type); return; } |