2 * Mini sort implementation for busybox
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by John Beppu <beppu@lineo.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
31 static const char sort_usage[] =
32 "Usage: sort [OPTION]... [FILE]...\n\n"
35 /* typedefs _______________________________________________________________ */
39 char *data; /* line data */
40 struct Line *next; /* pointer to next line node */
43 /* singly-linked list of lines */
45 int len; /* number of Lines */
46 Line **sorted; /* array fed to qsort */
48 Line *head; /* head of List */
49 Line *current; /* current Line */
52 /* comparison function */
53 typedef int (Compare)(const void *, const void *);
56 /* methods ________________________________________________________________ */
58 static const int max = 1024;
65 self = malloc(1 * sizeof(Line));
69 /* Initialize Line with string */
71 line_init(Line *self, const char *string)
73 self->data = malloc((strlen(string) + 1) * sizeof(char));
74 if (self->data == NULL) { return NULL; }
75 strcpy(self->data, string);
80 /* Construct Line from FILE* */
82 line_newFromFile(FILE *src)
87 if (fgets(buffer, max, src)) {
89 if (self == NULL) { return NULL; }
90 line_init(self, buffer);
98 line_release(Line *self)
112 compare_ascii(const void *a, const void *b)
122 // fprintf(stdout, "> %p: %s< %p: %s", x, x->data, y, y->data);
123 return strcmp(x->data, y->data);
128 compare_numeric(const void *a, const void *b)
138 list_init(List *self)
143 self->current = NULL;
147 /* for simplicity, the List gains ownership of the line */
149 list_insert(List *self, Line *line)
151 if (line == NULL) { return NULL; }
153 /* first insertion */
154 if (self->head == NULL) {
156 self->current = line;
158 /* all subsequent insertions */
160 self->current->next = line;
161 self->current = line;
167 /* order the list according to compare() */
169 list_sort(List *self, Compare *compare)
174 /* mallocate array of Line*s */
175 self->sorted = (Line **) malloc(self->len * sizeof(Line*));
176 if (self->sorted == NULL) { return NULL; }
178 /* fill array w/ List's contents */
182 self->sorted[i++] = line;
187 qsort(self->sorted, self->len, sizeof(Line*), compare);
191 /* precondition: list must be sorted */
193 list_writeToFile(List *self, FILE* dst)
196 Line **line = self->sorted;
198 if (self->sorted == NULL) { return NULL; }
199 for (i = 0; i < self->len; i++) {
200 fprintf(dst, "%s", line[i]->data);
207 list_release(List *self)
223 * to insert lines into
224 * then I need to sort this list
225 * and finally print it
229 sort_main(int argc, char **argv)
236 /* default behaviour */
239 for (i = 1; i < argc; i++) {
240 if (argv[i][0] == '-') {
247 fprintf(stderr, "sort: invalid option -- %c\n", opt);
255 /* initialize list */
258 /* go through remaining args (if any) */
260 while ( (l = line_newFromFile(stdin))) {
261 list_insert(&list, l);
263 list_sort(&list, compare_ascii);
264 list_writeToFile(&list, stdout);
267 for ( ; i < argc; i++) {
274 /* $Id: sort.c,v 1.6 1999/12/22 23:02:12 beppu Exp $ */
277 * Revision 1.6 1999/12/22 23:02:12 beppu
278 * oops.. qsort(2) misunderstanding on my part.
281 * Revision 1.5 1999/12/22 22:27:01 beppu
282 * playing w/ $Log: sort.c,v $
283 * playing w/ Revision 1.6 1999/12/22 23:02:12 beppu
284 * playing w/ oops.. qsort(2) misunderstanding on my part.
285 * playing w/ it's ok, now.