util: add eregcomp: show descriptive error message on regcomp error

This commit is contained in:
Hiltjo Posthuma 2014-11-16 15:16:14 +01:00 committed by sin
parent 25e3e068ed
commit ce90cc57d4
3 changed files with 31 additions and 0 deletions

View File

@ -25,6 +25,7 @@ LIB = \
util/ealloc.o \ util/ealloc.o \
util/enmasse.o \ util/enmasse.o \
util/eprintf.o \ util/eprintf.o \
util/eregcomp.o \
util/estrtod.o \ util/estrtod.o \
util/estrtol.o \ util/estrtol.o \
util/fnck.o \ util/fnck.o \

5
util.h
View File

@ -1,4 +1,5 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <regex.h>
#include <stddef.h> #include <stddef.h>
#include <sys/types.h> #include <sys/types.h>
#include "arg.h" #include "arg.h"
@ -34,6 +35,10 @@ size_t strlcat(char *, const char *, size_t);
#undef strlcpy #undef strlcpy
size_t strlcpy(char *, const char *, size_t); size_t strlcpy(char *, const char *, size_t);
/* regex */
int enregcomp(int, regex_t *, const char *, int);
int eregcomp(regex_t *, const char *, int);
/* misc */ /* misc */
void enmasse(int, char **, int (*)(const char *, const char *)); void enmasse(int, char **, int (*)(const char *, const char *));
void fnck(const char *, const char *, int (*)(const char *, const char *)); void fnck(const char *, const char *, int (*)(const char *, const char *));

25
util/eregcomp.c Normal file
View File

@ -0,0 +1,25 @@
#include <regex.h>
#include <stdio.h>
#include <sys/types.h>
#include "../util.h"
int
enregcomp(int status, regex_t *preg, const char *regex, int cflags)
{
char errbuf[BUFSIZ] = "";
int r;
if((r = regcomp(preg, regex, cflags)) == 0)
return r;
regerror(r, preg, errbuf, sizeof(errbuf));
enprintf(status, "invalid regex: %s\n", errbuf);
return r;
}
int
eregcomp(regex_t *preg, const char *regex, int cflags)
{
return enregcomp(1, preg, regex, cflags);
}