- don't free user-supplied string (via -e)
[oweals/busybox.git] / libbb / get_line_from_file.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
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
8  *
9  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10  */
11
12 #include "libbb.h"
13
14 /* This function reads an entire line from a text file, up to a newline
15  * or NUL byte, inclusive.  It returns a malloc'ed char * which
16  * must be free'ed by the caller.  If end is NULL '\n' isn't considered
17  * end of line.  If end isn't NULL, length of the chunk read is stored in it.
18  * Return NULL if EOF/error */
19 char* FAST_FUNC bb_get_chunk_from_file(FILE *file, int *end)
20 {
21         int ch;
22         int idx = 0;
23         char *linebuf = NULL;
24         int linebufsz = 0;
25
26         while ((ch = getc(file)) != EOF) {
27                 /* grow the line buffer as necessary */
28                 if (idx >= linebufsz) {
29                         linebufsz += 80;
30                         linebuf = xrealloc(linebuf, linebufsz);
31                 }
32                 linebuf[idx++] = (char) ch;
33                 if (!ch || (end && ch == '\n'))
34                         break;
35         }
36         if (end)
37                 *end = idx;
38         if (linebuf) {
39                 // huh, does fgets discard prior data on error like this?
40                 // I don't think so....
41                 //if (ferror(file)) {
42                 //      free(linebuf);
43                 //      return NULL;
44                 //}
45                 linebuf = xrealloc(linebuf, idx + 1);
46                 linebuf[idx] = '\0';
47         }
48         return linebuf;
49 }
50
51 /* Get line, including trailing \n if any */
52 char* FAST_FUNC xmalloc_fgets(FILE *file)
53 {
54         int i;
55
56         return bb_get_chunk_from_file(file, &i);
57 }
58
59 /* Get line.  Remove trailing \n */
60 char* FAST_FUNC xmalloc_fgetline(FILE *file)
61 {
62         int i;
63         char *c = bb_get_chunk_from_file(file, &i);
64
65         if (i && c[--i] == '\n')
66                 c[i] = '\0';
67
68         return c;
69 }
70
71 #if 0
72 /* Faster routines (~twice as fast). +170 bytes. Unused as of 2008-07.
73  *
74  * NB: they stop at NUL byte too.
75  * Performance is important here. Think "grep 50gigabyte_file"...
76  * Ironically, grep can't use it because of NUL issue.
77  * We sorely need C lib to provide fgets which reports size!
78  *
79  * Update:
80  * Actually, uclibc and glibc have it. man getline. It's GNUism,
81  *   but very useful one (if it's as fast as this code).
82  * TODO:
83  * - currently, sed and sort use bb_get_chunk_from_file and heavily
84  *   depend on its "stop on \n or \0" behavior, and STILL they fail
85  *   to handle all cases with embedded NULs correctly. So:
86  * - audit sed and sort; convert them to getline FIRST.
87  * - THEN ditch bb_get_chunk_from_file, replace it with getline.
88  * - provide getline implementation for non-GNU systems.
89  */
90
91 static char* xmalloc_fgets_internal(FILE *file, int *sizep)
92 {
93         int len;
94         int idx = 0;
95         char *linebuf = NULL;
96
97         while (1) {
98                 char *r;
99
100                 linebuf = xrealloc(linebuf, idx + 0x100);
101                 r = fgets(&linebuf[idx], 0x100, file);
102                 if (!r) {
103                         /* need to terminate in case this is error
104                          * (EOF puts NUL itself) */
105                         linebuf[idx] = '\0';
106                         break;
107                 }
108                 /* stupid. fgets knows the len, it should report it somehow */
109                 len = strlen(&linebuf[idx]);
110                 idx += len;
111                 if (len != 0xff || linebuf[idx - 1] == '\n')
112                         break;
113         }
114         *sizep = idx;
115         if (idx) {
116                 /* xrealloc(linebuf, idx + 1) is up to caller */
117                 return linebuf;
118         }
119         free(linebuf);
120         return NULL;
121 }
122
123 /* Get line, remove trailing \n */
124 char* FAST_FUNC xmalloc_fgetline_fast(FILE *file)
125 {
126         int sz;
127         char *r = xmalloc_fgets_internal(file, &sz);
128         if (r && r[sz - 1] == '\n')
129                 r[--sz] = '\0';
130         return r; /* not xrealloc(r, sz + 1)! */
131 }
132
133 char* FAST_FUNC xmalloc_fgets(FILE *file)
134 {
135         int sz;
136         return xmalloc_fgets_internal(file, &sz);
137 }
138
139 /* Get line, remove trailing \n */
140 char* FAST_FUNC xmalloc_fgetline(FILE *file)
141 {
142         int sz;
143         char *r = xmalloc_fgets_internal(file, &sz);
144         if (!r)
145                 return r;
146         if (r[sz - 1] == '\n')
147                 r[--sz] = '\0';
148         return xrealloc(r, sz + 1);
149 }
150 #endif