work in progress...
[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
94 /* Comparison */
95
96 static int
97 compare_ascii(const void *, const void *);
98
99 static int
100 compare_numeric(const void *, const void *);
101
102
103 /* List */
104
105 /* */
106 static List *
107 list_init(List *self)
108 {
109     self->len     = 0;
110     self->sorted  = NULL;
111     self->head    = NULL;
112     self->current = NULL;
113     return self;
114 }
115
116 /* for simplicity, the List gains ownership of the line */
117 static void
118 list_insert(List *self, Line *line)
119 {
120     if (line == NULL) { return NULL; }
121
122     /* first insertion */
123     if (self->head == NULL) {
124         self->head    = line;
125         self->current = line;
126
127     /* all subsequent insertions */
128     } else {
129         self->current->next = line;
130         self->current       = line;
131     }
132     self->len++;
133     return self;
134 }
135
136 /* */
137 static List *
138 list_sort(List *self);
139
140 /* precondition:  list must be sorted */
141 static List *
142 list_writeToFile(List *self, FILE* dst)
143 {
144     if (self->sorted == NULL) { return NULL; }
145 }
146
147 /* deallocate */
148 static List *
149 list_release(List *self)
150 {
151     return self;
152 }
153
154
155 /*
156  * I need a list
157  * to insert lines into
158  * then I need to sort this list
159  * and finally print it
160  */
161
162 int 
163 sort_main(int argc, char **argv)
164 {
165     int i;
166     char opt;
167
168     /* default behaviour */
169
170     /* parse argv[] */
171     for (i = 1; i < argc; i++) {
172         if (argv[i][0] == '-') {
173             opt = argv[i][1];
174             switch (opt) {
175                 case 'h':
176                     usage(sort_usage);
177                     break;
178                 default:
179                     fprintf(stderr, "sort: invalid option -- %c\n", opt);
180                     usage(sort_usage);
181             }
182         } else {
183             break;
184         }
185     }
186
187     /* go through remaining args (if any) */
188     if (i >= argc) {
189
190     } else {
191         for ( ; i < argc; i++) {
192         }
193     }
194
195     exit(0);
196 }
197
198 /* $Id: sort.c,v 1.2 1999/12/22 00:30:29 beppu Exp $ */