Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Rob Landley | 2b26fd5 | 2006-02-24 02:30:39 +0000 | [diff] [blame] | 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 |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 10 | */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
| 12 | |
Quentin Rameau | e2afae6 | 2018-04-01 19:49:58 +0200 | [diff] [blame] | 13 | char* FAST_FUNC bb_get_chunk_from_file(FILE *file, size_t *end) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 14 | { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 15 | int ch; |
Quentin Rameau | e2afae6 | 2018-04-01 19:49:58 +0200 | [diff] [blame] | 16 | size_t idx = 0; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 17 | char *linebuf = NULL; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 18 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 19 | while ((ch = getc(file)) != EOF) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 20 | /* grow the line buffer as necessary */ |
Quentin Rameau | e2afae6 | 2018-04-01 19:49:58 +0200 | [diff] [blame] | 21 | if (!(idx & 0xff)) { |
| 22 | if (idx == ((size_t)-1) - 0xff) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 23 | bb_die_memory_exhausted(); |
Denys Vlasenko | a1a4483 | 2011-06-17 03:37:43 +0200 | [diff] [blame] | 24 | linebuf = xrealloc(linebuf, idx + 0x100); |
Quentin Rameau | e2afae6 | 2018-04-01 19:49:58 +0200 | [diff] [blame] | 25 | } |
Bernhard Reutner-Fischer | 2d1a6e7 | 2006-06-10 11:04:43 +0000 | [diff] [blame] | 26 | linebuf[idx++] = (char) ch; |
Denys Vlasenko | a1a4483 | 2011-06-17 03:37:43 +0200 | [diff] [blame] | 27 | if (ch == '\0') |
Bernhard Reutner-Fischer | 2d1a6e7 | 2006-06-10 11:04:43 +0000 | [diff] [blame] | 28 | break; |
Denys Vlasenko | a1a4483 | 2011-06-17 03:37:43 +0200 | [diff] [blame] | 29 | if (end && ch == '\n') |
| 30 | break; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 31 | } |
Denys Vlasenko | a1a4483 | 2011-06-17 03:37:43 +0200 | [diff] [blame] | 32 | if (end) |
Bernhard Reutner-Fischer | 2d1a6e7 | 2006-06-10 11:04:43 +0000 | [diff] [blame] | 33 | *end = idx; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 34 | if (linebuf) { |
Denis Vlasenko | 8b22b07 | 2006-12-02 17:58:10 +0000 | [diff] [blame] | 35 | // huh, does fgets discard prior data on error like this? |
Denis Vlasenko | ab24e18 | 2006-11-30 16:41:15 +0000 | [diff] [blame] | 36 | // I don't think so.... |
| 37 | //if (ferror(file)) { |
| 38 | // free(linebuf); |
| 39 | // return NULL; |
| 40 | //} |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 41 | linebuf = xrealloc(linebuf, idx + 1); |
Denis Vlasenko | 8b22b07 | 2006-12-02 17:58:10 +0000 | [diff] [blame] | 42 | linebuf[idx] = '\0'; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 43 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 44 | return linebuf; |
| 45 | } |
| 46 | |
Denis Vlasenko | b97c984 | 2006-10-01 21:05:12 +0000 | [diff] [blame] | 47 | /* Get line, including trailing \n if any */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 48 | char* FAST_FUNC xmalloc_fgets(FILE *file) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | { |
Denys Vlasenko | 22a9951 | 2018-09-02 18:48:09 +0200 | [diff] [blame] | 50 | size_t i; |
Denys Vlasenko | 0d598ab | 2018-09-02 18:35:29 +0200 | [diff] [blame] | 51 | |
| 52 | return bb_get_chunk_from_file(file, &i); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 53 | } |
Denis Vlasenko | b97c984 | 2006-10-01 21:05:12 +0000 | [diff] [blame] | 54 | /* Get line. Remove trailing \n */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 55 | char* FAST_FUNC xmalloc_fgetline(FILE *file) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 56 | { |
Quentin Rameau | e2afae6 | 2018-04-01 19:49:58 +0200 | [diff] [blame] | 57 | size_t i; |
Bernhard Reutner-Fischer | 2d1a6e7 | 2006-06-10 11:04:43 +0000 | [diff] [blame] | 58 | char *c = bb_get_chunk_from_file(file, &i); |
| 59 | |
| 60 | if (i && c[--i] == '\n') |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 61 | c[i] = '\0'; |
Bernhard Reutner-Fischer | 2d1a6e7 | 2006-06-10 11:04:43 +0000 | [diff] [blame] | 62 | |
Rob Landley | 2b26fd5 | 2006-02-24 02:30:39 +0000 | [diff] [blame] | 63 | return c; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 64 | } |
Denis Vlasenko | 2132e02 | 2008-07-15 10:33:12 +0000 | [diff] [blame] | 65 | |
Denis Vlasenko | 5e476ba | 2008-07-15 21:29:44 +0000 | [diff] [blame] | 66 | #if 0 |
Denis Vlasenko | 3fd15e1 | 2008-08-09 16:15:14 +0000 | [diff] [blame] | 67 | /* GNUism getline() should be faster (not tested) than a loop with fgetc */ |
| 68 | |
| 69 | /* Get line, including trailing \n if any */ |
| 70 | char* FAST_FUNC xmalloc_fgets(FILE *file) |
| 71 | { |
| 72 | char *res_buf = NULL; |
| 73 | size_t res_sz; |
| 74 | |
| 75 | if (getline(&res_buf, &res_sz, file) == -1) { |
| 76 | free(res_buf); /* uclibc allocates a buffer even on EOF. WTF? */ |
| 77 | res_buf = NULL; |
| 78 | } |
| 79 | //TODO: trimming to res_sz? |
| 80 | return res_buf; |
| 81 | } |
| 82 | /* Get line. Remove trailing \n */ |
| 83 | char* FAST_FUNC xmalloc_fgetline(FILE *file) |
| 84 | { |
| 85 | char *res_buf = NULL; |
| 86 | size_t res_sz; |
| 87 | |
| 88 | res_sz = getline(&res_buf, &res_sz, file); |
| 89 | |
| 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? |
| 94 | } else { |
| 95 | free(res_buf); /* uclibc allocates a buffer even on EOF. WTF? */ |
| 96 | res_buf = NULL; |
| 97 | } |
| 98 | return res_buf; |
| 99 | } |
| 100 | |
| 101 | #endif |
| 102 | |
| 103 | #if 0 |
Denis Vlasenko | 2132e02 | 2008-07-15 10:33:12 +0000 | [diff] [blame] | 104 | /* Faster routines (~twice as fast). +170 bytes. Unused as of 2008-07. |
| 105 | * |
| 106 | * NB: they stop at NUL byte too. |
| 107 | * Performance is important here. Think "grep 50gigabyte_file"... |
Denis Vlasenko | 5e476ba | 2008-07-15 21:29:44 +0000 | [diff] [blame] | 108 | * Ironically, grep can't use it because of NUL issue. |
Denis Vlasenko | 2132e02 | 2008-07-15 10:33:12 +0000 | [diff] [blame] | 109 | * We sorely need C lib to provide fgets which reports size! |
Denis Vlasenko | 5e476ba | 2008-07-15 21:29:44 +0000 | [diff] [blame] | 110 | * |
| 111 | * Update: |
| 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). |
| 114 | * TODO: |
| 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. |
Denis Vlasenko | 2132e02 | 2008-07-15 10:33:12 +0000 | [diff] [blame] | 121 | */ |
| 122 | |
| 123 | static char* xmalloc_fgets_internal(FILE *file, int *sizep) |
| 124 | { |
| 125 | int len; |
| 126 | int idx = 0; |
| 127 | char *linebuf = NULL; |
| 128 | |
| 129 | while (1) { |
| 130 | char *r; |
| 131 | |
| 132 | linebuf = xrealloc(linebuf, idx + 0x100); |
| 133 | r = fgets(&linebuf[idx], 0x100, file); |
| 134 | if (!r) { |
| 135 | /* need to terminate in case this is error |
| 136 | * (EOF puts NUL itself) */ |
| 137 | linebuf[idx] = '\0'; |
| 138 | break; |
| 139 | } |
| 140 | /* stupid. fgets knows the len, it should report it somehow */ |
| 141 | len = strlen(&linebuf[idx]); |
| 142 | idx += len; |
| 143 | if (len != 0xff || linebuf[idx - 1] == '\n') |
| 144 | break; |
| 145 | } |
| 146 | *sizep = idx; |
| 147 | if (idx) { |
| 148 | /* xrealloc(linebuf, idx + 1) is up to caller */ |
| 149 | return linebuf; |
| 150 | } |
| 151 | free(linebuf); |
| 152 | return NULL; |
| 153 | } |
| 154 | |
| 155 | /* Get line, remove trailing \n */ |
| 156 | char* FAST_FUNC xmalloc_fgetline_fast(FILE *file) |
| 157 | { |
| 158 | int sz; |
| 159 | char *r = xmalloc_fgets_internal(file, &sz); |
| 160 | if (r && r[sz - 1] == '\n') |
| 161 | r[--sz] = '\0'; |
| 162 | return r; /* not xrealloc(r, sz + 1)! */ |
| 163 | } |
| 164 | |
Denis Vlasenko | 2132e02 | 2008-07-15 10:33:12 +0000 | [diff] [blame] | 165 | char* FAST_FUNC xmalloc_fgets(FILE *file) |
| 166 | { |
| 167 | int sz; |
| 168 | return xmalloc_fgets_internal(file, &sz); |
| 169 | } |
| 170 | |
| 171 | /* Get line, remove trailing \n */ |
| 172 | char* FAST_FUNC xmalloc_fgetline(FILE *file) |
| 173 | { |
| 174 | int sz; |
| 175 | char *r = xmalloc_fgets_internal(file, &sz); |
| 176 | if (!r) |
| 177 | return r; |
| 178 | if (r[sz - 1] == '\n') |
| 179 | r[--sz] = '\0'; |
| 180 | return xrealloc(r, sz + 1); |
| 181 | } |
| 182 | #endif |