Imported Upstream version 1.8.7
[debian/sudo] / common / atobool.c
1 /*
2  * Copyright (c) 2010-2012 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <config.h>
18
19 #include <sys/types.h>
20
21 #include <stdio.h>
22 #ifdef STDC_HEADERS
23 # include <stdlib.h>
24 # include <stddef.h>
25 #else
26 # ifdef HAVE_STDLIB_H
27 #  include <stdlib.h>
28 # endif
29 #endif /* STDC_HEADERS */
30 #ifdef HAVE_STRING_H
31 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
32 #  include <memory.h>
33 # endif
34 # include <string.h>
35 #endif /* HAVE_STRING_H */
36 #ifdef HAVE_STRINGS_H
37 # include <strings.h>
38 #endif /* HAVE_STRINGS_H */
39
40 #include "missing.h"
41 #include "sudo_debug.h"
42
43 int
44 atobool(const char *str)
45 {
46     debug_decl(atobool, SUDO_DEBUG_UTIL)
47
48     switch (*str) {
49         case '0':
50         case '1':
51             if (str[1] == '\0')
52                 return *str - '0';
53             break;
54         case 'y':
55         case 'Y':
56             if (strcasecmp(str, "yes") == 0)
57                 return 1;
58             break;
59         case 't':
60         case 'T':
61             if (strcasecmp(str, "true") == 0)
62                 return 1;
63             break;
64         case 'o':
65         case 'O':
66             if (strcasecmp(str, "on") == 0)
67                 return 1;
68             if (strcasecmp(str, "off") == 0)
69                 return 0;
70             break;
71         case 'n':
72         case 'N':
73             if (strcasecmp(str, "no") == 0)
74                 return 0;
75             break;
76         case 'f':
77         case 'F':
78             if (strcasecmp(str, "false") == 0)
79                 return 0;
80             break;
81     }
82     debug_return_int(-1);
83 }