debug: Improve fcntl.h fortify warnings with clang
[glibc.git] / posix / bug-regex6.c
blob2f038d9b8a628484f0dbdcbcd385c726a07e90d5
1 /* Test for regexec.
2 Copyright (C) 2002-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <locale.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <regex.h>
26 int
27 main (int argc, char *argv[])
29 regex_t re;
30 regmatch_t mat[10];
31 int i, j, ret = 0;
32 const char *locales[] = { "C", "C.UTF-8", "de_DE.UTF-8" };
33 const char *string = "http://www.regex.com/pattern/matching.html#intro";
34 regmatch_t expect[10] = {
35 { 0, 48 }, { 0, 5 }, { 0, 4 }, { 5, 20 }, { 7, 20 }, { 20, 42 },
36 { -1, -1 }, { -1, -1 }, { 42, 48 }, { 43, 48 } };
38 for (i = 0; i < sizeof (locales) / sizeof (locales[0]); ++i)
40 if (setlocale (LC_ALL, locales[i]) == NULL)
42 puts ("cannot set locale");
43 ret = 1;
45 else if (regcomp (&re,
46 "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?",
47 REG_EXTENDED) != REG_NOERROR)
49 puts ("cannot compile the regular expression");
50 ret = 1;
52 else if (regexec (&re, string, 10, mat, 0) == REG_NOMATCH)
54 puts ("no match");
55 ret = 1;
57 else
59 if (! memcmp (mat, expect, sizeof (mat)))
60 printf ("matching ok for %s locale\n", locales[i]);
61 else
63 printf ("matching failed for %s locale:\n", locales[i]);
64 ret = 1;
65 for (j = 0; j < 9; ++j)
66 if (mat[j].rm_so != -1)
67 printf ("%d: %.*s\n", j, mat[j].rm_eo - mat[j].rm_so,
68 string + mat[j].rm_so);
73 return ret;