Back to index

glibc  2.9
bug-regex12.c
Go to the documentation of this file.
00001 /* Regular expression tests.
00002    Copyright (C) 2002, 2003 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2002.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <sys/types.h>
00022 #include <mcheck.h>
00023 #include <regex.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 
00027 /* Tests supposed to not match.  */
00028 struct
00029 {
00030   const char *pattern;
00031   const char *string;
00032   int flags, nmatch;
00033 } tests[] = {
00034   { "^<\\([^~]*\\)\\([^~]\\)[^~]*~\\1\\(.\\).*|=.*\\3.*\\2",
00035     "<,.8~2,~so-|=-~.0,123456789<><", REG_NOSUB, 0 },
00036   /* In ERE, all carets must be treated as anchors.  */
00037   { "a^b", "a^b", REG_EXTENDED, 0 }
00038 };
00039 
00040 int
00041 main (void)
00042 {
00043   regex_t re;
00044   regmatch_t rm[4];
00045   size_t i;
00046   int n, ret = 0;
00047 
00048   mtrace ();
00049 
00050   for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
00051     {
00052       n = regcomp (&re, tests[i].pattern, tests[i].flags);
00053       if (n != 0)
00054        {
00055          char buf[500];
00056          regerror (n, &re, buf, sizeof (buf));
00057          printf ("regcomp %zd failed: %s\n", i, buf);
00058          ret = 1;
00059          continue;
00060        }
00061 
00062       if (! regexec (&re, tests[i].string, tests[i].nmatch,
00063                    tests[i].nmatch ? rm : NULL, 0))
00064        {
00065          printf ("regexec %zd incorrectly matched\n", i);
00066          ret = 1;
00067        }
00068 
00069       regfree (&re);
00070     }
00071 
00072   return ret;
00073 }