blob: 5078f49eab46c1fbf6a4db78b9004b7dae8b1958 [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 *
Rob Landley2b26fd52006-02-24 02:30:39 +00009 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenaad1a882001-03-16 22:47:14 +000010 */
11
Eric Andersenaad1a882001-03-16 22:47:14 +000012#include "libbb.h"
13
Denis Vlasenkof7996f32007-01-11 17:20:00 +000014/* This function reads an entire line from a text file, up to a newline
Denis Vlasenko8b22b072006-12-02 17:58:10 +000015 * or NUL byte, inclusive. It returns a malloc'ed char * which must be
Denis Vlasenko9a7cef92006-12-20 02:46:48 +000016 * stored and free'ed by the caller. If end is null '\n' isn't considered
Denis Vlasenkob97c9842006-10-01 21:05:12 +000017 * end of line. If end isn't null, length of the chunk read is stored in it. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000018
Bernhard Reutner-Fischer2d1a6e72006-06-10 11:04:43 +000019char *bb_get_chunk_from_file(FILE * file, int *end)
Eric Andersenaad1a882001-03-16 22:47:14 +000020{
Eric Andersenaad1a882001-03-16 22:47:14 +000021 int ch;
22 int idx = 0;
23 char *linebuf = NULL;
24 int linebufsz = 0;
25
Manuel Novoa III cad53642003-03-19 09:13:01 +000026 while ((ch = getc(file)) != EOF) {
Eric Andersenaad1a882001-03-16 22:47:14 +000027 /* grow the line buffer as necessary */
Denis Vlasenko8b22b072006-12-02 17:58:10 +000028 if (idx >= linebufsz) {
Rob Landley2b26fd52006-02-24 02:30:39 +000029 linebuf = xrealloc(linebuf, linebufsz += 80);
Manuel Novoa III cad53642003-03-19 09:13:01 +000030 }
Bernhard Reutner-Fischer2d1a6e72006-06-10 11:04:43 +000031 linebuf[idx++] = (char) ch;
32 if (!ch || (end && ch == '\n'))
33 break;
Eric Andersenaad1a882001-03-16 22:47:14 +000034 }
Bernhard Reutner-Fischer2d1a6e72006-06-10 11:04:43 +000035 if (end)
36 *end = idx;
Manuel Novoa III cad53642003-03-19 09:13:01 +000037 if (linebuf) {
Denis Vlasenko8b22b072006-12-02 17:58:10 +000038 // huh, does fgets discard prior data on error like this?
Denis Vlasenkoab24e182006-11-30 16:41:15 +000039 // I don't think so....
40 //if (ferror(file)) {
41 // free(linebuf);
42 // return NULL;
43 //}
Denis Vlasenko372686b2006-10-12 22:42:33 +000044 linebuf = xrealloc(linebuf, idx+1);
Denis Vlasenko8b22b072006-12-02 17:58:10 +000045 linebuf[idx] = '\0';
Manuel Novoa III cad53642003-03-19 09:13:01 +000046 }
Eric Andersenaad1a882001-03-16 22:47:14 +000047 return linebuf;
48}
49
Denis Vlasenkob97c9842006-10-01 21:05:12 +000050/* Get line, including trailing \n if any */
Denis Vlasenko2d5ca602006-10-12 22:43:20 +000051char *xmalloc_fgets(FILE * file)
Manuel Novoa III cad53642003-03-19 09:13:01 +000052{
Rob Landley2b26fd52006-02-24 02:30:39 +000053 int i;
Bernhard Reutner-Fischer2d1a6e72006-06-10 11:04:43 +000054
Rob Landley2b26fd52006-02-24 02:30:39 +000055 return bb_get_chunk_from_file(file, &i);
Manuel Novoa III cad53642003-03-19 09:13:01 +000056}
57
Denis Vlasenkob97c9842006-10-01 21:05:12 +000058/* Get line. Remove trailing \n */
Denis Vlasenko2d5ca602006-10-12 22:43:20 +000059char *xmalloc_getline(FILE * file)
Manuel Novoa III cad53642003-03-19 09:13:01 +000060{
Rob Landley2b26fd52006-02-24 02:30:39 +000061 int i;
Bernhard Reutner-Fischer2d1a6e72006-06-10 11:04:43 +000062 char *c = bb_get_chunk_from_file(file, &i);
63
64 if (i && c[--i] == '\n')
Denis Vlasenko9a7cef92006-12-20 02:46:48 +000065 c[i] = '\0';
Bernhard Reutner-Fischer2d1a6e72006-06-10 11:04:43 +000066
Rob Landley2b26fd52006-02-24 02:30:39 +000067 return c;
Manuel Novoa III cad53642003-03-19 09:13:01 +000068}