diff options
author | John Beppu <beppu@lbox.org> | 1999-12-22 00:30:29 +0000 |
---|---|---|
committer | John Beppu <beppu@lbox.org> | 1999-12-22 00:30:29 +0000 |
commit | 38efa7902e7d266fbc78e1d39780ca0866d98dad (patch) | |
tree | 885e15f1c38f7c0f62fa0b8720e904e84b9e737f | |
parent | c0ca473af9a5afd17fd6dd916bb6008a036efb20 (diff) | |
download | busybox-w32-38efa7902e7d266fbc78e1d39780ca0866d98dad.tar.gz busybox-w32-38efa7902e7d266fbc78e1d39780ca0866d98dad.tar.bz2 busybox-w32-38efa7902e7d266fbc78e1d39780ca0866d98dad.zip |
work in progress...
-rw-r--r-- | coreutils/sort.c | 93 | ||||
-rw-r--r-- | sort.c | 93 |
2 files changed, 164 insertions, 22 deletions
diff --git a/coreutils/sort.c b/coreutils/sort.c index 4ab673b4d..d82351797 100644 --- a/coreutils/sort.c +++ b/coreutils/sort.c | |||
@@ -32,6 +32,7 @@ static const char sort_usage[] = | |||
32 | "Usage: sort [OPTION]... [FILE]...\n\n" | 32 | "Usage: sort [OPTION]... [FILE]...\n\n" |
33 | ; | 33 | ; |
34 | 34 | ||
35 | /* structs ________________________________________________________________ */ | ||
35 | 36 | ||
36 | /* line node */ | 37 | /* line node */ |
37 | typedef struct { | 38 | typedef struct { |
@@ -42,19 +43,51 @@ typedef struct { | |||
42 | /* singly-linked list of lines */ | 43 | /* singly-linked list of lines */ |
43 | typedef struct { | 44 | typedef struct { |
44 | int len; /* number of Lines */ | 45 | int len; /* number of Lines */ |
45 | Line *line; /* array fed to qsort */ | 46 | Line *sorted; /* array fed to qsort */ |
47 | |||
46 | Line *head; /* head of List */ | 48 | Line *head; /* head of List */ |
49 | Line *current /* current Line */ | ||
47 | } List; | 50 | } List; |
48 | 51 | ||
49 | 52 | ||
50 | /* Line methods */ | 53 | /* methods ________________________________________________________________ */ |
51 | 54 | ||
52 | static const int max = 1024; | 55 | static const int max = 1024; |
53 | 56 | ||
57 | /* mallocate Line */ | ||
54 | static Line * | 58 | static Line * |
55 | line_new() | 59 | line_alloc() |
56 | { | 60 | { |
57 | char buffer[max]; | 61 | Line *self; |
62 | self = malloc(1 * sizeof(Line)); | ||
63 | return self; | ||
64 | } | ||
65 | |||
66 | /* Initialize Line with string */ | ||
67 | static Line * | ||
68 | line_init(Line *self, const char *string) | ||
69 | { | ||
70 | self->data = malloc((strlen(string) + 1) * sizeof(char)); | ||
71 | if (self->data == NULL) { return NULL; } | ||
72 | strcpy(self->data, string); | ||
73 | self->next = NULL; | ||
74 | return self; | ||
75 | } | ||
76 | |||
77 | /* Construct Line from FILE* */ | ||
78 | static Line * | ||
79 | line_newFromFile(FILE *src) | ||
80 | { | ||
81 | char buffer[max]; | ||
82 | Line *self; | ||
83 | |||
84 | if (fgets(buffer, max, src)) { | ||
85 | self = line_alloc(); | ||
86 | if (self == NULL) { return NULL; } | ||
87 | line_init(self, buffer); | ||
88 | return self; | ||
89 | } | ||
90 | return NULL; | ||
58 | } | 91 | } |
59 | 92 | ||
60 | 93 | ||
@@ -69,15 +102,54 @@ compare_numeric(const void *, const void *); | |||
69 | 102 | ||
70 | /* List */ | 103 | /* List */ |
71 | 104 | ||
72 | static void | 105 | /* */ |
73 | list_insert(); | 106 | static List * |
107 | list_init(List *self) | ||
108 | { | ||
109 | self->len = 0; | ||
110 | self->sorted = NULL; | ||
111 | self->head = NULL; | ||
112 | self->current = NULL; | ||
113 | return self; | ||
114 | } | ||
74 | 115 | ||
116 | /* for simplicity, the List gains ownership of the line */ | ||
75 | static void | 117 | static void |
76 | list_sort(); | 118 | list_insert(List *self, Line *line) |
119 | { | ||
120 | if (line == NULL) { return NULL; } | ||
77 | 121 | ||
78 | static void | 122 | /* first insertion */ |
79 | list_print(); | 123 | if (self->head == NULL) { |
124 | self->head = line; | ||
125 | self->current = line; | ||
126 | |||
127 | /* all subsequent insertions */ | ||
128 | } else { | ||
129 | self->current->next = line; | ||
130 | self->current = line; | ||
131 | } | ||
132 | self->len++; | ||
133 | return self; | ||
134 | } | ||
80 | 135 | ||
136 | /* */ | ||
137 | static List * | ||
138 | list_sort(List *self); | ||
139 | |||
140 | /* precondition: list must be sorted */ | ||
141 | static List * | ||
142 | list_writeToFile(List *self, FILE* dst) | ||
143 | { | ||
144 | if (self->sorted == NULL) { return NULL; } | ||
145 | } | ||
146 | |||
147 | /* deallocate */ | ||
148 | static List * | ||
149 | list_release(List *self) | ||
150 | { | ||
151 | return self; | ||
152 | } | ||
81 | 153 | ||
82 | 154 | ||
83 | /* | 155 | /* |
@@ -123,5 +195,4 @@ sort_main(int argc, char **argv) | |||
123 | exit(0); | 195 | exit(0); |
124 | } | 196 | } |
125 | 197 | ||
126 | /* $Date: 1999/12/21 20:00:35 $ */ | 198 | /* $Id: sort.c,v 1.2 1999/12/22 00:30:29 beppu Exp $ */ |
127 | /* $Id: sort.c,v 1.1 1999/12/21 20:00:35 beppu Exp $ */ | ||
@@ -32,6 +32,7 @@ static const char sort_usage[] = | |||
32 | "Usage: sort [OPTION]... [FILE]...\n\n" | 32 | "Usage: sort [OPTION]... [FILE]...\n\n" |
33 | ; | 33 | ; |
34 | 34 | ||
35 | /* structs ________________________________________________________________ */ | ||
35 | 36 | ||
36 | /* line node */ | 37 | /* line node */ |
37 | typedef struct { | 38 | typedef struct { |
@@ -42,19 +43,51 @@ typedef struct { | |||
42 | /* singly-linked list of lines */ | 43 | /* singly-linked list of lines */ |
43 | typedef struct { | 44 | typedef struct { |
44 | int len; /* number of Lines */ | 45 | int len; /* number of Lines */ |
45 | Line *line; /* array fed to qsort */ | 46 | Line *sorted; /* array fed to qsort */ |
47 | |||
46 | Line *head; /* head of List */ | 48 | Line *head; /* head of List */ |
49 | Line *current /* current Line */ | ||
47 | } List; | 50 | } List; |
48 | 51 | ||
49 | 52 | ||
50 | /* Line methods */ | 53 | /* methods ________________________________________________________________ */ |
51 | 54 | ||
52 | static const int max = 1024; | 55 | static const int max = 1024; |
53 | 56 | ||
57 | /* mallocate Line */ | ||
54 | static Line * | 58 | static Line * |
55 | line_new() | 59 | line_alloc() |
56 | { | 60 | { |
57 | char buffer[max]; | 61 | Line *self; |
62 | self = malloc(1 * sizeof(Line)); | ||
63 | return self; | ||
64 | } | ||
65 | |||
66 | /* Initialize Line with string */ | ||
67 | static Line * | ||
68 | line_init(Line *self, const char *string) | ||
69 | { | ||
70 | self->data = malloc((strlen(string) + 1) * sizeof(char)); | ||
71 | if (self->data == NULL) { return NULL; } | ||
72 | strcpy(self->data, string); | ||
73 | self->next = NULL; | ||
74 | return self; | ||
75 | } | ||
76 | |||
77 | /* Construct Line from FILE* */ | ||
78 | static Line * | ||
79 | line_newFromFile(FILE *src) | ||
80 | { | ||
81 | char buffer[max]; | ||
82 | Line *self; | ||
83 | |||
84 | if (fgets(buffer, max, src)) { | ||
85 | self = line_alloc(); | ||
86 | if (self == NULL) { return NULL; } | ||
87 | line_init(self, buffer); | ||
88 | return self; | ||
89 | } | ||
90 | return NULL; | ||
58 | } | 91 | } |
59 | 92 | ||
60 | 93 | ||
@@ -69,15 +102,54 @@ compare_numeric(const void *, const void *); | |||
69 | 102 | ||
70 | /* List */ | 103 | /* List */ |
71 | 104 | ||
72 | static void | 105 | /* */ |
73 | list_insert(); | 106 | static List * |
107 | list_init(List *self) | ||
108 | { | ||
109 | self->len = 0; | ||
110 | self->sorted = NULL; | ||
111 | self->head = NULL; | ||
112 | self->current = NULL; | ||
113 | return self; | ||
114 | } | ||
74 | 115 | ||
116 | /* for simplicity, the List gains ownership of the line */ | ||
75 | static void | 117 | static void |
76 | list_sort(); | 118 | list_insert(List *self, Line *line) |
119 | { | ||
120 | if (line == NULL) { return NULL; } | ||
77 | 121 | ||
78 | static void | 122 | /* first insertion */ |
79 | list_print(); | 123 | if (self->head == NULL) { |
124 | self->head = line; | ||
125 | self->current = line; | ||
126 | |||
127 | /* all subsequent insertions */ | ||
128 | } else { | ||
129 | self->current->next = line; | ||
130 | self->current = line; | ||
131 | } | ||
132 | self->len++; | ||
133 | return self; | ||
134 | } | ||
80 | 135 | ||
136 | /* */ | ||
137 | static List * | ||
138 | list_sort(List *self); | ||
139 | |||
140 | /* precondition: list must be sorted */ | ||
141 | static List * | ||
142 | list_writeToFile(List *self, FILE* dst) | ||
143 | { | ||
144 | if (self->sorted == NULL) { return NULL; } | ||
145 | } | ||
146 | |||
147 | /* deallocate */ | ||
148 | static List * | ||
149 | list_release(List *self) | ||
150 | { | ||
151 | return self; | ||
152 | } | ||
81 | 153 | ||
82 | 154 | ||
83 | /* | 155 | /* |
@@ -123,5 +195,4 @@ sort_main(int argc, char **argv) | |||
123 | exit(0); | 195 | exit(0); |
124 | } | 196 | } |
125 | 197 | ||
126 | /* $Date: 1999/12/21 20:00:35 $ */ | 198 | /* $Id: sort.c,v 1.2 1999/12/22 00:30:29 beppu Exp $ */ |
127 | /* $Id: sort.c,v 1.1 1999/12/21 20:00:35 beppu Exp $ */ | ||