[BusyBox-cvs] busybox.stable/libbb get_line_from_file.c, 1.2, 1.3 libbb.h, 1.74, 1.75
Erik Andersen
andersen at busybox.net
Tue Dec 23 21:36:58 UTC 2003
Update of /var/cvs/busybox.stable/libbb
In directory nail:/tmp/cvs-serv7455/libbb
Modified Files:
get_line_from_file.c libbb.h
Log Message:
remove duplication
Index: get_line_from_file.c
===================================================================
RCS file: /var/cvs/busybox.stable/libbb/get_line_from_file.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- get_line_from_file.c 6 Dec 2001 07:50:44 -0000 1.2
+++ get_line_from_file.c 23 Dec 2003 21:36:55 -0000 1.3
@@ -2,8 +2,8 @@
/*
* Utility routines.
*
- * Copyright (C) many different people. If you wrote this, please
- * acknowledge your work.
+ * Copyright (C) many different people.
+ * If you wrote this, please acknowledge your work.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -21,39 +21,54 @@
*/
#include <stdio.h>
+#include <stdlib.h>
#include "libbb.h"
-
-
-/* get_line_from_file() - This function reads an entire line from a text file
+/* get_line_from_file() - This function reads an entire line from a text file,
* up to a newline. It returns a malloc'ed char * which must be stored and
- * free'ed by the caller. */
-extern char *get_line_from_file(FILE *file)
+ * free'ed by the caller. If 'c' is nonzero, the trailing '\n' (if any)
+ * is removed. In event of a read error or EOF, NULL is returned. */
+
+static char *private_get_line_from_file(FILE *file, int c)
{
- static const int GROWBY = 80; /* how large we will grow strings by */
+#define GROWBY (80) /* how large we will grow strings by */
int ch;
int idx = 0;
char *linebuf = NULL;
int linebufsz = 0;
- while (1) {
- ch = fgetc(file);
- if (ch == EOF)
- break;
+ while ((ch = getc(file)) != EOF) {
/* grow the line buffer as necessary */
- while (idx > linebufsz-2)
+ if (idx > linebufsz - 2) {
linebuf = xrealloc(linebuf, linebufsz += GROWBY);
+ }
linebuf[idx++] = (char)ch;
- if (ch == '\n' || ch == '\0')
+ if (ch == '\n' || ch == '\0') {
+ if (c) {
+ --idx;
+ }
break;
+ }
}
+ if (linebuf) {
+ if (ferror(file)) {
+ free(linebuf);
+ return NULL;
+ }
+ linebuf[idx] = 0;
+ }
+ return linebuf;
+}
- if (idx == 0)
- return NULL;
+extern char *get_line_from_file(FILE *file)
+{
+ return private_get_line_from_file(file, 0);
+}
- linebuf[idx] = 0;
- return linebuf;
+extern char *get_chomped_line_from_file(FILE *file)
+{
+ return private_get_line_from_file(file, 1);
}
Index: libbb.h
===================================================================
RCS file: /var/cvs/busybox.stable/libbb/libbb.h,v
retrieving revision 1.74
retrieving revision 1.75
diff -u -d -r1.74 -r1.75
--- libbb.h 18 Sep 2002 19:21:07 -0000 1.74
+++ libbb.h 23 Dec 2003 21:36:55 -0000 1.75
@@ -119,6 +119,7 @@
extern long* find_pid_by_name( char* pidName);
extern char *find_real_root_device_name(const char* name);
extern char *get_line_from_file(FILE *file);
+extern char *get_chomped_line_from_file(FILE *file);
extern void print_file(FILE *file);
extern int copyfd(int fd1, int fd2);
extern int print_file_by_name(char *filename);
More information about the busybox-cvs
mailing list