Better way to check for namespace aliasing.
[oweals/busybox.git] / sort.c
diff --git a/sort.c b/sort.c
index bab832f801e99ff5f1f4b2328a74f999b5ec5da8..0fe7bf99bb0ce7532ddc55669aa5e08706cbc35b 100644 (file)
--- a/sort.c
+++ b/sort.c
@@ -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);
 }
 
 
@@ -173,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;
 }
 
@@ -221,17 +244,31 @@ 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':
+                   /* what's the diff between -g && -n? */
+                   compare = compare_numeric;      
+                   break;
                case 'h':
                    usage(sort_usage);
                    break;
+               case 'n':
+                   /* what's the diff between -g && -n? */
+                   compare = compare_numeric;      
+                   break;
+               case 'r':
+                   /* reverse */
+                   break;
                default:
                    fprintf(stderr, "sort: invalid option -- %c\n", opt);
                    usage(sort_usage);
@@ -241,29 +278,35 @@ sort_main(int argc, char **argv)
        }
     }
 
-    /* initialize list */
-    list_init(&list);
+    /* this could be factored better */
 
-    /* go through remaining args (if any) */
+    /* work w/ stdin */
     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);
+
+    /* work w/ what's left in argv[] */
     } else {
+       FILE    *src;
+
        for ( ; i < argc; i++) {
+           src = fopen(argv[i], "r");
+           if (src == NULL) { break; }
+           while ( (l = line_newFromFile(src))) {
+               list_insert(&list, l);
+           }
+           fclose(src);
        }
+       list_sort(&list, compare);
+       list_writeToFile(&list, stdout);
+       list_release(&list);
     }
 
     exit(0);
 }
 
-/* $Id: sort.c,v 1.5 1999/12/22 22:27:01 beppu Exp $ */
-/* 
- * $Log: sort.c,v $
- * Revision 1.5  1999/12/22 22:27:01  beppu
- * playing w/ $Log$
- *
- */
+/* $Id: sort.c,v 1.8 1999/12/23 22:46:10 beppu Exp $ */