aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Beppu <beppu@lbox.org>1999-12-22 22:24:52 +0000
committerJohn Beppu <beppu@lbox.org>1999-12-22 22:24:52 +0000
commitf3e59041b5d841422e8f04408a66603badb5ee9c (patch)
treed2a973bf4dbd6127badd08de9a7a1e5fdf5ee0d1
parent019513a59ffd966cca51d6616757295a46869e4a (diff)
downloadbusybox-w32-f3e59041b5d841422e8f04408a66603badb5ee9c.tar.gz
busybox-w32-f3e59041b5d841422e8f04408a66603badb5ee9c.tar.bz2
busybox-w32-f3e59041b5d841422e8f04408a66603badb5ee9c.zip
the base is nearly done.
need to implement various comparison functions, now.
-rw-r--r--coreutils/sort.c93
-rw-r--r--sort.c93
2 files changed, 144 insertions, 42 deletions
diff --git a/coreutils/sort.c b/coreutils/sort.c
index f3f9fca1d..965152648 100644
--- a/coreutils/sort.c
+++ b/coreutils/sort.c
@@ -32,23 +32,26 @@ 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/* typedefs _______________________________________________________________ */
36 36
37/* line node */ 37/* line node */
38typedef struct { 38typedef struct Line {
39 char *data; /* line data */ 39 char *data; /* line data */
40 struct Line *next; /* pointer to next line node */ 40 struct Line *next; /* pointer to next line node */
41} Line; 41} Line;
42 42
43/* singly-linked list of lines */ 43/* singly-linked list of lines */
44typedef struct { 44typedef struct {
45 int len; /* number of Lines */ 45 int len; /* number of Lines */
46 Line *sorted; /* array fed to qsort */ 46 Line **sorted; /* array fed to qsort */
47 47
48 Line *head; /* head of List */ 48 Line *head; /* head of List */
49 Line *current /* current Line */ 49 Line *current; /* current Line */
50} List; 50} List;
51 51
52/* comparison function */
53typedef int (Compare)(const void *, const void *);
54
52 55
53/* methods ________________________________________________________________ */ 56/* methods ________________________________________________________________ */
54 57
@@ -105,10 +108,16 @@ line_release(Line *self)
105/* Comparison */ 108/* Comparison */
106 109
107static int 110static int
108compare_ascii(const void *, const void *); 111compare_ascii(const void *a, const void *b)
112{
113 return 0;
114}
109 115
110static int 116static int
111compare_numeric(const void *, const void *); 117compare_numeric(const void *a, const void *b)
118{
119 return 0;
120}
112 121
113 122
114/* List */ 123/* List */
@@ -125,7 +134,7 @@ list_init(List *self)
125} 134}
126 135
127/* for simplicity, the List gains ownership of the line */ 136/* for simplicity, the List gains ownership of the line */
128static void 137static List *
129list_insert(List *self, Line *line) 138list_insert(List *self, Line *line)
130{ 139{
131 if (line == NULL) { return NULL; } 140 if (line == NULL) { return NULL; }
@@ -137,26 +146,54 @@ list_insert(List *self, Line *line)
137 146
138 /* all subsequent insertions */ 147 /* all subsequent insertions */
139 } else { 148 } else {
140 self->current->next = line; 149 /* <?> the following cast shouldn't be necessary */
150 self->current->next = line;
141 self->current = line; 151 self->current = line;
142 } 152 }
143 self->len++; 153 self->len++;
144 return self; 154 return self;
145} 155}
146 156
147/* */ 157/* order the list according to compare() */
148static List * 158static List *
149list_sort(List *self); 159list_sort(List *self, Compare *compare)
160{
161 int i;
162 Line *line;
163
164 /* mallocate array of Line*s */
165 self->sorted = (Line **) malloc(self->len * sizeof(Line*));
166 if (self->sorted == NULL) { return NULL; }
167
168 /* fill array w/ List's contents */
169 i = 0;
170 line = self->head;
171 while (line) {
172 self->sorted[i++] = line;
173 line = line->next;
174 }
175
176 /* apply qsort */
177 qsort(self->sorted, sizeof(Line*), self->len, compare);
178 return self;
179}
150 180
151/* precondition: list must be sorted */ 181/* precondition: list must be sorted */
152static List * 182static List *
153list_writeToFile(List *self, FILE* dst) 183list_writeToFile(List *self, FILE* dst)
154{ 184{
185 int i;
186 Line **line = self->sorted;
187
155 if (self->sorted == NULL) { return NULL; } 188 if (self->sorted == NULL) { return NULL; }
189 for (i = 0; i < self->len; i++) {
190 fprintf(dst, "%s", line[i]->data);
191 }
192 return self;
156} 193}
157 194
158/* deallocate */ 195/* deallocate */
159static List * 196static void
160list_release(List *self) 197list_release(List *self)
161{ 198{
162 Line *i; 199 Line *i;
@@ -166,9 +203,8 @@ list_release(List *self)
166 while (i) { 203 while (i) {
167 die = i; 204 die = i;
168 i = die->next; 205 i = die->next;
169 line_delete(die); 206 line_release(die);
170 } 207 }
171 return self; /* bad poetry? */
172} 208}
173 209
174 210
@@ -182,8 +218,10 @@ list_release(List *self)
182int 218int
183sort_main(int argc, char **argv) 219sort_main(int argc, char **argv)
184{ 220{
185 int i; 221 int i;
186 char opt; 222 char opt;
223 List list;
224 Line *l;
187 225
188 /* default behaviour */ 226 /* default behaviour */
189 227
@@ -204,9 +242,17 @@ sort_main(int argc, char **argv)
204 } 242 }
205 } 243 }
206 244
245 /* initialize list */
246 list_init(&list);
247
207 /* go through remaining args (if any) */ 248 /* go through remaining args (if any) */
208 if (i >= argc) { 249 if (i >= argc) {
209 250 while ( (l = line_newFromFile(stdin))) {
251 list_insert(&list, l);
252 }
253 list_sort(&list, compare_ascii);
254 list_writeToFile(&list, stdout);
255 list_release(&list);
210 } else { 256 } else {
211 for ( ; i < argc; i++) { 257 for ( ; i < argc; i++) {
212 } 258 }
@@ -215,4 +261,9 @@ sort_main(int argc, char **argv)
215 exit(0); 261 exit(0);
216} 262}
217 263
218/* $Id: sort.c,v 1.3 1999/12/22 17:57:31 beppu Exp $ */ 264/* $Id: sort.c,v 1.4 1999/12/22 22:24:52 beppu Exp $ */
265/* $Log: sort.c,v $
266 * Revision 1.4 1999/12/22 22:24:52 beppu
267 * the base is nearly done.
268 * need to implement various comparison functions, now.
269 * */
diff --git a/sort.c b/sort.c
index f3f9fca1d..965152648 100644
--- a/sort.c
+++ b/sort.c
@@ -32,23 +32,26 @@ 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/* typedefs _______________________________________________________________ */
36 36
37/* line node */ 37/* line node */
38typedef struct { 38typedef struct Line {
39 char *data; /* line data */ 39 char *data; /* line data */
40 struct Line *next; /* pointer to next line node */ 40 struct Line *next; /* pointer to next line node */
41} Line; 41} Line;
42 42
43/* singly-linked list of lines */ 43/* singly-linked list of lines */
44typedef struct { 44typedef struct {
45 int len; /* number of Lines */ 45 int len; /* number of Lines */
46 Line *sorted; /* array fed to qsort */ 46 Line **sorted; /* array fed to qsort */
47 47
48 Line *head; /* head of List */ 48 Line *head; /* head of List */
49 Line *current /* current Line */ 49 Line *current; /* current Line */
50} List; 50} List;
51 51
52/* comparison function */
53typedef int (Compare)(const void *, const void *);
54
52 55
53/* methods ________________________________________________________________ */ 56/* methods ________________________________________________________________ */
54 57
@@ -105,10 +108,16 @@ line_release(Line *self)
105/* Comparison */ 108/* Comparison */
106 109
107static int 110static int
108compare_ascii(const void *, const void *); 111compare_ascii(const void *a, const void *b)
112{
113 return 0;
114}
109 115
110static int 116static int
111compare_numeric(const void *, const void *); 117compare_numeric(const void *a, const void *b)
118{
119 return 0;
120}
112 121
113 122
114/* List */ 123/* List */
@@ -125,7 +134,7 @@ list_init(List *self)
125} 134}
126 135
127/* for simplicity, the List gains ownership of the line */ 136/* for simplicity, the List gains ownership of the line */
128static void 137static List *
129list_insert(List *self, Line *line) 138list_insert(List *self, Line *line)
130{ 139{
131 if (line == NULL) { return NULL; } 140 if (line == NULL) { return NULL; }
@@ -137,26 +146,54 @@ list_insert(List *self, Line *line)
137 146
138 /* all subsequent insertions */ 147 /* all subsequent insertions */
139 } else { 148 } else {
140 self->current->next = line; 149 /* <?> the following cast shouldn't be necessary */
150 self->current->next = line;
141 self->current = line; 151 self->current = line;
142 } 152 }
143 self->len++; 153 self->len++;
144 return self; 154 return self;
145} 155}
146 156
147/* */ 157/* order the list according to compare() */
148static List * 158static List *
149list_sort(List *self); 159list_sort(List *self, Compare *compare)
160{
161 int i;
162 Line *line;
163
164 /* mallocate array of Line*s */
165 self->sorted = (Line **) malloc(self->len * sizeof(Line*));
166 if (self->sorted == NULL) { return NULL; }
167
168 /* fill array w/ List's contents */
169 i = 0;
170 line = self->head;
171 while (line) {
172 self->sorted[i++] = line;
173 line = line->next;
174 }
175
176 /* apply qsort */
177 qsort(self->sorted, sizeof(Line*), self->len, compare);
178 return self;
179}
150 180
151/* precondition: list must be sorted */ 181/* precondition: list must be sorted */
152static List * 182static List *
153list_writeToFile(List *self, FILE* dst) 183list_writeToFile(List *self, FILE* dst)
154{ 184{
185 int i;
186 Line **line = self->sorted;
187
155 if (self->sorted == NULL) { return NULL; } 188 if (self->sorted == NULL) { return NULL; }
189 for (i = 0; i < self->len; i++) {
190 fprintf(dst, "%s", line[i]->data);
191 }
192 return self;
156} 193}
157 194
158/* deallocate */ 195/* deallocate */
159static List * 196static void
160list_release(List *self) 197list_release(List *self)
161{ 198{
162 Line *i; 199 Line *i;
@@ -166,9 +203,8 @@ list_release(List *self)
166 while (i) { 203 while (i) {
167 die = i; 204 die = i;
168 i = die->next; 205 i = die->next;
169 line_delete(die); 206 line_release(die);
170 } 207 }
171 return self; /* bad poetry? */
172} 208}
173 209
174 210
@@ -182,8 +218,10 @@ list_release(List *self)
182int 218int
183sort_main(int argc, char **argv) 219sort_main(int argc, char **argv)
184{ 220{
185 int i; 221 int i;
186 char opt; 222 char opt;
223 List list;
224 Line *l;
187 225
188 /* default behaviour */ 226 /* default behaviour */
189 227
@@ -204,9 +242,17 @@ sort_main(int argc, char **argv)
204 } 242 }
205 } 243 }
206 244
245 /* initialize list */
246 list_init(&list);
247
207 /* go through remaining args (if any) */ 248 /* go through remaining args (if any) */
208 if (i >= argc) { 249 if (i >= argc) {
209 250 while ( (l = line_newFromFile(stdin))) {
251 list_insert(&list, l);
252 }
253 list_sort(&list, compare_ascii);
254 list_writeToFile(&list, stdout);
255 list_release(&list);
210 } else { 256 } else {
211 for ( ; i < argc; i++) { 257 for ( ; i < argc; i++) {
212 } 258 }
@@ -215,4 +261,9 @@ sort_main(int argc, char **argv)
215 exit(0); 261 exit(0);
216} 262}
217 263
218/* $Id: sort.c,v 1.3 1999/12/22 17:57:31 beppu Exp $ */ 264/* $Id: sort.c,v 1.4 1999/12/22 22:24:52 beppu Exp $ */
265/* $Log: sort.c,v $
266 * Revision 1.4 1999/12/22 22:24:52 beppu
267 * the base is nearly done.
268 * need to implement various comparison functions, now.
269 * */