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 | #ifdef BB_FEATURE_SORT_REVERSE |
| 33 | #define APPLY_REVERSE(x) (reverse ? -(x) : (x)) |
| 34 | static int reverse = 0; |
| 35 | #else |
| 36 | #define APPLY_REVERSE(x) (x) |
| 37 | #endif |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 38 | |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 39 | /* typedefs _______________________________________________________________ */ |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 40 | |
| 41 | /* line node */ |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 42 | typedef struct Line { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 43 | char *data; /* line data */ |
| 44 | struct Line *next; /* pointer to next line node */ |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 45 | } Line; |
| 46 | |
| 47 | /* singly-linked list of lines */ |
| 48 | typedef struct { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 49 | int len; /* number of Lines */ |
| 50 | Line **sorted; /* array fed to qsort */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 51 | Line *head; /* head of List */ |
| 52 | Line *current; /* current Line */ |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 53 | } List; |
| 54 | |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 55 | /* comparison function */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | typedef int (Compare) (const void *, const void *); |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 57 | |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 58 | |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 59 | /* methods ________________________________________________________________ */ |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 60 | |
| 61 | static const int max = 1024; |
| 62 | |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 63 | /* mallocate Line */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 64 | static Line *line_alloc() |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 65 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 66 | Line *self; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 67 | self = malloc(1 * sizeof(Line)); |
| 68 | return self; |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 69 | } |
| 70 | |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 71 | /* Construct Line from FILE* */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 72 | static Line *line_newFromFile(FILE * src) |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 73 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | Line *self; |
John Beppu | 5a728cf | 2000-04-17 04:22:09 +0000 | [diff] [blame] | 75 | char *cstring = NULL; |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 76 | |
Mark Whitley | 1ca4177 | 2000-06-28 22:15:26 +0000 | [diff] [blame] | 77 | if ((cstring = get_line_from_file(src))) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 78 | self = line_alloc(); |
| 79 | if (self == NULL) { |
| 80 | return NULL; |
| 81 | } |
John Beppu | 5a728cf | 2000-04-17 04:22:09 +0000 | [diff] [blame] | 82 | self->data = cstring; |
| 83 | self->next = NULL; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 84 | return self; |
| 85 | } |
| 86 | return NULL; |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 87 | } |
| 88 | |
John Beppu | 019513a | 1999-12-22 17:57:31 +0000 | [diff] [blame] | 89 | /* Line destructor */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 90 | static Line *line_release(Line * self) |
John Beppu | 019513a | 1999-12-22 17:57:31 +0000 | [diff] [blame] | 91 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 92 | if (self->data) { |
| 93 | free(self->data); |
| 94 | free(self); |
| 95 | } |
| 96 | return self; |
John Beppu | 019513a | 1999-12-22 17:57:31 +0000 | [diff] [blame] | 97 | } |
| 98 | |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 99 | |
| 100 | /* Comparison */ |
| 101 | |
John Beppu | 568cb7b | 1999-12-22 23:02:12 +0000 | [diff] [blame] | 102 | /* ascii order */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 103 | static int compare_ascii(const void *a, const void *b) |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 104 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 105 | Line **doh; |
| 106 | Line *x, *y; |
John Beppu | 568cb7b | 1999-12-22 23:02:12 +0000 | [diff] [blame] | 107 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 108 | doh = (Line **) a; |
| 109 | x = *doh; |
| 110 | doh = (Line **) b; |
| 111 | y = *doh; |
John Beppu | 568cb7b | 1999-12-22 23:02:12 +0000 | [diff] [blame] | 112 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 113 | // fprintf(stdout, "> %p: %s< %p: %s", x, x->data, y, y->data); |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 114 | return APPLY_REVERSE(strcmp(x->data, y->data)); |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 115 | } |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 116 | |
John Beppu | 568cb7b | 1999-12-22 23:02:12 +0000 | [diff] [blame] | 117 | /* numeric order */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 118 | static int compare_numeric(const void *a, const void *b) |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 119 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 120 | Line **doh; |
| 121 | Line *x, *y; |
| 122 | int xint, yint; |
John Beppu | ee512a3 | 1999-12-23 00:02:49 +0000 | [diff] [blame] | 123 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 124 | doh = (Line **) a; |
| 125 | x = *doh; |
| 126 | doh = (Line **) b; |
| 127 | y = *doh; |
John Beppu | ee512a3 | 1999-12-23 00:02:49 +0000 | [diff] [blame] | 128 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 129 | xint = strtoul(x->data, NULL, 10); |
| 130 | yint = strtoul(y->data, NULL, 10); |
John Beppu | ee512a3 | 1999-12-23 00:02:49 +0000 | [diff] [blame] | 131 | |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 132 | return APPLY_REVERSE(xint - yint); |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 133 | } |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 134 | |
| 135 | |
| 136 | /* List */ |
| 137 | |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 138 | /* */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 139 | static List *list_init(List * self) |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 140 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 141 | self->len = 0; |
| 142 | self->sorted = NULL; |
| 143 | self->head = NULL; |
| 144 | self->current = NULL; |
| 145 | return self; |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 146 | } |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 147 | |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 148 | /* for simplicity, the List gains ownership of the line */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 149 | static List *list_insert(List * self, Line * line) |
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 | if (line == NULL) { |
| 152 | return NULL; |
| 153 | } |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 154 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 155 | /* first insertion */ |
| 156 | if (self->head == NULL) { |
| 157 | self->head = line; |
| 158 | self->current = line; |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 159 | |
John Beppu | 5a728cf | 2000-04-17 04:22:09 +0000 | [diff] [blame] | 160 | /* all subsequent insertions */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 161 | } else { |
| 162 | self->current->next = line; |
| 163 | self->current = line; |
| 164 | } |
| 165 | self->len++; |
| 166 | return self; |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 167 | } |
| 168 | |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 169 | /* order the list according to compare() */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 170 | static List *list_sort(List * self, Compare * compare) |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 171 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 172 | int i; |
| 173 | Line *line; |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 174 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 175 | /* mallocate array of Line*s */ |
| 176 | self->sorted = (Line **) malloc(self->len * sizeof(Line *)); |
| 177 | if (self->sorted == NULL) { |
| 178 | return NULL; |
| 179 | } |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 180 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 181 | /* fill array w/ List's contents */ |
| 182 | i = 0; |
| 183 | line = self->head; |
| 184 | while (line) { |
| 185 | self->sorted[i++] = line; |
| 186 | line = line->next; |
| 187 | } |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 188 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 189 | /* apply qsort */ |
| 190 | qsort(self->sorted, self->len, sizeof(Line *), compare); |
| 191 | return self; |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 192 | } |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 193 | |
| 194 | /* precondition: list must be sorted */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 195 | static List *list_writeToFile(List * self, FILE * dst) |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 196 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 197 | int i; |
| 198 | Line **line = self->sorted; |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 199 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 200 | if (self->sorted == NULL) { |
| 201 | return NULL; |
| 202 | } |
| 203 | for (i = 0; i < self->len; i++) { |
| 204 | fprintf(dst, "%s", line[i]->data); |
| 205 | } |
| 206 | return self; |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /* deallocate */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 210 | static void list_release(List * self) |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 211 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 212 | Line *i; |
| 213 | Line *die; |
John Beppu | 019513a | 1999-12-22 17:57:31 +0000 | [diff] [blame] | 214 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 215 | i = self->head; |
| 216 | while (i) { |
| 217 | die = i; |
| 218 | i = die->next; |
| 219 | line_release(die); |
| 220 | } |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 221 | } |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 222 | |
| 223 | |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 224 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 225 | int sort_main(int argc, char **argv) |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 226 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 227 | int i; |
| 228 | char opt; |
| 229 | List list; |
| 230 | Line *l; |
| 231 | Compare *compare; |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 232 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 233 | /* init */ |
| 234 | compare = compare_ascii; |
| 235 | list_init(&list); |
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 | /* parse argv[] */ |
| 238 | for (i = 1; i < argc; i++) { |
| 239 | if (argv[i][0] == '-') { |
| 240 | opt = argv[i][1]; |
| 241 | switch (opt) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 242 | case 'h': |
| 243 | usage(sort_usage); |
| 244 | break; |
| 245 | case 'n': |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 246 | /* numeric comparison */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 247 | compare = compare_numeric; |
| 248 | break; |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 249 | #ifdef BB_FEATURE_SORT_REVERSE |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 250 | case 'r': |
| 251 | /* reverse */ |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 252 | reverse = 1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 253 | break; |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 254 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 255 | default: |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 256 | errorMsg("invalid option -- %c\n", opt); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 257 | usage(sort_usage); |
| 258 | } |
| 259 | } else { |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /* this could be factored better */ |
| 265 | |
| 266 | /* work w/ stdin */ |
| 267 | if (i >= argc) { |
| 268 | while ((l = line_newFromFile(stdin))) { |
| 269 | list_insert(&list, l); |
| 270 | } |
| 271 | list_sort(&list, compare); |
| 272 | list_writeToFile(&list, stdout); |
| 273 | list_release(&list); |
| 274 | |
| 275 | /* work w/ what's left in argv[] */ |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 276 | } else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 277 | FILE *src; |
| 278 | |
| 279 | for (; i < argc; i++) { |
| 280 | src = fopen(argv[i], "r"); |
| 281 | if (src == NULL) { |
| 282 | break; |
| 283 | } |
| 284 | while ((l = line_newFromFile(src))) { |
| 285 | list_insert(&list, l); |
| 286 | } |
| 287 | fclose(src); |
| 288 | } |
| 289 | list_sort(&list, compare); |
| 290 | list_writeToFile(&list, stdout); |
| 291 | list_release(&list); |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 292 | } |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 293 | |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 294 | return(0); |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Matt Kraai | bf181b9 | 2000-07-16 20:57:15 +0000 | [diff] [blame] | 297 | /* $Id: sort.c,v 1.20 2000/07/16 20:57:15 kraai Exp $ */ |