11305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood/* $OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $ */ 21305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood 31305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood/* 41305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * Copyright (c) 2004 Ted Unangst and Todd Miller 51305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * All rights reserved. 61305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * 71305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * Permission to use, copy, modify, and distribute this software for any 81305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * purpose with or without fee is hereby granted, provided that the above 91305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * copyright notice and this permission notice appear in all copies. 101305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * 111305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 121305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 131305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 141305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 151305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 161305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 171305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 181305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood */ 191305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood 201305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood/* OPENBSD ORIGINAL: lib/libc/stdlib/strtonum.c */ 211305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood 221305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood#include "includes.h" 231305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood 241305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood#ifndef HAVE_STRTONUM 251305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood#include <stdlib.h> 261305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood#include <limits.h> 271305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood#include <errno.h> 281305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood 291305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood#define INVALID 1 301305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood#define TOOSMALL 2 311305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood#define TOOLARGE 3 321305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood 331305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwoodlong long 341305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwoodstrtonum(const char *numstr, long long minval, long long maxval, 351305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood const char **errstrp) 361305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood{ 371305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood long long ll = 0; 381305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood char *ep; 391305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood int error = 0; 401305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood struct errval { 411305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood const char *errstr; 421305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood int err; 431305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood } ev[4] = { 441305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood { NULL, 0 }, 451305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood { "invalid", EINVAL }, 461305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood { "too small", ERANGE }, 471305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood { "too large", ERANGE }, 481305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood }; 491305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood 501305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood ev[0].err = errno; 511305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood errno = 0; 521305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood if (minval > maxval) 531305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood error = INVALID; 541305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood else { 551305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood ll = strtoll(numstr, &ep, 10); 561305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood if (numstr == ep || *ep != '\0') 571305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood error = INVALID; 581305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval) 591305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood error = TOOSMALL; 601305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval) 611305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood error = TOOLARGE; 621305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood } 631305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood if (errstrp != NULL) 641305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood *errstrp = ev[error].errstr; 651305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood errno = ev[error].err; 661305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood if (error) 671305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood ll = 0; 681305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood 691305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood return (ll); 701305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood} 711305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood 721305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood#endif /* HAVE_STRTONUM */ 73