aboutsummaryrefslogtreecommitdiff
path: root/coreutils/sort.c
diff options
context:
space:
mode:
authorJohn Beppu <beppu@lbox.org>1999-12-23 00:02:49 +0000
committerJohn Beppu <beppu@lbox.org>1999-12-23 00:02:49 +0000
commitee512a3f8620ab535716b8aa016b33610010920c (patch)
treef50f631422c487b83c6d95c39275844086ff9715 /coreutils/sort.c
parent568cb7b45f8ec1e0f6e6cad296c5431e66785a6b (diff)
downloadbusybox-w32-ee512a3f8620ab535716b8aa016b33610010920c.tar.gz
busybox-w32-ee512a3f8620ab535716b8aa016b33610010920c.tar.bz2
busybox-w32-ee512a3f8620ab535716b8aa016b33610010920c.zip
implemented numeric sort (sort -g)
Diffstat (limited to 'coreutils/sort.c')
-rw-r--r--coreutils/sort.c39
1 files changed, 30 insertions, 9 deletions
diff --git a/coreutils/sort.c b/coreutils/sort.c
index e5f229648..127d68319 100644
--- a/coreutils/sort.c
+++ b/coreutils/sort.c
@@ -115,9 +115,9 @@ compare_ascii(const void *a, const void *b)
115 Line *x, *y; 115 Line *x, *y;
116 116
117 doh = (Line **) a; 117 doh = (Line **) a;
118 x = (Line *) *doh; 118 x = *doh;
119 doh = (Line **) b; 119 doh = (Line **) b;
120 y = (Line *) *doh; 120 y = *doh;
121 121
122 // fprintf(stdout, "> %p: %s< %p: %s", x, x->data, y, y->data); 122 // fprintf(stdout, "> %p: %s< %p: %s", x, x->data, y, y->data);
123 return strcmp(x->data, y->data); 123 return strcmp(x->data, y->data);
@@ -127,7 +127,19 @@ compare_ascii(const void *a, const void *b)
127static int 127static int
128compare_numeric(const void *a, const void *b) 128compare_numeric(const void *a, const void *b)
129{ 129{
130 return 0; 130 Line **doh;
131 Line *x, *y;
132 int xint, yint;
133
134 doh = (Line **) a;
135 x = *doh;
136 doh = (Line **) b;
137 y = *doh;
138
139 xint = strtoul(x->data, NULL, 10);
140 yint = strtoul(y->data, NULL, 10);
141
142 return (xint - yint);
131} 143}
132 144
133 145
@@ -232,14 +244,20 @@ sort_main(int argc, char **argv)
232 char opt; 244 char opt;
233 List list; 245 List list;
234 Line *l; 246 Line *l;
247 Compare *compare;
235 248
236 /* default behaviour */ 249 /* init */
250 compare = compare_ascii;
251 list_init(&list);
237 252
238 /* parse argv[] */ 253 /* parse argv[] */
239 for (i = 1; i < argc; i++) { 254 for (i = 1; i < argc; i++) {
240 if (argv[i][0] == '-') { 255 if (argv[i][0] == '-') {
241 opt = argv[i][1]; 256 opt = argv[i][1];
242 switch (opt) { 257 switch (opt) {
258 case 'g':
259 compare = compare_numeric;
260 break;
243 case 'h': 261 case 'h':
244 usage(sort_usage); 262 usage(sort_usage);
245 break; 263 break;
@@ -252,15 +270,12 @@ sort_main(int argc, char **argv)
252 } 270 }
253 } 271 }
254 272
255 /* initialize list */
256 list_init(&list);
257
258 /* go through remaining args (if any) */ 273 /* go through remaining args (if any) */
259 if (i >= argc) { 274 if (i >= argc) {
260 while ( (l = line_newFromFile(stdin))) { 275 while ( (l = line_newFromFile(stdin))) {
261 list_insert(&list, l); 276 list_insert(&list, l);
262 } 277 }
263 list_sort(&list, compare_ascii); 278 list_sort(&list, compare);
264 list_writeToFile(&list, stdout); 279 list_writeToFile(&list, stdout);
265 list_release(&list); 280 list_release(&list);
266 } else { 281 } else {
@@ -271,15 +286,21 @@ sort_main(int argc, char **argv)
271 exit(0); 286 exit(0);
272} 287}
273 288
274/* $Id: sort.c,v 1.6 1999/12/22 23:02:12 beppu Exp $ */ 289/* $Id: sort.c,v 1.7 1999/12/23 00:02:49 beppu Exp $ */
275/* 290/*
276 * $Log: sort.c,v $ 291 * $Log: sort.c,v $
292 * Revision 1.7 1999/12/23 00:02:49 beppu
293 * implemented numeric sort (sort -g)
294 *
277 * Revision 1.6 1999/12/22 23:02:12 beppu 295 * Revision 1.6 1999/12/22 23:02:12 beppu
278 * oops.. qsort(2) misunderstanding on my part. 296 * oops.. qsort(2) misunderstanding on my part.
279 * it's ok, now. 297 * it's ok, now.
280 * 298 *
281 * Revision 1.5 1999/12/22 22:27:01 beppu 299 * Revision 1.5 1999/12/22 22:27:01 beppu
282 * playing w/ $Log: sort.c,v $ 300 * playing w/ $Log: sort.c,v $
301 * playing w/ Revision 1.7 1999/12/23 00:02:49 beppu
302 * playing w/ implemented numeric sort (sort -g)
303 * playing w/
283 * playing w/ Revision 1.6 1999/12/22 23:02:12 beppu 304 * playing w/ Revision 1.6 1999/12/22 23:02:12 beppu
284 * playing w/ oops.. qsort(2) misunderstanding on my part. 305 * playing w/ oops.. qsort(2) misunderstanding on my part.
285 * playing w/ it's ok, now. 306 * playing w/ it's ok, now.