1/* Test for regexec.
2 Copyright (C) 2002-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19#include <locale.h>
20#include <stdio.h>
21#include <string.h>
22#include <sys/types.h>
23#include <regex.h>
24
25
26int
27main (int argc, char *argv[])
28{
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 } };
37
38 for (i = 0; i < sizeof (locales) / sizeof (locales[0]); ++i)
39 {
40 if (setlocale (LC_ALL, locales[i]) == NULL)
41 {
42 puts (s: "cannot set locale");
43 ret = 1;
44 }
45 else if (regcomp (preg: &re,
46 pattern: "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?",
47 REG_EXTENDED) != REG_NOERROR)
48 {
49 puts (s: "cannot compile the regular expression");
50 ret = 1;
51 }
52 else if (regexec (preg: &re, String: string, nmatch: 10, pmatch: mat, eflags: 0) == REG_NOMATCH)
53 {
54 puts (s: "no match");
55 ret = 1;
56 }
57 else
58 {
59 if (! memcmp (mat, expect, sizeof (mat)))
60 printf (format: "matching ok for %s locale\n", locales[i]);
61 else
62 {
63 printf (format: "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 (format: "%d: %.*s\n", j, mat[j].rm_eo - mat[j].rm_so,
68 string + mat[j].rm_so);
69 }
70 }
71 }
72
73 return ret;
74}
75

source code of glibc/posix/bug-regex6.c