• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * dhcpcd - DHCP client daemon
3  * Copyright (c) 2006-2015 Roy Marples <roy@marples.name>
4  * All rights reserved
5 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <errno.h>
29 #include <inttypes.h>
30 #include <stdlib.h>
31 
32 #include "strtoi.h"
33 
34 intmax_t
strtoi(const char * __restrict nptr,char ** __restrict endptr,int base,intmax_t lo,intmax_t hi,int * rstatus)35 strtoi(const char * __restrict nptr, char ** __restrict endptr, int base,
36     intmax_t lo, intmax_t hi, int *rstatus)
37 {
38 	int serrno;
39 	intmax_t r;
40 	char *ep;
41 	int rep;
42 
43 	if (endptr == NULL)
44 		endptr = &ep;
45 	if (rstatus == NULL)
46 		rstatus = &rep;
47 
48 	serrno = errno;
49 	errno = 0;
50 	r = strtoimax(nptr, endptr, base);
51 	*rstatus = errno;
52 	errno = serrno;
53 
54 	if (*rstatus == 0) {
55 		if (nptr == *endptr)
56 			*rstatus = ECANCELED;
57 		else if (**endptr != '\0')
58 			*rstatus = ENOTSUP;
59 	}
60 
61 	if (r < lo) {
62 		if (*rstatus == 0)
63 			*rstatus = ERANGE;
64 		return lo;
65 	}
66 	if (r > hi) {
67 		if (*rstatus == 0)
68 			*rstatus = ERANGE;
69 		return hi;
70 	}
71 	return r;
72 }
73 
74 uintmax_t
strtou(const char * __restrict nptr,char ** __restrict endptr,int base,uintmax_t lo,uintmax_t hi,int * rstatus)75 strtou(const char * __restrict nptr, char ** __restrict endptr, int base,
76     uintmax_t lo, uintmax_t hi, int *rstatus)
77 {
78 	int serrno;
79 	uintmax_t r;
80 	char *ep;
81 	int rep;
82 
83 	if (endptr == NULL)
84 		endptr = &ep;
85 	if (rstatus == NULL)
86 		rstatus = &rep;
87 
88 	serrno = errno;
89 	errno = 0;
90 	r = strtoumax(nptr, endptr, base);
91 	*rstatus = errno;
92 	errno = serrno;
93 
94 	if (*rstatus == 0) {
95 		if (nptr == *endptr)
96 			*rstatus = ECANCELED;
97 		else if (**endptr != '\0')
98 			*rstatus = ENOTSUP;
99 	}
100 
101 	if (r < lo) {
102 		if (*rstatus == 0)
103 			*rstatus = ERANGE;
104 		return lo;
105 	}
106 	if (r > hi) {
107 		if (*rstatus == 0)
108 			*rstatus = ERANGE;
109 		return hi;
110 	}
111 	return r;
112 }
113