blob: 969d808cfd04a718ca45f153585b6244ca0b59aa [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 }
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}