blob: 903ff1fb684c6f88f7b018df5ce2ec8e2c697f00 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Rob Landley2b26fd52006-02-24 02:30:39 +00005 * 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 Andersenaad1a882001-03-16 22:47:14 +00008 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenaad1a882001-03-16 22:47:14 +000010 */
Eric Andersenaad1a882001-03-16 22:47:14 +000011#include "libbb.h"
12
Quentin Rameaue2afae62018-04-01 19:49:58 +020013char* FAST_FUNC bb_get_chunk_from_file(FILE *file, size_t *end)
Eric Andersenaad1a882001-03-16 22:47:14 +000014{
Eric Andersenaad1a882001-03-16 22:47:14 +000015 int ch;
Quentin Rameaue2afae62018-04-01 19:49:58 +020016 size_t idx = 0;
Eric Andersenaad1a882001-03-16 22:47:14 +000017 char *linebuf = NULL;
Eric Andersenaad1a882001-03-16 22:47:14 +000018
Manuel Novoa III cad53642003-03-19 09:13:01 +000019 while ((ch = getc(file)) != EOF) {
Eric Andersenaad1a882001-03-16 22:47:14 +000020 /* grow the line buffer as necessary */
Quentin Rameaue2afae62018-04-01 19:49:58 +020021 if (!(idx & 0xff)) {
22 if (idx == ((size_t)-1) - 0xff)
Denys Vlasenko899ae532018-04-01 19:59:37 +020023 bb_die_memory_exhausted();
Denys Vlasenkoa1a44832011-06-17 03:37:43 +020024 linebuf = xrealloc(linebuf, idx + 0x100);
Quentin Rameaue2afae62018-04-01 19:49:58 +020025 }
Bernhard Reutner-Fischer2d1a6e72006-06-10 11:04:43 +000026 linebuf[idx++] = (char) ch;
Denys Vlasenkoa1a44832011-06-17 03:37:43 +020027 if (ch == '\0')
Bernhard Reutner-Fischer2d1a6e72006-06-10 11:04:43 +000028 break;
Denys Vlasenkoa1a44832011-06-17 03:37:43 +020029 if (end && ch == '\n')
30 break;
Eric Andersenaad1a882001-03-16 22:47:14 +000031 }
Denys Vlasenkoa1a44832011-06-17 03:37:43 +020032 if (end)
Bernhard Reutner-Fischer2d1a6e72006-06-10 11:04:43 +000033 *end = idx;
Manuel Novoa III cad53642003-03-19 09:13:01 +000034 if (linebuf) {
Denis Vlasenko8b22b072006-12-02 17:58:10 +000035 // huh, does fgets discard prior data on error like this?
Denis Vlasenkoab24e182006-11-30 16:41:15 +000036 // I don't think so....
37 //if (ferror(file)) {
38 // free(linebuf);
39 // return NULL;
40 //}
Denis Vlasenkodeeed592008-07-08 05:14:36 +000041 linebuf = xrealloc(linebuf, idx + 1);
Denis Vlasenko8b22b072006-12-02 17:58:10 +000042 linebuf[idx] = '\0';
Manuel Novoa III cad53642003-03-19 09:13:01 +000043 }
Eric Andersenaad1a882001-03-16 22:47:14 +000044 return linebuf;
45}
46
Denis Vlasenkob97c9842006-10-01 21:05:12 +000047/* Get line, including trailing \n if any */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000048char* FAST_FUNC xmalloc_fgets(FILE *file)
Manuel Novoa III cad53642003-03-19 09:13:01 +000049{
Denys Vlasenko22a99512018-09-02 18:48:09 +020050 size_t i;
Denys Vlasenko0d598ab2018-09-02 18:35:29 +020051
52 return bb_get_chunk_from_file(file, &i);
Manuel Novoa III cad53642003-03-19 09:13:01 +000053}
Denis Vlasenkob97c9842006-10-01 21:05:12 +000054/* Get line. Remove trailing \n */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000055char* FAST_FUNC xmalloc_fgetline(FILE *file)
Manuel Novoa III cad53642003-03-19 09:13:01 +000056{
Quentin Rameaue2afae62018-04-01 19:49:58 +020057 size_t i;
Bernhard Reutner-Fischer2d1a6e72006-06-10 11:04:43 +000058 char *c = bb_get_chunk_from_file(file, &i);
59
60 if (i && c[--i] == '\n')
Denis Vlasenko9a7cef92006-12-20 02:46:48 +000061 c[i] = '\0';
Bernhard Reutner-Fischer2d1a6e72006-06-10 11:04:43 +000062
Rob Landley2b26fd52006-02-24 02:30:39 +000063 return c;
Manuel Novoa III cad53642003-03-19 09:13:01 +000064}
Denis Vlasenko2132e022008-07-15 10:33:12 +000065
Denis Vlasenko5e476ba2008-07-15 21:29:44 +000066#if 0
Denis Vlasenko3fd15e12008-08-09 16:15:14 +000067/* GNUism getline() should be faster (not tested) than a loop with fgetc */
68
69/* Get line, including trailing \n if any */
70char* 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 */
83char* 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 Vlasenko2132e022008-07-15 10:33:12 +0000104/* 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 Vlasenko5e476ba2008-07-15 21:29:44 +0000108 * Ironically, grep can't use it because of NUL issue.
Denis Vlasenko2132e022008-07-15 10:33:12 +0000109 * We sorely need C lib to provide fgets which reports size!
Denis Vlasenko5e476ba2008-07-15 21:29:44 +0000110 *
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 Vlasenko2132e022008-07-15 10:33:12 +0000121 */
122
123static 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 */
156char* 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 Vlasenko2132e022008-07-15 10:33:12 +0000165char* 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 */
172char* 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