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 /* structs ________________________________________________________________ */
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 */
53 /* methods ________________________________________________________________ */
55 static const int max = 1024;
62 self = malloc(1 * sizeof(Line));
66 /* Initialize Line with string */
68 line_init(Line *self, const char *string)
70 self->data = malloc((strlen(string) + 1) * sizeof(char));
71 if (self->data == NULL) { return NULL; }
72 strcpy(self->data, string);
77 /* Construct Line from FILE* */
79 line_newFromFile(FILE *src)
84 if (fgets(buffer, max, src)) {
86 if (self == NULL) { return NULL; }
87 line_init(self, buffer);
95 line_release(Line *self)
108 compare_ascii(const void *, const void *);
111 compare_numeric(const void *, const void *);
118 list_init(List *self)
123 self->current = NULL;
127 /* for simplicity, the List gains ownership of the line */
129 list_insert(List *self, Line *line)
131 if (line == NULL) { return NULL; }
133 /* first insertion */
134 if (self->head == NULL) {
136 self->current = line;
138 /* all subsequent insertions */
140 self->current->next = line;
141 self->current = line;
149 list_sort(List *self);
151 /* precondition: list must be sorted */
153 list_writeToFile(List *self, FILE* dst)
155 if (self->sorted == NULL) { return NULL; }
160 list_release(List *self)
171 return self; /* bad poetry? */
177 * to insert lines into
178 * then I need to sort this list
179 * and finally print it
183 sort_main(int argc, char **argv)
188 /* default behaviour */
191 for (i = 1; i < argc; i++) {
192 if (argv[i][0] == '-') {
199 fprintf(stderr, "sort: invalid option -- %c\n", opt);
207 /* go through remaining args (if any) */
211 for ( ; i < argc; i++) {
218 /* $Id: sort.c,v 1.3 1999/12/22 17:57:31 beppu Exp $ */