sbase

suckless unix tools
git clone git://git.2f30.org/sbase.git
Log | Files | Refs | README | LICENSE

commit ce90cc57d46faa40232dd963213c021708ea0d0e
parent 25e3e068ed0eeef22a3bd9b0fb37c083c4066561
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Sun, 16 Nov 2014 15:16:14 +0100

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

Diffstat:
Makefile | 1+
util.h | 5+++++
util/eregcomp.c | 25+++++++++++++++++++++++++
3 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -25,6 +25,7 @@ LIB = \ util/ealloc.o \ util/enmasse.o \ util/eprintf.o \ + util/eregcomp.o \ util/estrtod.o \ util/estrtol.o \ util/fnck.o \ diff --git a/util.h b/util.h @@ -1,4 +1,5 @@ /* See LICENSE file for copyright and license details. */ +#include <regex.h> #include <stddef.h> #include <sys/types.h> #include "arg.h" @@ -34,6 +35,10 @@ size_t strlcat(char *, const char *, size_t); #undef strlcpy 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 */ void enmasse(int, char **, int (*)(const char *, const char *)); void fnck(const char *, const char *, int (*)(const char *, const char *)); diff --git a/util/eregcomp.c b/util/eregcomp.c @@ -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); +}