summaryrefslogtreecommitdiffstats
path: root/pw
diff options
context:
space:
mode:
authorBaptiste Daroussin <bapt@FreeBSD.org>2015-07-30 06:14:47 +0000
committerBaptiste Daroussin <bapt@FreeBSD.org>2015-07-30 06:14:47 +0000
commit118a862383488360f366603cc32994fd65718474 (patch)
treec499fea9d3fea198958a8427dde46c7d3b4207c6 /pw
parentdef310326d4348f6b6e91e3f4323245d3e9f0ae9 (diff)
downloadpw-darwin-118a862383488360f366603cc32994fd65718474.tar.gz
pw-darwin-118a862383488360f366603cc32994fd65718474.tar.zst
pw-darwin-118a862383488360f366603cc32994fd65718474.zip
Improve strtounum
Fix many style bugs Better variable naming Use C99 'restrict' were apropriate Fix potential errno race Submitted by: bde
Diffstat (limited to 'pw')
-rw-r--r--pw/pw.h4
-rw-r--r--pw/strtounum.c41
2 files changed, 21 insertions, 24 deletions
diff --git a/pw/pw.h b/pw/pw.h
index 3ab3c74..a22572e 100644
--- a/pw/pw.h
+++ b/pw/pw.h
@@ -103,5 +103,5 @@ char *pw_pwcrypt(char *password);
extern const char *Modes[];
extern const char *Which[];
-uintmax_t strtounum(const char *numstr, uintmax_t minval, uintmax_t maxval,
- const char **errmsg);
+uintmax_t strtounum(const char * __restrict, uintmax_t, uintmax_t,
+ const char ** __restrict);
diff --git a/pw/strtounum.c b/pw/strtounum.c
index b394486..8d83470 100644
--- a/pw/strtounum.c
+++ b/pw/strtounum.c
@@ -34,41 +34,38 @@ __FBSDID("$FreeBSD$");
#include "pw.h"
-#define INVALID "invalid"
-#define TOOSMALL "too small"
-#define TOOLARGE "too large"
-
uintmax_t
-strtounum(const char *numstr, uintmax_t minval, uintmax_t maxval,
- const char **errstrp)
+strtounum(const char * __restrict np, uintmax_t minval, uintmax_t maxval,
+ const char ** __restrict errpp)
{
- uintmax_t ret = 0;
- char *ep;
+ char *endp;
+ uintmax_t ret;
if (minval > maxval) {
errno = EINVAL;
- if (errstrp != NULL)
- *errstrp = INVALID;
+ if (errpp != NULL)
+ *errpp = "invalid";
return (0);
}
-
- ret = strtoumax(numstr, &ep, 10);
- if (errno == EINVAL || numstr == ep || *ep != '\0') {
+ errno = 0;
+ ret = strtoumax(np, &endp, 10);
+ if (endp == np || *endp != '\0') {
errno = EINVAL;
- if (errstrp != NULL)
- *errstrp = INVALID;
+ if (errpp != NULL)
+ *errpp = "invalid";
return (0);
- } else if ((ret == 0 && errno == ERANGE) || ret < minval) {
+ }
+ if (ret < minval) {
errno = ERANGE;
- if (errstrp != NULL)
- *errstrp = TOOSMALL;
+ if (errpp != NULL)
+ *errpp = "too small";
return (0);
- } else if ((ret == UINTMAX_MAX && errno == ERANGE) || ret > maxval) {
+ }
+ if (errno == ERANGE || ret > maxval) {
errno = ERANGE;
- if (errstrp != NULL)
- *errstrp = TOOLARGE;
+ if (errpp != NULL)
+ *errpp = "too large";
return (0);
}
-
return (ret);
}