Imported Upstream version 1.8.2
[debian/sudo] / common / atobool.c
1 /*
2  * Copyright (c) 2010 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 #include <sys/param.h>
21
22 #include <stdio.h>
23 #ifdef STDC_HEADERS
24 # include <stdlib.h>
25 # include <stddef.h>
26 #else
27 # ifdef HAVE_STDLIB_H
28 #  include <stdlib.h>
29 # endif
30 #endif /* STDC_HEADERS */
31 #ifdef HAVE_STRING_H
32 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
33 #  include <memory.h>
34 # endif
35 # include <string.h>
36 #endif /* HAVE_STRING_H */
37 #ifdef HAVE_STRINGS_H
38 # include <strings.h>
39 #endif /* HAVE_STRINGS_H */
40
41 #include "missing.h"
42
43 int
44 atobool(const char *str)
45 {
46     switch (*str) {
47         case '0':
48         case '1':
49             if (str[1] == '\0')
50                 return *str - '0';
51             break;
52         case 'y':
53         case 'Y':
54             if (strcasecmp(str, "yes") == 0)
55                 return 1;
56             break;
57         case 't':
58         case 'T':
59             if (strcasecmp(str, "true") == 0)
60                 return 1;
61             break;
62         case 'o':
63         case 'O':
64             if (strcasecmp(str, "on") == 0)
65                 return 1;
66             if (strcasecmp(str, "off") == 0)
67                 return 0;
68             break;
69         case 'n':
70         case 'N':
71             if (strcasecmp(str, "no") == 0)
72                 return 0;
73             break;
74         case 'f':
75         case 'F':
76             if (strcasecmp(str, "false") == 0)
77                 return 0;
78             break;
79     }
80     return -1;
81 }