- Set errno = 0 before the call. Otherwise, it may contain anything. - ERANGE is not the only possible errno value of these functions. They can also set it to EINVAL. - Any errno value after these calls is bad; just compare against 0. - Don't check for the return value; just errno. This function is guaranteed to not modify errno on success (POSIX). - Check endptr == str, which may or may not set EINVAL. Suggested-by: Iker Pedrosa <ipedrosa@redhat.com> Signed-off-by: Alejandro Colomar <alx@kernel.org>
40 lines
746 B
C
40 lines
746 B
C
/*
|
|
* SPDX-FileCopyrightText: 2007 - 2009, Nicolas François
|
|
*
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*/
|
|
|
|
#include <config.h>
|
|
|
|
#ident "$Id: getlong.c 2763 2009-04-23 09:57:03Z nekral-guest $"
|
|
|
|
#include <stdlib.h>
|
|
#include <errno.h>
|
|
#include "prototypes.h"
|
|
|
|
/*
|
|
* getulong - extract an unsigned long integer provided by the numstr string in *result
|
|
*
|
|
* It supports decimal, hexadecimal or octal representations.
|
|
*
|
|
* Returns 0 on failure, 1 on success.
|
|
*/
|
|
int getulong (const char *numstr, /*@out@*/unsigned long *result)
|
|
{
|
|
unsigned long val;
|
|
char *endptr;
|
|
|
|
errno = 0;
|
|
val = strtoul(numstr, &endptr, 0);
|
|
if ( ('\0' == *numstr)
|
|
|| ('\0' != *endptr)
|
|
|| (0 != errno)
|
|
) {
|
|
return 0;
|
|
}
|
|
|
|
*result = val;
|
|
return 1;
|
|
}
|
|
|