File: mlmmj-list.c

package info (click to toggle)
mlmmj 1.2.17-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,836 kB
  • ctags: 585
  • sloc: ansic: 8,973; sh: 4,521; php: 1,057; perl: 643; makefile: 156
file content (207 lines) | stat: -rw-r--r-- 4,795 bytes parent folder | download
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* Copyright (C) 2004 Mads Martin Joergensen <mmj at mmj.dk>
 *
 * $Id: mlmmj-list.c,v 1.4 2008/10/30 19:13:09 mortenp Exp $
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

#include "mlmmj.h"
#include "wrappers.h"
#include "memory.h"
#include "strgen.h"
#include "log_error.h"

static void print_help(const char *prg)
{
        printf("Usage: %s -L /path/to/listdir [-c] [-d] [-h] [-m] [-n] "
			"[-o] [-s] [-V]\n"
	       " -L: Full path to list directory\n"
	       " -c: Print subscriber count\n"
	       " -d: Print for digesters list\n"
	       " -h: This help\n"
	       " -m: Print moderators for list\n"
	       " -n: Print for nomail version of list\n"
	       " -o: Print owner(s) of list\n"
	       " -s: Print normal subscribers (default) \n"
	       " -V: Print version\n", prg);
	exit(EXIT_SUCCESS);
}

int dumpcount(const char *filename, int *count)
{
	int fd;
	char *start, *next, *cur;
	struct stat st;
	size_t len;
	
	if((fd = open(filename, O_RDONLY)) < 0)
		return -1;

	if(stat(filename, &st) < 0)
		return -1;
	
	if(!S_ISREG(st.st_mode))
		return -1;
	
	/* Nobody there */
	if(st.st_size == 0) {
		return 0;
	}

	start = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
	if(start == MAP_FAILED)
		return -1;

	for(next = cur = start; next < start + st.st_size; next++) {
		if(*next == '\n' || next == start + st.st_size - 1) {
			len = next - cur;
			if(next == start + st.st_size - 1 && *next != '\n')
				len++;
			if(count)
				(*count)++;
			else {
				writen(STDOUT_FILENO, cur, len);
				writen(STDOUT_FILENO, "\n", 1);
			}
			cur = next + 1;
		}
	}
	munmap(start, st.st_size);
	close(fd);

	return 0;
}

int main(int argc, char **argv)
{
	int opt, ret, count = 0, docount = 0;
	char *listdir = NULL, *fileiter = NULL, *tmp;
	char *subddir, *subdir, *subfile = NULL;
	DIR *dirp;
	struct dirent *dp;
	enum subtype typesub = SUB_NORMAL;

	while ((opt = getopt(argc, argv, "cdhmnosVL:")) != -1) {
		switch(opt) {
		case 'c':
			docount = 1;
			break;
		case 'd':
			typesub = SUB_DIGEST;
			break;
		case 'h':
			print_help(argv[0]);
			break;
		case 'L':
			listdir = optarg;
			break;
		case 'm':
			typesub = SUB_FILE;
			subfile = "/control/moderators";
			break;
		case 'n':
			typesub = SUB_NOMAIL;
			break;
		case 'o':
			typesub = SUB_FILE;
			subfile = "/control/owner";
			break;
		case 'V':
			print_version(argv[0]);
			exit(EXIT_SUCCESS);
		default:
		case 's':
			typesub = SUB_NORMAL;
			break;
		}
	}

	if(listdir == NULL) {
		fprintf(stderr, "You have to specify -L\n"
				"%s -h for help\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	switch(typesub) {
		default:
		case SUB_NORMAL:
			subddir = "/subscribers.d/";
			break;
		case SUB_DIGEST:
			subddir = "/digesters.d/";
			break;
		case SUB_NOMAIL:
			subddir = "/nomailsubs.d/";
			break;
		case SUB_FILE:
			subddir = NULL;
			break;
	}
	
	if(subddir)
		subdir = concatstr(2, listdir, subddir);
	else
		subdir = NULL;

	if(subdir) {
		dirp = opendir(subdir);
		if(dirp == NULL) {
			fprintf(stderr, "Could not opendir(%s);\n", subdir);
			exit(EXIT_FAILURE);
		}
		while((dp = readdir(dirp)) != NULL) {
			if((strcmp(dp->d_name, "..") == 0) ||
					(strcmp(dp->d_name, ".") == 0))
				continue;

			fileiter = concatstr(2, subdir, dp->d_name);

			if(docount)
				dumpcount(fileiter, &count);
			else
				ret = dumpcount(fileiter, NULL);

			myfree(fileiter);
		}
		myfree(subdir);
		closedir(dirp);
	} else {
		tmp = concatstr(2, listdir, subfile);
		if(docount)
			dumpcount(tmp, &count);
		else
			dumpcount(tmp, NULL);
	}

	if(docount)
		printf("%d\n", count);


	return 0;
}