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 | * |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999,2000 by Lineo, inc. |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 7 | * Written by John Beppu <beppu@lineo.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "internal.h" |
| 26 | #include <sys/types.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <dirent.h> |
| 29 | #include <stdio.h> |
| 30 | #include <errno.h> |
| 31 | |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 32 | static const char sort_usage[] = "sort [-n]" |
| 33 | #ifdef BB_FEATURE_SORT_REVERSE |
| 34 | " [-r]" |
| 35 | #endif |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 36 | " [FILE]...\n" |
| 37 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 38 | "\nSorts lines of text in the specified files\n" |
| 39 | #endif |
| 40 | ; |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 41 | |
| 42 | #ifdef BB_FEATURE_SORT_REVERSE |
| 43 | #define APPLY_REVERSE(x) (reverse ? -(x) : (x)) |
| 44 | static int reverse = 0; |
| 45 | #else |
| 46 | #define APPLY_REVERSE(x) (x) |
| 47 | #endif |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 48 | |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 49 | /* typedefs _______________________________________________________________ */ |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 50 | |
| 51 | /* line node */ |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 52 | typedef struct Line { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 53 | char *data; /* line data */ |
| 54 | struct Line *next; /* pointer to next line node */ |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 55 | } Line; |
| 56 | |
| 57 | /* singly-linked list of lines */ |
| 58 | typedef struct { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 59 | int len; /* number of Lines */ |
| 60 | Line **sorted; /* array fed to qsort */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 61 | Line *head; /* head of List */ |
| 62 | Line *current; /* current Line */ |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 63 | } List; |
| 64 | |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 65 | /* comparison function */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 66 | typedef int (Compare) (const void *, const void *); |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 67 | |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 68 | |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 69 | /* methods ________________________________________________________________ */ |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 70 | |
| 71 | static const int max = 1024; |
| 72 | |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 73 | /* mallocate Line */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | static Line *line_alloc() |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 75 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 76 | Line *self; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 77 | self = malloc(1 * sizeof(Line)); |
| 78 | return self; |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 79 | } |
| 80 | |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 81 | /* Construct Line from FILE* */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 82 | static Line *line_newFromFile(FILE * src) |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 83 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 84 | Line *self; |
John Beppu | 5a728cf | 2000-04-17 04:22:09 +0000 | [diff] [blame] | 85 | char *cstring = NULL; |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 86 | |
Mark Whitley | 1ca4177 | 2000-06-28 22:15:26 +0000 | [diff] [blame] | 87 | if ((cstring = get_line_from_file(src))) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 88 | self = line_alloc(); |
| 89 | if (self == NULL) { |
| 90 | return NULL; |
| 91 | } |
John Beppu | 5a728cf | 2000-04-17 04:22:09 +0000 | [diff] [blame] | 92 | self->data = cstring; |
| 93 | self->next = NULL; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 94 | return self; |
| 95 | } |
| 96 | return NULL; |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 97 | } |
| 98 | |
John Beppu | 019513a | 1999-12-22 17:57:31 +0000 | [diff] [blame] | 99 | /* Line destructor */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 100 | static Line *line_release(Line * self) |
John Beppu | 019513a | 1999-12-22 17:57:31 +0000 | [diff] [blame] | 101 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 102 | if (self->data) { |
| 103 | free(self->data); |
| 104 | free(self); |
| 105 | } |
| 106 | return self; |
John Beppu | 019513a | 1999-12-22 17:57:31 +0000 | [diff] [blame] | 107 | } |
| 108 | |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 109 | |
| 110 | /* Comparison */ |
| 111 | |
John Beppu | 568cb7b | 1999-12-22 23:02:12 +0000 | [diff] [blame] | 112 | /* ascii order */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 113 | static int compare_ascii(const void *a, const void *b) |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 114 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 115 | Line **doh; |
| 116 | Line *x, *y; |
John Beppu | 568cb7b | 1999-12-22 23:02:12 +0000 | [diff] [blame] | 117 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 118 | doh = (Line **) a; |
| 119 | x = *doh; |
| 120 | doh = (Line **) b; |
| 121 | y = *doh; |
John Beppu | 568cb7b | 1999-12-22 23:02:12 +0000 | [diff] [blame] | 122 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 123 | // fprintf(stdout, "> %p: %s< %p: %s", x, x->data, y, y->data); |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 124 | return APPLY_REVERSE(strcmp(x->data, y->data)); |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 125 | } |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 126 | |
John Beppu | 568cb7b | 1999-12-22 23:02:12 +0000 | [diff] [blame] | 127 | /* numeric order */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 128 | static int compare_numeric(const void *a, const void *b) |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 129 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 130 | Line **doh; |
| 131 | Line *x, *y; |
| 132 | int xint, yint; |
John Beppu | ee512a3 | 1999-12-23 00:02:49 +0000 | [diff] [blame] | 133 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 134 | doh = (Line **) a; |
| 135 | x = *doh; |
| 136 | doh = (Line **) b; |
| 137 | y = *doh; |
John Beppu | ee512a3 | 1999-12-23 00:02:49 +0000 | [diff] [blame] | 138 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 139 | xint = strtoul(x->data, NULL, 10); |
| 140 | yint = strtoul(y->data, NULL, 10); |
John Beppu | ee512a3 | 1999-12-23 00:02:49 +0000 | [diff] [blame] | 141 | |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 142 | return APPLY_REVERSE(xint - yint); |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 143 | } |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 144 | |
| 145 | |
| 146 | /* List */ |
| 147 | |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 148 | /* */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 149 | static List *list_init(List * self) |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 150 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 151 | self->len = 0; |
| 152 | self->sorted = NULL; |
| 153 | self->head = NULL; |
| 154 | self->current = NULL; |
| 155 | return self; |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 156 | } |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 157 | |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 158 | /* for simplicity, the List gains ownership of the line */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 159 | static List *list_insert(List * self, Line * line) |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 160 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 161 | if (line == NULL) { |
| 162 | return NULL; |
| 163 | } |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 164 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 165 | /* first insertion */ |
| 166 | if (self->head == NULL) { |
| 167 | self->head = line; |
| 168 | self->current = line; |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 169 | |
John Beppu | 5a728cf | 2000-04-17 04:22:09 +0000 | [diff] [blame] | 170 | /* all subsequent insertions */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 171 | } else { |
| 172 | self->current->next = line; |
| 173 | self->current = line; |
| 174 | } |
| 175 | self->len++; |
| 176 | return self; |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 177 | } |
| 178 | |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 179 | /* order the list according to compare() */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 180 | static List *list_sort(List * self, Compare * compare) |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 181 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 182 | int i; |
| 183 | Line *line; |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 184 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 185 | /* mallocate array of Line*s */ |
| 186 | self->sorted = (Line **) malloc(self->len * sizeof(Line *)); |
| 187 | if (self->sorted == NULL) { |
| 188 | return NULL; |
| 189 | } |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 190 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 191 | /* fill array w/ List's contents */ |
| 192 | i = 0; |
| 193 | line = self->head; |
| 194 | while (line) { |
| 195 | self->sorted[i++] = line; |
| 196 | line = line->next; |
| 197 | } |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 198 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 199 | /* apply qsort */ |
| 200 | qsort(self->sorted, self->len, sizeof(Line *), compare); |
| 201 | return self; |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 202 | } |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 203 | |
| 204 | /* precondition: list must be sorted */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 205 | static List *list_writeToFile(List * self, FILE * dst) |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 206 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 207 | int i; |
| 208 | Line **line = self->sorted; |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 209 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 210 | if (self->sorted == NULL) { |
| 211 | return NULL; |
| 212 | } |
| 213 | for (i = 0; i < self->len; i++) { |
| 214 | fprintf(dst, "%s", line[i]->data); |
| 215 | } |
| 216 | return self; |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /* deallocate */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 220 | static void list_release(List * self) |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 221 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 222 | Line *i; |
| 223 | Line *die; |
John Beppu | 019513a | 1999-12-22 17:57:31 +0000 | [diff] [blame] | 224 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 225 | i = self->head; |
| 226 | while (i) { |
| 227 | die = i; |
| 228 | i = die->next; |
| 229 | line_release(die); |
| 230 | } |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 231 | } |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 232 | |
| 233 | |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 234 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 235 | int sort_main(int argc, char **argv) |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 236 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 237 | int i; |
| 238 | char opt; |
| 239 | List list; |
| 240 | Line *l; |
| 241 | Compare *compare; |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 242 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 243 | /* init */ |
| 244 | compare = compare_ascii; |
| 245 | list_init(&list); |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 246 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 247 | /* parse argv[] */ |
| 248 | for (i = 1; i < argc; i++) { |
| 249 | if (argv[i][0] == '-') { |
| 250 | opt = argv[i][1]; |
| 251 | switch (opt) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 252 | case 'h': |
| 253 | usage(sort_usage); |
| 254 | break; |
| 255 | case 'n': |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 256 | /* numeric comparison */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 257 | compare = compare_numeric; |
| 258 | break; |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 259 | #ifdef BB_FEATURE_SORT_REVERSE |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 260 | case 'r': |
| 261 | /* reverse */ |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 262 | reverse = 1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 263 | break; |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 264 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 265 | default: |
| 266 | fprintf(stderr, "sort: invalid option -- %c\n", opt); |
| 267 | usage(sort_usage); |
| 268 | } |
| 269 | } else { |
| 270 | break; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /* this could be factored better */ |
| 275 | |
| 276 | /* work w/ stdin */ |
| 277 | if (i >= argc) { |
| 278 | while ((l = line_newFromFile(stdin))) { |
| 279 | list_insert(&list, l); |
| 280 | } |
| 281 | list_sort(&list, compare); |
| 282 | list_writeToFile(&list, stdout); |
| 283 | list_release(&list); |
| 284 | |
| 285 | /* work w/ what's left in argv[] */ |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 286 | } else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 287 | FILE *src; |
| 288 | |
| 289 | for (; i < argc; i++) { |
| 290 | src = fopen(argv[i], "r"); |
| 291 | if (src == NULL) { |
| 292 | break; |
| 293 | } |
| 294 | while ((l = line_newFromFile(src))) { |
| 295 | list_insert(&list, l); |
| 296 | } |
| 297 | fclose(src); |
| 298 | } |
| 299 | list_sort(&list, compare); |
| 300 | list_writeToFile(&list, stdout); |
| 301 | list_release(&list); |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 302 | } |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 303 | |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 304 | return(0); |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Mark Whitley | 1ca4177 | 2000-06-28 22:15:26 +0000 | [diff] [blame] | 307 | /* $Id: sort.c,v 1.18 2000/06/28 22:15:26 markw Exp $ */ |