summaryrefslogtreecommitdiff
path: root/libbb/get_line_from_file.c
diff options
context:
space:
mode:
authorDenis Vlasenko2008-08-09 16:15:14 +0000
committerDenis Vlasenko2008-08-09 16:15:14 +0000
commit3fd15e197e21aa313ce56126ee814f0ebc884dee (patch)
tree38ac32cdea89bff09017eda0a1836e60f2c06749 /libbb/get_line_from_file.c
parentfb5902ca5cf802557eb1e3c56502a2f5e27242f4 (diff)
downloadbusybox-3fd15e197e21aa313ce56126ee814f0ebc884dee.zip
busybox-3fd15e197e21aa313ce56126ee814f0ebc884dee.tar.gz
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/get_line_from_file.c')
-rw-r--r--libbb/get_line_from_file.c43
1 files changed, 42 insertions, 1 deletions
diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c
index 56761f9..968d757 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.