diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-08-09 20:15:14 +0400 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-08-09 20:15:14 +0400 |
commit | 3fd15e197e21aa313ce56126ee814f0ebc884dee (patch) | |
tree | 38ac32cdea89bff09017eda0a1836e60f2c06749 /libbb | |
parent | fb5902ca5cf802557eb1e3c56502a2f5e27242f4 (diff) |
grep: option to use GNU regex matching instead of POSIX one.
This fixes problems with NULs in files being scanned, but
costs +800 bytes. The same can be done to sed (TODO).
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/get_line_from_file.c | 43 | ||||
-rw-r--r-- | libbb/xregcomp.c | 2 |
2 files changed, 43 insertions, 2 deletions
diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c index 56761f941..968d7572d 100644 --- a/libbb/get_line_from_file.c +++ b/libbb/get_line_from_file.c @@ -9,6 +9,10 @@ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ +/* for getline() [GNUism] */ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE 1 +#endif #include "libbb.h" /* This function reads an entire line from a text file, up to a newline @@ -55,7 +59,6 @@ char* FAST_FUNC xmalloc_fgets(FILE *file) return bb_get_chunk_from_file(file, &i); } - /* Get line. Remove trailing \n */ char* FAST_FUNC xmalloc_fgetline(FILE *file) { @@ -69,6 +72,44 @@ char* FAST_FUNC xmalloc_fgetline(FILE *file) } #if 0 + +/* GNUism getline() should be faster (not tested) than a loop with fgetc */ + +/* Get line, including trailing \n if any */ +char* FAST_FUNC xmalloc_fgets(FILE *file) +{ + char *res_buf = NULL; + size_t res_sz; + + if (getline(&res_buf, &res_sz, file) == -1) { + free(res_buf); /* uclibc allocates a buffer even on EOF. WTF? */ + res_buf = NULL; + } +//TODO: trimming to res_sz? + return res_buf; +} +/* Get line. Remove trailing \n */ +char* FAST_FUNC xmalloc_fgetline(FILE *file) +{ + char *res_buf = NULL; + size_t res_sz; + + res_sz = getline(&res_buf, &res_sz, file); + + if ((ssize_t)res_sz != -1) { + if (res_buf[res_sz - 1] == '\n') + res_buf[--res_sz] = '\0'; +//TODO: trimming to res_sz? + } else { + free(res_buf); /* uclibc allocates a buffer even on EOF. WTF? */ + res_buf = NULL; + } + return res_buf; +} + +#endif + +#if 0 /* Faster routines (~twice as fast). +170 bytes. Unused as of 2008-07. * * NB: they stop at NUL byte too. diff --git a/libbb/xregcomp.c b/libbb/xregcomp.c index abfa35ff1..61efb5bc6 100644 --- a/libbb/xregcomp.c +++ b/libbb/xregcomp.c @@ -27,6 +27,6 @@ void FAST_FUNC xregcomp(regex_t *preg, const char *regex, int cflags) { char *errmsg = regcomp_or_errmsg(preg, regex, cflags); if (errmsg) { - bb_error_msg_and_die("xregcomp: %s", errmsg); + bb_error_msg_and_die("bad regex '%s': %s", regex, errmsg); } } |