Yanked out the cstring_alloc() and cstring_lineFromFile() functions from
authorMark Whitley <markw@lineo.com>
Wed, 28 Jun 2000 22:15:26 +0000 (22:15 -0000)
committerMark Whitley <markw@lineo.com>
Wed, 28 Jun 2000 22:15:26 +0000 (22:15 -0000)
utility.c and replaced them with get_line_from_file() from the new grep.c.
Also changed declaration in internal.h and replaced instances of
cstring_lineFromFile() in dc.c and sort.c with get_line_from_file(). Tested
them and they worked fine.

coreutils/sort.c
dc.c
findutils/grep.c
grep.c
internal.h
miscutils/dc.c
sort.c
utility.c

index 93062faa4050f86a16134531ea92c1f7484b9804..a28122d510d420bc8bc525dd6092f6e5873b9860 100644 (file)
@@ -84,7 +84,7 @@ static Line *line_newFromFile(FILE * src)
        Line *self;
        char *cstring = NULL;
 
-       if ((cstring = cstring_lineFromFile(src))) {
+       if ((cstring = get_line_from_file(src))) {
                self = line_alloc();
                if (self == NULL) {
                        return NULL;
@@ -304,4 +304,4 @@ int sort_main(int argc, char **argv)
        return(0);
 }
 
-/* $Id: sort.c,v 1.17 2000/06/19 17:25:40 andersen Exp $ */
+/* $Id: sort.c,v 1.18 2000/06/28 22:15:26 markw Exp $ */
diff --git a/dc.c b/dc.c
index 31a5471c30e1752721a99e87c60c6fd39ad2ebf0..5bf3bc984684171c9f95f6b19819d2ecb3f2ba3a 100644 (file)
--- a/dc.c
+++ b/dc.c
@@ -170,7 +170,7 @@ int dc_main(int argc, char **argv)
                char *line   = NULL;
                char *cursor = NULL;
                char *token  = NULL;
-               while ((line = cstring_lineFromFile(stdin))) {
+               while ((line = get_line_from_file(stdin))) {
                        cursor = line;
                        len = number_of_tokens(line);
                        for (i = 0; i < len; i++) {
index aca469e2f21acc0925be4fb96d567a0250e9dd57..a374e114dfa6844f770a008d66319794171691a4 100644 (file)
@@ -46,8 +46,6 @@ static const char grep_usage[] =
 #endif
        ;
 
-static const int GROWBY = 80; /* how large we will grow strings by */
-
 /* options */
 static int ignore_case       = 0;
 static int print_filename    = 0;
@@ -62,35 +60,6 @@ static int nmatches = 0; /* keeps track of the number of matches */
 static char *cur_file = NULL; /* the current file we are reading */
 
 
-/* This returns a malloc'ed char * which must be stored and free'ed */
-/* XXX: This function should probably go in a 'common'/'util'/'misc' file
- * somewhere so it can be used by other folks. */
-static char *get_line_from_file(FILE *file)
-{
-       int ch;
-       int idx = 0;
-       char *linebuf = NULL;
-       int linebufsz = 0;
-
-       while (1) {
-               ch = fgetc(file);
-               if (ch == EOF)
-                       break;
-               /* grow the line buffer as necessary */
-               if (idx > linebufsz-1)
-                       linebuf = realloc(linebuf, linebufsz += GROWBY);
-               linebuf[idx++] = (char)ch;
-               if ((char)ch == '\n')
-                       break;
-       }
-
-       if (idx == 0)
-               return NULL;
-
-       linebuf[idx] = 0;
-       return linebuf;
-}
-
 static void print_matched_line(char *line, int linenum)
 {
        if (print_filename)
diff --git a/grep.c b/grep.c
index aca469e2f21acc0925be4fb96d567a0250e9dd57..a374e114dfa6844f770a008d66319794171691a4 100644 (file)
--- a/grep.c
+++ b/grep.c
@@ -46,8 +46,6 @@ static const char grep_usage[] =
 #endif
        ;
 
-static const int GROWBY = 80; /* how large we will grow strings by */
-
 /* options */
 static int ignore_case       = 0;
 static int print_filename    = 0;
@@ -62,35 +60,6 @@ static int nmatches = 0; /* keeps track of the number of matches */
 static char *cur_file = NULL; /* the current file we are reading */
 
 
-/* This returns a malloc'ed char * which must be stored and free'ed */
-/* XXX: This function should probably go in a 'common'/'util'/'misc' file
- * somewhere so it can be used by other folks. */
-static char *get_line_from_file(FILE *file)
-{
-       int ch;
-       int idx = 0;
-       char *linebuf = NULL;
-       int linebufsz = 0;
-
-       while (1) {
-               ch = fgetc(file);
-               if (ch == EOF)
-                       break;
-               /* grow the line buffer as necessary */
-               if (idx > linebufsz-1)
-                       linebuf = realloc(linebuf, linebufsz += GROWBY);
-               linebuf[idx++] = (char)ch;
-               if ((char)ch == '\n')
-                       break;
-       }
-
-       if (idx == 0)
-               return NULL;
-
-       linebuf[idx] = 0;
-       return linebuf;
-}
-
 static void print_matched_line(char *line, int linenum)
 {
        if (print_filename)
index 6e4f06cb516fcb5dc3d3aefb61a944f95892ce0c..41a72e18a79dd70f56bef98463aa79f7cdb5ec2f 100644 (file)
@@ -258,7 +258,7 @@ extern long getNum (const char *cp);
 extern pid_t* findPidByName( char* pidName);
 extern void *xmalloc (size_t size);
 extern int find_real_root_device_name(char* name);
-extern char *cstring_lineFromFile(FILE *f);
+extern char *get_line_from_file(FILE *file);
 
 /* These parse entries in /etc/passwd and /etc/group.  This is desirable
  * for BusyBox since we want to avoid using the glibc NSS stuff, which
index 31a5471c30e1752721a99e87c60c6fd39ad2ebf0..5bf3bc984684171c9f95f6b19819d2ecb3f2ba3a 100644 (file)
@@ -170,7 +170,7 @@ int dc_main(int argc, char **argv)
                char *line   = NULL;
                char *cursor = NULL;
                char *token  = NULL;
-               while ((line = cstring_lineFromFile(stdin))) {
+               while ((line = get_line_from_file(stdin))) {
                        cursor = line;
                        len = number_of_tokens(line);
                        for (i = 0; i < len; i++) {
diff --git a/sort.c b/sort.c
index 93062faa4050f86a16134531ea92c1f7484b9804..a28122d510d420bc8bc525dd6092f6e5873b9860 100644 (file)
--- a/sort.c
+++ b/sort.c
@@ -84,7 +84,7 @@ static Line *line_newFromFile(FILE * src)
        Line *self;
        char *cstring = NULL;
 
-       if ((cstring = cstring_lineFromFile(src))) {
+       if ((cstring = get_line_from_file(src))) {
                self = line_alloc();
                if (self == NULL) {
                        return NULL;
@@ -304,4 +304,4 @@ int sort_main(int argc, char **argv)
        return(0);
 }
 
-/* $Id: sort.c,v 1.17 2000/06/19 17:25:40 andersen Exp $ */
+/* $Id: sort.c,v 1.18 2000/06/28 22:15:26 markw Exp $ */
index a45edde5d4dfdffe95498ad68b520b66c3f35483..de53dbda8a5ec9f6ad0e752f885f4ea63c6b537a 100644 (file)
--- a/utility.c
+++ b/utility.c
@@ -1586,56 +1586,35 @@ extern int find_real_root_device_name(char* name)
 }
 #endif
 
-const unsigned int CSTRING_BUFFER_LENGTH = 1024;
-/* recursive parser that returns cstrings of arbitrary length
- * from a FILE* 
- */
-static char *
-cstring_alloc(FILE* f, int depth)
-{
-    char *cstring;
-    char buffer[CSTRING_BUFFER_LENGTH];
-    int         target = CSTRING_BUFFER_LENGTH * depth;
-    int  c, i, len, size;
-
-    /* fill buffer */
-    i = 0;
-       while ((c = fgetc(f)) != EOF) {
-               buffer[i] = (char) c;
-               if (buffer[i++] == 0x0a) { break; }
-               if (i == CSTRING_BUFFER_LENGTH) { break; }
-    }
-    len = i;
-
-    /* recurse or malloc? */
-    if (len == CSTRING_BUFFER_LENGTH) {
-               cstring = cstring_alloc(f, (depth + 1));
-    } else {
-               /* [special case] EOF */
-               if ((depth | len) == 0) { return NULL; }
-
-               /* malloc */
-               size = target + len + 1;
-               cstring = malloc(size);
-               if (!cstring) { return NULL; }
-               cstring[size - 1] = 0;
-    }
-
-    /* copy buffer */
-    if (cstring) {
-               memcpy(&cstring[target], buffer, len);
-    }
-    return cstring;
-}
+static const int GROWBY = 80; /* how large we will grow strings by */
 
-/* 
- * wrapper around recursive cstring_alloc 
- * it's the caller's responsibility to free the cstring
- */ 
-char *
-cstring_lineFromFile(FILE *f)
+/* 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)
 {
-    return cstring_alloc(f, 0);
+       int ch;
+       int idx = 0;
+       char *linebuf = NULL;
+       int linebufsz = 0;
+
+       while (1) {
+               ch = fgetc(file);
+               if (ch == EOF)
+                       break;
+               /* grow the line buffer as necessary */
+               if (idx > linebufsz-1)
+                       linebuf = realloc(linebuf, linebufsz += GROWBY);
+               linebuf[idx++] = (char)ch;
+               if ((char)ch == '\n')
+                       break;
+       }
+
+       if (idx == 0)
+               return NULL;
+
+       linebuf[idx] = 0;
+       return linebuf;
 }
 
 /* END CODE */