X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=coreutils%2Fsort.c;h=4f4979cc57facff7f476548e43fba6f8b9d2b01c;hb=fccaa3629b89bcfcd2d9b4126255cd31e0f5e174;hp=9707efa51c5f65f6e010e4ff9379f36f7407ce8e;hpb=3e6ff9017f5aa4ea41de2520a3f6d29fb20e0332;p=oweals%2Fbusybox.git diff --git a/coreutils/sort.c b/coreutils/sort.c index 9707efa51..4f4979cc5 100644 --- a/coreutils/sort.c +++ b/coreutils/sort.c @@ -33,7 +33,8 @@ static int compare_ascii(const void *x, const void *y) static int compare_numeric(const void *x, const void *y) { - return atoi(*(char **)x) - atoi(*(char **)y); + int z = atoi(*(char **)x) - atoi(*(char **)y); + return z ? z : strcmp(*(char **)x, *(char **)y); } int sort_main(int argc, char **argv) @@ -45,8 +46,11 @@ int sort_main(int argc, char **argv) #ifdef BB_FEATURE_SORT_REVERSE int reverse = FALSE; #endif +#ifdef BB_FEATURE_SORT_UNIQUE + int unique = FALSE; +#endif - while ((opt = getopt(argc, argv, "nr")) != -1) { + while ((opt = getopt(argc, argv, "nru")) != -1) { switch (opt) { case 'n': compare = compare_numeric; @@ -55,6 +59,11 @@ int sort_main(int argc, char **argv) case 'r': reverse = TRUE; break; +#endif +#ifdef BB_FEATURE_SORT_UNIQUE + case 'u': + unique = TRUE; + break; #endif default: show_usage(); @@ -70,6 +79,7 @@ int sort_main(int argc, char **argv) while ((line = get_line_from_file(fp)) != NULL) { lines = xrealloc(lines, sizeof(char *) * (nlines + 1)); + chomp(line); lines[nlines++] = line; } } @@ -79,12 +89,18 @@ int sort_main(int argc, char **argv) /* print it */ #ifdef BB_FEATURE_SORT_REVERSE - if (reverse) - for (i = nlines - 1; 0 <= i; i--) - fputs(lines[i], stdout); - else + if (reverse) { + for (i = --nlines; 0 <= i; i--) +#ifdef BB_FEATURE_SORT_UNIQUE + if((!unique) || (i == nlines) || (strcmp(lines[i + 1], lines[i]))) +#endif + puts(lines[i]); + } else +#endif + for (i = 0; i < nlines; i++) +#ifdef BB_FEATURE_SORT_UNIQUE + if((!unique) || (!i) || (strcmp(lines[i - 1], lines[i]))) #endif - for (i = 0; i < nlines; i++) - fputs(lines[i], stdout); + puts(lines[i]); return EXIT_SUCCESS; }