]>
Commit | Line | Data |
---|---|---|
0b57da2a DR |
1 | /* |
2 | * pacsort.c - a sort utility implementing alpm_pkg_vercmp | |
3 | * | |
4 | * Copyright (c) 2010-2011 Pacman Development Team <pacman-dev@archlinux.org> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
0b57da2a | 20 | #include <errno.h> |
a64a713f | 21 | #include <fnmatch.h> |
0b57da2a DR |
22 | #include <getopt.h> |
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | ||
27 | #include <alpm.h> | |
28 | ||
4ad69399 | 29 | #define DELIM ' ' |
0b57da2a DR |
30 | |
31 | struct buffer_t { | |
32 | char *mem; | |
33 | size_t len; | |
34 | size_t maxlen; | |
35 | }; | |
36 | ||
37 | struct list_t { | |
38 | char **list; | |
39 | size_t count; | |
40 | size_t maxcount; | |
41 | }; | |
42 | ||
43 | static struct options_t { | |
44 | int order; | |
45 | int sortkey; | |
46 | int null; | |
a64a713f | 47 | int filemode; |
0b57da2a DR |
48 | char delim; |
49 | } opts; | |
50 | ||
83ee9708 DR |
51 | #ifndef HAVE_STRNDUP |
52 | /* A quick and dirty implementation derived from glibc */ | |
53 | static size_t strnlen(const char *s, size_t max) | |
54 | { | |
4ad69399 JSJ |
55 | register const char *p; |
56 | for(p = s; *p && max--; ++p); | |
57 | return (p - s); | |
83ee9708 DR |
58 | } |
59 | ||
60 | char *strndup(const char *s, size_t n) | |
61 | { | |
4ad69399 JSJ |
62 | size_t len = strnlen(s, n); |
63 | char *new = (char *) malloc(len + 1); | |
83ee9708 | 64 | |
4ad69399 JSJ |
65 | if(new == NULL) |
66 | return NULL; | |
83ee9708 | 67 | |
4ad69399 JSJ |
68 | new[len] = '\0'; |
69 | return (char *)memcpy(new, s, len); | |
83ee9708 DR |
70 | } |
71 | #endif | |
72 | ||
0b57da2a DR |
73 | static struct buffer_t *buffer_new(size_t initial_size) |
74 | { | |
75 | struct buffer_t *buf; | |
76 | ||
77 | buf = calloc(1, sizeof(*buf)); | |
78 | if(!buf) { | |
79 | return NULL; | |
80 | } | |
81 | ||
82 | buf->mem = calloc(initial_size, sizeof(char)); | |
83 | if(!buf->mem) { | |
84 | free(buf); | |
85 | return NULL; | |
86 | } | |
87 | ||
88 | buf->len = 0; | |
89 | buf->maxlen = initial_size; | |
90 | ||
91 | return buf; | |
92 | } | |
93 | ||
94 | static void buffer_free(struct buffer_t *buf) | |
95 | { | |
96 | if(!buf) { | |
97 | return; | |
98 | } | |
99 | ||
100 | if(buf->mem) { | |
101 | free(buf->mem); | |
102 | } | |
103 | ||
104 | free(buf); | |
105 | } | |
106 | ||
107 | static int buffer_grow(struct buffer_t *buffer) | |
108 | { | |
109 | size_t newsz = buffer->maxlen * 2.5; | |
110 | buffer->mem = realloc(buffer->mem, newsz * sizeof(char)); | |
111 | if(!buffer->mem) { | |
112 | return 1; | |
113 | } | |
114 | buffer->maxlen = newsz; | |
115 | ||
116 | return 0; | |
117 | } | |
118 | ||
119 | static struct list_t *list_new(size_t initial_size) | |
120 | { | |
121 | struct list_t *list; | |
122 | ||
123 | list = calloc(1, sizeof(struct list_t)); | |
124 | if(!list) { | |
125 | return NULL; | |
126 | } | |
127 | ||
e9b8a769 | 128 | list->list = calloc(initial_size, sizeof(char *)); |
0b57da2a DR |
129 | if(!list->list) { |
130 | free(list); | |
131 | return NULL; | |
132 | } | |
133 | ||
134 | list->maxcount = initial_size; | |
135 | ||
136 | return list; | |
137 | } | |
138 | ||
139 | static int list_grow(struct list_t *list) | |
140 | { | |
141 | size_t newsz = list->maxcount * 2.5; | |
e9b8a769 | 142 | list->list = realloc(list->list, newsz * sizeof(char *)); |
0b57da2a DR |
143 | if(!list->list) { |
144 | return 1; | |
145 | } | |
146 | ||
147 | list->maxcount = newsz; | |
148 | ||
149 | return 0; | |
150 | } | |
151 | ||
152 | static int list_add(struct list_t *list, char *name) | |
153 | { | |
b3c0ae52 | 154 | if(!list || !name) { |
0b57da2a DR |
155 | return 1; |
156 | } | |
157 | ||
158 | if(list->count + 1 >= list->maxcount) { | |
159 | if(list_grow(list) != 0) { | |
160 | return 1; | |
161 | } | |
162 | } | |
163 | ||
164 | list->list[list->count] = name; | |
165 | list->count++; | |
166 | ||
167 | return 0; | |
168 | } | |
169 | ||
170 | static void list_free(struct list_t *list) | |
171 | { | |
172 | size_t i; | |
173 | ||
174 | if(!list) { | |
175 | return; | |
176 | } | |
177 | ||
178 | if(list->list) { | |
179 | for(i = 0; i < list->count; i++) { | |
180 | free(list->list[i]); | |
181 | } | |
182 | free(list->list); | |
0b57da2a | 183 | } |
343ea817 | 184 | free(list); |
0b57da2a DR |
185 | } |
186 | ||
187 | static char *explode(struct buffer_t *buffer, struct list_t *list) | |
188 | { | |
189 | char *name, *ptr, *end; | |
190 | const char linedelim = opts.null ? '\0' : '\n'; | |
191 | ||
192 | ptr = buffer->mem; | |
193 | while((end = memchr(ptr, linedelim, &buffer->mem[buffer->len] - ptr))) { | |
194 | *end = '\0'; | |
195 | name = strdup(ptr); | |
196 | list_add(list, name); | |
197 | ptr = end + 1; | |
198 | } | |
199 | ||
200 | return ptr; | |
201 | } | |
202 | ||
203 | static int splitfile(FILE *stream, struct buffer_t *buffer, struct list_t *list) | |
204 | { | |
205 | size_t nread; | |
206 | char *ptr; | |
207 | ||
208 | while(!feof(stream)) { | |
209 | /* check if a read of BUFSIZ chars will overflow */ | |
86eefc1a | 210 | if(buffer->len + BUFSIZ + 1 >= buffer->maxlen) { |
0b57da2a DR |
211 | if(buffer_grow(buffer) != 0) { |
212 | return 1; | |
213 | } | |
214 | } | |
215 | ||
216 | nread = fread(&buffer->mem[buffer->len], 1, BUFSIZ, stream); | |
217 | if(nread == 0) { | |
218 | break; /* EOF */ | |
219 | } | |
220 | buffer->len += nread; | |
221 | ||
222 | if((ptr = explode(buffer, list)) == NULL) { | |
223 | return 1; | |
224 | } | |
225 | ||
226 | if(ptr != buffer->mem) { | |
227 | /* realign the data in the buffer */ | |
228 | buffer->len = &buffer->mem[buffer->len] - ptr; | |
229 | memmove(&buffer->mem[0], ptr, buffer->len + 1); | |
230 | } | |
231 | } | |
232 | ||
233 | if(buffer->len) { | |
234 | char *name = strndup(buffer->mem, buffer->len + 1); | |
235 | if(list_add(list, name) != 0) { | |
236 | return 1; | |
237 | } | |
238 | } | |
239 | ||
240 | return 0; | |
241 | } | |
242 | ||
243 | /* returns a pointer to the nth column of a string without being destructive */ | |
244 | static const char *nth_column(const char *string) | |
245 | { | |
246 | const char *prev, *ptr; | |
247 | int col; | |
248 | ||
249 | ptr = prev = string; | |
250 | for(col = 1; ptr && col <= opts.sortkey; col++) { | |
251 | prev = ptr; | |
252 | ptr = strchr(ptr, opts.delim); | |
253 | if(ptr) { | |
254 | ptr++; | |
255 | } | |
256 | } | |
257 | ||
258 | return prev; | |
259 | } | |
260 | ||
261 | static int vercmp(const void *p1, const void *p2) | |
262 | { | |
263 | const char *name1, *name2; | |
a64a713f DR |
264 | char *fn1 = NULL, *fn2 = NULL; |
265 | int r; | |
0b57da2a DR |
266 | |
267 | name1 = *(const char **)p1; | |
268 | name2 = *(const char **)p2; | |
269 | ||
a64a713f DR |
270 | /* if we're operating in file mode, we modify the strings under certain |
271 | * conditions to appease alpm_pkg_vercmp(). If and only if both inputs end | |
272 | * with a suffix that appears to be a package name, we strip the suffix and | |
273 | * remove any leading paths. This means that strings such as: | |
274 | * | |
275 | * /var/cache/pacman/pkg/firefox-18.0-2-x86_64.pkg.tar.xz | |
276 | * firefox-18.0-2-x86_64.pkg.tar.gz | |
277 | * | |
278 | * Will be considered equal by this version comparison | |
279 | */ | |
280 | if(opts.filemode) { | |
281 | if(fnmatch("*-*.pkg.tar.?z", name1, 0) == 0 && | |
282 | fnmatch("*-*.pkg.tar.?z", name2, 0) == 0) { | |
283 | const char *start, *end; | |
284 | ||
285 | start = strrchr(name1, '/'); | |
286 | start = start ? start + 1 : name1; | |
287 | end = strrchr(name1, '-'); | |
288 | fn1 = strndup(start, end - start); | |
289 | ||
290 | start = strrchr(name2, '/'); | |
291 | start = start ? start + 1 : name2; | |
292 | end = strrchr(name2, '-'); | |
293 | fn2 = strndup(start, end - start); | |
294 | ||
295 | name1 = fn1; | |
296 | name2 = fn2; | |
297 | } | |
298 | } | |
299 | ||
0b57da2a | 300 | if(opts.sortkey == 0) { |
a64a713f | 301 | r = opts.order * alpm_pkg_vercmp(name1, name2); |
0b57da2a | 302 | } else { |
a64a713f | 303 | r = opts.order * alpm_pkg_vercmp(nth_column(name1), nth_column(name2)); |
0b57da2a | 304 | } |
a64a713f DR |
305 | |
306 | if(opts.filemode) { | |
307 | free(fn1); | |
308 | free(fn2); | |
309 | } | |
310 | ||
311 | return r; | |
0b57da2a DR |
312 | } |
313 | ||
314 | static char escape_char(const char *string) | |
315 | { | |
316 | const size_t len = strlen(string); | |
317 | ||
318 | if(!string || len > 2) { | |
319 | return -1; | |
320 | } | |
321 | ||
322 | if(len == 1) { | |
323 | return *string; | |
324 | } | |
325 | ||
326 | if(*string != '\\') { | |
327 | return -1; | |
328 | } | |
329 | ||
330 | switch(string[1]) { | |
331 | case 't': | |
332 | return '\t'; | |
333 | case 'n': | |
334 | return '\n'; | |
335 | case 'v': | |
336 | return '\v'; | |
337 | case '0': | |
338 | return '\0'; | |
339 | default: | |
340 | return -1; | |
341 | } | |
342 | } | |
343 | ||
344 | static void usage(void) | |
345 | { | |
346 | fprintf(stderr, "pacsort v" PACKAGE_VERSION "\n" | |
347 | "Usage: pacsort [options] [files...]\n\n" | |
a64a713f | 348 | " -f, --files assume inputs are filepaths of packages\n" |
0b57da2a DR |
349 | " -h, --help display this help message\n" |
350 | " -k, --key <index> sort input starting on specified column\n" | |
351 | " -r, --reverse sort in reverse order (default: oldest to newest)\n" | |
352 | " -t, --separator <sep> specify field separator (default: space)\n" | |
353 | " -z, --null lines end with null bytes, not newlines\n\n"); | |
354 | } | |
355 | ||
356 | static int parse_options(int argc, char **argv) | |
357 | { | |
358 | int opt; | |
359 | ||
360 | static const struct option opttable[] = { | |
a64a713f | 361 | {"files", no_argument, 0, 'f'}, |
0b57da2a DR |
362 | {"help", no_argument, 0, 'h'}, |
363 | {"key", required_argument, 0, 'k'}, | |
364 | {"reverse", no_argument, 0, 'r'}, | |
365 | {"separator", required_argument, 0, 't'}, | |
366 | {"null", no_argument, 0, 'z'}, | |
367 | {0, 0, 0, 0} | |
368 | }; | |
369 | ||
a64a713f | 370 | while((opt = getopt_long(argc, argv, "fhk:rt:z", opttable, NULL)) != -1) { |
0b57da2a | 371 | switch(opt) { |
a64a713f DR |
372 | case 'f': |
373 | opts.filemode = 1; | |
374 | break; | |
0b57da2a DR |
375 | case 'h': |
376 | return 1; | |
377 | case 'k': | |
378 | opts.sortkey = (int)strtol(optarg, NULL, 10); | |
379 | if(opts.sortkey <= 0) { | |
380 | fprintf(stderr, "error: invalid sort key -- %s\n", optarg); | |
381 | return 1; | |
382 | } | |
383 | break; | |
384 | case 'r': | |
385 | opts.order = -1; | |
386 | break; | |
387 | case 't': | |
388 | opts.delim = escape_char(optarg); | |
389 | if(opts.delim == -1) { | |
390 | fprintf(stderr, "error: invalid field separator -- `%s'\n", optarg); | |
391 | return 1; | |
392 | } | |
393 | break; | |
394 | case 'z': | |
395 | opts.null = 1; | |
396 | break; | |
397 | default: | |
398 | return 1; | |
399 | } | |
400 | } | |
401 | ||
402 | return 0; | |
403 | } | |
404 | ||
405 | int main(int argc, char *argv[]) | |
406 | { | |
407 | struct list_t *list; | |
408 | struct buffer_t *buffer; | |
409 | size_t i; | |
410 | ||
411 | /* option defaults */ | |
412 | opts.order = 1; | |
413 | opts.delim = DELIM; | |
414 | opts.sortkey = 0; | |
415 | opts.null = 0; | |
416 | ||
417 | if(parse_options(argc, argv) != 0) { | |
418 | usage(); | |
419 | return 2; | |
420 | } | |
421 | ||
422 | list = list_new(100); | |
423 | buffer = buffer_new(BUFSIZ * 3); | |
424 | ||
425 | if(optind == argc) { | |
426 | if(splitfile(stdin, buffer, list) != 0) { | |
427 | fprintf(stderr, "%s: memory exhausted\n", argv[0]); | |
428 | return ENOMEM; | |
429 | } | |
430 | } else { | |
431 | while(optind < argc) { | |
432 | FILE *input = fopen(argv[optind], "r"); | |
433 | if(input) { | |
434 | if(splitfile(input, buffer, list) != 0) { | |
435 | fprintf(stderr, "%s: memory exhausted\n", argv[0]); | |
436 | return ENOMEM; | |
437 | } | |
438 | fclose(input); | |
439 | } else { | |
440 | fprintf(stderr, "%s: %s: %s\n", argv[0], argv[optind], strerror(errno)); | |
441 | } | |
442 | optind++; | |
443 | } | |
444 | } | |
445 | ||
446 | if(list->count) { | |
447 | const char linedelim = opts.null ? '\0' : '\n'; | |
448 | qsort(list->list, list->count, sizeof(char *), vercmp); | |
449 | for(i = 0; i < list->count; i++) { | |
450 | printf("%s%c", list->list[i], linedelim); | |
451 | } | |
0b57da2a DR |
452 | } |
453 | ||
ffdfc825 | 454 | list_free(list); |
0b57da2a DR |
455 | buffer_free(buffer); |
456 | ||
457 | return 0; | |
458 | } | |
459 | ||
460 | /* vim: set ts=2 sw=2 noet: */ |