]> git.ipfire.org Git - thirdparty/glibc.git/blame - posix/bug-regex6.c
Update copyright notices with scripts/update-copyrights
[thirdparty/glibc.git] / posix / bug-regex6.c
CommitLineData
81c64d40 1/* Test for regexec.
d4697bc9 2 Copyright (C) 2002-2014 Free Software Foundation, Inc.
81c64d40
UD
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2002.
5
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.
10
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.
15
16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
81c64d40 19
81c64d40
UD
20#include <locale.h>
21#include <stdio.h>
d683fe11
UD
22#include <string.h>
23#include <sys/types.h>
24#include <regex.h>
25
81c64d40
UD
26
27int
28main (int argc, char *argv[])
29{
30 regex_t re;
31 regmatch_t mat[10];
32 int i, j, ret = 0;
d683fe11
UD
33 const char *locales[] = { "C", "de_DE.UTF-8" };
34 const char *string = "http://www.regex.com/pattern/matching.html#intro";
81c64d40
UD
35 regmatch_t expect[10] = {
36 { 0, 48 }, { 0, 5 }, { 0, 4 }, { 5, 20 }, { 7, 20 }, { 20, 42 },
37 { -1, -1 }, { -1, -1 }, { 42, 48 }, { 43, 48 } };
38
39 for (i = 0; i < sizeof (locales) / sizeof (locales[0]); ++i)
40 {
41 if (setlocale (LC_ALL, locales[i]) == NULL)
42 {
43 puts ("cannot set locale");
44 ret = 1;
45 }
46 else if (regcomp (&re,
47 "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?",
48 REG_EXTENDED) != REG_NOERROR)
49 {
d683fe11 50 puts ("cannot compile the regular expression");
81c64d40
UD
51 ret = 1;
52 }
53 else if (regexec (&re, string, 10, mat, 0) == REG_NOMATCH)
54 {
55 puts ("no match");
56 ret = 1;
57 }
58 else
59 {
60 if (! memcmp (mat, expect, sizeof (mat)))
61 printf ("matching ok for %s locale\n", locales[i]);
62 else
63 {
64 printf ("matching failed for %s locale:\n", locales[i]);
65 ret = 1;
66 for (j = 0; j < 9; ++j)
67 if (mat[j].rm_so != -1)
68 printf ("%d: %.*s\n", j, mat[j].rm_eo - mat[j].rm_so,
69 string + mat[j].rm_so);
70 }
71 }
72 }
73
74 return ret;
75}