/src/sudo/lib/util/strtobool.c
Line | Count | Source (jump to first uncovered line) |
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 | | /* |
20 | | * This is an open source non-commercial project. Dear PVS-Studio, please check it. |
21 | | * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com |
22 | | */ |
23 | | |
24 | | #include <config.h> |
25 | | #include <string.h> |
26 | | #ifdef HAVE_STRINGS_H |
27 | | # include <strings.h> |
28 | | #endif /* HAVE_STRINGS_H */ |
29 | | |
30 | | #include "sudo_compat.h" |
31 | | #include "sudo_debug.h" |
32 | | #include "sudo_util.h" |
33 | | |
34 | | int |
35 | | sudo_strtobool_v1(const char *str) |
36 | 12.9k | { |
37 | 12.9k | debug_decl(sudo_strtobool, SUDO_DEBUG_UTIL); |
38 | | |
39 | 12.9k | switch (*str) { |
40 | 2.28k | case '0': |
41 | 8.31k | case '1': |
42 | 8.31k | if (str[1] == '\0') |
43 | 8.02k | debug_return_int(*str - '0'); |
44 | 288 | break; |
45 | 325 | case 'y': |
46 | 595 | case 'Y': |
47 | 595 | if (strcasecmp(str, "yes") == 0) |
48 | 342 | debug_return_int(1); |
49 | 253 | break; |
50 | 400 | case 't': |
51 | 828 | case 'T': |
52 | 828 | if (strcasecmp(str, "true") == 0) |
53 | 575 | debug_return_int(1); |
54 | 253 | break; |
55 | 496 | case 'o': |
56 | 940 | case 'O': |
57 | 940 | if (strcasecmp(str, "on") == 0) |
58 | 477 | debug_return_int(1); |
59 | 463 | if (strcasecmp(str, "off") == 0) |
60 | 216 | debug_return_int(0); |
61 | 247 | break; |
62 | 415 | case 'n': |
63 | 635 | case 'N': |
64 | 635 | if (strcasecmp(str, "no") == 0) |
65 | 402 | debug_return_int(0); |
66 | 233 | break; |
67 | 258 | case 'f': |
68 | 608 | case 'F': |
69 | 608 | if (strcasecmp(str, "false") == 0) |
70 | 229 | debug_return_int(0); |
71 | 379 | break; |
72 | 12.9k | } |
73 | 12.9k | sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, |
74 | 2.70k | "invalid boolean value \"%s\"", str); |
75 | | |
76 | 2.70k | debug_return_int(-1); |
77 | 2.70k | } |