malloc.3: ffix
[man-pages.git] / man3 / rpmatch.3
blob846c492b709027d572f2e5c5042d09e780eee11a
1 .\" Copyright (C) 2006 Justin Pryzby <pryzbyj@justinpryzby.com>
2 .\"
3 .\" %%%LICENSE_START(PERMISSIVE_MISC)
4 .\" Permission is hereby granted, free of charge, to any person obtaining
5 .\" a copy of this software and associated documentation files (the
6 .\" "Software"), to deal in the Software without restriction, including
7 .\" without limitation the rights to use, copy, modify, merge, publish,
8 .\" distribute, sublicense, and/or sell copies of the Software, and to
9 .\" permit persons to whom the Software is furnished to do so, subject to
10 .\" the following conditions:
11 .\"
12 .\" The above copyright notice and this permission notice shall be
13 .\" included in all copies or substantial portions of the Software.
14 .\"
15 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 .\" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 .\" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 .\" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 .\" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 .\" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 .\" %%%LICENSE_END
23 .\"
24 .\" References:
25 .\"   glibc manual and source
26 .\"
27 .\" 2006-05-19, mtk, various edits and example program
28 .\"
29 .TH RPMATCH 3 2021-03-22 "GNU" "Linux Programmer's Manual"
30 .SH NAME
31 rpmatch \- determine if the answer to a question is affirmative or negative
32 .SH SYNOPSIS
33 .nf
34 .B #include <stdlib.h>
35 .PP
36 .BI "int rpmatch(const char *" response );
37 .fi
38 .PP
39 .RS -4
40 Feature Test Macro Requirements for glibc (see
41 .BR feature_test_macros (7)):
42 .RE
43 .PP
44 .BR rpmatch ():
45 .nf
46     Since glibc 2.19:
47         _DEFAULT_SOURCE
48     Glibc 2.19 and earlier:
49         _SVID_SOURCE
50 .fi
51 .SH DESCRIPTION
52 .BR rpmatch ()
53 handles a user response to yes or no questions, with
54 support for internationalization.
55 .PP
56 .I response
57 should be a null-terminated string containing a
58 user-supplied response, perhaps obtained with
59 .BR fgets (3)
61 .BR getline (3).
62 .PP
63 The user's language preference is taken into account per the
64 environment variables
65 .BR LANG ,
66 .BR LC_MESSAGES ,
67 and
68 .BR LC_ALL ,
69 if the program has called
70 .BR setlocale (3)
71 to effect their changes.
72 .PP
73 Regardless of the locale, responses matching
74 .B \(ha[Yy]
75 are always accepted as affirmative, and those matching
76 .B \(ha[Nn]
77 are always accepted as negative.
78 .SH RETURN VALUE
79 After examining
80 .IR response ,
81 .BR rpmatch ()
82 returns 0 for a recognized negative response ("no"), 1
83 for a recognized positive response ("yes"), and \-1 when the value
85 .I response
86 is unrecognized.
87 .SH ERRORS
88 A return value of \-1 may indicate either an invalid input, or some
89 other error.
90 It is incorrect to only test if the return value is nonzero.
91 .PP
92 .BR rpmatch ()
93 can fail for any of the reasons that
94 .BR regcomp (3)
96 .BR regexec (3)
97 can fail; the cause of the error
98 is not available from
99 .I errno
100 or anywhere else, but indicates a
101 failure of the regex engine (but this case is indistinguishable from
102 that of an unrecognized value of
103 .IR response ).
104 .SH ATTRIBUTES
105 For an explanation of the terms used in this section, see
106 .BR attributes (7).
107 .ad l
110 allbox;
111 lbx lb lb
112 l l l.
113 Interface       Attribute       Value
115 .BR rpmatch ()
116 T}      Thread safety   MT-Safe locale
120 .sp 1
121 .SH CONFORMING TO
122 .BR rpmatch ()
123 is not required by any standard, but
124 is available on a few other systems.
125 .\" It is available on at least AIX 5.1 and FreeBSD 6.0.
126 .SH BUGS
128 .BR rpmatch ()
129 implementation looks at only the first character
131 .IR response .
132 As a consequence, "nyes" returns 0, and
133 "ynever; not in a million years" returns 1.
134 It would be preferable to accept input strings much more
135 strictly, for example (using the extended regular
136 expression notation described in
137 .BR regex (7)):
138 .B \(ha([yY]|yes|YES)$
140 .BR \(ha([nN]|no|NO)$ .
141 .SH EXAMPLES
142 The following program displays the results when
143 .BR rpmatch ()
144 is applied to the string given in the program's command-line argument.
147 #define _SVID_SOURCE
148 #include <locale.h>
149 #include <stdlib.h>
150 #include <string.h>
151 #include <stdio.h>
154 main(int argc, char *argv[])
156     if (argc != 2 || strcmp(argv[1], "\-\-help") == 0) {
157         fprintf(stderr, "%s response\en", argv[0]);
158         exit(EXIT_FAILURE);
159     }
161     setlocale(LC_ALL, "");
162     printf("rpmatch() returns: %d\en", rpmatch(argv[1]));
163     exit(EXIT_SUCCESS);
166 .SH SEE ALSO
167 .BR fgets (3),
168 .BR getline (3),
169 .BR nl_langinfo (3),
170 .BR regcomp (3),
171 .BR setlocale (3)