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);
97 compare_ascii(const void *, const void *);
100 compare_numeric(const void *, const void *);
107 list_init(List *self)
112 self->current = NULL;
116 /* for simplicity, the List gains ownership of the line */
118 list_insert(List *self, Line *line)
120 if (line == NULL) { return NULL; }
122 /* first insertion */
123 if (self->head == NULL) {
125 self->current = line;
127 /* all subsequent insertions */
129 self->current->next = line;
130 self->current = line;
138 list_sort(List *self);
140 /* precondition: list must be sorted */
142 list_writeToFile(List *self, FILE* dst)
144 if (self->sorted == NULL) { return NULL; }
149 list_release(List *self)
157 * to insert lines into
158 * then I need to sort this list
159 * and finally print it
163 sort_main(int argc, char **argv)
168 /* default behaviour */
171 for (i = 1; i < argc; i++) {
172 if (argv[i][0] == '-') {
179 fprintf(stderr, "sort: invalid option -- %c\n", opt);
187 /* go through remaining args (if any) */
191 for ( ; i < argc; i++) {
198 /* $Id: sort.c,v 1.2 1999/12/22 00:30:29 beppu Exp $ */