122665b25SBaptiste Daroussin /*-
2d2d022b9SBaptiste Daroussin * Copyright (C) 2015 Baptiste Daroussin <bapt@FreeBSD.org>
3c308ef6fSBaptiste Daroussin * All rights reserved.
422665b25SBaptiste Daroussin *
522665b25SBaptiste Daroussin * Redistribution and use in source and binary forms, with or without
622665b25SBaptiste Daroussin * modification, are permitted provided that the following conditions
722665b25SBaptiste Daroussin * are met:
822665b25SBaptiste Daroussin * 1. Redistributions of source code must retain the above copyright
9c308ef6fSBaptiste Daroussin * notice, this list of conditions and the following disclaimer
10c308ef6fSBaptiste Daroussin * in this position and unchanged.
1122665b25SBaptiste Daroussin * 2. Redistributions in binary form must reproduce the above copyright
1222665b25SBaptiste Daroussin * notice, this list of conditions and the following disclaimer in the
1322665b25SBaptiste Daroussin * documentation and/or other materials provided with the distribution.
1422665b25SBaptiste Daroussin *
15c308ef6fSBaptiste Daroussin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16c308ef6fSBaptiste Daroussin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17c308ef6fSBaptiste Daroussin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18c308ef6fSBaptiste Daroussin * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19c308ef6fSBaptiste Daroussin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20c308ef6fSBaptiste Daroussin * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21c308ef6fSBaptiste Daroussin * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22c308ef6fSBaptiste Daroussin * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23c308ef6fSBaptiste Daroussin * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24c308ef6fSBaptiste Daroussin * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2522665b25SBaptiste Daroussin */
2622665b25SBaptiste Daroussin
2722665b25SBaptiste Daroussin #include <sys/cdefs.h>
2822665b25SBaptiste Daroussin #include <errno.h>
2922665b25SBaptiste Daroussin #include <inttypes.h>
3022665b25SBaptiste Daroussin #include <limits.h>
3122665b25SBaptiste Daroussin #include <stdlib.h>
3222665b25SBaptiste Daroussin
3322665b25SBaptiste Daroussin #include "pw.h"
3422665b25SBaptiste Daroussin
3522665b25SBaptiste Daroussin uintmax_t
strtounum(const char * __restrict np,uintmax_t minval,uintmax_t maxval,const char ** __restrict errpp)36a86e343dSBaptiste Daroussin strtounum(const char * __restrict np, uintmax_t minval, uintmax_t maxval,
37a86e343dSBaptiste Daroussin const char ** __restrict errpp)
3822665b25SBaptiste Daroussin {
39a86e343dSBaptiste Daroussin char *endp;
40a86e343dSBaptiste Daroussin uintmax_t ret;
4122665b25SBaptiste Daroussin
42*bb1816daSBaptiste Daroussin *errpp = NULL;
4322665b25SBaptiste Daroussin if (minval > maxval) {
4422665b25SBaptiste Daroussin errno = EINVAL;
45a86e343dSBaptiste Daroussin *errpp = "invalid";
4622665b25SBaptiste Daroussin return (0);
4722665b25SBaptiste Daroussin }
48a86e343dSBaptiste Daroussin errno = 0;
49a86e343dSBaptiste Daroussin ret = strtoumax(np, &endp, 10);
50a86e343dSBaptiste Daroussin if (endp == np || *endp != '\0') {
5122665b25SBaptiste Daroussin errno = EINVAL;
52a86e343dSBaptiste Daroussin *errpp = "invalid";
5322665b25SBaptiste Daroussin return (0);
5422665b25SBaptiste Daroussin }
55a86e343dSBaptiste Daroussin if (ret < minval) {
56a86e343dSBaptiste Daroussin errno = ERANGE;
57a86e343dSBaptiste Daroussin *errpp = "too small";
58a86e343dSBaptiste Daroussin return (0);
59a86e343dSBaptiste Daroussin }
60a86e343dSBaptiste Daroussin if (errno == ERANGE || ret > maxval) {
61a86e343dSBaptiste Daroussin errno = ERANGE;
62a86e343dSBaptiste Daroussin *errpp = "too large";
63a86e343dSBaptiste Daroussin return (0);
64a86e343dSBaptiste Daroussin }
6522665b25SBaptiste Daroussin return (ret);
6622665b25SBaptiste Daroussin }
67