blob: 5870012d01fdb8034ee5bed8e3dd48f78e1de27c [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenaad1a882001-03-16 22:47:14 +00009 */
Eric Andersenaad1a882001-03-16 22:47:14 +000010#include "libbb.h"
"Vladimir N. Oleynik"23f62fc2005-09-14 16:59:11 +000011#include "xregex.h"
Eric Andersenaad1a882001-03-16 22:47:14 +000012
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000013char* FAST_FUNC regcomp_or_errmsg(regex_t *preg, const char *regex, int cflags)
Eric Andersenaad1a882001-03-16 22:47:14 +000014{
Denis Vlasenko3f3190e2006-12-21 00:22:03 +000015 int ret = regcomp(preg, regex, cflags);
16 if (ret) {
Eric Andersenaad1a882001-03-16 22:47:14 +000017 int errmsgsz = regerror(ret, preg, NULL, 0);
18 char *errmsg = xmalloc(errmsgsz);
19 regerror(ret, preg, errmsg, errmsgsz);
Denis Vlasenkoe865e812006-12-21 13:24:58 +000020 return errmsg;
21 }
22 return NULL;
23}
24
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000025void FAST_FUNC xregcomp(regex_t *preg, const char *regex, int cflags)
Denis Vlasenkoe865e812006-12-21 13:24:58 +000026{
27 char *errmsg = regcomp_or_errmsg(preg, regex, cflags);
28 if (errmsg) {
Denis Vlasenko3fd15e12008-08-09 16:15:14 +000029 bb_error_msg_and_die("bad regex '%s': %s", regex, errmsg);
Eric Andersenaad1a882001-03-16 22:47:14 +000030 }
31}