aboutsummaryrefslogtreecommitdiff
path: root/utility.c
diff options
context:
space:
mode:
authorbeppu <beppu@69ca8d6d-28ef-0310-b511-8ec308f3f277>2000-04-17 04:22:09 +0000
committerbeppu <beppu@69ca8d6d-28ef-0310-b511-8ec308f3f277>2000-04-17 04:22:09 +0000
commitcdab153cc57f12291471186bd083c278eb47bb42 (patch)
treedb9e84821fb8742d8c10636388db640f1f763c73 /utility.c
parent489710b1137b8e37331918848d06c1be1794e0e6 (diff)
downloadbusybox-w32-cdab153cc57f12291471186bd083c278eb47bb42.tar.gz
busybox-w32-cdab153cc57f12291471186bd083c278eb47bb42.tar.bz2
busybox-w32-cdab153cc57f12291471186bd083c278eb47bb42.zip
+ 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. git-svn-id: svn://busybox.net/trunk/busybox@463 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to '')
-rw-r--r--utility.c51
1 files changed, 51 insertions, 0 deletions
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)
1521} 1521}
1522#endif 1522#endif
1523 1523
1524const unsigned int CSTRING_BUFFER_LENGTH = 128;
1525/* recursive parser that returns cstrings of arbitrary length
1526 * from a FILE*
1527 */
1528static char *
1529cstring_alloc(FILE* f, int depth)
1530{
1531 char *cstring;
1532 char buffer[CSTRING_BUFFER_LENGTH];
1533 int target = CSTRING_BUFFER_LENGTH * depth;
1534 int i, len;
1535 int size;
1536
1537 /* fill buffer */
1538 i = 0;
1539 while ((buffer[i] = fgetc(f)) != EOF) {
1540 if (buffer[i++] == 0x0a) { break; }
1541 if (i == CSTRING_BUFFER_LENGTH) { break; }
1542 }
1543 len = i;
1544
1545 /* recurse or malloc? */
1546 if (len == CSTRING_BUFFER_LENGTH) {
1547 cstring = cstring_alloc(f, (depth + 1));
1548 } else {
1549 /* [special case] EOF */
1550 if ((depth | len) == 0) { return NULL; }
1551
1552 /* malloc */
1553 size = target + len + 1;
1554 cstring = malloc(size);
1555 if (!cstring) { return NULL; }
1556 cstring[size - 1] = 0;
1557 }
1558
1559 /* copy buffer */
1560 if (cstring) {
1561 memcpy(&cstring[target], buffer, len);
1562 }
1563 return cstring;
1564}
1565
1566/*
1567 * wrapper around recursive cstring_alloc
1568 * it's the caller's responsibility to free the cstring
1569 */
1570char *
1571cstring_lineFromFile(FILE *f)
1572{
1573 return cstring_alloc(f, 0);
1574}
1524 1575
1525/* END CODE */ 1576/* END CODE */
1526/* 1577/*