cleanups from (Kamil Rytarowski)
This commit is contained in:
parent
566fb07644
commit
3d8157c11a
|
@ -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 the range [
|
|||
].
|
||||
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,
|
||||
|
|
|
@ -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/01/16 18:41:33 christos Exp $");
|
|||
#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;
|
||||
}
|
||||
|
|
|
@ -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 the range [
|
|||
].
|
||||
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,
|
||||
|
|
Loading…
Reference in New Issue