aboutsummaryrefslogtreecommitdiff
path: root/coreutils/sort.c
diff options
context:
space:
mode:
Diffstat (limited to 'coreutils/sort.c')
-rw-r--r--coreutils/sort.c98
1 files changed, 46 insertions, 52 deletions
diff --git a/coreutils/sort.c b/coreutils/sort.c
index fc12dfb01..8cc4d8886 100644
--- a/coreutils/sort.c
+++ b/coreutils/sort.c
@@ -2,7 +2,6 @@
2/* 2/*
3 * Mini sort implementation for busybox 3 * Mini sort implementation for busybox
4 * 4 *
5 *
6 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu> 5 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
7 * 6 *
8 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
@@ -21,10 +20,20 @@
21 * 20 *
22 */ 21 */
23 22
24#include <getopt.h> 23/* BB_AUDIT SUSv3 _NOT_ compliant -- a number of options are not supported. */
25#include <string.h> 24/* http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html */
25
26/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
27 *
28 * Now does proper error checking on i/o. Plus some space savings.
29 */
30
31#include <stdio.h>
26#include <stdlib.h> 32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
27#include "busybox.h" 35#include "busybox.h"
36#include "libcoreutils/coreutils.h"
28 37
29static int compare_ascii(const void *x, const void *y) 38static int compare_ascii(const void *x, const void *y)
30{ 39{
@@ -41,66 +50,51 @@ int sort_main(int argc, char **argv)
41{ 50{
42 FILE *fp; 51 FILE *fp;
43 char *line, **lines = NULL; 52 char *line, **lines = NULL;
44 int i, opt, nlines = 0; 53 int i, nlines = 0, inc;
45 int (*compare)(const void *, const void *) = compare_ascii; 54 int (*compare)(const void *, const void *) = compare_ascii;
46#ifdef CONFIG_FEATURE_SORT_REVERSE
47 int reverse = FALSE;
48#endif
49#ifdef CONFIG_FEATURE_SORT_UNIQUE
50 int unique = FALSE;
51#endif
52 55
53 while ((opt = getopt(argc, argv, "nru")) != -1) { 56 int flags;
54 switch (opt) { 57
55 case 'n': 58 bb_default_error_retval = 2;
56 compare = compare_numeric; 59
57 break; 60 flags = bb_getopt_ulflags(argc, argv, "nru");
58#ifdef CONFIG_FEATURE_SORT_REVERSE 61 if (flags & 1) {
59 case 'r': 62 compare = compare_numeric;
60 reverse = TRUE;
61 break;
62#endif
63#ifdef CONFIG_FEATURE_SORT_UNIQUE
64 case 'u':
65 unique = TRUE;
66 break;
67#endif
68 default:
69 show_usage();
70 }
71 } 63 }
72 64
73 /* read the input */ 65 argv += optind;
74 for (i = optind; i == optind || i < argc; i++) { 66 if (!*argv) {
75 if (argv[i] == NULL) 67 *--argv = "-";
76 fp = stdin; 68 }
77 else
78 fp = xfopen(argv[i], "r");
79 69
80 while ((line = get_line_from_file(fp)) != NULL) { 70 do {
71 fp = xgetoptfile_sort_uniq(argv, "r");
72 while ((line = bb_get_chomped_line_from_file(fp)) != NULL) {
81 lines = xrealloc(lines, sizeof(char *) * (nlines + 1)); 73 lines = xrealloc(lines, sizeof(char *) * (nlines + 1));
82 chomp(line);
83 lines[nlines++] = line; 74 lines[nlines++] = line;
84 } 75 }
85 } 76 bb_xferror(fp, *argv);
77 bb_fclose_nonstdin(fp);
78 } while (*++argv);
86 79
87 /* sort it */ 80 /* sort it */
88 qsort(lines, nlines, sizeof(char *), compare); 81 qsort(lines, nlines, sizeof(char *), compare);
89 82
90 /* print it */ 83 /* print it */
91#ifdef CONFIG_FEATURE_SORT_REVERSE 84 i = 0;
92 if (reverse) { 85 --nlines;
93 for (i = --nlines; 0 <= i; i--) 86 if ((inc = 1 - (flags & 2)) < 0) { /* reverse */
94#ifdef CONFIG_FEATURE_SORT_UNIQUE 87 i = nlines;
95 if((!unique) || (i == nlines) || (strcmp(lines[i + 1], lines[i]))) 88 }
96#endif 89 flags &= 4;
97 puts(lines[i]); 90
98 } else 91 while (nlines >= 0) {
99#endif 92 if (!flags || !nlines || strcmp(lines[i+inc], lines[i])) {
100 for (i = 0; i < nlines; i++) 93 puts(lines[i]);
101#ifdef CONFIG_FEATURE_SORT_UNIQUE 94 }
102 if((!unique) || (!i) || (strcmp(lines[i - 1], lines[i]))) 95 i += inc;
103#endif 96 --nlines;
104 puts(lines[i]); 97 }
105 return EXIT_SUCCESS; 98
99 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
106} 100}