libite 2.6.1
strtonum.c File Reference
#include <errno.h>
#include <limits.h>
#include <stdlib.h>

Go to the source code of this file.

Macros

#define INVALID   1
 
#define TOOSMALL   2
 
#define TOOLARGE   3
 
#define LLONG_MAX   0x7fffffffffffffffLL
 
#define LLONG_MIN   (-0x7fffffffffffffffLL - 1)
 

Functions

long long strtonum (const char *numstr, long long minval, long long maxval, const char **errstrp)
 

Detailed Description

Author
Ted Unangst
Todd Miller
Date
2004

Definition in file strtonum.c.

Macro Definition Documentation

◆ INVALID

#define INVALID   1

internal

Definition at line 33 of file strtonum.c.

◆ LLONG_MAX

#define LLONG_MAX   0x7fffffffffffffffLL

internal

Definition at line 38 of file strtonum.c.

◆ LLONG_MIN

#define LLONG_MIN   (-0x7fffffffffffffffLL - 1)

internal

Definition at line 42 of file strtonum.c.

◆ TOOLARGE

#define TOOLARGE   3

internal

Definition at line 35 of file strtonum.c.

◆ TOOSMALL

#define TOOSMALL   2

internal

Definition at line 34 of file strtonum.c.

Function Documentation

◆ strtonum()

long long strtonum ( const char * numstr,
long long minval,
long long maxval,
const char ** errstrp )

Reliably convert string value to an integer

Parameters
numstrString to convert to a number
minvalLower bound to check number against
maxvalUpper bound to check number against
errstrpPointer to error string

This function converts the string in numstr to a long long value. The function was designed to facilitate safe, robust programming and overcome the shortcomings of the atoi(3) and strtol(3) family of interfaces.

The string may begin with an arbitrary amount of whitespace (as determined by isspace(3)) followed by a single optional ‘+’ or ‘-’ sign.

The remainder of the string is converted to a long long value according to base 10.

The value obtained is then checked against the provided minval and maxval bounds. If errstrp is non-NULL, strtonum() stores an error string in *errstrp indicating the failure.

Returns
The result of the conversion, unless the value would exceed the provided bounds or is invalid. On error, 0 is returned, errno is set, and errstrp points to an error message. *errstr* is set to NULL on success; this fact can be used to differentiate a successful return of 0 from an error.

Definition at line 75 of file strtonum.c.