2011-08-05 21:20:20 -04:00
|
|
|
/*
|
|
|
|
* pacsort.c - a sort utility implementing alpm_pkg_vercmp
|
|
|
|
*
|
2016-01-02 22:48:43 -05:00
|
|
|
* Copyright (c) 2010-2016 Pacman Development Team <pacman-dev@archlinux.org>
|
2011-08-05 21:20:20 -04:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2013-01-19 12:00:20 -05:00
|
|
|
#include <fnmatch.h>
|
2011-08-05 21:20:20 -04:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <alpm.h>
|
2015-02-27 14:43:23 -05:00
|
|
|
#include "util-common.h"
|
2011-08-05 21:20:20 -04:00
|
|
|
|
2013-07-21 21:09:57 -04:00
|
|
|
#define DELIM ' '
|
2011-08-05 21:20:20 -04:00
|
|
|
|
2014-11-18 09:02:06 -05:00
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(a, b) \
|
|
|
|
__extension__({ \
|
|
|
|
__typeof__(a) _a = (a); \
|
|
|
|
__typeof__(b) _b = (b); \
|
|
|
|
_a < _b ? _a : _b; \
|
|
|
|
})
|
|
|
|
#endif
|
|
|
|
|
2011-08-05 21:20:20 -04:00
|
|
|
struct buffer_t {
|
|
|
|
char *mem;
|
|
|
|
size_t len;
|
|
|
|
size_t maxlen;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct list_t {
|
2014-11-18 09:02:06 -05:00
|
|
|
void **list;
|
2011-08-05 21:20:20 -04:00
|
|
|
size_t count;
|
|
|
|
size_t maxcount;
|
|
|
|
};
|
|
|
|
|
2014-11-18 09:02:06 -05:00
|
|
|
struct input_t {
|
|
|
|
char *data;
|
|
|
|
int is_file;
|
|
|
|
|
|
|
|
const char *pkgname;
|
|
|
|
size_t pkgname_len;
|
|
|
|
|
|
|
|
const char *pkgver;
|
|
|
|
size_t pkgver_len;
|
|
|
|
};
|
|
|
|
|
2011-08-05 21:20:20 -04:00
|
|
|
static struct options_t {
|
|
|
|
int order;
|
|
|
|
int sortkey;
|
|
|
|
int null;
|
2013-01-19 12:00:20 -05:00
|
|
|
int filemode;
|
2014-01-28 21:40:37 -05:00
|
|
|
int help;
|
2011-08-05 21:20:20 -04:00
|
|
|
char delim;
|
|
|
|
} opts;
|
|
|
|
|
|
|
|
static struct buffer_t *buffer_new(size_t initial_size)
|
|
|
|
{
|
|
|
|
struct buffer_t *buf;
|
|
|
|
|
|
|
|
buf = calloc(1, sizeof(*buf));
|
|
|
|
if(!buf) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf->mem = calloc(initial_size, sizeof(char));
|
|
|
|
if(!buf->mem) {
|
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf->len = 0;
|
|
|
|
buf->maxlen = initial_size;
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void buffer_free(struct buffer_t *buf)
|
|
|
|
{
|
|
|
|
if(!buf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-28 11:50:44 -05:00
|
|
|
free(buf->mem);
|
2011-08-05 21:20:20 -04:00
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int buffer_grow(struct buffer_t *buffer)
|
|
|
|
{
|
|
|
|
size_t newsz = buffer->maxlen * 2.5;
|
2015-08-10 15:42:38 -04:00
|
|
|
char* new_mem = realloc(buffer->mem, newsz * sizeof(char));
|
|
|
|
if(!new_mem) {
|
2011-08-05 21:20:20 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2015-08-10 15:42:38 -04:00
|
|
|
buffer->mem = new_mem;
|
2011-08-05 21:20:20 -04:00
|
|
|
buffer->maxlen = newsz;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct list_t *list_new(size_t initial_size)
|
|
|
|
{
|
|
|
|
struct list_t *list;
|
|
|
|
|
|
|
|
list = calloc(1, sizeof(struct list_t));
|
|
|
|
if(!list) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-08-22 01:00:33 -04:00
|
|
|
list->list = calloc(initial_size, sizeof(char *));
|
2011-08-05 21:20:20 -04:00
|
|
|
if(!list->list) {
|
|
|
|
free(list);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
list->maxcount = initial_size;
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int list_grow(struct list_t *list)
|
|
|
|
{
|
|
|
|
size_t newsz = list->maxcount * 2.5;
|
2015-08-10 15:42:38 -04:00
|
|
|
void **new_list = realloc(list->list, newsz * sizeof(char *));
|
|
|
|
if(!new_list) {
|
2011-08-05 21:20:20 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-08-10 15:42:38 -04:00
|
|
|
list->list = new_list;
|
2011-08-05 21:20:20 -04:00
|
|
|
list->maxcount = newsz;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-18 09:02:06 -05:00
|
|
|
static int list_add(struct list_t *list, void *obj)
|
2011-08-05 21:20:20 -04:00
|
|
|
{
|
2014-11-18 09:02:06 -05:00
|
|
|
if(!list || !obj) {
|
2011-08-05 21:20:20 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(list->count + 1 >= list->maxcount) {
|
|
|
|
if(list_grow(list) != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-18 09:02:06 -05:00
|
|
|
list->list[list->count] = obj;
|
2011-08-05 21:20:20 -04:00
|
|
|
list->count++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-18 09:02:06 -05:00
|
|
|
static void list_free(struct list_t *list, void (*freefn)(void *))
|
2011-08-05 21:20:20 -04:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if(!list) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(list->list) {
|
|
|
|
for(i = 0; i < list->count; i++) {
|
2014-11-18 09:02:06 -05:00
|
|
|
freefn(list->list[i]);
|
2011-08-05 21:20:20 -04:00
|
|
|
}
|
|
|
|
free(list->list);
|
|
|
|
}
|
2011-08-22 01:00:34 -04:00
|
|
|
free(list);
|
2011-08-05 21:20:20 -04:00
|
|
|
}
|
|
|
|
|
2014-11-18 09:02:06 -05:00
|
|
|
static void input_free(void *p)
|
|
|
|
{
|
|
|
|
struct input_t *in = p;
|
|
|
|
|
|
|
|
if(in == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(in->data);
|
|
|
|
free(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct input_t *input_new(const char *path, int pathlen)
|
|
|
|
{
|
|
|
|
const char *pkgver_end;
|
|
|
|
const char *slash;
|
|
|
|
struct input_t *in;
|
|
|
|
|
|
|
|
in = calloc(1, sizeof(struct input_t));
|
|
|
|
if(in == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
in->data = strndup(path, pathlen);
|
|
|
|
if(in->data == NULL) {
|
2014-12-23 20:57:11 -05:00
|
|
|
free(in);
|
2014-11-18 09:02:06 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
in->is_file = fnmatch("*-*.pkg.tar.?z", in->data, 0) == 0;
|
|
|
|
if(!in->is_file) {
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* for files, we parse the pkgname and pkgrel from the full filename. */
|
|
|
|
|
|
|
|
slash = strrchr(in->data, '/');
|
|
|
|
if(slash == NULL) {
|
|
|
|
in->pkgname = in->data;
|
|
|
|
} else {
|
|
|
|
in->pkgname = slash + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pkgver_end = strrchr(in->pkgname, '-');
|
|
|
|
|
|
|
|
/* read backwards through pkgrel */
|
|
|
|
for(in->pkgver = pkgver_end - 1;
|
|
|
|
in->pkgver > in->pkgname && *in->pkgver != '-';
|
|
|
|
--in->pkgver)
|
|
|
|
;
|
|
|
|
/* read backwards through pkgver */
|
|
|
|
for(--in->pkgver;
|
|
|
|
in->pkgver > in->pkgname && *in->pkgver != '-';
|
|
|
|
--in->pkgver)
|
|
|
|
;
|
|
|
|
++in->pkgver;
|
|
|
|
|
|
|
|
in->pkgname_len = in->pkgver - in->pkgname - 1;
|
|
|
|
in->pkgver_len = pkgver_end - in->pkgver;
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2011-08-05 21:20:20 -04:00
|
|
|
static char *explode(struct buffer_t *buffer, struct list_t *list)
|
|
|
|
{
|
2014-11-18 09:02:06 -05:00
|
|
|
char *ptr, *end;
|
2011-08-05 21:20:20 -04:00
|
|
|
const char linedelim = opts.null ? '\0' : '\n';
|
2014-11-18 09:02:06 -05:00
|
|
|
struct input_t *meta;
|
2011-08-05 21:20:20 -04:00
|
|
|
|
|
|
|
ptr = buffer->mem;
|
|
|
|
while((end = memchr(ptr, linedelim, &buffer->mem[buffer->len] - ptr))) {
|
|
|
|
*end = '\0';
|
2014-11-18 09:02:06 -05:00
|
|
|
meta = input_new(ptr, end - ptr);
|
2015-08-10 15:42:37 -04:00
|
|
|
if(meta == NULL || list_add(list, meta) != 0) {
|
|
|
|
input_free(meta);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-08-05 21:20:20 -04:00
|
|
|
ptr = end + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int splitfile(FILE *stream, struct buffer_t *buffer, struct list_t *list)
|
|
|
|
{
|
|
|
|
size_t nread;
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
while(!feof(stream)) {
|
|
|
|
/* check if a read of BUFSIZ chars will overflow */
|
2013-01-03 16:48:53 -05:00
|
|
|
if(buffer->len + BUFSIZ + 1 >= buffer->maxlen) {
|
2011-08-05 21:20:20 -04:00
|
|
|
if(buffer_grow(buffer) != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nread = fread(&buffer->mem[buffer->len], 1, BUFSIZ, stream);
|
|
|
|
if(nread == 0) {
|
|
|
|
break; /* EOF */
|
|
|
|
}
|
|
|
|
buffer->len += nread;
|
|
|
|
|
|
|
|
if((ptr = explode(buffer, list)) == NULL) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ptr != buffer->mem) {
|
|
|
|
/* realign the data in the buffer */
|
|
|
|
buffer->len = &buffer->mem[buffer->len] - ptr;
|
|
|
|
memmove(&buffer->mem[0], ptr, buffer->len + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(buffer->len) {
|
2014-11-18 09:02:06 -05:00
|
|
|
struct input_t *meta = input_new(buffer->mem, buffer->len + 1);
|
|
|
|
if(meta == NULL || list_add(list, meta) != 0) {
|
2015-08-10 15:42:37 -04:00
|
|
|
input_free(meta);
|
2011-08-05 21:20:20 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns a pointer to the nth column of a string without being destructive */
|
|
|
|
static const char *nth_column(const char *string)
|
|
|
|
{
|
|
|
|
const char *prev, *ptr;
|
|
|
|
int col;
|
|
|
|
|
|
|
|
ptr = prev = string;
|
2015-04-23 02:45:51 -04:00
|
|
|
for(col = 0; ptr && col < opts.sortkey; col++) {
|
2011-08-05 21:20:20 -04:00
|
|
|
prev = ptr;
|
|
|
|
ptr = strchr(ptr, opts.delim);
|
|
|
|
if(ptr) {
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
|
2014-11-18 09:02:06 -05:00
|
|
|
static int compare_versions(const char *v1, const char *v2)
|
2011-08-05 21:20:20 -04:00
|
|
|
{
|
|
|
|
if(opts.sortkey == 0) {
|
2014-11-18 09:02:06 -05:00
|
|
|
return opts.order * alpm_pkg_vercmp(v1, v2);
|
2011-08-05 21:20:20 -04:00
|
|
|
} else {
|
2014-11-18 09:02:06 -05:00
|
|
|
return opts.order * alpm_pkg_vercmp(nth_column(v1), nth_column(v2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compare_files(const struct input_t *meta1, const struct input_t *meta2)
|
|
|
|
{
|
|
|
|
int cmp;
|
|
|
|
char *verbuf;
|
|
|
|
const char *v1, *v2;
|
|
|
|
|
|
|
|
/* sort first by package name */
|
|
|
|
cmp = memcmp(meta1->pkgname, meta2->pkgname,
|
|
|
|
MIN(meta1->pkgname_len, meta2->pkgname_len));
|
|
|
|
|
|
|
|
/* 1) package names differ, sort by package name */
|
|
|
|
if(cmp != 0) {
|
|
|
|
return opts.order * cmp;
|
2011-08-05 21:20:20 -04:00
|
|
|
}
|
2013-01-19 12:00:20 -05:00
|
|
|
|
2014-11-18 09:02:06 -05:00
|
|
|
/* 2) prefixes are the same but length differs, sort by length */
|
|
|
|
if(meta1->pkgname_len != meta2->pkgname_len) {
|
|
|
|
return opts.order * (meta1->pkgname_len - meta2->pkgname_len);
|
2013-01-19 12:00:20 -05:00
|
|
|
}
|
|
|
|
|
2014-11-18 09:02:06 -05:00
|
|
|
/* allocate once with enough space for both pkgver */
|
|
|
|
verbuf = calloc(1, meta1->pkgver_len + 1 + meta2->pkgver_len + 1);
|
|
|
|
memcpy(verbuf, meta1->pkgver, meta1->pkgver_len);
|
|
|
|
memcpy(&verbuf[meta1->pkgver_len + 1], meta2->pkgver, meta2->pkgver_len);
|
|
|
|
|
|
|
|
/* 3) sort by package version */
|
|
|
|
v1 = verbuf;
|
|
|
|
v2 = verbuf + meta1->pkgver_len + 1;
|
|
|
|
cmp = compare_versions(v1, v2);
|
|
|
|
free(verbuf);
|
|
|
|
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vercmp(const void *p1, const void *p2)
|
|
|
|
{
|
|
|
|
const struct input_t *meta1, *meta2;
|
|
|
|
|
|
|
|
meta1 = *(struct input_t **)p1;
|
|
|
|
meta2 = *(struct input_t **)p2;
|
|
|
|
|
|
|
|
if(opts.filemode && meta1->is_file && meta2->is_file) {
|
|
|
|
return compare_files(meta1, meta2);
|
|
|
|
} else {
|
|
|
|
return compare_versions(meta1->data, meta2->data);
|
|
|
|
}
|
2011-08-05 21:20:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static char escape_char(const char *string)
|
|
|
|
{
|
2015-02-24 10:01:49 -05:00
|
|
|
if(!string) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-08-05 21:20:20 -04:00
|
|
|
const size_t len = strlen(string);
|
|
|
|
|
2015-02-24 10:01:49 -05:00
|
|
|
if(len > 2) {
|
2011-08-05 21:20:20 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(len == 1) {
|
|
|
|
return *string;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(*string != '\\') {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(string[1]) {
|
|
|
|
case 't':
|
|
|
|
return '\t';
|
|
|
|
case 'n':
|
|
|
|
return '\n';
|
|
|
|
case 'v':
|
|
|
|
return '\v';
|
|
|
|
case '0':
|
|
|
|
return '\0';
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
{
|
2013-09-03 15:20:50 -04:00
|
|
|
fprintf(stderr, "pacsort (pacman) v" PACKAGE_VERSION "\n\n"
|
|
|
|
"A sort utility implementing alpm_pkg_vercmp.\n\n"
|
2011-08-05 21:20:20 -04:00
|
|
|
"Usage: pacsort [options] [files...]\n\n"
|
2013-09-03 15:20:50 -04:00
|
|
|
" -f, --files assume inputs are file paths of packages\n"
|
2011-08-05 21:20:20 -04:00
|
|
|
" -h, --help display this help message\n"
|
|
|
|
" -k, --key <index> sort input starting on specified column\n"
|
|
|
|
" -r, --reverse sort in reverse order (default: oldest to newest)\n"
|
|
|
|
" -t, --separator <sep> specify field separator (default: space)\n"
|
|
|
|
" -z, --null lines end with null bytes, not newlines\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_options(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int opt;
|
|
|
|
|
|
|
|
static const struct option opttable[] = {
|
2013-01-19 12:00:20 -05:00
|
|
|
{"files", no_argument, 0, 'f'},
|
2011-08-05 21:20:20 -04:00
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{"key", required_argument, 0, 'k'},
|
|
|
|
{"reverse", no_argument, 0, 'r'},
|
|
|
|
{"separator", required_argument, 0, 't'},
|
|
|
|
{"null", no_argument, 0, 'z'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2013-01-19 12:00:20 -05:00
|
|
|
while((opt = getopt_long(argc, argv, "fhk:rt:z", opttable, NULL)) != -1) {
|
2011-08-05 21:20:20 -04:00
|
|
|
switch(opt) {
|
2013-01-19 12:00:20 -05:00
|
|
|
case 'f':
|
|
|
|
opts.filemode = 1;
|
|
|
|
break;
|
2011-08-05 21:20:20 -04:00
|
|
|
case 'h':
|
2014-01-28 21:40:37 -05:00
|
|
|
opts.help = 1;
|
|
|
|
return 0;
|
2011-08-05 21:20:20 -04:00
|
|
|
case 'k':
|
|
|
|
opts.sortkey = (int)strtol(optarg, NULL, 10);
|
|
|
|
if(opts.sortkey <= 0) {
|
|
|
|
fprintf(stderr, "error: invalid sort key -- %s\n", optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
opts.order = -1;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
opts.delim = escape_char(optarg);
|
|
|
|
if(opts.delim == -1) {
|
|
|
|
fprintf(stderr, "error: invalid field separator -- `%s'\n", optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
opts.null = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct list_t *list;
|
|
|
|
struct buffer_t *buffer;
|
|
|
|
size_t i;
|
2015-08-10 15:42:39 -04:00
|
|
|
int ret = 0;
|
2011-08-05 21:20:20 -04:00
|
|
|
|
|
|
|
/* option defaults */
|
|
|
|
opts.order = 1;
|
|
|
|
opts.delim = DELIM;
|
|
|
|
opts.sortkey = 0;
|
|
|
|
opts.null = 0;
|
|
|
|
|
|
|
|
if(parse_options(argc, argv) != 0) {
|
|
|
|
usage();
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2014-01-28 21:40:37 -05:00
|
|
|
if(opts.help) {
|
|
|
|
usage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-05 21:20:20 -04:00
|
|
|
list = list_new(100);
|
|
|
|
buffer = buffer_new(BUFSIZ * 3);
|
|
|
|
|
|
|
|
if(optind == argc) {
|
|
|
|
if(splitfile(stdin, buffer, list) != 0) {
|
|
|
|
fprintf(stderr, "%s: memory exhausted\n", argv[0]);
|
2015-08-10 15:42:39 -04:00
|
|
|
ret = ENOMEM;
|
|
|
|
goto cleanup;
|
2011-08-05 21:20:20 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while(optind < argc) {
|
|
|
|
FILE *input = fopen(argv[optind], "r");
|
|
|
|
if(input) {
|
|
|
|
if(splitfile(input, buffer, list) != 0) {
|
|
|
|
fprintf(stderr, "%s: memory exhausted\n", argv[0]);
|
2015-08-10 15:42:39 -04:00
|
|
|
fclose(input);
|
|
|
|
ret = ENOMEM;
|
|
|
|
goto cleanup;
|
2011-08-05 21:20:20 -04:00
|
|
|
}
|
|
|
|
fclose(input);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s: %s: %s\n", argv[0], argv[optind], strerror(errno));
|
|
|
|
}
|
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(list->count) {
|
|
|
|
const char linedelim = opts.null ? '\0' : '\n';
|
2014-11-18 09:02:06 -05:00
|
|
|
qsort(list->list, list->count, sizeof(void *), vercmp);
|
2011-08-05 21:20:20 -04:00
|
|
|
for(i = 0; i < list->count; i++) {
|
2014-11-18 09:02:06 -05:00
|
|
|
const struct input_t *in = list->list[i];
|
|
|
|
printf("%s%c", in->data, linedelim);
|
2011-08-05 21:20:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-10 15:42:39 -04:00
|
|
|
cleanup:
|
2014-11-18 09:02:06 -05:00
|
|
|
list_free(list, input_free);
|
2011-08-05 21:20:20 -04:00
|
|
|
buffer_free(buffer);
|
|
|
|
|
2015-08-10 15:42:39 -04:00
|
|
|
return ret;
|
2011-08-05 21:20:20 -04:00
|
|
|
}
|
|
|
|
|
2014-01-22 18:06:11 -05:00
|
|
|
/* vim: set noet: */
|