aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Whitley <markw@lineo.com>2000-06-28 22:15:26 +0000
committerMark Whitley <markw@lineo.com>2000-06-28 22:15:26 +0000
commit1ca41775bbdc07cf67be79aebc566754c9c02855 (patch)
tree0ac134f0a80036aec272b04c3a057ea2ae055b20
parentd37218941c37795cc8e96ddb3312d83fb2269d5a (diff)
downloadbusybox-w32-1ca41775bbdc07cf67be79aebc566754c9c02855.tar.gz
busybox-w32-1ca41775bbdc07cf67be79aebc566754c9c02855.tar.bz2
busybox-w32-1ca41775bbdc07cf67be79aebc566754c9c02855.zip
Yanked out the cstring_alloc() and cstring_lineFromFile() functions from
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.
-rw-r--r--coreutils/sort.c4
-rw-r--r--dc.c2
-rw-r--r--findutils/grep.c31
-rw-r--r--grep.c31
-rw-r--r--internal.h2
-rw-r--r--miscutils/dc.c2
-rw-r--r--sort.c4
-rw-r--r--utility.c75
8 files changed, 34 insertions, 117 deletions
diff --git a/coreutils/sort.c b/coreutils/sort.c
index 93062faa4..a28122d51 100644
--- a/coreutils/sort.c
+++ b/coreutils/sort.c
@@ -84,7 +84,7 @@ static Line *line_newFromFile(FILE * src)
84 Line *self; 84 Line *self;
85 char *cstring = NULL; 85 char *cstring = NULL;
86 86
87 if ((cstring = cstring_lineFromFile(src))) { 87 if ((cstring = get_line_from_file(src))) {
88 self = line_alloc(); 88 self = line_alloc();
89 if (self == NULL) { 89 if (self == NULL) {
90 return NULL; 90 return NULL;
@@ -304,4 +304,4 @@ int sort_main(int argc, char **argv)
304 return(0); 304 return(0);
305} 305}
306 306
307/* $Id: sort.c,v 1.17 2000/06/19 17:25:40 andersen Exp $ */ 307/* $Id: sort.c,v 1.18 2000/06/28 22:15:26 markw Exp $ */
diff --git a/dc.c b/dc.c
index 31a5471c3..5bf3bc984 100644
--- a/dc.c
+++ b/dc.c
@@ -170,7 +170,7 @@ int dc_main(int argc, char **argv)
170 char *line = NULL; 170 char *line = NULL;
171 char *cursor = NULL; 171 char *cursor = NULL;
172 char *token = NULL; 172 char *token = NULL;
173 while ((line = cstring_lineFromFile(stdin))) { 173 while ((line = get_line_from_file(stdin))) {
174 cursor = line; 174 cursor = line;
175 len = number_of_tokens(line); 175 len = number_of_tokens(line);
176 for (i = 0; i < len; i++) { 176 for (i = 0; i < len; i++) {
diff --git a/findutils/grep.c b/findutils/grep.c
index aca469e2f..a374e114d 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -46,8 +46,6 @@ static const char grep_usage[] =
46#endif 46#endif
47 ; 47 ;
48 48
49static const int GROWBY = 80; /* how large we will grow strings by */
50
51/* options */ 49/* options */
52static int ignore_case = 0; 50static int ignore_case = 0;
53static int print_filename = 0; 51static int print_filename = 0;
@@ -62,35 +60,6 @@ static int nmatches = 0; /* keeps track of the number of matches */
62static char *cur_file = NULL; /* the current file we are reading */ 60static char *cur_file = NULL; /* the current file we are reading */
63 61
64 62
65/* This returns a malloc'ed char * which must be stored and free'ed */
66/* XXX: This function should probably go in a 'common'/'util'/'misc' file
67 * somewhere so it can be used by other folks. */
68static char *get_line_from_file(FILE *file)
69{
70 int ch;
71 int idx = 0;
72 char *linebuf = NULL;
73 int linebufsz = 0;
74
75 while (1) {
76 ch = fgetc(file);
77 if (ch == EOF)
78 break;
79 /* grow the line buffer as necessary */
80 if (idx > linebufsz-1)
81 linebuf = realloc(linebuf, linebufsz += GROWBY);
82 linebuf[idx++] = (char)ch;
83 if ((char)ch == '\n')
84 break;
85 }
86
87 if (idx == 0)
88 return NULL;
89
90 linebuf[idx] = 0;
91 return linebuf;
92}
93
94static void print_matched_line(char *line, int linenum) 63static void print_matched_line(char *line, int linenum)
95{ 64{
96 if (print_filename) 65 if (print_filename)
diff --git a/grep.c b/grep.c
index aca469e2f..a374e114d 100644
--- a/grep.c
+++ b/grep.c
@@ -46,8 +46,6 @@ static const char grep_usage[] =
46#endif 46#endif
47 ; 47 ;
48 48
49static const int GROWBY = 80; /* how large we will grow strings by */
50
51/* options */ 49/* options */
52static int ignore_case = 0; 50static int ignore_case = 0;
53static int print_filename = 0; 51static int print_filename = 0;
@@ -62,35 +60,6 @@ static int nmatches = 0; /* keeps track of the number of matches */
62static char *cur_file = NULL; /* the current file we are reading */ 60static char *cur_file = NULL; /* the current file we are reading */
63 61
64 62
65/* This returns a malloc'ed char * which must be stored and free'ed */
66/* XXX: This function should probably go in a 'common'/'util'/'misc' file
67 * somewhere so it can be used by other folks. */
68static char *get_line_from_file(FILE *file)
69{
70 int ch;
71 int idx = 0;
72 char *linebuf = NULL;
73 int linebufsz = 0;
74
75 while (1) {
76 ch = fgetc(file);
77 if (ch == EOF)
78 break;
79 /* grow the line buffer as necessary */
80 if (idx > linebufsz-1)
81 linebuf = realloc(linebuf, linebufsz += GROWBY);
82 linebuf[idx++] = (char)ch;
83 if ((char)ch == '\n')
84 break;
85 }
86
87 if (idx == 0)
88 return NULL;
89
90 linebuf[idx] = 0;
91 return linebuf;
92}
93
94static void print_matched_line(char *line, int linenum) 63static void print_matched_line(char *line, int linenum)
95{ 64{
96 if (print_filename) 65 if (print_filename)
diff --git a/internal.h b/internal.h
index 6e4f06cb5..41a72e18a 100644
--- a/internal.h
+++ b/internal.h
@@ -258,7 +258,7 @@ extern long getNum (const char *cp);
258extern pid_t* findPidByName( char* pidName); 258extern pid_t* findPidByName( char* pidName);
259extern void *xmalloc (size_t size); 259extern void *xmalloc (size_t size);
260extern int find_real_root_device_name(char* name); 260extern int find_real_root_device_name(char* name);
261extern char *cstring_lineFromFile(FILE *f); 261extern char *get_line_from_file(FILE *file);
262 262
263/* These parse entries in /etc/passwd and /etc/group. This is desirable 263/* These parse entries in /etc/passwd and /etc/group. This is desirable
264 * for BusyBox since we want to avoid using the glibc NSS stuff, which 264 * for BusyBox since we want to avoid using the glibc NSS stuff, which
diff --git a/miscutils/dc.c b/miscutils/dc.c
index 31a5471c3..5bf3bc984 100644
--- a/miscutils/dc.c
+++ b/miscutils/dc.c
@@ -170,7 +170,7 @@ int dc_main(int argc, char **argv)
170 char *line = NULL; 170 char *line = NULL;
171 char *cursor = NULL; 171 char *cursor = NULL;
172 char *token = NULL; 172 char *token = NULL;
173 while ((line = cstring_lineFromFile(stdin))) { 173 while ((line = get_line_from_file(stdin))) {
174 cursor = line; 174 cursor = line;
175 len = number_of_tokens(line); 175 len = number_of_tokens(line);
176 for (i = 0; i < len; i++) { 176 for (i = 0; i < len; i++) {
diff --git a/sort.c b/sort.c
index 93062faa4..a28122d51 100644
--- a/sort.c
+++ b/sort.c
@@ -84,7 +84,7 @@ static Line *line_newFromFile(FILE * src)
84 Line *self; 84 Line *self;
85 char *cstring = NULL; 85 char *cstring = NULL;
86 86
87 if ((cstring = cstring_lineFromFile(src))) { 87 if ((cstring = get_line_from_file(src))) {
88 self = line_alloc(); 88 self = line_alloc();
89 if (self == NULL) { 89 if (self == NULL) {
90 return NULL; 90 return NULL;
@@ -304,4 +304,4 @@ int sort_main(int argc, char **argv)
304 return(0); 304 return(0);
305} 305}
306 306
307/* $Id: sort.c,v 1.17 2000/06/19 17:25:40 andersen Exp $ */ 307/* $Id: sort.c,v 1.18 2000/06/28 22:15:26 markw Exp $ */
diff --git a/utility.c b/utility.c
index a45edde5d..de53dbda8 100644
--- a/utility.c
+++ b/utility.c
@@ -1586,56 +1586,35 @@ extern int find_real_root_device_name(char* name)
1586} 1586}
1587#endif 1587#endif
1588 1588
1589const unsigned int CSTRING_BUFFER_LENGTH = 1024; 1589static const int GROWBY = 80; /* how large we will grow strings by */
1590/* recursive parser that returns cstrings of arbitrary length
1591 * from a FILE*
1592 */
1593static char *
1594cstring_alloc(FILE* f, int depth)
1595{
1596 char *cstring;
1597 char buffer[CSTRING_BUFFER_LENGTH];
1598 int target = CSTRING_BUFFER_LENGTH * depth;
1599 int c, i, len, size;
1600
1601 /* fill buffer */
1602 i = 0;
1603 while ((c = fgetc(f)) != EOF) {
1604 buffer[i] = (char) c;
1605 if (buffer[i++] == 0x0a) { break; }
1606 if (i == CSTRING_BUFFER_LENGTH) { break; }
1607 }
1608 len = i;
1609
1610 /* recurse or malloc? */
1611 if (len == CSTRING_BUFFER_LENGTH) {
1612 cstring = cstring_alloc(f, (depth + 1));
1613 } else {
1614 /* [special case] EOF */
1615 if ((depth | len) == 0) { return NULL; }
1616
1617 /* malloc */
1618 size = target + len + 1;
1619 cstring = malloc(size);
1620 if (!cstring) { return NULL; }
1621 cstring[size - 1] = 0;
1622 }
1623
1624 /* copy buffer */
1625 if (cstring) {
1626 memcpy(&cstring[target], buffer, len);
1627 }
1628 return cstring;
1629}
1630 1590
1631/* 1591/* get_line_from_file() - This function reads an entire line from a text file
1632 * wrapper around recursive cstring_alloc 1592 * up to a newline. It returns a malloc'ed char * which must be stored and
1633 * it's the caller's responsibility to free the cstring 1593 * free'ed by the caller. */
1634 */ 1594extern char *get_line_from_file(FILE *file)
1635char *
1636cstring_lineFromFile(FILE *f)
1637{ 1595{
1638 return cstring_alloc(f, 0); 1596 int ch;
1597 int idx = 0;
1598 char *linebuf = NULL;
1599 int linebufsz = 0;
1600
1601 while (1) {
1602 ch = fgetc(file);
1603 if (ch == EOF)
1604 break;
1605 /* grow the line buffer as necessary */
1606 if (idx > linebufsz-1)
1607 linebuf = realloc(linebuf, linebufsz += GROWBY);
1608 linebuf[idx++] = (char)ch;
1609 if ((char)ch == '\n')
1610 break;
1611 }
1612
1613 if (idx == 0)
1614 return NULL;
1615
1616 linebuf[idx] = 0;
1617 return linebuf;
1639} 1618}
1640 1619
1641/* END CODE */ 1620/* END CODE */