1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) many different people.
6  * If you wrote this, please acknowledge your work.
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9  */
10 #include "libbb.h"
11 #include "xregex.h"
12 
regcomp_or_errmsg(regex_t * preg,const char * regex,int cflags)13 char* FAST_FUNC regcomp_or_errmsg(regex_t *preg, const char *regex, int cflags)
14 {
15 	int ret = regcomp(preg, regex, cflags);
16 	if (ret) {
17 		int errmsgsz = regerror(ret, preg, NULL, 0);
18 		char *errmsg = xmalloc(errmsgsz);
19 		regerror(ret, preg, errmsg, errmsgsz);
20 		return errmsg;
21 	}
22 	return NULL;
23 }
24 
xregcomp(regex_t * preg,const char * regex,int cflags)25 void FAST_FUNC xregcomp(regex_t *preg, const char *regex, int cflags)
26 {
27 	char *errmsg = regcomp_or_errmsg(preg, regex, cflags);
28 	if (errmsg) {
29 		bb_error_msg_and_die("bad regex '%s': %s", regex, errmsg);
30 	}
31 }
32