Line data Source code
1 : /*
2 : SSSD
3 :
4 : SSSD Utility functions
5 :
6 : Copyright (C) Stephen Gallagher <sgallagh@redhat.com> 2009
7 :
8 : This program is free software; you can redistribute it and/or modify
9 : it under the terms of the GNU General Public License as published by
10 : the Free Software Foundation; either version 3 of the License, or
11 : (at your option) any later version.
12 :
13 : This program is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with this program. If not, see <http://www.gnu.org/licenses/>.
20 : */
21 :
22 : #include <ctype.h>
23 : #include <stdlib.h>
24 : #include <errno.h>
25 : #include "config.h"
26 : #include "util/util.h"
27 : #include "util/strtonum.h"
28 :
29 : /* strtoint32 */
30 884 : int32_t strtoint32(const char *nptr, char **endptr, int base)
31 : {
32 884 : long long ret = 0;
33 :
34 884 : errno = 0;
35 884 : ret = strtoll(nptr, endptr, base);
36 :
37 884 : if (ret > INT32_MAX) {
38 1 : errno = ERANGE;
39 1 : return INT32_MAX;
40 : }
41 883 : else if (ret < INT32_MIN) {
42 1 : errno = ERANGE;
43 1 : return INT32_MIN;
44 : }
45 :
46 : /* If errno was set by strtoll, we'll pass it back as-is */
47 882 : return (int32_t)ret;
48 : }
49 :
50 :
51 : /* strtouint32 */
52 97 : uint32_t strtouint32(const char *nptr, char **endptr, int base)
53 : {
54 97 : unsigned long long ret = 0;
55 97 : errno = 0;
56 97 : ret = strtoull(nptr, endptr, base);
57 :
58 97 : if (ret > UINT32_MAX) {
59 4 : errno = ERANGE;
60 4 : return UINT32_MAX;
61 : }
62 :
63 : /* If errno was set by strtoll, we'll pass it back as-is */
64 93 : return (uint32_t)ret;
65 : }
66 :
67 :
68 : /* strtouint16 */
69 8 : uint16_t strtouint16(const char *nptr, char **endptr, int base)
70 : {
71 8 : unsigned long long ret = 0;
72 8 : errno = 0;
73 8 : ret = strtoull(nptr, endptr, base);
74 :
75 8 : if (ret > UINT16_MAX) {
76 2 : errno = ERANGE;
77 2 : return UINT16_MAX;
78 : }
79 :
80 : /* If errno was set by strtoll, we'll pass it back as-is */
81 6 : return (uint16_t)ret;
82 : }
83 :
|