Fix build of nptl/tst-thread_local1.cc with GCC 12
[glibc.git] / stdlib / rpmatch.c
blob2743743c9030568d2a4049bb3232be5fab8dc44d
1 /* Determine whether string value is affirmation or negative response
2 according to current locale's data.
3 This file is part of the GNU C Library.
4 Copyright (C) 1996-2021 Free Software Foundation, Inc.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
20 #include <langinfo.h>
21 #include <stdlib.h>
22 #include <regex.h>
25 /* Match against one of the response patterns, compiling the pattern
26 first if necessary. */
27 static int
28 try (const char *response,
29 const int tag, const int match, const int nomatch,
30 const char **lastp, regex_t *re)
32 const char *pattern = nl_langinfo (tag);
33 if (pattern != *lastp)
35 /* The pattern has changed. */
36 if (*lastp != NULL)
38 /* Free the old compiled pattern. */
39 __regfree (re);
40 *lastp = NULL;
42 /* Compile the pattern and cache it for future runs. */
43 if (__regcomp (re, pattern, REG_EXTENDED) != 0)
44 return -1;
45 *lastp = pattern;
48 /* Try the pattern. */
49 return __regexec (re, response, 0, NULL, 0) == 0 ? match : nomatch;
52 int
53 rpmatch (const char *response)
55 /* We cache the response patterns and compiled regexps here. */
56 static const char *yesexpr, *noexpr;
57 static regex_t yesre, nore;
59 return (try (response, YESEXPR, 1, 0, &yesexpr, &yesre) ?:
60 try (response, NOEXPR, 0, -1, &noexpr, &nore));