diff options
author | John Beppu <beppu@lbox.org> | 2000-04-17 04:22:09 +0000 |
---|---|---|
committer | John Beppu <beppu@lbox.org> | 2000-04-17 04:22:09 +0000 |
commit | 5a728cfdfeaa0c6db8485bec230e24b3ca03806b (patch) | |
tree | db9e84821fb8742d8c10636388db640f1f763c73 | |
parent | 3becdfc31635bec63b6cbefde148d9bd3f3678a1 (diff) | |
download | busybox-w32-5a728cfdfeaa0c6db8485bec230e24b3ca03806b.tar.gz busybox-w32-5a728cfdfeaa0c6db8485bec230e24b3ca03806b.tar.bz2 busybox-w32-5a728cfdfeaa0c6db8485bec230e24b3ca03806b.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.
-rw-r--r-- | coreutils/sort.c | 32 | ||||
-rw-r--r-- | internal.h | 2 | ||||
-rw-r--r-- | sort.c | 32 | ||||
-rw-r--r-- | utility.c | 51 |
4 files changed, 65 insertions, 52 deletions
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 { | |||
54 | typedef struct { | 54 | typedef struct { |
55 | int len; /* number of Lines */ | 55 | int len; /* number of Lines */ |
56 | Line **sorted; /* array fed to qsort */ | 56 | Line **sorted; /* array fed to qsort */ |
57 | |||
58 | Line *head; /* head of List */ | 57 | Line *head; /* head of List */ |
59 | Line *current; /* current Line */ | 58 | Line *current; /* current Line */ |
60 | } List; | 59 | } List; |
@@ -71,36 +70,23 @@ static const int max = 1024; | |||
71 | static Line *line_alloc() | 70 | static Line *line_alloc() |
72 | { | 71 | { |
73 | Line *self; | 72 | Line *self; |
74 | |||
75 | self = malloc(1 * sizeof(Line)); | 73 | self = malloc(1 * sizeof(Line)); |
76 | return self; | 74 | return self; |
77 | } | 75 | } |
78 | 76 | ||
79 | /* Initialize Line with string */ | ||
80 | static Line *line_init(Line * self, const char *string) | ||
81 | { | ||
82 | self->data = malloc((strlen(string) + 1) * sizeof(char)); | ||
83 | |||
84 | if (self->data == NULL) { | ||
85 | return NULL; | ||
86 | } | ||
87 | strcpy(self->data, string); | ||
88 | self->next = NULL; | ||
89 | return self; | ||
90 | } | ||
91 | |||
92 | /* Construct Line from FILE* */ | 77 | /* Construct Line from FILE* */ |
93 | static Line *line_newFromFile(FILE * src) | 78 | static Line *line_newFromFile(FILE * src) |
94 | { | 79 | { |
95 | char buffer[max]; | ||
96 | Line *self; | 80 | Line *self; |
81 | char *cstring = NULL; | ||
97 | 82 | ||
98 | if (fgets(buffer, max, src)) { | 83 | if ((cstring = cstring_lineFromFile(src))) { |
99 | self = line_alloc(); | 84 | self = line_alloc(); |
100 | if (self == NULL) { | 85 | if (self == NULL) { |
101 | return NULL; | 86 | return NULL; |
102 | } | 87 | } |
103 | line_init(self, buffer); | 88 | self->data = cstring; |
89 | self->next = NULL; | ||
104 | return self; | 90 | return self; |
105 | } | 91 | } |
106 | return NULL; | 92 | return NULL; |
@@ -177,7 +163,7 @@ static List *list_insert(List * self, Line * line) | |||
177 | self->head = line; | 163 | self->head = line; |
178 | self->current = line; | 164 | self->current = line; |
179 | 165 | ||
180 | /* all subsequent insertions */ | 166 | /* all subsequent insertions */ |
181 | } else { | 167 | } else { |
182 | self->current->next = line; | 168 | self->current->next = line; |
183 | self->current = line; | 169 | self->current = line; |
@@ -241,12 +227,6 @@ static void list_release(List * self) | |||
241 | } | 227 | } |
242 | 228 | ||
243 | 229 | ||
244 | /* | ||
245 | * I need a list | ||
246 | * to insert lines into | ||
247 | * then I need to sort this list | ||
248 | * and finally print it | ||
249 | */ | ||
250 | 230 | ||
251 | int sort_main(int argc, char **argv) | 231 | int sort_main(int argc, char **argv) |
252 | { | 232 | { |
@@ -320,4 +300,4 @@ int sort_main(int argc, char **argv) | |||
320 | exit(0); | 300 | exit(0); |
321 | } | 301 | } |
322 | 302 | ||
323 | /* $Id: sort.c,v 1.14 2000/04/15 16:34:54 erik Exp $ */ | 303 | /* $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); | |||
219 | extern pid_t* findPidByName( char* pidName); | 219 | extern pid_t* findPidByName( char* pidName); |
220 | extern void *xmalloc (size_t size); | 220 | extern void *xmalloc (size_t size); |
221 | extern int find_real_root_device_name(char* name); | 221 | extern int find_real_root_device_name(char* name); |
222 | extern char *cstring_lineFromFile(FILE *f); | ||
223 | |||
222 | 224 | ||
223 | #if defined BB_INIT || defined BB_SYSLOGD | 225 | #if defined BB_INIT || defined BB_SYSLOGD |
224 | extern int device_open(char *device, int mode); | 226 | extern int device_open(char *device, int mode); |
@@ -54,7 +54,6 @@ typedef struct Line { | |||
54 | typedef struct { | 54 | typedef struct { |
55 | int len; /* number of Lines */ | 55 | int len; /* number of Lines */ |
56 | Line **sorted; /* array fed to qsort */ | 56 | Line **sorted; /* array fed to qsort */ |
57 | |||
58 | Line *head; /* head of List */ | 57 | Line *head; /* head of List */ |
59 | Line *current; /* current Line */ | 58 | Line *current; /* current Line */ |
60 | } List; | 59 | } List; |
@@ -71,36 +70,23 @@ static const int max = 1024; | |||
71 | static Line *line_alloc() | 70 | static Line *line_alloc() |
72 | { | 71 | { |
73 | Line *self; | 72 | Line *self; |
74 | |||
75 | self = malloc(1 * sizeof(Line)); | 73 | self = malloc(1 * sizeof(Line)); |
76 | return self; | 74 | return self; |
77 | } | 75 | } |
78 | 76 | ||
79 | /* Initialize Line with string */ | ||
80 | static Line *line_init(Line * self, const char *string) | ||
81 | { | ||
82 | self->data = malloc((strlen(string) + 1) * sizeof(char)); | ||
83 | |||
84 | if (self->data == NULL) { | ||
85 | return NULL; | ||
86 | } | ||
87 | strcpy(self->data, string); | ||
88 | self->next = NULL; | ||
89 | return self; | ||
90 | } | ||
91 | |||
92 | /* Construct Line from FILE* */ | 77 | /* Construct Line from FILE* */ |
93 | static Line *line_newFromFile(FILE * src) | 78 | static Line *line_newFromFile(FILE * src) |
94 | { | 79 | { |
95 | char buffer[max]; | ||
96 | Line *self; | 80 | Line *self; |
81 | char *cstring = NULL; | ||
97 | 82 | ||
98 | if (fgets(buffer, max, src)) { | 83 | if ((cstring = cstring_lineFromFile(src))) { |
99 | self = line_alloc(); | 84 | self = line_alloc(); |
100 | if (self == NULL) { | 85 | if (self == NULL) { |
101 | return NULL; | 86 | return NULL; |
102 | } | 87 | } |
103 | line_init(self, buffer); | 88 | self->data = cstring; |
89 | self->next = NULL; | ||
104 | return self; | 90 | return self; |
105 | } | 91 | } |
106 | return NULL; | 92 | return NULL; |
@@ -177,7 +163,7 @@ static List *list_insert(List * self, Line * line) | |||
177 | self->head = line; | 163 | self->head = line; |
178 | self->current = line; | 164 | self->current = line; |
179 | 165 | ||
180 | /* all subsequent insertions */ | 166 | /* all subsequent insertions */ |
181 | } else { | 167 | } else { |
182 | self->current->next = line; | 168 | self->current->next = line; |
183 | self->current = line; | 169 | self->current = line; |
@@ -241,12 +227,6 @@ static void list_release(List * self) | |||
241 | } | 227 | } |
242 | 228 | ||
243 | 229 | ||
244 | /* | ||
245 | * I need a list | ||
246 | * to insert lines into | ||
247 | * then I need to sort this list | ||
248 | * and finally print it | ||
249 | */ | ||
250 | 230 | ||
251 | int sort_main(int argc, char **argv) | 231 | int sort_main(int argc, char **argv) |
252 | { | 232 | { |
@@ -320,4 +300,4 @@ int sort_main(int argc, char **argv) | |||
320 | exit(0); | 300 | exit(0); |
321 | } | 301 | } |
322 | 302 | ||
323 | /* $Id: sort.c,v 1.14 2000/04/15 16:34:54 erik Exp $ */ | 303 | /* $Id: sort.c,v 1.15 2000/04/17 04:22:09 beppu Exp $ */ |
@@ -1521,6 +1521,57 @@ extern int find_real_root_device_name(char* name) | |||
1521 | } | 1521 | } |
1522 | #endif | 1522 | #endif |
1523 | 1523 | ||
1524 | const unsigned int CSTRING_BUFFER_LENGTH = 128; | ||
1525 | /* recursive parser that returns cstrings of arbitrary length | ||
1526 | * from a FILE* | ||
1527 | */ | ||
1528 | static char * | ||
1529 | cstring_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 | */ | ||
1570 | char * | ||
1571 | cstring_lineFromFile(FILE *f) | ||
1572 | { | ||
1573 | return cstring_alloc(f, 0); | ||
1574 | } | ||
1524 | 1575 | ||
1525 | /* END CODE */ | 1576 | /* END CODE */ |
1526 | /* | 1577 | /* |