/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  | 24.0k  | { | 
32  | 24.0k  |     debug_decl(sudo_strtobool, SUDO_DEBUG_UTIL);  | 
33  |  |  | 
34  | 24.0k  |     switch (*str) { | 
35  | 4.05k  |   case '0':  | 
36  | 9.89k  |   case '1':  | 
37  | 9.89k  |       if (str[1] == '\0')  | 
38  | 9.39k  |     debug_return_int(*str - '0');  | 
39  | 502  |       break;  | 
40  | 751  |   case 'y':  | 
41  | 1.75k  |   case 'Y':  | 
42  | 1.75k  |       if (strcasecmp(str, "yes") == 0)  | 
43  | 981  |     debug_return_int(1);  | 
44  | 772  |       break;  | 
45  | 1.63k  |   case 't':  | 
46  | 2.26k  |   case 'T':  | 
47  | 2.26k  |       if (strcasecmp(str, "true") == 0)  | 
48  | 1.50k  |     debug_return_int(1);  | 
49  | 762  |       break;  | 
50  | 1.70k  |   case 'o':  | 
51  | 2.43k  |   case 'O':  | 
52  | 2.43k  |       if (strcasecmp(str, "on") == 0)  | 
53  | 972  |     debug_return_int(1);  | 
54  | 1.46k  |       if (strcasecmp(str, "off") == 0)  | 
55  | 598  |     debug_return_int(0);  | 
56  | 868  |       break;  | 
57  | 1.02k  |   case 'n':  | 
58  | 2.03k  |   case 'N':  | 
59  | 2.03k  |       if (strcasecmp(str, "no") == 0)  | 
60  | 1.35k  |     debug_return_int(0);  | 
61  | 680  |       break;  | 
62  | 923  |   case 'f':  | 
63  | 3.17k  |   case 'F':  | 
64  | 3.17k  |       if (strcasecmp(str, "false") == 0)  | 
65  | 1.16k  |     debug_return_int(0);  | 
66  | 2.01k  |       break;  | 
67  | 24.0k  |     }  | 
68  | 24.0k  |     sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,  | 
69  | 8.10k  |   "invalid boolean value \"%s\"", str);  | 
70  |  |  | 
71  | 8.10k  |     debug_return_int(-1);  | 
72  | 8.10k  | }  |