implemented numeric sort (sort -g)
[oweals/busybox.git] / coreutils / sort.c
index 9651526483987f06ac7725a28df1c881da4b9418..127d683198039470a1623aeff360a8924d8b5476 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Mini find implementation for busybox
+ * Mini sort implementation for busybox
  *
  *
  * Copyright (C) 1999 by Lineo, inc.
@@ -107,16 +107,39 @@ line_release(Line *self)
 
 /* Comparison */
 
+/* ascii order */
 static int
 compare_ascii(const void *a, const void *b)
 {
-    return 0;
+    Line **doh;
+    Line *x, *y;
+
+    doh = (Line **) a;
+    x   = *doh;
+    doh = (Line **) b;
+    y   = *doh;
+
+    // fprintf(stdout, "> %p: %s< %p: %s", x, x->data, y, y->data);
+    return strcmp(x->data, y->data);
 }
 
+/* numeric order */
 static int
 compare_numeric(const void *a, const void *b)
 {
-    return 0;
+    Line    **doh;
+    Line    *x, *y;
+    int            xint, yint;
+
+    doh  = (Line **) a;
+    x    = *doh;
+    doh  = (Line **) b;
+    y    = *doh;
+
+    xint = strtoul(x->data, NULL, 10);
+    yint = strtoul(y->data, NULL, 10);
+
+    return (xint - yint);
 }
 
 
@@ -146,7 +169,6 @@ list_insert(List *self, Line *line)
 
     /* all subsequent insertions */
     } else {
-       /* <?> the following cast shouldn't be necessary */
        self->current->next = line; 
        self->current       = line;
     }
@@ -174,7 +196,7 @@ list_sort(List *self, Compare *compare)
     }
 
     /* apply qsort */
-    qsort(self->sorted, sizeof(Line*), self->len, compare);
+    qsort(self->sorted, self->len, sizeof(Line*), compare);
     return self;
 }
 
@@ -222,14 +244,20 @@ sort_main(int argc, char **argv)
     char    opt;
     List    list;
     Line    *l;
+    Compare *compare;
 
-    /* default behaviour */
+    /* init */
+    compare = compare_ascii;
+    list_init(&list);
 
     /* parse argv[] */
     for (i = 1; i < argc; i++) {
        if (argv[i][0] == '-') {
            opt = argv[i][1];
            switch (opt) {
+               case 'g':
+                   compare = compare_numeric;
+                   break;
                case 'h':
                    usage(sort_usage);
                    break;
@@ -242,15 +270,12 @@ sort_main(int argc, char **argv)
        }
     }
 
-    /* initialize list */
-    list_init(&list);
-
     /* go through remaining args (if any) */
     if (i >= argc) {
        while ( (l = line_newFromFile(stdin))) {
            list_insert(&list, l);
        }
-       list_sort(&list, compare_ascii);
+       list_sort(&list, compare);
        list_writeToFile(&list, stdout);
        list_release(&list);
     } else {
@@ -261,9 +286,24 @@ sort_main(int argc, char **argv)
     exit(0);
 }
 
-/* $Id: sort.c,v 1.4 1999/12/22 22:24:52 beppu Exp $ */
-/* $Log: sort.c,v $
- * Revision 1.4  1999/12/22 22:24:52  beppu
- *     the base is nearly done.
- *     need to implement various comparison functions, now.
- * */
+/* $Id: sort.c,v 1.7 1999/12/23 00:02:49 beppu Exp $ */
+/* 
+ * $Log: sort.c,v $
+ * Revision 1.7  1999/12/23 00:02:49  beppu
+ *     implemented numeric sort (sort -g)
+ *
+ * Revision 1.6  1999/12/22 23:02:12  beppu
+ *     oops..  qsort(2) misunderstanding on my part.
+ *     it's ok, now.
+ *
+ * Revision 1.5  1999/12/22 22:27:01  beppu
+ * playing w/ $Log: sort.c,v $
+ * playing w/ Revision 1.7  1999/12/23 00:02:49  beppu
+ * playing w/  implemented numeric sort (sort -g)
+ * playing w/
+ * playing w/ Revision 1.6  1999/12/22 23:02:12  beppu
+ * playing w/  oops..  qsort(2) misunderstanding on my part.
+ * playing w/  it's ok, now.
+ * playing w/
+ *
+ */