aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Beppu <beppu@lbox.org>2000-04-17 04:22:09 +0000
committerJohn Beppu <beppu@lbox.org>2000-04-17 04:22:09 +0000
commit5a728cfdfeaa0c6db8485bec230e24b3ca03806b (patch)
treedb9e84821fb8742d8c10636388db640f1f763c73
parent3becdfc31635bec63b6cbefde148d9bd3f3678a1 (diff)
downloadbusybox-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.c32
-rw-r--r--internal.h2
-rw-r--r--sort.c32
-rw-r--r--utility.c51
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 {
54typedef struct { 54typedef 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;
71static Line *line_alloc() 70static 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 */
80static 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* */
93static Line *line_newFromFile(FILE * src) 78static 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
251int sort_main(int argc, char **argv) 231int 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);
219extern pid_t* findPidByName( char* pidName); 219extern pid_t* findPidByName( char* pidName);
220extern void *xmalloc (size_t size); 220extern void *xmalloc (size_t size);
221extern int find_real_root_device_name(char* name); 221extern int find_real_root_device_name(char* name);
222extern char *cstring_lineFromFile(FILE *f);
223
222 224
223#if defined BB_INIT || defined BB_SYSLOGD 225#if defined BB_INIT || defined BB_SYSLOGD
224extern int device_open(char *device, int mode); 226extern 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 {
54typedef struct { 54typedef 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;
71static Line *line_alloc() 70static 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 */
80static 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* */
93static Line *line_newFromFile(FILE * src) 78static 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
251int sort_main(int argc, char **argv) 231int 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/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/*