summaryrefslogtreecommitdiff
path: root/sbin/disklabel
diff options
context:
space:
mode:
authorKenneth R Westerback <krw@cvs.openbsd.org>2018-04-27 20:20:13 +0000
committerKenneth R Westerback <krw@cvs.openbsd.org>2018-04-27 20:20:13 +0000
commit53aa870dc4279ac9152b0d337342bf2ae6e9774c (patch)
tree1d2d742911399b7b242f45c5f3d41cb464fad242 /sbin/disklabel
parent4c0894ca8858f4a5a8f17b97c465ff65b8f91f25 (diff)
Create and use a couple of #defines for the magic return
values of getuint64 that indicate errors. Suggested by & ok otto@
Diffstat (limited to 'sbin/disklabel')
-rw-r--r--sbin/disklabel/editor.c78
1 files changed, 42 insertions, 36 deletions
diff --git a/sbin/disklabel/editor.c b/sbin/disklabel/editor.c
index d93946ce6a0..2545e0813db 100644
--- a/sbin/disklabel/editor.c
+++ b/sbin/disklabel/editor.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: editor.c,v 1.332 2018/04/26 15:27:26 krw Exp $ */
+/* $OpenBSD: editor.c,v 1.333 2018/04/27 20:20:12 krw Exp $ */
/*
* Copyright (c) 1997-2000 Todd C. Miller <Todd.Miller@courtesan.com>
@@ -48,6 +48,10 @@
#define DO_CONVERSIONS 0x00000001
#define DO_ROUNDING 0x00000002
+/* Special return values for getuint64() */
+#define CMD_ABORTED (ULLONG_MAX - 1)
+#define CMD_BADVALUE (ULLONG_MAX)
+
/* structure to describe a portion of a disk */
struct diskchunk {
u_int64_t start;
@@ -725,9 +729,9 @@ editor_resize(struct disklabel *lp, char *p)
"new size or amount to grow (+) or shrink (-) partition including "
"unit", sz, sz + editor_countfree(lp), 0, DO_CONVERSIONS);
- if (secs == ULLONG_MAX - 1)
+ if (secs == CMD_ABORTED)
return;
- else if (secs == ULLONG_MAX)
+ else if (secs == CMD_BADVALUE)
return;
else if (secs == 0) {
fputs("The size must be > 0 sectors\n", stderr);
@@ -1097,8 +1101,10 @@ getstring(const char *prompt, const char *helpstring, const char *oval)
}
/*
- * Returns ULLONG_MAX on error
- * Usually only called by helper functions.
+ * Returns
+ * 0 .. CMD_ABORTED - 1 ==> valid value
+ * CMD_BADVALUE ==> invalid value
+ * CMD_ABORTED ==> ^D on input
*/
u_int64_t
getuint64(struct disklabel *lp, char *prompt, char *helpstring,
@@ -1117,7 +1123,7 @@ getuint64(struct disklabel *lp, char *prompt, char *helpstring,
p = getstring(prompt, helpstring, buf);
if (p == NULL)
- return (ULLONG_MAX - 1);
+ return (CMD_ABORTED);
if (strlcpy(buf, p, sizeof(buf)) >= sizeof(buf))
goto invalid;
n = strlen(buf);
@@ -1199,7 +1205,7 @@ getuint64(struct disklabel *lp, char *prompt, char *helpstring,
else
d = d / (-mult) * percent;
- if (d < ULLONG_MAX - 1) {
+ if (d < CMD_ABORTED) {
rval = d;
} else {
goto invalid;
@@ -1207,7 +1213,7 @@ getuint64(struct disklabel *lp, char *prompt, char *helpstring,
/* Range check then apply [+-] operator */
if (operator == '+') {
- if (ULLONG_MAX - 2 - oval >= rval)
+ if (CMD_ABORTED - oval > rval)
rval += oval;
else {
goto invalid;
@@ -1222,7 +1228,7 @@ getuint64(struct disklabel *lp, char *prompt, char *helpstring,
}
}
}
- if ((flags & DO_ROUNDING) && rval != ULLONG_MAX) {
+ if ((flags & DO_ROUNDING) && rval != CMD_BADVALUE) {
/* Round to nearest cylinder unless given in sectors */
if (
#ifdef SUN_CYLCHECK
@@ -1252,7 +1258,7 @@ getuint64(struct disklabel *lp, char *prompt, char *helpstring,
invalid:
errno = EINVAL;
fputs("Invalid entry\n", stderr);
- return (ULLONG_MAX);
+ return (CMD_BADVALUE);
}
/*
@@ -1367,10 +1373,10 @@ edit_parms(struct disklabel *lp)
ui = getuint64(lp, "sectors/track",
"The Number of sectors per track.", lp->d_nsectors,
lp->d_nsectors, 0, 0);
- if (ui == ULLONG_MAX - 1) {
+ if (ui == CMD_ABORTED) {
*lp = oldlabel; /* undo damage */
return;
- } else if (ui == ULLONG_MAX)
+ } else if (ui == CMD_BADVALUE)
; /* Try again. */
else if (ui > UINT32_MAX)
fprintf(stderr, "sectors/track must be <= %u\n",
@@ -1385,10 +1391,10 @@ edit_parms(struct disklabel *lp)
ui = getuint64(lp, "tracks/cylinder",
"The number of tracks per cylinder.", lp->d_ntracks,
lp->d_ntracks, 0, 0);
- if (ui == ULLONG_MAX - 1) {
+ if (ui == CMD_ABORTED) {
*lp = oldlabel; /* undo damage */
return;
- } else if (ui == ULLONG_MAX)
+ } else if (ui == CMD_BADVALUE)
; /* Try again. */
else if (ui > UINT32_MAX)
fprintf(stderr, "tracks/cylinder must be <= %u\n",
@@ -1404,10 +1410,10 @@ edit_parms(struct disklabel *lp)
"The number of sectors per cylinder (Usually sectors/track "
"* tracks/cylinder).", lp->d_secpercyl, lp->d_secpercyl,
0, 0);
- if (ui == ULLONG_MAX - 1) {
+ if (ui == CMD_ABORTED) {
*lp = oldlabel; /* undo damage */
return;
- } else if (ui == ULLONG_MAX)
+ } else if (ui == CMD_BADVALUE)
; /* Try again. */
else if (ui > UINT32_MAX)
fprintf(stderr, "sectors/cylinder must be <= %u\n",
@@ -1422,10 +1428,10 @@ edit_parms(struct disklabel *lp)
ui = getuint64(lp, "number of cylinders",
"The total number of cylinders on the disk.",
lp->d_ncylinders, lp->d_ncylinders, 0, 0);
- if (ui == ULLONG_MAX - 1) {
+ if (ui == CMD_ABORTED) {
*lp = oldlabel; /* undo damage */
return;
- } else if (ui == ULLONG_MAX)
+ } else if (ui == CMD_BADVALUE)
; /* Try again. */
else if (ui > UINT32_MAX)
fprintf(stderr, "number of cylinders must be < %u\n",
@@ -1442,10 +1448,10 @@ edit_parms(struct disklabel *lp)
ui = getuint64(lp, "total sectors",
"The total number of sectors on the disk.",
nsec, nsec, 0, 0);
- if (ui == ULLONG_MAX - 1) {
+ if (ui == CMD_ABORTED) {
*lp = oldlabel; /* undo damage */
return;
- } else if (ui == ULLONG_MAX)
+ } else if (ui == CMD_BADVALUE)
; /* Try again. */
else if (ui > DL_GETDSIZE(lp) &&
ending_sector == DL_GETDSIZE(lp)) {
@@ -1591,9 +1597,9 @@ set_bounds(struct disklabel *lp)
ui = getuint64(lp, "Starting sector",
"The start of the OpenBSD portion of the disk.",
starting_sector, DL_GETDSIZE(lp), 0, 0);
- if (ui == ULLONG_MAX - 1)
+ if (ui == CMD_ABORTED)
return;
- else if (ui == ULLONG_MAX)
+ else if (ui == CMD_BADVALUE)
; /* Try again. */
else if (ui >= DL_GETDSIZE(lp))
fprintf(stderr, "starting sector must be < %llu\n",
@@ -1609,9 +1615,9 @@ set_bounds(struct disklabel *lp)
"The size of the OpenBSD portion of the disk ('*' for the "
"entire disk).", ending_sector - starting_sector,
DL_GETDSIZE(lp) - start_temp, 0, 0);
- if (ui == ULLONG_MAX - 1)
+ if (ui == CMD_ABORTED)
return;
- else if (ui == ULLONG_MAX)
+ else if (ui == CMD_BADVALUE)
; /* Try again. */
else if (ui > DL_GETDSIZE(lp) - start_temp)
fprintf(stderr, "size must be <= %llu\n",
@@ -1888,9 +1894,9 @@ get_offset(struct disklabel *lp, int partno)
DL_GETPOFFSET(pp), 0, DO_CONVERSIONS |
(pp->p_fstype == FS_BSDFFS ? DO_ROUNDING : 0));
- if (ui == ULLONG_MAX - 1)
+ if (ui == CMD_ABORTED)
;
- else if (ui == ULLONG_MAX)
+ else if (ui == CMD_BADVALUE)
;
else if (ui < starting_sector || ui >= ending_sector)
fprintf(stderr, "The offset must be >= %llu and < %llu, "
@@ -1937,9 +1943,9 @@ get_size(struct disklabel *lp, int partno)
DO_CONVERSIONS | ((pp->p_fstype == FS_BSDFFS ||
pp->p_fstype == FS_SWAP) ? DO_ROUNDING : 0));
- if (ui == ULLONG_MAX - 1)
+ if (ui == CMD_ABORTED)
;
- else if (ui == ULLONG_MAX)
+ else if (ui == CMD_BADVALUE)
;
else if (ui == 0)
fputs("The size must be > 0 sectors\n", stderr);
@@ -1981,9 +1987,9 @@ get_cpg(struct disklabel *lp, int partno)
ui = getuint64(lp, "cpg",
"Size of partition in fs blocks.",
pp->p_cpg, pp->p_cpg, 0, 0);
- if (ui == ULLONG_MAX - 1)
+ if (ui == CMD_ABORTED)
return (1);
- else if (ui == ULLONG_MAX)
+ else if (ui == CMD_BADVALUE)
; /* Try again. */
else if (ui > USHRT_MAX)
fprintf(stderr, "Error: cpg should be smaller than "
@@ -2029,9 +2035,9 @@ get_fsize(struct disklabel *lp, int partno)
ui = getuint64(lp, "fragment size",
"Size of ffs block fragments. A multiple of the disk "
"sector-size.", fsize, ULLONG_MAX-2, 0, 0);
- if (ui == ULLONG_MAX - 1)
+ if (ui == CMD_ABORTED)
return (1);
- else if (ui == ULLONG_MAX)
+ else if (ui == CMD_BADVALUE)
; /* Try again. */
else if (ui < lp->d_secsize || (ui % lp->d_secsize) != 0)
fprintf(stderr, "Error: fragment size must be a "
@@ -2075,9 +2081,9 @@ get_bsize(struct disklabel *lp, int partno)
fsize * frag, ULLONG_MAX - 2, 0, 0);
/* sanity checks */
- if (ui == ULLONG_MAX - 1)
+ if (ui == CMD_ABORTED)
return (1);
- else if (ui == ULLONG_MAX)
+ else if (ui == CMD_BADVALUE)
; /* Try again. */
else if (ui < getpagesize())
fprintf(stderr,
@@ -2164,9 +2170,9 @@ get_fstype(struct disklabel *lp, int partno)
"Filesystem type as a decimal number; usually 7 "
"(4.2BSD) or 1 (swap).",
pp->p_fstype, pp->p_fstype, 0, 0);
- if (ui == ULLONG_MAX - 1)
+ if (ui == CMD_ABORTED)
return (1);
- else if (ui == ULLONG_MAX)
+ else if (ui == CMD_BADVALUE)
; /* Try again. */
else if (ui > UINT8_MAX)
fprintf(stderr, "FS type must be < %u\n",