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 /coreutils | |
| 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.
Diffstat (limited to 'coreutils')
| -rw-r--r-- | coreutils/sort.c | 32 |
1 files changed, 6 insertions, 26 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 $ */ |
