1 /*
2 * Copyright (c) 2001-2021 The strace developers.
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: LGPL-2.1-or-later
6 */
7
8 #ifdef HAVE_CONFIG_H
9 # include "config.h"
10 #endif
11
12 #include <errno.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "string_to_uint.h"
17
18 long long
19 string_to_uint_ex(const char *const str, char **const endptr,
20 const unsigned long long max_val,
21 const char *const accepted_ending)
22 {
23 char *end;
24 long long val;
25
26 if (!*str)
27 return -1;
28
29 errno = 0;
30 val = strtoll(str, &end, 10);
31
32 if (str == end || val < 0 || (unsigned long long) val > max_val
33 || (val == LLONG_MAX && errno == ERANGE))
34 return -1;
35
36 if (*end && (!accepted_ending || !strchr(accepted_ending, *end)))
37 return -1;
38
39 if (endptr)
40 *endptr = end;
41
42 return val;
43 }