leafnode  1.12.0
About: Leafnode is a store & forward NNTP proxy for small (dialup) sites.
  Fossies Dox: leafnode-1.12.0.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

lsort.c
Go to the documentation of this file.
1/*
2lsort - re-sort groupinfo files from versions prior to 1.9.3
3
4Written and Copyright 1999 by Joerg Dietrich <joerg@dietrich.net>
5Modified and copyright of the modifications 2002 by Ralf Wildenhues
6<ralf.wildenhues@gmx.de>.
7Modified and copyright of the modifications 2002, 2009 by Matthias
8Andree <matthias.andree@gmx.de>
9
10See 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
20int verbose = 0;
21int debug = 0;
22
23static int
24comp(const void *a, const void *b)
25{
26 return strcasecmp(*(const char *const *)a, *(const char *const *)b);
27}
28
29int
30main(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}
char * critrealloc(char *a, size_t size, const char *message)
Definition: critmem.c:79
char * critstrdup(const char *source, const char *message)
Definition: critmem.c:92
char * critmalloc(size_t size, const char *message)
Definition: critmem.c:61
void critsyslog(int do_log)
Definition: critmem.c:101
char * getaline(FILE *f)
Definition: getaline.c:84
const char * spooldir
int xsnprintf(char *str, size_t n, const char *format,...)
Definition: miscutil.c:798
int verbose
Definition: lsort.c:20
static int comp(const void *a, const void *b)
Definition: lsort.c:24
int main(void)
Definition: lsort.c:30
int debug
Definition: lsort.c:21