aboutsummaryrefslogtreecommitdiff
path: root/sort.c
diff options
context:
space:
mode:
Diffstat (limited to 'sort.c')
-rw-r--r--sort.c28
1 files changed, 19 insertions, 9 deletions
diff --git a/sort.c b/sort.c
index 609c5e08c..e6894f6c3 100644
--- a/sort.c
+++ b/sort.c
@@ -29,7 +29,18 @@
29#include <stdio.h> 29#include <stdio.h>
30#include <errno.h> 30#include <errno.h>
31 31
32static const char sort_usage[] = "sort [OPTION]... [FILE]...\n\n"; 32static const char sort_usage[] = "sort [-n]"
33#ifdef BB_FEATURE_SORT_REVERSE
34" [-r]"
35#endif
36" [FILE]...\n\n";
37
38#ifdef BB_FEATURE_SORT_REVERSE
39#define APPLY_REVERSE(x) (reverse ? -(x) : (x))
40static int reverse = 0;
41#else
42#define APPLY_REVERSE(x) (x)
43#endif
33 44
34/* typedefs _______________________________________________________________ */ 45/* typedefs _______________________________________________________________ */
35 46
@@ -120,7 +131,7 @@ static int compare_ascii(const void *a, const void *b)
120 y = *doh; 131 y = *doh;
121 132
122 // fprintf(stdout, "> %p: %s< %p: %s", x, x->data, y, y->data); 133 // fprintf(stdout, "> %p: %s< %p: %s", x, x->data, y, y->data);
123 return strcmp(x->data, y->data); 134 return APPLY_REVERSE(strcmp(x->data, y->data));
124} 135}
125 136
126/* numeric order */ 137/* numeric order */
@@ -138,7 +149,7 @@ static int compare_numeric(const void *a, const void *b)
138 xint = strtoul(x->data, NULL, 10); 149 xint = strtoul(x->data, NULL, 10);
139 yint = strtoul(y->data, NULL, 10); 150 yint = strtoul(y->data, NULL, 10);
140 151
141 return (xint - yint); 152 return APPLY_REVERSE(xint - yint);
142} 153}
143 154
144 155
@@ -254,20 +265,19 @@ int sort_main(int argc, char **argv)
254 if (argv[i][0] == '-') { 265 if (argv[i][0] == '-') {
255 opt = argv[i][1]; 266 opt = argv[i][1];
256 switch (opt) { 267 switch (opt) {
257 case 'g':
258 /* what's the diff between -g && -n? */
259 compare = compare_numeric;
260 break;
261 case 'h': 268 case 'h':
262 usage(sort_usage); 269 usage(sort_usage);
263 break; 270 break;
264 case 'n': 271 case 'n':
265 /* what's the diff between -g && -n? */ 272 /* numeric comparison */
266 compare = compare_numeric; 273 compare = compare_numeric;
267 break; 274 break;
275#ifdef BB_FEATURE_SORT_REVERSE
268 case 'r': 276 case 'r':
269 /* reverse */ 277 /* reverse */
278 reverse = 1;
270 break; 279 break;
280#endif
271 default: 281 default:
272 fprintf(stderr, "sort: invalid option -- %c\n", opt); 282 fprintf(stderr, "sort: invalid option -- %c\n", opt);
273 usage(sort_usage); 283 usage(sort_usage);
@@ -310,4 +320,4 @@ int sort_main(int argc, char **argv)
310 exit(0); 320 exit(0);
311} 321}
312 322
313/* $Id: sort.c,v 1.11 2000/02/08 19:58:47 erik Exp $ */ 323/* $Id: sort.c,v 1.12 2000/03/04 21:19:32 erik Exp $ */