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