From 3fd15e197e21aa313ce56126ee814f0ebc884dee Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Sat, 9 Aug 2008 16:15:14 +0000 Subject: 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). --- libbb/get_line_from_file.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'libbb/get_line_from_file.c') 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) { @@ -68,6 +71,44 @@ char* FAST_FUNC xmalloc_fgetline(FILE *file) return c; } +#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. * -- cgit v1.2.3