Back to index

glibc  2.9
bug-regex7.c
Go to the documentation of this file.
00001 /* Test for regs allocation in re_search and re_match.
00002    Copyright (C) 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Stepan Kasal <kasal@math.cas.cz>, 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 <locale.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <sys/types.h>
00026 #include <regex.h>
00027 
00028 
00029 int
00030 main (void)
00031 {
00032   struct re_pattern_buffer regex;
00033   struct re_registers regs;
00034   const char *s;
00035   int match, n;
00036   int result = 0;
00037 
00038   memset (&regex, '\0', sizeof (regex));
00039   regs.start = regs.end = NULL;
00040   regs.num_regs = 0;
00041   s = re_compile_pattern ("a", 1, &regex);
00042   if (s != NULL)
00043     {
00044       puts ("failed to compile pattern \"a\"");
00045       result = 1;
00046     }
00047   else
00048     {
00049       match = re_search (&regex, "baobab", 6, 0, 6, &regs);
00050       n = 1;
00051       if (match != 1)
00052        {
00053          printf ("re_search returned %d, expected 1\n", match);
00054          result = 1;
00055        }
00056       else if (regs.num_regs <= n || regs.start[n] != -1 || regs.end[n] != -1)
00057        {
00058          puts ("re_search failed to fill the -1 sentinel");
00059          result = 1;
00060        }
00061     }
00062 
00063   free (regex.buffer);
00064   memset (&regex, '\0', sizeof (regex));
00065 
00066   s = re_compile_pattern ("\\(\\(\\(a\\)\\)\\)", 13, &regex);
00067   if (s != NULL)
00068     {
00069       puts ("failed to compile pattern /\\(\\(\\(a\\)\\)\\)/");
00070       result = 1;
00071     }
00072   else
00073     {
00074       match = re_match (&regex, "apl", 3, 0, &regs);
00075       n = 4;
00076       if (match != 1)
00077        {
00078          printf ("re_match returned %d, expected 1\n", match);
00079          result = 1;
00080        }
00081       else if (regs.num_regs <= n || regs.start[n] != -1 || regs.end[n] != -1)
00082        {
00083          puts ("re_match failed to fill the -1 sentinel");
00084          result = 1;
00085        }
00086     }
00087 
00088   if (result == 0)
00089     puts (" -> OK");
00090 
00091   return result;
00092 }