From: John Beppu Date: Mon, 17 Apr 2000 04:22:09 +0000 (-0000) Subject: + in the interest of robustness, I added X-Git-Tag: 0_43~23 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=5a728cfdfeaa0c6db8485bec230e24b3ca03806b;p=oweals%2Fbusybox.git + in the interest of robustness, I added utility.c :: cstring_alloc() utility.c :: cstring_lineFromFile() /* they're at the bottom */ so that I could read in lines of arbitrary length from FILE*s (instead of using fgets(huge_ass_buffer,...)). + I tested it out on sort, and it seems to be fine. --- diff --git a/coreutils/sort.c b/coreutils/sort.c index 4301f4303..49eb4fd72 100644 --- a/coreutils/sort.c +++ b/coreutils/sort.c @@ -54,7 +54,6 @@ typedef struct Line { typedef struct { int len; /* number of Lines */ Line **sorted; /* array fed to qsort */ - Line *head; /* head of List */ Line *current; /* current Line */ } List; @@ -71,36 +70,23 @@ static const int max = 1024; static Line *line_alloc() { Line *self; - self = malloc(1 * sizeof(Line)); return self; } -/* Initialize Line with string */ -static Line *line_init(Line * self, const char *string) -{ - self->data = malloc((strlen(string) + 1) * sizeof(char)); - - if (self->data == NULL) { - return NULL; - } - strcpy(self->data, string); - self->next = NULL; - return self; -} - /* Construct Line from FILE* */ static Line *line_newFromFile(FILE * src) { - char buffer[max]; Line *self; + char *cstring = NULL; - if (fgets(buffer, max, src)) { + if ((cstring = cstring_lineFromFile(src))) { self = line_alloc(); if (self == NULL) { return NULL; } - line_init(self, buffer); + self->data = cstring; + self->next = NULL; return self; } return NULL; @@ -177,7 +163,7 @@ static List *list_insert(List * self, Line * line) self->head = line; self->current = line; - /* all subsequent insertions */ + /* all subsequent insertions */ } else { self->current->next = line; self->current = line; @@ -241,12 +227,6 @@ static void list_release(List * self) } -/* - * I need a list - * to insert lines into - * then I need to sort this list - * and finally print it - */ int sort_main(int argc, char **argv) { @@ -320,4 +300,4 @@ int sort_main(int argc, char **argv) exit(0); } -/* $Id: sort.c,v 1.14 2000/04/15 16:34:54 erik Exp $ */ +/* $Id: sort.c,v 1.15 2000/04/17 04:22:09 beppu Exp $ */ diff --git a/internal.h b/internal.h index 18159b1d3..8c97a090a 100644 --- a/internal.h +++ b/internal.h @@ -219,6 +219,8 @@ 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); + #if defined BB_INIT || defined BB_SYSLOGD extern int device_open(char *device, int mode); diff --git a/sort.c b/sort.c index 4301f4303..49eb4fd72 100644 --- a/sort.c +++ b/sort.c @@ -54,7 +54,6 @@ typedef struct Line { typedef struct { int len; /* number of Lines */ Line **sorted; /* array fed to qsort */ - Line *head; /* head of List */ Line *current; /* current Line */ } List; @@ -71,36 +70,23 @@ static const int max = 1024; static Line *line_alloc() { Line *self; - self = malloc(1 * sizeof(Line)); return self; } -/* Initialize Line with string */ -static Line *line_init(Line * self, const char *string) -{ - self->data = malloc((strlen(string) + 1) * sizeof(char)); - - if (self->data == NULL) { - return NULL; - } - strcpy(self->data, string); - self->next = NULL; - return self; -} - /* Construct Line from FILE* */ static Line *line_newFromFile(FILE * src) { - char buffer[max]; Line *self; + char *cstring = NULL; - if (fgets(buffer, max, src)) { + if ((cstring = cstring_lineFromFile(src))) { self = line_alloc(); if (self == NULL) { return NULL; } - line_init(self, buffer); + self->data = cstring; + self->next = NULL; return self; } return NULL; @@ -177,7 +163,7 @@ static List *list_insert(List * self, Line * line) self->head = line; self->current = line; - /* all subsequent insertions */ + /* all subsequent insertions */ } else { self->current->next = line; self->current = line; @@ -241,12 +227,6 @@ static void list_release(List * self) } -/* - * I need a list - * to insert lines into - * then I need to sort this list - * and finally print it - */ int sort_main(int argc, char **argv) { @@ -320,4 +300,4 @@ int sort_main(int argc, char **argv) exit(0); } -/* $Id: sort.c,v 1.14 2000/04/15 16:34:54 erik Exp $ */ +/* $Id: sort.c,v 1.15 2000/04/17 04:22:09 beppu Exp $ */ diff --git a/utility.c b/utility.c index c3a102c5e..42b8dc1e9 100644 --- a/utility.c +++ b/utility.c @@ -1521,6 +1521,57 @@ extern int find_real_root_device_name(char* name) } #endif +const unsigned int CSTRING_BUFFER_LENGTH = 128; +/* 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 i, len; + int size; + + /* fill buffer */ + i = 0; + while ((buffer[i] = fgetc(f)) != EOF) { + 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; +} + +/* + * wrapper around recursive cstring_alloc + * it's the caller's responsibility to free the cstring + */ +char * +cstring_lineFromFile(FILE *f) +{ + return cstring_alloc(f, 0); +} /* END CODE */ /*