diff options
author | Kenneth R Westerback <krw@cvs.openbsd.org> | 2012-07-08 17:46:45 +0000 |
---|---|---|
committer | Kenneth R Westerback <krw@cvs.openbsd.org> | 2012-07-08 17:46:45 +0000 |
commit | 1416753cd220ebfd02ade01e94570b11fd6d1816 (patch) | |
tree | 1b502de55916ada3567220f421e9bf7446226a9a /sbin/fdisk | |
parent | c4db1c74a810e872f25999b27dc35359ad1eccd0 (diff) |
Split out an ask_pid() function rather than over-parameterizing the
ask_num() function. Remove now unneeded 'flags' and 'help' parameters
from both. Display out of range values in hex in ask_pid(), since
we are seeking hex input.
ok guenther@
Diffstat (limited to 'sbin/fdisk')
-rw-r--r-- | sbin/fdisk/cmd.c | 22 | ||||
-rw-r--r-- | sbin/fdisk/misc.c | 60 | ||||
-rw-r--r-- | sbin/fdisk/misc.h | 5 |
3 files changed, 60 insertions, 27 deletions
diff --git a/sbin/fdisk/cmd.c b/sbin/fdisk/cmd.c index d20ac3e1708..7d6fddfa118 100644 --- a/sbin/fdisk/cmd.c +++ b/sbin/fdisk/cmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd.c,v 1.51 2012/07/08 12:29:04 krw Exp $ */ +/* $OpenBSD: cmd.c,v 1.52 2012/07/08 17:46:44 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -82,12 +82,12 @@ Xdisk(cmd_t *cmd, disk_t *disk, mbr_t *mbr, mbr_t *tt, int offset) /* Ask for new info */ if (ask_yn("Change disk geometry?")) { - disk->real->cylinders = ask_num("BIOS Cylinders", ASK_DEC, - disk->real->cylinders, 1, maxcyl, NULL); - disk->real->heads = ask_num("BIOS Heads", ASK_DEC, - disk->real->heads, 1, maxhead, NULL); - disk->real->sectors = ask_num("BIOS Sectors", ASK_DEC, - disk->real->sectors, 1, maxsec, NULL); + disk->real->cylinders = ask_num("BIOS Cylinders", + disk->real->cylinders, 1, maxcyl); + disk->real->heads = ask_num("BIOS Heads", + disk->real->heads, 1, maxhead); + disk->real->sectors = ask_num("BIOS Sectors", + disk->real->sectors, 1, maxsec); disk->real->size = disk->real->cylinders * disk->real->heads * disk->real->sectors; @@ -112,8 +112,7 @@ Xswap(cmd_t *cmd, disk_t *disk, mbr_t *mbr, mbr_t *tt, int offset) return (ret); } - pt = ask_num("Swap with what partition?", ASK_DEC, - -1, 0, 3, NULL); + pt = ask_num("Swap with what partition?", -1, 0, 3); if (pt < 0 || pt > 3) { printf("Invalid partition number %d.\n", pt); return (ret); @@ -152,7 +151,7 @@ Xedit(cmd_t *cmd, disk_t *disk, mbr_t *mbr, mbr_t *tt, int offset) ret = Xsetpid(cmd, disk, mbr, tt, offset); #define EDIT(p, v, n, m) \ - if ((num = ask_num(p, ASK_DEC, v, n, m, NULL)) != v) \ + if ((num = ask_num(p, v, n, m)) != v) \ ret = CMD_DIRTY; \ v = num; @@ -242,8 +241,7 @@ Xsetpid(cmd_t *cmd, disk_t *disk, mbr_t *mbr, mbr_t *tt, int offset) PRT_print(pn, pp, NULL); /* Ask for partition type */ - num = ask_num("Partition id ('0' to disable) ", ASK_HEX, pp->id, 0, - 0xFF, PRT_printall); + num = ask_pid("Partition id ('0' to disable) ", pp->id, 0, 0xFF); if (num != pp->id) ret = CMD_DIRTY; diff --git a/sbin/fdisk/misc.c b/sbin/fdisk/misc.c index 7ee1d812864..81bc99622d8 100644 --- a/sbin/fdisk/misc.c +++ b/sbin/fdisk/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.23 2010/07/04 22:15:31 halex Exp $ */ +/* $OpenBSD: misc.c,v 1.24 2012/07/08 17:46:44 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -89,8 +89,46 @@ ask_cmd(cmd_t *cmd) } int -ask_num(const char *str, int flags, int dflt, int low, int high, - void (*help)(void)) +ask_num(const char *str, int dflt, int low, int high) +{ + char lbuf[100], *cp; + size_t lbuflen; + int num; + + if (dflt < low) + dflt = low; + else if (dflt > high) + dflt = high; + + do { + printf("%s [%d - %d]: [%d] ", str, low, high, dflt); + + if (fgets(lbuf, sizeof lbuf, stdin) == NULL) + errx(1, "eof"); + lbuflen = strlen(lbuf); + if (lbuflen > 0 && lbuf[lbuflen - 1] == '\n') + lbuf[lbuflen - 1] = '\0'; + + /* Convert */ + cp = lbuf; + num = strtol(lbuf, &cp, 10); + + /* Make sure only number present */ + if (cp == lbuf) + num = dflt; + if (*cp != '\0') { + printf("'%s' is not a valid number.\n", lbuf); + num = low - 1; + } else if (num < low || num > high) { + printf("'%d' is out of range.\n", num); + } + } while (num < low || num > high); + + return (num); +} + +int +ask_pid(const char *str, int dflt, int low, int high) { char lbuf[100], *cp; size_t lbuflen; @@ -103,12 +141,8 @@ ask_num(const char *str, int flags, int dflt, int low, int high, do { again: - if (flags == ASK_HEX) - printf("%s [%X - %X]: [%X] ", str, low, high, dflt); - else - printf("%s [%d - %d]: [%d] ", str, low, high, dflt); - if (help) - printf("(? for help) "); + printf("%s [%X - %X]: [%X] ", str, low, high, dflt); + printf("(? for help) "); if (fgets(lbuf, sizeof lbuf, stdin) == NULL) errx(1, "eof"); @@ -116,14 +150,14 @@ again: if (lbuflen > 0 && lbuf[lbuflen - 1] == '\n') lbuf[lbuflen - 1] = '\0'; - if (help && lbuf[0] == '?') { - (*help)(); + if (lbuf[0] == '?') { + PRT_printall(); goto again; } /* Convert */ cp = lbuf; - num = strtol(lbuf, &cp, ((flags==ASK_HEX)?16:10)); + num = strtol(lbuf, &cp, 16); /* Make sure only number present */ if (cp == lbuf) @@ -132,7 +166,7 @@ again: printf("'%s' is not a valid number.\n", lbuf); num = low - 1; } else if (num < low || num > high) { - printf("'%d' is out of range.\n", num); + printf("'%x' is out of range.\n", num); } } while (num < low || num > high); diff --git a/sbin/fdisk/misc.h b/sbin/fdisk/misc.h index 57f2a046445..ebdfe904c94 100644 --- a/sbin/fdisk/misc.h +++ b/sbin/fdisk/misc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.h,v 1.12 2005/11/21 01:59:24 krw Exp $ */ +/* $OpenBSD: misc.h,v 1.13 2012/07/08 17:46:44 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -52,7 +52,8 @@ extern struct unit_type unit_types[]; /* Prototypes */ int unit_lookup(char *); int ask_cmd(cmd_t *); -int ask_num(const char *, int, int, int, int, void (*help)(void)); +int ask_num(const char *, int, int, int); +int ask_pid(const char *, int, int, int); int ask_yn(const char *); u_int16_t getshort(void *); u_int32_t getlong(void *); |