Module Name: src
Committed By: christos
Date: Sun Jan 18 18:01:41 UTC 2015
Modified Files:
src/lib/libc/stdlib: strtol.3 strtonum.c strtoul.3
Log Message:
cleanups from (Kamil Rytarowski)
To generate a diff of this commit:
cvs rdiff -u -r1.28 -r1.29 src/lib/libc/stdlib/strtol.3
cvs rdiff -u -r1.1 -r1.2 src/lib/libc/stdlib/strtonum.c
cvs rdiff -u -r1.27 -r1.28 src/lib/libc/stdlib/strtoul.3
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/lib/libc/stdlib/strtol.3
diff -u src/lib/libc/stdlib/strtol.3:1.28 src/lib/libc/stdlib/strtol.3:1.29
--- src/lib/libc/stdlib/strtol.3:1.28 Fri Jan 16 18:46:37 2015
+++ src/lib/libc/stdlib/strtol.3 Sun Jan 18 13:01:41 2015
@@ -1,4 +1,4 @@
-.\" $NetBSD: strtol.3,v 1.28 2015/01/16 23:46:37 wiz Exp $
+.\" $NetBSD: strtol.3,v 1.29 2015/01/18 18:01:41 christos Exp $
.\"
.\" Copyright (c) 1990, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@@ -55,7 +55,7 @@
.Pp
.In inttypes.h
.Ft intmax_t
-.Fn strtoi "const char * restrict nptr" "char ** restrict endptr" "int base" "intmax_t lo" "intmax_t hi" "int *rerror"
+.Fn strtoi "const char * restrict nptr" "char ** restrict endptr" "int base" "intmax_t lo" "intmax_t hi" "int *rstatus"
.Ft intmax_t
.Fn strtoimax "const char * restrict nptr" "char ** restrict endptr" "int base"
.Pp
@@ -92,7 +92,7 @@ value.
The
.Fn strtoi
function
-is using internally
+uses internally
.Fn strtoimax
and ensures that the result is always in the range [
.Fa lo ..
@@ -100,16 +100,19 @@ and ensures that the result is always in
].
In adddition it always places
.Dv 0
-on success or an error value in the
-.Fa rerror
+on success or a conversion status in the
+.Fa rstatus
argument, avoiding the
.Dv errno
gymnastics the other functions require.
The
-.Fa rerror
+.Fn strtoi
+function doesn't affect errno on exit.
+The
+.Fa rstatus
argument can be
.Dv NULL
-if errors are to be ignored.
+if conversion status is to be ignored.
The
.Fn strtoq
function
@@ -323,7 +326,7 @@ In addition to the above errors
returns:
.Bl -tag -width Er
.It Bq Er ECANCELED
-The string did not contain any characters that could be converted.
+The string did not contain any characters that were converted.
.It Bq Er ENOTSUP
The string contained non-numeric characters that did not get converted.
In this case,
Index: src/lib/libc/stdlib/strtonum.c
diff -u src/lib/libc/stdlib/strtonum.c:1.1 src/lib/libc/stdlib/strtonum.c:1.2
--- src/lib/libc/stdlib/strtonum.c:1.1 Fri Jan 16 13:41:33 2015
+++ src/lib/libc/stdlib/strtonum.c Sun Jan 18 13:01:41 2015
@@ -1,4 +1,4 @@
-/* $NetBSD: strtonum.c,v 1.1 2015/01/16 18:41:33 christos Exp $ */
+/* $NetBSD: strtonum.c,v 1.2 2015/01/18 18:01:41 christos Exp $ */
/*-
* Copyright (c) 2014 The NetBSD Foundation, Inc.
* All rights reserved.
@@ -29,7 +29,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: strtonum.c,v 1.1 2015/01/16 18:41:33 christos Exp $");
+__RCSID("$NetBSD: strtonum.c,v 1.2 2015/01/18 18:01:41 christos Exp $");
#define _OPENBSD_SOURCE
#include <stdio.h>
@@ -37,31 +37,28 @@ __RCSID("$NetBSD: strtonum.c,v 1.1 2015/
#include <errno.h>
#include <inttypes.h>
-/*
- * Problems with the strtonum(3) API:
- * - will return 0 on failure; 0 might not be in range, so
- * that necessitates an error check even if you want to avoid it.
- * - does not differentiate 'illegal' returns, so we can't tell
- * the difference between partial and no conversions.
- * - returns english strings
- * - can't set the base, or find where the conversion ended
- */
long long
-strtonum(const char * __restrict ptr, long long lo, long long hi,
- const char ** __restrict res)
+strtonum(const char *nptr, long long minval, long long maxval,
+ const char **errstr)
{
int e;
- intmax_t rv;
+ long long rv;
const char *resp;
- if (res == NULL)
- res = &resp;
+ if (errstr == NULL)
+ errstr = &resp;
+
+ rv = strtoi(nptr, NULL, 0, minval, maxval, &e);
- rv = strtoi(ptr, NULL, 0, lo, hi, &e);
if (e == 0) {
- *res = NULL;
+ *errstr = NULL;
return rv;
}
- *res = e != ERANGE ? "invalid" : (rv == hi ? "too large" : "too small");
+
+ if (e == ERANGE)
+ *errstr = (rv == maxval ? "too large" : "too small");
+ else
+ *errstr = "invalid";
+
return 0;
}
Index: src/lib/libc/stdlib/strtoul.3
diff -u src/lib/libc/stdlib/strtoul.3:1.27 src/lib/libc/stdlib/strtoul.3:1.28
--- src/lib/libc/stdlib/strtoul.3:1.27 Fri Jan 16 18:46:37 2015
+++ src/lib/libc/stdlib/strtoul.3 Sun Jan 18 13:01:41 2015
@@ -1,4 +1,4 @@
-.\" $NetBSD: strtoul.3,v 1.27 2015/01/16 23:46:37 wiz Exp $
+.\" $NetBSD: strtoul.3,v 1.28 2015/01/18 18:01:41 christos Exp $
.\"
.\" Copyright (c) 1990, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@@ -55,7 +55,7 @@
.Pp
.In inttypes.h
.Ft uintmax_t
-.Fn strtou "const char * restrict nptr" "char ** restrict endptr" "int base" "uintmax_t lo" "uintmax_t hi" "int *rerror"
+.Fn strtou "const char * restrict nptr" "char ** restrict endptr" "int base" "uintmax_t lo" "uintmax_t hi" "int *rstatus"
.Ft uintmax_t
.Fn strtoumax "const char * restrict nptr" "char ** restrict endptr" "int base"
.Pp
@@ -91,7 +91,7 @@ to an
value.
.Fn strtou
function
-is using internally
+uses internally
.Fn strtoumax
and ensures that the result is always in the range [
.Fa lo ..
@@ -99,16 +99,16 @@ and ensures that the result is always in
].
In adddition it always places
.Dv 0
-on success or an error value in the
-.Fa rerror
+on success or a conversion status in the
+.Fa rstatus
argument, avoiding the
.Dv errno
gymnastics the other functions require.
The
-.Fa rerror
+.Fa rstatus
argument can be
.Dv NULL
-if errors are to be ignored.
+if conversion status is to be ignored.
The
.Fn strtouq
function
@@ -296,7 +296,7 @@ In addition to the above errors
returns:
.Bl -tag -width Er
.It Bq Er ECANCELED
-The string did not contain any characters that could be converted.
+The string did not contain any characters that were converted.
.It Bq Er ENOTSUP
The string contained non-numeric characters that did not get converted.
In this case,