1 | /* |
---|
2 | FUNCTION |
---|
3 | <<rpmatch>>---determine whether response to question is affirmative or negative |
---|
4 | |
---|
5 | INDEX |
---|
6 | rpmatch |
---|
7 | |
---|
8 | SYNOPSIS |
---|
9 | #include <stdlib.h> |
---|
10 | int rpmatch(const char *<[response]>); |
---|
11 | |
---|
12 | DESCRIPTION |
---|
13 | The <<rpmatch>> function determines whether <[response]> is an affirmative |
---|
14 | or negative response to a question according to the current locale. |
---|
15 | |
---|
16 | RETURNS |
---|
17 | <<rpmatch>> returns 1 if <[response]> is affirmative, 0 if negative, or -1 |
---|
18 | if not recognized as either. |
---|
19 | |
---|
20 | PORTABILITY |
---|
21 | <<rpmatch>> is a BSD extension also found in glibc. |
---|
22 | |
---|
23 | NOTES |
---|
24 | No supporting OS subroutines are required. |
---|
25 | */ |
---|
26 | |
---|
27 | /* This code is originally taken from FreeBSD. */ |
---|
28 | /*- |
---|
29 | * Copyright (c) 2004-2005 Tim J. Robbins. |
---|
30 | * All rights reserved. |
---|
31 | * |
---|
32 | * Redistribution and use in source and binary forms, with or without |
---|
33 | * modification, are permitted provided that the following conditions |
---|
34 | * are met: |
---|
35 | * 1. Redistributions of source code must retain the above copyright |
---|
36 | * notice, this list of conditions and the following disclaimer. |
---|
37 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
38 | * notice, this list of conditions and the following disclaimer in the |
---|
39 | * documentation and/or other materials provided with the distribution. |
---|
40 | * |
---|
41 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
51 | * SUCH DAMAGE. |
---|
52 | */ |
---|
53 | |
---|
54 | #include <sys/cdefs.h> |
---|
55 | #include <sys/types.h> |
---|
56 | |
---|
57 | #include <langinfo.h> |
---|
58 | #include <regex.h> |
---|
59 | #include <stdlib.h> |
---|
60 | |
---|
61 | int |
---|
62 | rpmatch (const char *response) |
---|
63 | { |
---|
64 | regex_t yes, no; |
---|
65 | int ret; |
---|
66 | |
---|
67 | if (regcomp(&yes, nl_langinfo(YESEXPR), REG_EXTENDED|REG_NOSUB) != 0) |
---|
68 | return (-1); |
---|
69 | if (regcomp(&no, nl_langinfo(NOEXPR), REG_EXTENDED|REG_NOSUB) != 0) { |
---|
70 | regfree(&yes); |
---|
71 | return (-1); |
---|
72 | } |
---|
73 | if (regexec(&yes, response, 0, NULL, 0) == 0) |
---|
74 | ret = 1; |
---|
75 | else if (regexec(&no, response, 0, NULL, 0) == 0) |
---|
76 | ret = 0; |
---|
77 | else |
---|
78 | ret = -1; |
---|
79 | regfree(&yes); |
---|
80 | regfree(&no); |
---|
81 | return (ret); |
---|
82 | } |
---|