blob: b424d59e913ade059c0434d85c12c545289b6311 [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 Vlasenkoab24e182006-11-30 16:41:15 +000014/* This function reads an entire line from a text file,
Rob Landley2b26fd52006-02-24 02:30:39 +000015 * up to a newline or NUL byte. It returns a malloc'ed char * which must be
16 * 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 */
Glenn L McGrath2570b432003-09-16 05:25:43 +000028 if (idx > linebufsz - 2) {
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 Vlasenkoab24e182006-11-30 16:41:15 +000038 // huh, is fgets discards prior data on error like this?
39 // 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);
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 linebuf[idx] = 0;
46 }
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')
65 c[i] = 0;
66
Rob Landley2b26fd52006-02-24 02:30:39 +000067 return c;
Manuel Novoa III cad53642003-03-19 09:13:01 +000068}