diff options
author | John Beppu | 2000-04-17 04:22:09 +0000 |
---|---|---|
committer | John Beppu | 2000-04-17 04:22:09 +0000 |
commit | 5a728cfdfeaa0c6db8485bec230e24b3ca03806b (patch) | |
tree | db9e84821fb8742d8c10636388db640f1f763c73 /utility.c | |
parent | 3becdfc31635bec63b6cbefde148d9bd3f3678a1 (diff) | |
download | busybox-5a728cfdfeaa0c6db8485bec230e24b3ca03806b.zip busybox-5a728cfdfeaa0c6db8485bec230e24b3ca03806b.tar.gz |
+ 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.
Diffstat (limited to 'utility.c')
-rw-r--r-- | utility.c | 51 |
1 files changed, 51 insertions, 0 deletions
@@ -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 */ /* |