blob: de49eb51d9bd228cd081699d34ecd2e37b111153 [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
12#include <stdio.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000013#include <stdlib.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000014#include "libbb.h"
15
Manuel Novoa III cad53642003-03-19 09:13:01 +000016/* get_line_from_file() - This function reads an entire line from a text file,
Rob Landley2b26fd52006-02-24 02:30:39 +000017 * up to a newline or NUL byte. It returns a malloc'ed char * which must be
18 * stored and free'ed by the caller. If end is null '\n' isn't considered
Denis Vlasenkob97c9842006-10-01 21:05:12 +000019 * 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 +000020
Bernhard Reutner-Fischer2d1a6e72006-06-10 11:04:43 +000021char *bb_get_chunk_from_file(FILE * file, int *end)
Eric Andersenaad1a882001-03-16 22:47:14 +000022{
Eric Andersenaad1a882001-03-16 22:47:14 +000023 int ch;
24 int idx = 0;
25 char *linebuf = NULL;
26 int linebufsz = 0;
27
Manuel Novoa III cad53642003-03-19 09:13:01 +000028 while ((ch = getc(file)) != EOF) {
Eric Andersenaad1a882001-03-16 22:47:14 +000029 /* grow the line buffer as necessary */
Glenn L McGrath2570b432003-09-16 05:25:43 +000030 if (idx > linebufsz - 2) {
Rob Landley2b26fd52006-02-24 02:30:39 +000031 linebuf = xrealloc(linebuf, linebufsz += 80);
Manuel Novoa III cad53642003-03-19 09:13:01 +000032 }
Bernhard Reutner-Fischer2d1a6e72006-06-10 11:04:43 +000033 linebuf[idx++] = (char) ch;
34 if (!ch || (end && ch == '\n'))
35 break;
Eric Andersenaad1a882001-03-16 22:47:14 +000036 }
Bernhard Reutner-Fischer2d1a6e72006-06-10 11:04:43 +000037 if (end)
38 *end = idx;
Manuel Novoa III cad53642003-03-19 09:13:01 +000039 if (linebuf) {
40 if (ferror(file)) {
41 free(linebuf);
42 return NULL;
43 }
44 linebuf[idx] = 0;
45 }
Eric Andersenaad1a882001-03-16 22:47:14 +000046 return linebuf;
47}
48
Denis Vlasenkob97c9842006-10-01 21:05:12 +000049/* Get line, including trailing \n if any */
Bernhard Reutner-Fischer2d1a6e72006-06-10 11:04:43 +000050char *bb_get_line_from_file(FILE * file)
Manuel Novoa III cad53642003-03-19 09:13:01 +000051{
Rob Landley2b26fd52006-02-24 02:30:39 +000052 int i;
Bernhard Reutner-Fischer2d1a6e72006-06-10 11:04:43 +000053
Rob Landley2b26fd52006-02-24 02:30:39 +000054 return bb_get_chunk_from_file(file, &i);
Manuel Novoa III cad53642003-03-19 09:13:01 +000055}
56
Denis Vlasenkob97c9842006-10-01 21:05:12 +000057/* Get line. Remove trailing \n */
Bernhard Reutner-Fischer2d1a6e72006-06-10 11:04:43 +000058char *bb_get_chomped_line_from_file(FILE * file)
Manuel Novoa III cad53642003-03-19 09:13:01 +000059{
Rob Landley2b26fd52006-02-24 02:30:39 +000060 int i;
Bernhard Reutner-Fischer2d1a6e72006-06-10 11:04:43 +000061 char *c = bb_get_chunk_from_file(file, &i);
62
63 if (i && c[--i] == '\n')
64 c[i] = 0;
65
Rob Landley2b26fd52006-02-24 02:30:39 +000066 return c;
Manuel Novoa III cad53642003-03-19 09:13:01 +000067}