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 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 25 | #include "busybox.h" |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 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; |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 67 | self = xmalloc(1 * sizeof(Line)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 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(); |
John Beppu | 5a728cf | 2000-04-17 04:22:09 +0000 | [diff] [blame] | 79 | self->data = cstring; |
| 80 | self->next = NULL; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 81 | return self; |
| 82 | } |
| 83 | return NULL; |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 84 | } |
| 85 | |
John Beppu | 019513a | 1999-12-22 17:57:31 +0000 | [diff] [blame] | 86 | /* Line destructor */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 87 | static Line *line_release(Line * self) |
John Beppu | 019513a | 1999-12-22 17:57:31 +0000 | [diff] [blame] | 88 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 89 | if (self->data) { |
| 90 | free(self->data); |
| 91 | free(self); |
| 92 | } |
| 93 | return self; |
John Beppu | 019513a | 1999-12-22 17:57:31 +0000 | [diff] [blame] | 94 | } |
| 95 | |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 96 | |
| 97 | /* Comparison */ |
| 98 | |
John Beppu | 568cb7b | 1999-12-22 23:02:12 +0000 | [diff] [blame] | 99 | /* ascii order */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 100 | static int compare_ascii(const void *a, const void *b) |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 101 | { |
John Beppu | 8d369e9 | 2000-09-28 17:49:59 +0000 | [diff] [blame] | 102 | Line **val; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 103 | Line *x, *y; |
John Beppu | 568cb7b | 1999-12-22 23:02:12 +0000 | [diff] [blame] | 104 | |
John Beppu | 8d369e9 | 2000-09-28 17:49:59 +0000 | [diff] [blame] | 105 | val = (Line **) a; |
| 106 | x = *val; |
| 107 | val = (Line **) b; |
| 108 | y = *val; |
John Beppu | 568cb7b | 1999-12-22 23:02:12 +0000 | [diff] [blame] | 109 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 110 | // fprintf(stdout, "> %p: %s< %p: %s", x, x->data, y, y->data); |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 111 | return APPLY_REVERSE(strcmp(x->data, y->data)); |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 112 | } |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 113 | |
John Beppu | 568cb7b | 1999-12-22 23:02:12 +0000 | [diff] [blame] | 114 | /* numeric order */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 115 | static int compare_numeric(const void *a, const void *b) |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 116 | { |
John Beppu | 8d369e9 | 2000-09-28 17:49:59 +0000 | [diff] [blame] | 117 | Line **val; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 118 | Line *x, *y; |
| 119 | int xint, yint; |
John Beppu | ee512a3 | 1999-12-23 00:02:49 +0000 | [diff] [blame] | 120 | |
John Beppu | 8d369e9 | 2000-09-28 17:49:59 +0000 | [diff] [blame] | 121 | val = (Line **) a; |
| 122 | x = *val; |
| 123 | val = (Line **) b; |
| 124 | y = *val; |
John Beppu | ee512a3 | 1999-12-23 00:02:49 +0000 | [diff] [blame] | 125 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 126 | xint = strtoul(x->data, NULL, 10); |
| 127 | yint = strtoul(y->data, NULL, 10); |
John Beppu | ee512a3 | 1999-12-23 00:02:49 +0000 | [diff] [blame] | 128 | |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 129 | return APPLY_REVERSE(xint - yint); |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 130 | } |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 131 | |
| 132 | |
| 133 | /* List */ |
| 134 | |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 135 | /* */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 136 | static List *list_init(List * self) |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 137 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 138 | self->len = 0; |
| 139 | self->sorted = NULL; |
| 140 | self->head = NULL; |
| 141 | self->current = NULL; |
| 142 | return self; |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 143 | } |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 144 | |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 145 | /* for simplicity, the List gains ownership of the line */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 146 | static List *list_insert(List * self, Line * line) |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 147 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 148 | if (line == NULL) { |
| 149 | return NULL; |
| 150 | } |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 151 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 152 | /* first insertion */ |
| 153 | if (self->head == NULL) { |
| 154 | self->head = line; |
| 155 | self->current = line; |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 156 | |
John Beppu | 5a728cf | 2000-04-17 04:22:09 +0000 | [diff] [blame] | 157 | /* all subsequent insertions */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 158 | } else { |
| 159 | self->current->next = line; |
| 160 | self->current = line; |
| 161 | } |
| 162 | self->len++; |
| 163 | return self; |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 164 | } |
| 165 | |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 166 | /* order the list according to compare() */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 167 | static List *list_sort(List * self, Compare * compare) |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 168 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 169 | int i; |
| 170 | Line *line; |
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 | /* mallocate array of Line*s */ |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 173 | self->sorted = (Line **) xmalloc(self->len * sizeof(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 | /* fill array w/ List's contents */ |
| 176 | i = 0; |
| 177 | line = self->head; |
| 178 | while (line) { |
| 179 | self->sorted[i++] = line; |
| 180 | line = line->next; |
| 181 | } |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 182 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 183 | /* apply qsort */ |
| 184 | qsort(self->sorted, self->len, sizeof(Line *), compare); |
| 185 | return self; |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 186 | } |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 187 | |
| 188 | /* precondition: list must be sorted */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 189 | static List *list_writeToFile(List * self, FILE * dst) |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 190 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 191 | int i; |
| 192 | Line **line = self->sorted; |
John Beppu | f3e5904 | 1999-12-22 22:24:52 +0000 | [diff] [blame] | 193 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 194 | if (self->sorted == NULL) { |
| 195 | return NULL; |
| 196 | } |
| 197 | for (i = 0; i < self->len; i++) { |
| 198 | fprintf(dst, "%s", line[i]->data); |
| 199 | } |
| 200 | return self; |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /* deallocate */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 204 | static void list_release(List * self) |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 205 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 206 | Line *i; |
| 207 | Line *die; |
John Beppu | 019513a | 1999-12-22 17:57:31 +0000 | [diff] [blame] | 208 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 209 | i = self->head; |
| 210 | while (i) { |
| 211 | die = i; |
| 212 | i = die->next; |
| 213 | line_release(die); |
| 214 | } |
John Beppu | 38efa79 | 1999-12-22 00:30:29 +0000 | [diff] [blame] | 215 | } |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 216 | |
| 217 | |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 218 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 219 | int sort_main(int argc, char **argv) |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 220 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 221 | int i; |
| 222 | char opt; |
| 223 | List list; |
| 224 | Line *l; |
| 225 | Compare *compare; |
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 | /* init */ |
| 228 | compare = compare_ascii; |
| 229 | list_init(&list); |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 230 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 231 | /* parse argv[] */ |
| 232 | for (i = 1; i < argc; i++) { |
| 233 | if (argv[i][0] == '-') { |
| 234 | opt = argv[i][1]; |
| 235 | switch (opt) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 236 | case 'h': |
| 237 | usage(sort_usage); |
| 238 | break; |
| 239 | case 'n': |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 240 | /* numeric comparison */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 241 | compare = compare_numeric; |
| 242 | break; |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 243 | #ifdef BB_FEATURE_SORT_REVERSE |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 244 | case 'r': |
| 245 | /* reverse */ |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 246 | reverse = 1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 247 | break; |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 248 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 249 | default: |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 250 | error_msg("invalid option -- %c\n", opt); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 251 | usage(sort_usage); |
| 252 | } |
| 253 | } else { |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 258 | if (i >= argc) { |
John Beppu | 8d369e9 | 2000-09-28 17:49:59 +0000 | [diff] [blame] | 259 | |
| 260 | /* work w/ stdin */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 261 | while ((l = line_newFromFile(stdin))) { |
| 262 | list_insert(&list, l); |
| 263 | } |
John Beppu | 8d369e9 | 2000-09-28 17:49:59 +0000 | [diff] [blame] | 264 | |
| 265 | } else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 266 | |
| 267 | /* work w/ what's left in argv[] */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 268 | FILE *src; |
| 269 | |
| 270 | for (; i < argc; i++) { |
| 271 | src = fopen(argv[i], "r"); |
| 272 | if (src == NULL) { |
| 273 | break; |
| 274 | } |
| 275 | while ((l = line_newFromFile(src))) { |
| 276 | list_insert(&list, l); |
| 277 | } |
| 278 | fclose(src); |
| 279 | } |
John Beppu | 8d369e9 | 2000-09-28 17:49:59 +0000 | [diff] [blame] | 280 | |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 281 | } |
John Beppu | 8d369e9 | 2000-09-28 17:49:59 +0000 | [diff] [blame] | 282 | list_sort(&list, compare); |
| 283 | list_writeToFile(&list, stdout); |
| 284 | list_release(&list); |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 285 | |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 286 | return(0); |
John Beppu | c0ca473 | 1999-12-21 20:00:35 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 289 | /* $Id: sort.c,v 1.24 2000/12/07 19:56:48 markw Exp $ */ |