blob: 5ad497ffab9657f56575c0e352577a668657083d [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
19 * and 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
Rob Landley2b26fd52006-02-24 02:30:39 +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 }
Eric Andersenaad1a882001-03-16 22:47:14 +000033 linebuf[idx++] = (char)ch;
Rob Landley2b26fd52006-02-24 02:30:39 +000034 if (!ch || (end && ch == '\n')) break;
Eric Andersenaad1a882001-03-16 22:47:14 +000035 }
Rob Landley2b26fd52006-02-24 02:30:39 +000036 if (end) *end = idx;
Manuel Novoa III cad53642003-03-19 09:13:01 +000037 if (linebuf) {
38 if (ferror(file)) {
39 free(linebuf);
40 return NULL;
41 }
42 linebuf[idx] = 0;
43 }
Eric Andersenaad1a882001-03-16 22:47:14 +000044 return linebuf;
45}
46
Rob Landley2b26fd52006-02-24 02:30:39 +000047/* Get line, including trailing /n if any */
Manuel Novoa III cad53642003-03-19 09:13:01 +000048extern char *bb_get_line_from_file(FILE *file)
49{
Rob Landley2b26fd52006-02-24 02:30:39 +000050 int i;
51 return bb_get_chunk_from_file(file, &i);
Manuel Novoa III cad53642003-03-19 09:13:01 +000052}
53
Rob Landley2b26fd52006-02-24 02:30:39 +000054/* Get line. Remove trailing /n */
Manuel Novoa III cad53642003-03-19 09:13:01 +000055extern char *bb_get_chomped_line_from_file(FILE *file)
56{
Rob Landley2b26fd52006-02-24 02:30:39 +000057 int i;
58 char *c=bb_get_chunk_from_file(file, &i);
59 if(i) c[--i]=0;
60
61 return c;
Manuel Novoa III cad53642003-03-19 09:13:01 +000062}