1rpmatch(3) Library Functions Manual rpmatch(3)
2
3
4
6 rpmatch - determine if the answer to a question is affirmative or nega‐
7 tive
8
10 Standard C library (libc, -lc)
11
13 #include <stdlib.h>
14
15 int rpmatch(const char *response);
16
17 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19 rpmatch():
20 Since glibc 2.19:
21 _DEFAULT_SOURCE
22 glibc 2.19 and earlier:
23 _SVID_SOURCE
24
26 rpmatch() handles a user response to yes or no questions, with support
27 for internationalization.
28
29 response should be a null-terminated string containing a user-supplied
30 response, perhaps obtained with fgets(3) or getline(3).
31
32 The user's language preference is taken into account per the environ‐
33 ment variables LANG, LC_MESSAGES, and LC_ALL, if the program has called
34 setlocale(3) to effect their changes.
35
36 Regardless of the locale, responses matching ^[Yy] are always accepted
37 as affirmative, and those matching ^[Nn] are always accepted as nega‐
38 tive.
39
41 After examining response, rpmatch() returns 0 for a recognized negative
42 response ("no"), 1 for a recognized positive response ("yes"), and -1
43 when the value of response is unrecognized.
44
46 A return value of -1 may indicate either an invalid input, or some
47 other error. It is incorrect to only test if the return value is non‐
48 zero.
49
50 rpmatch() can fail for any of the reasons that regcomp(3) or regexec(3)
51 can fail; the cause of the error is not available from errno or any‐
52 where else, but indicates a failure of the regex engine (but this case
53 is indistinguishable from that of an unrecognized value of response).
54
56 For an explanation of the terms used in this section, see at‐
57 tributes(7).
58
59 ┌─────────────────────────────────────┬───────────────┬────────────────┐
60 │Interface │ Attribute │ Value │
61 ├─────────────────────────────────────┼───────────────┼────────────────┤
62 │rpmatch() │ Thread safety │ MT-Safe locale │
63 └─────────────────────────────────────┴───────────────┴────────────────┘
64
66 None.
67
69 GNU, FreeBSD, AIX.
70
72 The YESEXPR and NOEXPR of some locales (including "C") only inspect the
73 first character of the response. This can mean that "yno" et al. re‐
74 solve to 1. This is an unfortunate historical side-effect which should
75 be fixed in time with proper localisation, and should not deter from
76 rpmatch() being the proper way to distinguish between binary answers.
77
79 The following program displays the results when rpmatch() is applied to
80 the string given in the program's command-line argument.
81
82 #define _DEFAULT_SOURCE
83 #include <locale.h>
84 #include <stdio.h>
85 #include <stdlib.h>
86 #include <string.h>
87
88 int
89 main(int argc, char *argv[])
90 {
91 if (argc != 2 || strcmp(argv[1], "--help") == 0) {
92 fprintf(stderr, "%s response\n", argv[0]);
93 exit(EXIT_FAILURE);
94 }
95
96 setlocale(LC_ALL, "");
97 printf("rpmatch() returns: %d\n", rpmatch(argv[1]));
98 exit(EXIT_SUCCESS);
99 }
100
102 fgets(3), getline(3), nl_langinfo(3), regcomp(3), setlocale(3)
103
104
105
106Linux man-pages 6.04 2023-03-30 rpmatch(3)