1 /*
2 lsort - re-sort groupinfo files from versions prior to 1.9.3
3 
4 Written and Copyright 1999 by Joerg Dietrich <joerg@dietrich.net>
5 Modified and copyright of the modifications 2002 by Ralf Wildenhues
6 <ralf.wildenhues@gmx.de>.
7 Modified and copyright of the modifications 2002, 2009 by Matthias
8 Andree <matthias.andree@gmx.de>
9 
10 See file COPYING for restrictions on the use of this software.
11 */
12 
13 #include "leafnode.h"
14 #include "critmem.h"
15 #include <stdlib.h>
16 #include <string.h>
17 #include <stdio.h>
18 #include <errno.h>
19 
20 int verbose = 0;
21 int debug = 0;
22 
23 static int
comp(const void * a,const void * b)24 comp(const void *a, const void *b)
25 {
26     return strcasecmp(*(const char *const *)a, *(const char *const *)b);
27 }
28 
29 int
main(void)30 main(void)
31 {
32     char *l;
33     char *path; /* RATS: ignore */
34     size_t l_path;
35     const char *tackon = "/leaf.node/groupinfo.old";
36     const char *const myname = "lsort";
37     char **act = NULL;
38     unsigned long acount = 0, allocd = 0;
39     unsigned long i;
40     FILE *f;
41     int err = 0;
42 
43     critsyslog(0);
44     l_path = strlen(spooldir) + strlen(tackon) + 1;
45     path = critmalloc(l_path + 1, myname);
46     xsnprintf(path, l_path, "%s%s", spooldir, tackon);
47 
48     f = fopen(path, "r");
49     if (f == NULL) {
50 	fprintf(stderr, "lsort: cannot open \"%s\": %s.\n", path, strerror(errno));
51 	exit(EXIT_FAILURE);
52     }
53 
54     while ((l = getaline(f))) {
55 	if (acount >= allocd) {
56 	    allocd ? (allocd <<= 1) : (allocd = 256);
57 	    act = (char **)critrealloc((char *)act, allocd * sizeof(char *), myname);
58 	}
59 	act[acount] = critstrdup(l, myname);
60 	acount++;
61     }
62     fclose(f);
63 
64     qsort(act, acount, sizeof(char *), &comp);
65 
66     clearerr(stdout);
67     for (i = 0; i < acount; i++) {
68 	int ok;
69 	ok = printf("%s\n", act[i]);
70 	free(act[i]);
71 	if (ok < 0) { err = 1; break; }
72     }
73     free(act);
74 
75     if (fflush(stdout)) err = 1;
76     if (ferror(stdout)) err = 1;
77     if (err) { perror("lsort"); }
78     return err ? EXIT_FAILURE : EXIT_SUCCESS;
79 }
80