changeset 433:9e7aaecf0683

Fix sort -uc (pointer vs pointer to pointer confusion, covered by typecast).
author Rob Landley <rob@landley.net>
date Tue, 07 Feb 2012 00:31:37 -0600
parents 01473712c9fe
children 580f4647fe2e
files toys/sort.c
diffstat 1 files changed, 11 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/toys/sort.c	Mon Feb 06 21:15:19 2012 -0600
+++ b/toys/sort.c	Tue Feb 07 00:31:37 2012 -0600
@@ -259,7 +259,7 @@
 static int compare_keys(const void *xarg, const void *yarg)
 {
     int flags = toys.optflags, retval = 0;
-    char *x, *y, **xx = (char **)xarg, **yy = (char **)yarg;
+    char *x, *y, *xx = *(char **)xarg, *yy = *(char **)yarg;
     struct sort_key *key;
 
     if (CFG_SORT_BIG) {
@@ -270,23 +270,23 @@
 
             // Chop out and modify key chunks, handling -dfib
 
-            x = get_key_data(*xx, key, flags);
-            y = get_key_data(*yy, key, flags);
+            x = get_key_data(xx, key, flags);
+            y = get_key_data(yy, key, flags);
 
             retval = compare_values(flags, x, y);
 
             // Free the copies get_key_data() made.
 
-            if (x != *xx) free(x);
-            if (y != *yy) free(y);
+            if (x != xx) free(x);
+            if (y != yy) free(y);
 
             if (retval) break;
         }
-    } else retval = compare_values(flags, *xx, *yy);
+    } else retval = compare_values(flags, xx, yy);
 
     // Perform fallback sort if necessary
     if (!retval && !(CFG_SORT_BIG && (toys.optflags&FLAG_s))) {
-        retval = strcmp(*xx, *yy);
+        retval = strcmp(xx, yy);
         flags = toys.optflags;
     }
 
@@ -308,11 +308,9 @@
         if (CFG_SORT_BIG && (toys.optflags&FLAG_c)) {
             int j = (toys.optflags&FLAG_u) ? -1 : 0;
 
-            if (TT.linecount && compare_keys((char *)TT.lines,line)>j)
+            if (TT.lines && compare_keys((char **)&TT.lines, &line)>j)
                 error_exit("%s: Check line %d\n", name, TT.linecount);
-
-            if (TT.lines) free(TT.lines);
-            else TT.linecount = 0;
+            free(TT.lines);
             TT.lines = (char **)line;
         } else {
             if (!(TT.linecount&63))
@@ -391,7 +389,7 @@
 
     // The compare (-c) logic was handled in sort_read(),
     // so if we got here, we're done.
-    if (CFG_SORT_BIG && (toys.optflags&FLAG_c)) return;
+    if (CFG_SORT_BIG && (toys.optflags&FLAG_c)) goto exit_now;
 
     // Perform the actual sort
     qsort(TT.lines, TT.linecount, sizeof(char *), compare_keys);
@@ -416,6 +414,7 @@
         xwrite(fd, "\n", 1);
     }
 
+exit_now:
     if (CFG_TOYBOX_FREE) {
       if (fd != 1) close(fd);
       free(TT.lines);