diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-08-09 16:15:14 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-08-09 16:15:14 +0000 |
commit | 3fd15e197e21aa313ce56126ee814f0ebc884dee (patch) | |
tree | 38ac32cdea89bff09017eda0a1836e60f2c06749 /libbb/get_line_from_file.c | |
parent | fb5902ca5cf802557eb1e3c56502a2f5e27242f4 (diff) | |
download | busybox-w32-3fd15e197e21aa313ce56126ee814f0ebc884dee.tar.gz busybox-w32-3fd15e197e21aa313ce56126ee814f0ebc884dee.tar.bz2 busybox-w32-3fd15e197e21aa313ce56126ee814f0ebc884dee.zip |
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.c | 43 |
1 files changed, 42 insertions, 1 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 @@ | |||
9 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | 9 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
10 | */ | 10 | */ |
11 | 11 | ||
12 | /* for getline() [GNUism] */ | ||
13 | #ifndef _GNU_SOURCE | ||
14 | #define _GNU_SOURCE 1 | ||
15 | #endif | ||
12 | #include "libbb.h" | 16 | #include "libbb.h" |
13 | 17 | ||
14 | /* This function reads an entire line from a text file, up to a newline | 18 | /* 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) | |||
55 | 59 | ||
56 | return bb_get_chunk_from_file(file, &i); | 60 | return bb_get_chunk_from_file(file, &i); |
57 | } | 61 | } |
58 | |||
59 | /* Get line. Remove trailing \n */ | 62 | /* Get line. Remove trailing \n */ |
60 | char* FAST_FUNC xmalloc_fgetline(FILE *file) | 63 | char* FAST_FUNC xmalloc_fgetline(FILE *file) |
61 | { | 64 | { |
@@ -69,6 +72,44 @@ char* FAST_FUNC xmalloc_fgetline(FILE *file) | |||
69 | } | 72 | } |
70 | 73 | ||
71 | #if 0 | 74 | #if 0 |
75 | |||
76 | /* GNUism getline() should be faster (not tested) than a loop with fgetc */ | ||
77 | |||
78 | /* Get line, including trailing \n if any */ | ||
79 | char* FAST_FUNC xmalloc_fgets(FILE *file) | ||
80 | { | ||
81 | char *res_buf = NULL; | ||
82 | size_t res_sz; | ||
83 | |||
84 | if (getline(&res_buf, &res_sz, file) == -1) { | ||
85 | free(res_buf); /* uclibc allocates a buffer even on EOF. WTF? */ | ||
86 | res_buf = NULL; | ||
87 | } | ||
88 | //TODO: trimming to res_sz? | ||
89 | return res_buf; | ||
90 | } | ||
91 | /* Get line. Remove trailing \n */ | ||
92 | char* FAST_FUNC xmalloc_fgetline(FILE *file) | ||
93 | { | ||
94 | char *res_buf = NULL; | ||
95 | size_t res_sz; | ||
96 | |||
97 | res_sz = getline(&res_buf, &res_sz, file); | ||
98 | |||
99 | if ((ssize_t)res_sz != -1) { | ||
100 | if (res_buf[res_sz - 1] == '\n') | ||
101 | res_buf[--res_sz] = '\0'; | ||
102 | //TODO: trimming to res_sz? | ||
103 | } else { | ||
104 | free(res_buf); /* uclibc allocates a buffer even on EOF. WTF? */ | ||
105 | res_buf = NULL; | ||
106 | } | ||
107 | return res_buf; | ||
108 | } | ||
109 | |||
110 | #endif | ||
111 | |||
112 | #if 0 | ||
72 | /* Faster routines (~twice as fast). +170 bytes. Unused as of 2008-07. | 113 | /* Faster routines (~twice as fast). +170 bytes. Unused as of 2008-07. |
73 | * | 114 | * |
74 | * NB: they stop at NUL byte too. | 115 | * NB: they stop at NUL byte too. |