OSDN Git Service

(split) LDP: Update original to LDP v3.50.
[linuxjm/LDP_man-pages.git] / original / man3 / rpmatch.3
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 2007-07-26 "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
36 .BI "int rpmatch(const char *" response );
37 .fi
38 .sp
39 .in -4n
40 Feature Test Macro Requirements for glibc (see
41 .BR feature_test_macros (7)):
42 .in
43 .sp
44 .BR rpmatch ():
45 _SVID_SOURCE
46 .SH DESCRIPTION
47 .BR rpmatch ()
48 handles a user response to yes or no questions, with
49 support for internationalization.
50
51 \fIresponse\fP should be a null-terminated string containing a
52 user-supplied response, perhaps obtained with
53 .BR fgets (3)
54 or
55 .BR getline (3).
56
57 The user's language preference is taken into account per the
58 environment variables \fBLANG\fP, \fBLC_MESSAGES\fP, and \fBLC_ALL\fP,
59 if the program has called
60 .BR setlocale (3)
61 to effect their changes.
62
63 Regardless of the locale, responses matching \fB^[Yy]\fP are always
64 accepted as affirmative, and those matching \fB^[Nn]\fP are always
65 accepted as negative.
66 .SH RETURN VALUE
67 After examining
68 .IR response ,
69 .BR rpmatch ()
70 returns 0 for a recognized negative response ("no"), 1
71 for a recognized positive response ("yes"), and \-1 when the value
72 of \fIresponse\fP is unrecognized.
73 .SH ERRORS
74 A return value of \-1 may indicate either an invalid input, or some
75 other error.
76 It is incorrect to only test if the return value is nonzero.
77
78 .BR rpmatch ()
79 can fail for any of the reasons that
80 .BR regcomp (3)
81 or
82 .BR regexec (3)
83 can fail; the cause of the error
84 is not available from \fIerrno\fP or anywhere else, but indicates a
85 failure of the regex engine (but this case is indistinguishable from
86 that of an unrecognized value of \fIresponse\fP).
87 .SH CONFORMING TO
88 .BR rpmatch ()
89 is not required by any standard, but
90 is available on a few other systems.
91 .\" It is available on at least AIX 5.1 and FreeBSD 6.0.
92 .SH BUGS
93 The
94 .BR rpmatch ()
95 implementation looks at only the first character
96 of \fIresponse\fP.
97 As a consequence, "nyes" returns 0, and
98 "ynever; not in a million years" returns 1.
99 It would be preferable to accept input strings much more
100 strictly, for example (using the extended regular
101 expression notation described in
102 .BR regex (7)):
103 \fB^([yY]|yes|YES)$\fP and \fB^([nN]|no|NO)$\fP.
104 .SH EXAMPLE
105 The following program displays the results when
106 .BR rpmatch ()
107 is applied to the string given in the program's command-line argument.
108 .nf
109
110 #define _SVID_SOURCE
111 #include <locale.h>
112 #include <stdlib.h>
113 #include <string.h>
114 #include <stdio.h>
115
116 int
117 main(int argc, char *argv[])
118 {
119     if (argc != 2 || strcmp(argv[1], "\-\-help") == 0) {
120         fprintf(stderr, "%s response\\n", argv[0]);
121         exit(EXIT_FAILURE);
122     }
123
124     setlocale(LC_ALL, "");
125     printf("rpmatch() returns: %d\\n", rpmatch(argv[1]));
126     exit(EXIT_SUCCESS);
127 }
128 .fi
129 .SH SEE ALSO
130 .BR fgets (3),
131 .BR getline (3),
132 .BR nl_langinfo (3),
133 .BR regcomp (3),
134 .BR setlocale (3)