/src/sudo/lib/util/strtobool.c
Line | Count | Source |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2010-2016 Todd C. Miller <Todd.Miller@sudo.ws> |
5 | | * |
6 | | * Permission to use, copy, modify, and distribute this software for any |
7 | | * purpose with or without fee is hereby granted, provided that the above |
8 | | * copyright notice and this permission notice appear in all copies. |
9 | | * |
10 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | | */ |
18 | | |
19 | | #include <config.h> |
20 | | #include <string.h> |
21 | | #ifdef HAVE_STRINGS_H |
22 | | # include <strings.h> |
23 | | #endif /* HAVE_STRINGS_H */ |
24 | | |
25 | | #include <sudo_compat.h> |
26 | | #include <sudo_debug.h> |
27 | | #include <sudo_util.h> |
28 | | |
29 | | int |
30 | | sudo_strtobool_v1(const char *str) |
31 | 14.9k | { |
32 | 14.9k | debug_decl(sudo_strtobool, SUDO_DEBUG_UTIL); |
33 | | |
34 | 14.9k | switch (*str) { |
35 | 2.70k | case '0': |
36 | 7.39k | case '1': |
37 | 7.39k | if (str[1] == '\0') |
38 | 7.10k | debug_return_int(*str - '0'); |
39 | 292 | break; |
40 | 292 | case 'y': |
41 | 550 | case 'Y': |
42 | 550 | if (strcasecmp(str, "yes") == 0) |
43 | 252 | debug_return_int(1); |
44 | 298 | break; |
45 | 903 | case 't': |
46 | 1.13k | case 'T': |
47 | 1.13k | if (strcasecmp(str, "true") == 0) |
48 | 872 | debug_return_int(1); |
49 | 261 | break; |
50 | 534 | case 'o': |
51 | 844 | case 'O': |
52 | 844 | if (strcasecmp(str, "on") == 0) |
53 | 369 | debug_return_int(1); |
54 | 475 | if (strcasecmp(str, "off") == 0) |
55 | 210 | debug_return_int(0); |
56 | 265 | break; |
57 | 597 | case 'n': |
58 | 862 | case 'N': |
59 | 862 | if (strcasecmp(str, "no") == 0) |
60 | 629 | debug_return_int(0); |
61 | 233 | break; |
62 | 255 | case 'f': |
63 | 1.93k | case 'F': |
64 | 1.93k | if (strcasecmp(str, "false") == 0) |
65 | 422 | debug_return_int(0); |
66 | 1.51k | break; |
67 | 14.9k | } |
68 | 14.9k | sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, |
69 | 5.13k | "invalid boolean value \"%s\"", str); |
70 | | |
71 | 5.13k | debug_return_int(-1); |
72 | 5.13k | } |