diff options
| author | Mark Whitley <markw@lineo.com> | 2000-06-28 22:15:26 +0000 |
|---|---|---|
| committer | Mark Whitley <markw@lineo.com> | 2000-06-28 22:15:26 +0000 |
| commit | 1ca41775bbdc07cf67be79aebc566754c9c02855 (patch) | |
| tree | 0ac134f0a80036aec272b04c3a057ea2ae055b20 | |
| parent | d37218941c37795cc8e96ddb3312d83fb2269d5a (diff) | |
| download | busybox-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.c | 4 | ||||
| -rw-r--r-- | dc.c | 2 | ||||
| -rw-r--r-- | findutils/grep.c | 31 | ||||
| -rw-r--r-- | grep.c | 31 | ||||
| -rw-r--r-- | internal.h | 2 | ||||
| -rw-r--r-- | miscutils/dc.c | 2 | ||||
| -rw-r--r-- | sort.c | 4 | ||||
| -rw-r--r-- | utility.c | 75 |
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 $ */ |
| @@ -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 | ||
| 49 | static const int GROWBY = 80; /* how large we will grow strings by */ | ||
| 50 | |||
| 51 | /* options */ | 49 | /* options */ |
| 52 | static int ignore_case = 0; | 50 | static int ignore_case = 0; |
| 53 | static int print_filename = 0; | 51 | static int print_filename = 0; |
| @@ -62,35 +60,6 @@ static int nmatches = 0; /* keeps track of the number of matches */ | |||
| 62 | static char *cur_file = NULL; /* the current file we are reading */ | 60 | static 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. */ | ||
| 68 | static 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 | |||
| 94 | static void print_matched_line(char *line, int linenum) | 63 | static void print_matched_line(char *line, int linenum) |
| 95 | { | 64 | { |
| 96 | if (print_filename) | 65 | if (print_filename) |
| @@ -46,8 +46,6 @@ static const char grep_usage[] = | |||
| 46 | #endif | 46 | #endif |
| 47 | ; | 47 | ; |
| 48 | 48 | ||
| 49 | static const int GROWBY = 80; /* how large we will grow strings by */ | ||
| 50 | |||
| 51 | /* options */ | 49 | /* options */ |
| 52 | static int ignore_case = 0; | 50 | static int ignore_case = 0; |
| 53 | static int print_filename = 0; | 51 | static int print_filename = 0; |
| @@ -62,35 +60,6 @@ static int nmatches = 0; /* keeps track of the number of matches */ | |||
| 62 | static char *cur_file = NULL; /* the current file we are reading */ | 60 | static 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. */ | ||
| 68 | static 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 | |||
| 94 | static void print_matched_line(char *line, int linenum) | 63 | static 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); | |||
| 258 | extern pid_t* findPidByName( char* pidName); | 258 | extern pid_t* findPidByName( char* pidName); |
| 259 | extern void *xmalloc (size_t size); | 259 | extern void *xmalloc (size_t size); |
| 260 | extern int find_real_root_device_name(char* name); | 260 | extern int find_real_root_device_name(char* name); |
| 261 | extern char *cstring_lineFromFile(FILE *f); | 261 | extern 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++) { |
| @@ -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 $ */ |
| @@ -1586,56 +1586,35 @@ extern int find_real_root_device_name(char* name) | |||
| 1586 | } | 1586 | } |
| 1587 | #endif | 1587 | #endif |
| 1588 | 1588 | ||
| 1589 | const unsigned int CSTRING_BUFFER_LENGTH = 1024; | 1589 | static 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 | */ | ||
| 1593 | static char * | ||
| 1594 | cstring_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 | */ | 1594 | extern char *get_line_from_file(FILE *file) |
| 1635 | char * | ||
| 1636 | cstring_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 */ |
