1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 2005, 2006 Rob Landley <rob@landley.net>
6 * Copyright (C) 2004 Erik Andersen <andersen@codepoet.org>
7 * Copyright (C) 2001 Matt Krai
9 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
13 char* FAST_FUNC bb_get_chunk_from_file(FILE *file, size_t *end)
19 while ((ch = getc(file)) != EOF) {
20 /* grow the line buffer as necessary */
22 if (idx == ((size_t)-1) - 0xff)
23 bb_die_memory_exhausted();
24 linebuf = xrealloc(linebuf, idx + 0x100);
26 linebuf[idx++] = (char) ch;
29 if (end && ch == '\n')
35 // huh, does fgets discard prior data on error like this?
36 // I don't think so....
41 linebuf = xrealloc(linebuf, idx + 1);
47 /* Get line, including trailing \n if any */
48 char* FAST_FUNC xmalloc_fgets(FILE *file)
52 return bb_get_chunk_from_file(file, &i);
54 /* Get line. Remove trailing \n */
55 char* FAST_FUNC xmalloc_fgetline(FILE *file)
58 char *c = bb_get_chunk_from_file(file, &i);
60 if (i && c[--i] == '\n')
67 /* GNUism getline() should be faster (not tested) than a loop with fgetc */
69 /* Get line, including trailing \n if any */
70 char* FAST_FUNC xmalloc_fgets(FILE *file)
75 if (getline(&res_buf, &res_sz, file) == -1) {
76 free(res_buf); /* uclibc allocates a buffer even on EOF. WTF? */
79 //TODO: trimming to res_sz?
82 /* Get line. Remove trailing \n */
83 char* FAST_FUNC xmalloc_fgetline(FILE *file)
88 res_sz = getline(&res_buf, &res_sz, file);
90 if ((ssize_t)res_sz != -1) {
91 if (res_buf[res_sz - 1] == '\n')
92 res_buf[--res_sz] = '\0';
93 //TODO: trimming to res_sz?
95 free(res_buf); /* uclibc allocates a buffer even on EOF. WTF? */
104 /* Faster routines (~twice as fast). +170 bytes. Unused as of 2008-07.
106 * NB: they stop at NUL byte too.
107 * Performance is important here. Think "grep 50gigabyte_file"...
108 * Ironically, grep can't use it because of NUL issue.
109 * We sorely need C lib to provide fgets which reports size!
112 * Actually, uclibc and glibc have it. man getline. It's GNUism,
113 * but very useful one (if it's as fast as this code).
115 * - currently, sed and sort use bb_get_chunk_from_file and heavily
116 * depend on its "stop on \n or \0" behavior, and STILL they fail
117 * to handle all cases with embedded NULs correctly. So:
118 * - audit sed and sort; convert them to getline FIRST.
119 * - THEN ditch bb_get_chunk_from_file, replace it with getline.
120 * - provide getline implementation for non-GNU systems.
123 static char* xmalloc_fgets_internal(FILE *file, int *sizep)
127 char *linebuf = NULL;
132 linebuf = xrealloc(linebuf, idx + 0x100);
133 r = fgets(&linebuf[idx], 0x100, file);
135 /* need to terminate in case this is error
136 * (EOF puts NUL itself) */
140 /* stupid. fgets knows the len, it should report it somehow */
141 len = strlen(&linebuf[idx]);
143 if (len != 0xff || linebuf[idx - 1] == '\n')
148 /* xrealloc(linebuf, idx + 1) is up to caller */
155 /* Get line, remove trailing \n */
156 char* FAST_FUNC xmalloc_fgetline_fast(FILE *file)
159 char *r = xmalloc_fgets_internal(file, &sz);
160 if (r && r[sz - 1] == '\n')
162 return r; /* not xrealloc(r, sz + 1)! */
165 char* FAST_FUNC xmalloc_fgets(FILE *file)
168 return xmalloc_fgets_internal(file, &sz);
171 /* Get line, remove trailing \n */
172 char* FAST_FUNC xmalloc_fgetline(FILE *file)
175 char *r = xmalloc_fgets_internal(file, &sz);
178 if (r[sz - 1] == '\n')
180 return xrealloc(r, sz + 1);