2 * Mini find 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)
111 compare_ascii(const void *a, const void *b)
117 compare_numeric(const void *a, const void *b)
127 list_init(List *self)
132 self->current = NULL;
136 /* for simplicity, the List gains ownership of the line */
138 list_insert(List *self, Line *line)
140 if (line == NULL) { return NULL; }
142 /* first insertion */
143 if (self->head == NULL) {
145 self->current = line;
147 /* all subsequent insertions */
149 /* <?> the following cast shouldn't be necessary */
150 self->current->next = line;
151 self->current = line;
157 /* order the list according to compare() */
159 list_sort(List *self, Compare *compare)
164 /* mallocate array of Line*s */
165 self->sorted = (Line **) malloc(self->len * sizeof(Line*));
166 if (self->sorted == NULL) { return NULL; }
168 /* fill array w/ List's contents */
172 self->sorted[i++] = line;
177 qsort(self->sorted, sizeof(Line*), self->len, compare);
181 /* precondition: list must be sorted */
183 list_writeToFile(List *self, FILE* dst)
186 Line **line = self->sorted;
188 if (self->sorted == NULL) { return NULL; }
189 for (i = 0; i < self->len; i++) {
190 fprintf(dst, "%s", line[i]->data);
197 list_release(List *self)
213 * to insert lines into
214 * then I need to sort this list
215 * and finally print it
219 sort_main(int argc, char **argv)
226 /* default behaviour */
229 for (i = 1; i < argc; i++) {
230 if (argv[i][0] == '-') {
237 fprintf(stderr, "sort: invalid option -- %c\n", opt);
245 /* initialize list */
248 /* go through remaining args (if any) */
250 while ( (l = line_newFromFile(stdin))) {
251 list_insert(&list, l);
253 list_sort(&list, compare_ascii);
254 list_writeToFile(&list, stdout);
257 for ( ; i < argc; i++) {
264 /* $Id: sort.c,v 1.4 1999/12/22 22:24:52 beppu Exp $ */
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.