1RPMATCH(3) Linux Programmer's Manual RPMATCH(3)
2
3
4
6 rpmatch - determine if the answer to a question is affirmative or nega‐
7 tive
8
10 #include <stdlib.h>
11
12 int rpmatch(const char *response);
13
14 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
15
16 rpmatch():
17 Since glibc 2.19:
18 _DEFAULT_SOURCE
19 Glibc 2.19 and earlier:
20 _SVID_SOURCE
21
23 rpmatch() handles a user response to yes or no questions, with support
24 for internationalization.
25
26 response should be a null-terminated string containing a user-supplied
27 response, perhaps obtained with fgets(3) or getline(3).
28
29 The user's language preference is taken into account per the environ‐
30 ment variables LANG, LC_MESSAGES, and LC_ALL, if the program has called
31 setlocale(3) to effect their changes.
32
33 Regardless of the locale, responses matching ^[Yy] are always accepted
34 as affirmative, and those matching ^[Nn] are always accepted as nega‐
35 tive.
36
38 After examining response, rpmatch() returns 0 for a recognized negative
39 response ("no"), 1 for a recognized positive response ("yes"), and -1
40 when the value of response is unrecognized.
41
43 A return value of -1 may indicate either an invalid input, or some
44 other error. It is incorrect to only test if the return value is
45 nonzero.
46
47 rpmatch() can fail for any of the reasons that regcomp(3) or regexec(3)
48 can fail; the cause of the error is not available from errno or any‐
49 where else, but indicates a failure of the regex engine (but this case
50 is indistinguishable from that of an unrecognized value of response).
51
53 For an explanation of the terms used in this section, see
54 attributes(7).
55
56 ┌──────────┬───────────────┬────────────────┐
57 │Interface │ Attribute │ Value │
58 ├──────────┼───────────────┼────────────────┤
59 │rpmatch() │ Thread safety │ MT-Safe locale │
60 └──────────┴───────────────┴────────────────┘
61
63 rpmatch() is not required by any standard, but is available on a few
64 other systems.
65
67 The rpmatch() implementation looks at only the first character of
68 response. As a consequence, "nyes" returns 0, and "ynever; not in a
69 million years" returns 1. It would be preferable to accept input
70 strings much more strictly, for example (using the extended regular
71 expression notation described in regex(7)): ^([yY]|yes|YES)$ and
72 ^([nN]|no|NO)$.
73
75 The following program displays the results when rpmatch() is applied to
76 the string given in the program's command-line argument.
77
78 #define _SVID_SOURCE
79 #include <locale.h>
80 #include <stdlib.h>
81 #include <string.h>
82 #include <stdio.h>
83
84 int
85 main(int argc, char *argv[])
86 {
87 if (argc != 2 || strcmp(argv[1], "--help") == 0) {
88 fprintf(stderr, "%s response\n", argv[0]);
89 exit(EXIT_FAILURE);
90 }
91
92 setlocale(LC_ALL, "");
93 printf("rpmatch() returns: %d\n", rpmatch(argv[1]));
94 exit(EXIT_SUCCESS);
95 }
96
98 fgets(3), getline(3), nl_langinfo(3), regcomp(3), setlocale(3)
99
101 This page is part of release 4.16 of the Linux man-pages project. A
102 description of the project, information about reporting bugs, and the
103 latest version of this page, can be found at
104 https://www.kernel.org/doc/man-pages/.
105
106
107
108GNU 2017-09-15 RPMATCH(3)