1c74dfa2fSAndre Oppermann /*- 2c74dfa2fSAndre Oppermann * Copyright (c) 2004 Ted Unangst and Todd Miller 3c74dfa2fSAndre Oppermann * All rights reserved. 4c74dfa2fSAndre Oppermann * 5c74dfa2fSAndre Oppermann * Permission to use, copy, modify, and distribute this software for any 6c74dfa2fSAndre Oppermann * purpose with or without fee is hereby granted, provided that the above 7c74dfa2fSAndre Oppermann * copyright notice and this permission notice appear in all copies. 8c74dfa2fSAndre Oppermann * 9c74dfa2fSAndre Oppermann * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10c74dfa2fSAndre Oppermann * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11c74dfa2fSAndre Oppermann * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12c74dfa2fSAndre Oppermann * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13c74dfa2fSAndre Oppermann * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14c74dfa2fSAndre Oppermann * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15c74dfa2fSAndre Oppermann * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16c74dfa2fSAndre Oppermann * 17c74dfa2fSAndre Oppermann * $OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $ 18c74dfa2fSAndre Oppermann */ 19c74dfa2fSAndre Oppermann 20c74dfa2fSAndre Oppermann #include <sys/cdefs.h> 21c74dfa2fSAndre Oppermann __FBSDID("$FreeBSD$"); 22c74dfa2fSAndre Oppermann 23c74dfa2fSAndre Oppermann #include <errno.h> 24c74dfa2fSAndre Oppermann #include <limits.h> 25c74dfa2fSAndre Oppermann #include <stdlib.h> 26c74dfa2fSAndre Oppermann 27c74dfa2fSAndre Oppermann #define INVALID 1 28c74dfa2fSAndre Oppermann #define TOOSMALL 2 29c74dfa2fSAndre Oppermann #define TOOLARGE 3 30c74dfa2fSAndre Oppermann 31c74dfa2fSAndre Oppermann long long 32c74dfa2fSAndre Oppermann strtonum(const char *numstr, long long minval, long long maxval, 33c74dfa2fSAndre Oppermann const char **errstrp) 34c74dfa2fSAndre Oppermann { 35c74dfa2fSAndre Oppermann long long ll = 0; 36c74dfa2fSAndre Oppermann char *ep; 37c74dfa2fSAndre Oppermann int error = 0; 38c74dfa2fSAndre Oppermann struct errval { 39c74dfa2fSAndre Oppermann const char *errstr; 40c74dfa2fSAndre Oppermann int err; 41c74dfa2fSAndre Oppermann } ev[4] = { 42c74dfa2fSAndre Oppermann { NULL, 0 }, 43c74dfa2fSAndre Oppermann { "invalid", EINVAL }, 44c74dfa2fSAndre Oppermann { "too small", ERANGE }, 45c74dfa2fSAndre Oppermann { "too large", ERANGE }, 46c74dfa2fSAndre Oppermann }; 47c74dfa2fSAndre Oppermann 48c74dfa2fSAndre Oppermann ev[0].err = errno; 49c74dfa2fSAndre Oppermann errno = 0; 50c74dfa2fSAndre Oppermann if (minval > maxval) 51c74dfa2fSAndre Oppermann error = INVALID; 52c74dfa2fSAndre Oppermann else { 53c74dfa2fSAndre Oppermann ll = strtoll(numstr, &ep, 10); 54c74dfa2fSAndre Oppermann if (numstr == ep || *ep != '\0') 55c74dfa2fSAndre Oppermann error = INVALID; 56c74dfa2fSAndre Oppermann else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval) 57c74dfa2fSAndre Oppermann error = TOOSMALL; 58c74dfa2fSAndre Oppermann else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval) 59c74dfa2fSAndre Oppermann error = TOOLARGE; 60c74dfa2fSAndre Oppermann } 61c74dfa2fSAndre Oppermann if (errstrp != NULL) 62c74dfa2fSAndre Oppermann *errstrp = ev[error].errstr; 63c74dfa2fSAndre Oppermann errno = ev[error].err; 64c74dfa2fSAndre Oppermann if (error) 65c74dfa2fSAndre Oppermann ll = 0; 66c74dfa2fSAndre Oppermann 67c74dfa2fSAndre Oppermann return (ll); 68c74dfa2fSAndre Oppermann } 69