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