File: lsort.c

package info (click to toggle)
leafnode 1.11.11-1
  • links: PTS
  • area: main
  • in suites: bullseye, sid
  • size: 2,468 kB
  • sloc: ansic: 10,914; sh: 1,748; xml: 628; makefile: 291; perl: 84; sed: 4
file content (79 lines) | stat: -rw-r--r-- 1,906 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
lsort - re-sort groupinfo files from versions prior to 1.9.3

Written and Copyright 1999 by Joerg Dietrich <joerg@dietrich.net>
Modified and copyright of the modifications 2002 by Ralf Wildenhues
<ralf.wildenhues@gmx.de>.
Modified and copyright of the modifications 2002, 2009 by Matthias
Andree <matthias.andree@gmx.de>

See file COPYING for restrictions on the use of this software.
*/

#include "leafnode.h"
#include "critmem.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>

int verbose = 0;
int debug = 0;

static int
comp(const void *a, const void *b)
{
    return strcasecmp(*(const char *const *)a, *(const char *const *)b);
}

int
main(void)
{
    char *l;
    char *path; /* RATS: ignore */
    size_t l_path;
    const char *tackon = "/leaf.node/groupinfo.old";
    const char *const myname = "lsort";
    char **act = NULL;
    unsigned long acount = 0, allocd = 0;
    unsigned long i;
    FILE *f;
    int err = 0;

    critsyslog(0);
    l_path = strlen(spooldir) + strlen(tackon) + 1;
    path = critmalloc(l_path + 1, myname);
    xsnprintf(path, l_path, "%s%s", spooldir, tackon);

    f = fopen(path, "r");
    if (f == NULL) {
	fprintf(stderr, "lsort: cannot open \"%s\": %s.\n", path, strerror(errno));
	exit(EXIT_FAILURE);
    }

    while ((l = getaline(f))) {
	if (acount >= allocd) {
	    allocd ? (allocd <<= 1) : (allocd = 256);
	    act = (char **)critrealloc((char *)act, allocd * sizeof(char *), myname);
	}
	act[acount] = critstrdup(l, myname);
	acount++;
    }
    fclose(f);

    qsort(act, acount, sizeof(char *), &comp);

    clearerr(stdout);
    for (i = 0; i < acount; i++) {
	int ok;
	ok = printf("%s\n", act[i]);
	free(act[i]);
	if (ok < 0) { err = 1; break; }
    }
    free(act);

    if (fflush(stdout)) err = 1;
    if (ferror(stdout)) err = 1;
    if (err) { perror("lsort"); }
    return err ? EXIT_FAILURE : EXIT_SUCCESS;
}