]> git.ipfire.org Git - thirdparty/glibc.git/blame - posix/bug-regex6.c
Remove "Contributed by" lines
[thirdparty/glibc.git] / posix / bug-regex6.c
CommitLineData
81c64d40 1/* Test for regexec.
2b778ceb 2 Copyright (C) 2002-2021 Free Software Foundation, Inc.
81c64d40 3 This file is part of the GNU C Library.
81c64d40
UD
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
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
81c64d40 18
81c64d40
UD
19#include <locale.h>
20#include <stdio.h>
d683fe11
UD
21#include <string.h>
22#include <sys/types.h>
23#include <regex.h>
24
81c64d40
UD
25
26int
27main (int argc, char *argv[])
28{
29 regex_t re;
30 regmatch_t mat[10];
31 int i, j, ret = 0;
d683fe11
UD
32 const char *locales[] = { "C", "de_DE.UTF-8" };
33 const char *string = "http://www.regex.com/pattern/matching.html#intro";
81c64d40
UD
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 ("cannot set locale");
43 ret = 1;
44 }
45 else if (regcomp (&re,
46 "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?",
47 REG_EXTENDED) != REG_NOERROR)
48 {
d683fe11 49 puts ("cannot compile the regular expression");
81c64d40
UD
50 ret = 1;
51 }
52 else if (regexec (&re, string, 10, mat, 0) == REG_NOMATCH)
53 {
54 puts ("no match");
55 ret = 1;
56 }
57 else
58 {
59 if (! memcmp (mat, expect, sizeof (mat)))
60 printf ("matching ok for %s locale\n", locales[i]);
61 else
62 {
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);
69 }
70 }
71 }
72
73 return ret;
74}