Files
sbase/sort.c

79 lines
1.6 KiB
C
Raw Normal View History

2011-06-02 13:03:34 +01:00
/* See LICENSE file for copyright and license details. */
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "text.h"
#include "util.h"
static int linecmp(const char **, const char **);
static bool rflag = false;
2011-06-02 13:09:30 +01:00
static bool uflag = false;
struct linebuf {
char **lines;
long nlines;
};
#define EMPTY_LINEBUF {NULL, 0,}
static struct linebuf linebuf = EMPTY_LINEBUF;
static void getlines(FILE *, struct linebuf *);
2011-06-02 13:03:34 +01:00
int
main(int argc, char *argv[])
{
char c;
long i;
FILE *fp;
2011-06-02 13:09:30 +01:00
while((c = getopt(argc, argv, "ru")) != -1)
2011-06-02 13:03:34 +01:00
switch(c) {
case 'r':
rflag = true;
break;
2011-06-02 13:09:30 +01:00
case 'u':
uflag = true;
break;
2011-06-02 13:03:34 +01:00
default:
2012-05-20 14:38:52 +00:00
exit(2);
2011-06-02 13:03:34 +01:00
}
if(optind == argc)
getlines(stdin, &linebuf);
2011-06-02 13:03:34 +01:00
else for(; optind < argc; optind++) {
if(!(fp = fopen(argv[optind], "r")))
eprintf("fopen %s:", argv[optind]);
getlines(fp, &linebuf);
2011-06-02 13:03:34 +01:00
fclose(fp);
}
qsort(linebuf.lines, linebuf.nlines, sizeof *linebuf.lines, (int (*)(const void *, const void *))linecmp);
2011-06-02 13:03:34 +01:00
for(i = 0; i < linebuf.nlines; i++)
if(!uflag || i == 0 || strcmp(linebuf.lines[i], linebuf.lines[i-1]) != 0)
fputs(linebuf.lines[i], stdout);
2011-06-02 13:03:34 +01:00
return EXIT_SUCCESS;
}
void
getlines(FILE *fp, struct linebuf *b)
2011-06-02 13:03:34 +01:00
{
char *line = NULL;
size_t size = 0;
while(afgets(&line, &size, fp)) {
if(!(b->lines = realloc(b->lines, ++b->nlines * sizeof *b->lines)))
2011-06-02 13:03:34 +01:00
eprintf("realloc:");
if(!(b->lines[b->nlines-1] = malloc(strlen(line)+1)))
2011-06-02 13:03:34 +01:00
eprintf("malloc:");
strcpy(b->lines[b->nlines-1], line);
2011-06-02 13:03:34 +01:00
}
free(line);
}
int
linecmp(const char **a, const char **b)
{
return strcmp(*a, *b) * (rflag ? -1 : +1);
}