f3f9fca1d671a9b249a6c97b4f1b8165edfc85c7
[oweals/busybox.git] / coreutils / sort.c
1 /*
2  * Mini find implementation for busybox
3  *
4  *
5  * Copyright (C) 1999 by Lineo, inc.
6  * Written by John Beppu <beppu@lineo.com>
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #include "internal.h"
25 #include <sys/types.h>
26 #include <fcntl.h>
27 #include <dirent.h>
28 #include <stdio.h>
29 #include <errno.h>
30
31 static const char sort_usage[] =
32 "Usage: sort [OPTION]... [FILE]...\n\n"
33 ;
34
35 /* structs ________________________________________________________________ */
36
37 /* line node */
38 typedef struct {
39     char        *data;      /* line data */
40     struct Line *next;      /* pointer to next line node */
41 } Line;
42
43 /* singly-linked list of lines */
44 typedef struct {
45     int         len;        /* number of Lines */
46     Line        *sorted;    /* array fed to qsort */
47
48     Line        *head;      /* head of List */
49     Line        *current    /* current Line */
50 } List;
51
52
53 /* methods ________________________________________________________________ */
54
55 static const int max = 1024;
56
57 /* mallocate Line */
58 static Line *
59 line_alloc()
60 {
61     Line *self;
62     self = malloc(1 * sizeof(Line));
63     return self;
64 }
65
66 /* Initialize Line with string */
67 static Line *
68 line_init(Line *self, const char *string)
69 {
70     self->data = malloc((strlen(string) + 1) * sizeof(char));
71     if (self->data == NULL) { return NULL; }
72     strcpy(self->data, string);
73     self->next = NULL;
74     return self;
75 }
76
77 /* Construct Line from FILE* */
78 static Line *
79 line_newFromFile(FILE *src)
80 {
81     char    buffer[max];
82     Line    *self;
83
84     if (fgets(buffer, max, src)) {
85         self = line_alloc();
86         if (self == NULL) { return NULL; }
87         line_init(self, buffer);
88         return self;
89     }
90     return NULL;
91 }
92
93 /* Line destructor */
94 static Line *
95 line_release(Line *self)
96 {
97     if (self->data) { 
98         free(self->data); 
99         free(self);
100     }
101     return self;
102 }
103
104
105 /* Comparison */
106
107 static int
108 compare_ascii(const void *, const void *);
109
110 static int
111 compare_numeric(const void *, const void *);
112
113
114 /* List */
115
116 /* */
117 static List *
118 list_init(List *self)
119 {
120     self->len     = 0;
121     self->sorted  = NULL;
122     self->head    = NULL;
123     self->current = NULL;
124     return self;
125 }
126
127 /* for simplicity, the List gains ownership of the line */
128 static void
129 list_insert(List *self, Line *line)
130 {
131     if (line == NULL) { return NULL; }
132
133     /* first insertion */
134     if (self->head == NULL) {
135         self->head    = line;
136         self->current = line;
137
138     /* all subsequent insertions */
139     } else {
140         self->current->next = line;
141         self->current       = line;
142     }
143     self->len++;
144     return self;
145 }
146
147 /* */
148 static List *
149 list_sort(List *self);
150
151 /* precondition:  list must be sorted */
152 static List *
153 list_writeToFile(List *self, FILE* dst)
154 {
155     if (self->sorted == NULL) { return NULL; }
156 }
157
158 /* deallocate */
159 static List *
160 list_release(List *self)
161 {
162     Line *i;
163     Line *die;
164
165     i = self->head;
166     while (i) {
167         die = i;
168         i = die->next;
169         line_delete(die);
170     }
171     return self; /* bad poetry? */
172 }
173
174
175 /*
176  * I need a list
177  * to insert lines into
178  * then I need to sort this list
179  * and finally print it
180  */
181
182 int 
183 sort_main(int argc, char **argv)
184 {
185     int i;
186     char opt;
187
188     /* default behaviour */
189
190     /* parse argv[] */
191     for (i = 1; i < argc; i++) {
192         if (argv[i][0] == '-') {
193             opt = argv[i][1];
194             switch (opt) {
195                 case 'h':
196                     usage(sort_usage);
197                     break;
198                 default:
199                     fprintf(stderr, "sort: invalid option -- %c\n", opt);
200                     usage(sort_usage);
201             }
202         } else {
203             break;
204         }
205     }
206
207     /* go through remaining args (if any) */
208     if (i >= argc) {
209
210     } else {
211         for ( ; i < argc; i++) {
212         }
213     }
214
215     exit(0);
216 }
217
218 /* $Id: sort.c,v 1.3 1999/12/22 17:57:31 beppu Exp $ */