1 /* vi: set sw=4 ts=4: */
3 * Mini sort implementation for busybox
6 * Copyright (C) 1999,2000 by Lineo, inc.
7 * Written by John Beppu <beppu@lineo.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/types.h>
32 static const char sort_usage[] = "sort [-n]"
33 #ifdef BB_FEATURE_SORT_REVERSE
38 #ifdef BB_FEATURE_SORT_REVERSE
39 #define APPLY_REVERSE(x) (reverse ? -(x) : (x))
40 static int reverse = 0;
42 #define APPLY_REVERSE(x) (x)
45 /* typedefs _______________________________________________________________ */
49 char *data; /* line data */
50 struct Line *next; /* pointer to next line node */
53 /* singly-linked list of lines */
55 int len; /* number of Lines */
56 Line **sorted; /* array fed to qsort */
58 Line *head; /* head of List */
59 Line *current; /* current Line */
62 /* comparison function */
63 typedef int (Compare) (const void *, const void *);
66 /* methods ________________________________________________________________ */
68 static const int max = 1024;
71 static Line *line_alloc()
75 self = malloc(1 * sizeof(Line));
79 /* Initialize Line with string */
80 static Line *line_init(Line * self, const char *string)
82 self->data = malloc((strlen(string) + 1) * sizeof(char));
84 if (self->data == NULL) {
87 strcpy(self->data, string);
92 /* Construct Line from FILE* */
93 static Line *line_newFromFile(FILE * src)
98 if (fgets(buffer, max, src)) {
103 line_init(self, buffer);
109 /* Line destructor */
110 static Line *line_release(Line * self)
123 static int compare_ascii(const void *a, const void *b)
133 // fprintf(stdout, "> %p: %s< %p: %s", x, x->data, y, y->data);
134 return APPLY_REVERSE(strcmp(x->data, y->data));
138 static int compare_numeric(const void *a, const void *b)
149 xint = strtoul(x->data, NULL, 10);
150 yint = strtoul(y->data, NULL, 10);
152 return APPLY_REVERSE(xint - yint);
159 static List *list_init(List * self)
164 self->current = NULL;
168 /* for simplicity, the List gains ownership of the line */
169 static List *list_insert(List * self, Line * line)
175 /* first insertion */
176 if (self->head == NULL) {
178 self->current = line;
180 /* all subsequent insertions */
182 self->current->next = line;
183 self->current = line;
189 /* order the list according to compare() */
190 static List *list_sort(List * self, Compare * compare)
195 /* mallocate array of Line*s */
196 self->sorted = (Line **) malloc(self->len * sizeof(Line *));
197 if (self->sorted == NULL) {
201 /* fill array w/ List's contents */
205 self->sorted[i++] = line;
210 qsort(self->sorted, self->len, sizeof(Line *), compare);
214 /* precondition: list must be sorted */
215 static List *list_writeToFile(List * self, FILE * dst)
218 Line **line = self->sorted;
220 if (self->sorted == NULL) {
223 for (i = 0; i < self->len; i++) {
224 fprintf(dst, "%s", line[i]->data);
230 static void list_release(List * self)
246 * to insert lines into
247 * then I need to sort this list
248 * and finally print it
251 int sort_main(int argc, char **argv)
260 compare = compare_ascii;
264 for (i = 1; i < argc; i++) {
265 if (argv[i][0] == '-') {
272 /* numeric comparison */
273 compare = compare_numeric;
275 #ifdef BB_FEATURE_SORT_REVERSE
282 fprintf(stderr, "sort: invalid option -- %c\n", opt);
290 /* this could be factored better */
294 while ((l = line_newFromFile(stdin))) {
295 list_insert(&list, l);
297 list_sort(&list, compare);
298 list_writeToFile(&list, stdout);
301 /* work w/ what's left in argv[] */
305 for (; i < argc; i++) {
306 src = fopen(argv[i], "r");
310 while ((l = line_newFromFile(src))) {
311 list_insert(&list, l);
315 list_sort(&list, compare);
316 list_writeToFile(&list, stdout);
323 /* $Id: sort.c,v 1.13 2000/04/13 01:18:56 erik Exp $ */