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)  

grouplist.c
Go to the documentation of this file.
1/*
2 grouplist.c -- determine list of all groups in the spool.
3 Copyright (C) 2002 - 2010 Matthias Andree
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19#include "leafnode.h"
20#include "mastring.h"
21
22#include <unistd.h>
23#include <sys/types.h>
24#include "system.h"
25#include <sys/stat.h>
26#include <string.h>
27#include <stdlib.h>
28#include "wantassert.h"
29#include <assert.h>
30
31#ifdef TEST
32#include <stdio.h>
33#else
34#include "ln_log.h"
35#endif
36
37static int myscandir(struct stringlist **l, const char *prefix, int toplevel)
38{
39 DIR *d = opendir(".");
40 struct dirent *de;
41 int added = 0, founddir = 0;
42
43 assert(l);
44 assert(prefix);
45
46 if (d == NULL) {
47#ifdef TEST
48 fprintf(stderr, "cannot open directory: %s, see next error\n", strerror(errno));
49#else
51 "cannot open directory: %m, see next error");
52#endif
53 return -1;
54 }
55 while ((de = readdir(d)) != NULL) {
56 struct stat st;
57 mastr *p;
58
59 if (strcmp(de->d_name, ".overview") != 0
60 && strchr(de->d_name, '.') != NULL) {
61 continue;
62 }
63 if (toplevel && strcmp(de->d_name, "lost+found") == 0) {
64 continue;
65 }
66 p = mastr_new(4096);
67 if (lstat(de->d_name, &st)) goto bail;
68 if (added == 0 && S_ISREG(st.st_mode)) {
69 added = 1;
70 prependtolist(l, prefix);
71 } else if (S_ISDIR(st.st_mode)) {
72 int offset;
73 founddir = 1;
74 offset = (de->d_name[0] == '-');
75 if (prefix != NULL && *prefix != '\0')
76 (void)mastr_vcat(p, prefix, ".", de->d_name + offset, NULL);
77 else
78 (void)mastr_cpy(p, de->d_name);
79 if (chdir(de->d_name) == 0) {
80 if (myscandir(l, mastr_str(p), 0)) {
81#ifdef TEST
82 fprintf(stderr, "myscandir(\"%s\") failed.\n", de->d_name);
83#else
84 ln_log(LNLOG_SERR, LNLOG_CTOP, "myscandir(\"%s\") failed.", de->d_name);
85#endif
86 goto bail;
87 }
88 if (chdir("..")) goto bail;
89 } else {
90#ifdef TEST
91 fprintf(stderr, "chdir(\"%s\") failed: %s\n", de->d_name, strerror(errno));
92#else
93 ln_log(LNLOG_SERR, LNLOG_CTOP, "chdir(\"%s\") failed: %m", de->d_name);
94#endif
95 }
96 mastr_delete(p);
97 continue;
98 bail:
99 mastr_delete(p);
100 (void)closedir(d);
101 return -1;
102 }
103 mastr_delete(p);
104 }
105 (void)closedir(d);
106 if (!added && !founddir) {
107 prependtolist(l, prefix);
108 }
109 return 0;
110}
111
112/*@null@*/ struct stringlist *
114{
115 struct stringlist *g = NULL;
116
117 if (chdir(spooldir)) return NULL;
118 if (myscandir(&g, "", 1) != 0 && g != NULL) {
119 freelist(g);
120 g = NULL;
121 }
122 return g;
123}
124
125#ifdef TEST
126int verbose=0;
127int debug=0;
128
129int main(int argc, char **argv) {
130 struct stringlist *t, *q, *l;
131
132 if (argc > 1) { spooldir = argv[1]; }
133 l = get_grouplist();
134 if (!l) fputs("failed\n", stderr);
135 else for (t = l; t; ) {
136 puts(t->string);
137 q = t->next;
138 free(t);
139 t = q;
140 }
141 return 0;
142}
143#endif
int main(void)
Definition: amiroot.c:12
int verbose
Definition: applyfilter.c:31
int debug
Definition: applyfilter.c:30
static int myscandir(struct stringlist **l, const char *prefix, int toplevel)
Definition: grouplist.c:37
struct stringlist * get_grouplist(void)
Definition: grouplist.c:113
const char * spooldir
void freelist(struct stringlist *list)
Definition: miscutil.c:626
void prependtolist(struct stringlist **list, const char *newentry)
Definition: miscutil.c:558
void ln_log(int sev, int ctx, const char *format,...)
Definition: ln_log.c:103
#define LNLOG_SERR
Definition: ln_log.h:13
#define LNLOG_CTOP
Definition: ln_log.h:22
int mastr_cpy(mastr *m, const char *s)
Definition: mastring.c:102
void mastr_delete(mastr *m)
Definition: mastring.c:223
mastr * mastr_new(size_t size)
Definition: mastring.c:62
int mastr_vcat(mastr *m,...)
Definition: mastring.c:147
#define mastr_str(m)
Definition: mastring.h:57
Definition: mastring.h:28
struct stringlist * next
Definition: leafnode.h:166
char string[1]
Definition: leafnode.h:167
#define dirent
Definition: system.h:21