blob: 61efb5bc6bb64fa6476b7322fe1d8c39f41fadcd [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) many different people.
Eric Andersencb81e642003-07-14 21:21:08 +00006 * If you wrote this, please acknowledge your work.
Eric Andersenaad1a882001-03-16 22:47:14 +00007 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenaad1a882001-03-16 22:47:14 +00009 */
10
Eric Andersenaad1a882001-03-16 22:47:14 +000011#include "libbb.h"
"Vladimir N. Oleynik"23f62fc2005-09-14 16:59:11 +000012#include "xregex.h"
Eric Andersenaad1a882001-03-16 22:47:14 +000013
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000014char* FAST_FUNC regcomp_or_errmsg(regex_t *preg, const char *regex, int cflags)
Eric Andersenaad1a882001-03-16 22:47:14 +000015{
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000016 int ret = regcomp(preg, regex, cflags);
17 if (ret) {
Eric Andersenaad1a882001-03-16 22:47:14 +000018 int errmsgsz = regerror(ret, preg, NULL, 0);
19 char *errmsg = xmalloc(errmsgsz);
20 regerror(ret, preg, errmsg, errmsgsz);
Denis Vlasenkoe865e812006-12-21 13:24:58 +000021 return errmsg;
22 }
23 return NULL;
24}
25
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000026void FAST_FUNC xregcomp(regex_t *preg, const char *regex, int cflags)
Denis Vlasenkoe865e812006-12-21 13:24:58 +000027{
28 char *errmsg = regcomp_or_errmsg(preg, regex, cflags);
29 if (errmsg) {
Denis Vlasenko3fd15e12008-08-09 16:15:14 +000030 bb_error_msg_and_die("bad regex '%s': %s", regex, errmsg);
Eric Andersenaad1a882001-03-16 22:47:14 +000031 }
32}