Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 2 | /* |
John Beppu | 568cb7b | 1999-12-22 23:02:12 +0000 | [diff] [blame] | 3 | * Mini sort implementation for busybox |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 4 | * |
| 5 | * |
Matt Kraai | 5e8c0ff | 2000-12-20 20:49:56 +0000 | [diff] [blame] | 6 | * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu> |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 7 | * |
| 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 Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 24 | #include <getopt.h> |
Eric Andersen | eba8ed7 | 2001-03-09 14:36:42 +0000 | [diff] [blame] | 25 | #include <string.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 27 | #include "busybox.h" |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 28 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 29 | static int compare_ascii(const void *x, const void *y) |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 30 | { |
Matt Kraai | 5e8c0ff | 2000-12-20 20:49:56 +0000 | [diff] [blame] | 31 | return strcmp(*(char **)x, *(char **)y); |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 34 | static int compare_numeric(const void *x, const void *y) |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 35 | { |
Mark Whitley | 3828dbe | 2001-04-17 17:47:33 +0000 | [diff] [blame] | 36 | int z = atoi(*(char **)x) - atoi(*(char **)y); |
| 37 | return z ? z : strcmp(*(char **)x, *(char **)y); |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 40 | int sort_main(int argc, char **argv) |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 41 | { |
Matt Kraai | 5e8c0ff | 2000-12-20 20:49:56 +0000 | [diff] [blame] | 42 | 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 Whitley | fccaa36 | 2001-04-17 18:56:18 +0000 | [diff] [blame^] | 49 | #ifdef BB_FEATURE_SORT_UNIQUE |
| 50 | int unique = FALSE; |
| 51 | #endif |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 52 | |
Mark Whitley | fccaa36 | 2001-04-17 18:56:18 +0000 | [diff] [blame^] | 53 | while ((opt = getopt(argc, argv, "nru")) != -1) { |
Matt Kraai | 5e8c0ff | 2000-12-20 20:49:56 +0000 | [diff] [blame] | 54 | switch (opt) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 55 | case 'n': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | compare = compare_numeric; |
| 57 | break; |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 58 | #ifdef BB_FEATURE_SORT_REVERSE |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 59 | case 'r': |
Matt Kraai | 5e8c0ff | 2000-12-20 20:49:56 +0000 | [diff] [blame] | 60 | reverse = TRUE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 61 | break; |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 62 | #endif |
Mark Whitley | fccaa36 | 2001-04-17 18:56:18 +0000 | [diff] [blame^] | 63 | #ifdef BB_FEATURE_SORT_UNIQUE |
| 64 | case 'u': |
| 65 | unique = TRUE; |
| 66 | break; |
| 67 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 68 | default: |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 69 | show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Matt Kraai | 5e8c0ff | 2000-12-20 20:49:56 +0000 | [diff] [blame] | 73 | /* 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 Beppu | 8d369e9 | 2000-09-28 17:49:59 +0000 | [diff] [blame] | 79 | |
Matt Kraai | 5e8c0ff | 2000-12-20 20:49:56 +0000 | [diff] [blame] | 80 | while ((line = get_line_from_file(fp)) != NULL) { |
| 81 | lines = xrealloc(lines, sizeof(char *) * (nlines + 1)); |
Mark Whitley | 6e808ca | 2001-04-17 18:26:11 +0000 | [diff] [blame] | 82 | chomp(line); |
Matt Kraai | 5e8c0ff | 2000-12-20 20:49:56 +0000 | [diff] [blame] | 83 | lines[nlines++] = line; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 84 | } |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 85 | } |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 86 | |
Matt Kraai | 5e8c0ff | 2000-12-20 20:49:56 +0000 | [diff] [blame] | 87 | /* sort it */ |
| 88 | qsort(lines, nlines, sizeof(char *), compare); |
| 89 | |
| 90 | /* print it */ |
| 91 | #ifdef BB_FEATURE_SORT_REVERSE |
Mark Whitley | fccaa36 | 2001-04-17 18:56:18 +0000 | [diff] [blame^] | 92 | 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 Kraai | 5e8c0ff | 2000-12-20 20:49:56 +0000 | [diff] [blame] | 96 | #endif |
Mark Whitley | fccaa36 | 2001-04-17 18:56:18 +0000 | [diff] [blame^] | 97 | 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 Kraai | 5e8c0ff | 2000-12-20 20:49:56 +0000 | [diff] [blame] | 105 | return EXIT_SUCCESS; |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 106 | } |