blob: 4f4979cc57facff7f476548e43fba6f8b9d2b01c [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
John Beppuc0ca4731999-12-21 20:00:35 +00002/*
John Beppu568cb7b1999-12-22 23:02:12 +00003 * Mini sort implementation for busybox
John Beppuc0ca4731999-12-21 20:00:35 +00004 *
5 *
Matt Kraai5e8c0ff2000-12-20 20:49:56 +00006 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
John Beppuc0ca4731999-12-21 20:00:35 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Eric Andersened3ef502001-01-27 08:24:39 +000024#include <getopt.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000025#include <string.h>
Eric Andersened3ef502001-01-27 08:24:39 +000026#include <stdlib.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000027#include "busybox.h"
John Beppuc0ca4731999-12-21 20:00:35 +000028
Eric Andersen3e6ff902001-03-09 21:24:12 +000029static int compare_ascii(const void *x, const void *y)
John Beppuc0ca4731999-12-21 20:00:35 +000030{
Matt Kraai5e8c0ff2000-12-20 20:49:56 +000031 return strcmp(*(char **)x, *(char **)y);
John Beppu38efa791999-12-22 00:30:29 +000032}
33
Eric Andersen3e6ff902001-03-09 21:24:12 +000034static int compare_numeric(const void *x, const void *y)
John Beppu38efa791999-12-22 00:30:29 +000035{
Mark Whitley3828dbe2001-04-17 17:47:33 +000036 int z = atoi(*(char **)x) - atoi(*(char **)y);
37 return z ? z : strcmp(*(char **)x, *(char **)y);
John Beppuc0ca4731999-12-21 20:00:35 +000038}
39
Erik Andersene49d5ec2000-02-08 19:58:47 +000040int sort_main(int argc, char **argv)
John Beppuc0ca4731999-12-21 20:00:35 +000041{
Matt Kraai5e8c0ff2000-12-20 20:49:56 +000042 FILE *fp;
43 char *line, **lines = NULL;
44 int i, opt, nlines = 0;
45 int (*compare)(const void *, const void *) = compare_ascii;
46#ifdef BB_FEATURE_SORT_REVERSE
47 int reverse = FALSE;
48#endif
Mark Whitleyfccaa362001-04-17 18:56:18 +000049#ifdef BB_FEATURE_SORT_UNIQUE
50 int unique = FALSE;
51#endif
John Beppuc0ca4731999-12-21 20:00:35 +000052
Mark Whitleyfccaa362001-04-17 18:56:18 +000053 while ((opt = getopt(argc, argv, "nru")) != -1) {
Matt Kraai5e8c0ff2000-12-20 20:49:56 +000054 switch (opt) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000055 case 'n':
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 compare = compare_numeric;
57 break;
Erik Andersen029011b2000-03-04 21:19:32 +000058#ifdef BB_FEATURE_SORT_REVERSE
Erik Andersene49d5ec2000-02-08 19:58:47 +000059 case 'r':
Matt Kraai5e8c0ff2000-12-20 20:49:56 +000060 reverse = TRUE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 break;
Erik Andersen029011b2000-03-04 21:19:32 +000062#endif
Mark Whitleyfccaa362001-04-17 18:56:18 +000063#ifdef BB_FEATURE_SORT_UNIQUE
64 case 'u':
65 unique = TRUE;
66 break;
67#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 default:
Eric Andersen67991cf2001-02-14 21:23:06 +000069 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000070 }
71 }
72
Matt Kraai5e8c0ff2000-12-20 20:49:56 +000073 /* read the input */
74 for (i = optind; i == optind || i < argc; i++) {
75 if (argv[i] == NULL)
76 fp = stdin;
77 else
78 fp = xfopen(argv[i], "r");
John Beppu8d369e92000-09-28 17:49:59 +000079
Matt Kraai5e8c0ff2000-12-20 20:49:56 +000080 while ((line = get_line_from_file(fp)) != NULL) {
81 lines = xrealloc(lines, sizeof(char *) * (nlines + 1));
Mark Whitley6e808ca2001-04-17 18:26:11 +000082 chomp(line);
Matt Kraai5e8c0ff2000-12-20 20:49:56 +000083 lines[nlines++] = line;
Erik Andersene49d5ec2000-02-08 19:58:47 +000084 }
John Beppuc0ca4731999-12-21 20:00:35 +000085 }
John Beppuc0ca4731999-12-21 20:00:35 +000086
Matt Kraai5e8c0ff2000-12-20 20:49:56 +000087 /* sort it */
88 qsort(lines, nlines, sizeof(char *), compare);
89
90 /* print it */
91#ifdef BB_FEATURE_SORT_REVERSE
Mark Whitleyfccaa362001-04-17 18:56:18 +000092 if (reverse) {
93 for (i = --nlines; 0 <= i; i--)
94#ifdef BB_FEATURE_SORT_UNIQUE
95 if((!unique) || (i == nlines) || (strcmp(lines[i + 1], lines[i])))
Matt Kraai5e8c0ff2000-12-20 20:49:56 +000096#endif
Mark Whitleyfccaa362001-04-17 18:56:18 +000097 puts(lines[i]);
98 } else
99#endif
100 for (i = 0; i < nlines; i++)
101#ifdef BB_FEATURE_SORT_UNIQUE
102 if((!unique) || (!i) || (strcmp(lines[i - 1], lines[i])))
103#endif
104 puts(lines[i]);
Matt Kraai5e8c0ff2000-12-20 20:49:56 +0000105 return EXIT_SUCCESS;
John Beppuc0ca4731999-12-21 20:00:35 +0000106}