1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2012 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 
24 /*
25  * posix regex executor
26  * single unsized-string interface
27  */
28 
29 #include "reglib.h"
30 
31 /*
32  * standard wrapper for the sized-record interface
33  */
34 
35 int
regexec(const regex_t * p,const char * s,size_t nmatch,regmatch_t * match,regflags_t flags)36 regexec(const regex_t* p, const char* s, size_t nmatch, regmatch_t* match, regflags_t flags)
37 {
38 	if (flags & REG_STARTEND)
39 	{
40 		int		r;
41 		int		m = match->rm_so;
42 		regmatch_t*	e;
43 
44 		if (!(r = regnexec(p, s + m, match->rm_eo - m, nmatch, match, flags)) && m > 0)
45 			for (e = match + nmatch; match < e; match++)
46 				if (match->rm_so >= 0)
47 				{
48 					match->rm_so += m;
49 					match->rm_eo += m;
50 				}
51 		return r;
52 	}
53 	return regnexec(p, s, s ? strlen(s) : 0, nmatch, match, flags);
54 }
55 
56 /*
57  * 20120528: regoff_t changed from int to ssize_t
58  */
59 
60 #if defined(__EXPORT__)
61 #define extern		__EXPORT__
62 #endif
63 
64 #undef	regexec
65 #if _map_libc
66 #define regexec		_ast_regexec
67 #endif
68 
69 extern int
regexec(const regex_t * p,const char * s,size_t nmatch,oldregmatch_t * oldmatch,regflags_t flags)70 regexec(const regex_t* p, const char* s, size_t nmatch, oldregmatch_t* oldmatch, regflags_t flags)
71 {
72 	if (oldmatch)
73 	{
74 		regmatch_t*	match;
75 		size_t		i;
76 		int		r;
77 
78 		if (!(match = oldof(0, regmatch_t, nmatch, 0)))
79 			return -1;
80 		if (!(r = regexec_20120528(p, s, nmatch, match, flags)))
81 			for (i = 0; i < nmatch; i++)
82 			{
83 				oldmatch[i].rm_so = match[i].rm_so;
84 				oldmatch[i].rm_eo = match[i].rm_eo;
85 			}
86 		free(match);
87 		return r;
88 	}
89 	return regexec_20120528(p, s, 0, NiL, flags);
90 }
91