Mercurial > hg > mlmmj
annotate src/mlmmj-list.c @ 756:f8f3f4525a2b
Cosmetic changes to help messages
author | Ben Schmidt |
---|---|
date | Fri, 08 Oct 2010 01:47:46 +1100 |
parents | 945ef20e9d08 |
children |
rev | line source |
---|---|
367 | 1 /* Copyright (C) 2004 Mads Martin Joergensen <mmj at mmj.dk> |
2 * | |
3 * $Id$ | |
4 * | |
5 * Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 * of this software and associated documentation files (the "Software"), to | |
7 * deal in the Software without restriction, including without limitation the | |
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
9 * sell copies of the Software, and to permit persons to whom the Software is | |
10 * furnished to do so, subject to the following conditions: | |
11 * | |
12 * The above copyright notice and this permission notice shall be included in | |
13 * all copies or substantial portions of the Software. | |
14 * | |
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
21 * IN THE SOFTWARE. | |
22 */ | |
23 | |
24 #include <stdio.h> | |
25 #include <stdlib.h> | |
26 #include <unistd.h> | |
27 #include <fcntl.h> | |
28 #include <dirent.h> | |
29 #include <string.h> | |
30 #include <sys/types.h> | |
31 #include <sys/stat.h> | |
32 #include <sys/mman.h> | |
33 | |
34 #include "mlmmj.h" | |
35 #include "wrappers.h" | |
36 #include "memory.h" | |
37 #include "strgen.h" | |
38 #include "log_error.h" | |
39 | |
40 static void print_help(const char *prg) | |
41 { | |
756 | 42 printf("Usage: %s -L /path/to/listdir\n" |
43 " [-c] [-d] [-h] [-m] [-n] [-o] [-s] [-V]\n" | |
367 | 44 " -L: Full path to list directory\n" |
389 | 45 " -c: Print subscriber count\n" |
367 | 46 " -d: Print for digesters list\n" |
389 | 47 " -h: This help\n" |
48 " -m: Print moderators for list\n" | |
378
dcf35885d50d
Make the mlmmj-list switches more compatible with the rest
mmj
parents:
367
diff
changeset
|
49 " -n: Print for nomail version of list\n" |
389 | 50 " -o: Print owner(s) of list\n" |
51 " -s: Print normal subscribers (default) \n" | |
367 | 52 " -V: Print version\n", prg); |
53 exit(EXIT_SUCCESS); | |
54 } | |
55 | |
389 | 56 int dumpcount(const char *filename, int *count) |
57 { | |
58 int fd; | |
59 char *start, *next, *cur; | |
60 struct stat st; | |
61 size_t len; | |
62 | |
63 if((fd = open(filename, O_RDONLY)) < 0) | |
64 return -1; | |
65 | |
66 if(stat(filename, &st) < 0) | |
67 return -1; | |
68 | |
69 if(!S_ISREG(st.st_mode)) | |
70 return -1; | |
663
945ef20e9d08
Fixed mmap()ing of zero-sized files (Robin H. Johnson)
mortenp
parents:
389
diff
changeset
|
71 |
945ef20e9d08
Fixed mmap()ing of zero-sized files (Robin H. Johnson)
mortenp
parents:
389
diff
changeset
|
72 /* Nobody there */ |
945ef20e9d08
Fixed mmap()ing of zero-sized files (Robin H. Johnson)
mortenp
parents:
389
diff
changeset
|
73 if(st.st_size == 0) { |
945ef20e9d08
Fixed mmap()ing of zero-sized files (Robin H. Johnson)
mortenp
parents:
389
diff
changeset
|
74 return 0; |
945ef20e9d08
Fixed mmap()ing of zero-sized files (Robin H. Johnson)
mortenp
parents:
389
diff
changeset
|
75 } |
389 | 76 |
77 start = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); | |
78 if(start == MAP_FAILED) | |
79 return -1; | |
80 | |
81 for(next = cur = start; next < start + st.st_size; next++) { | |
82 if(*next == '\n' || next == start + st.st_size - 1) { | |
83 len = next - cur; | |
84 if(next == start + st.st_size - 1 && *next != '\n') | |
85 len++; | |
86 if(count) | |
87 (*count)++; | |
88 else { | |
89 writen(STDOUT_FILENO, cur, len); | |
90 writen(STDOUT_FILENO, "\n", 1); | |
91 } | |
92 cur = next + 1; | |
93 } | |
94 } | |
95 munmap(start, st.st_size); | |
96 close(fd); | |
97 | |
98 return 0; | |
99 } | |
100 | |
367 | 101 int main(int argc, char **argv) |
102 { | |
389 | 103 int opt, ret, count = 0, docount = 0; |
104 char *listdir = NULL, *fileiter = NULL, *tmp; | |
105 char *subddir, *subdir, *subfile = NULL; | |
367 | 106 DIR *dirp; |
107 struct dirent *dp; | |
108 enum subtype typesub = SUB_NORMAL; | |
109 | |
389 | 110 while ((opt = getopt(argc, argv, "cdhmnosVL:")) != -1) { |
367 | 111 switch(opt) { |
378
dcf35885d50d
Make the mlmmj-list switches more compatible with the rest
mmj
parents:
367
diff
changeset
|
112 case 'c': |
dcf35885d50d
Make the mlmmj-list switches more compatible with the rest
mmj
parents:
367
diff
changeset
|
113 docount = 1; |
dcf35885d50d
Make the mlmmj-list switches more compatible with the rest
mmj
parents:
367
diff
changeset
|
114 break; |
367 | 115 case 'd': |
116 typesub = SUB_DIGEST; | |
117 break; | |
118 case 'h': | |
119 print_help(argv[0]); | |
120 break; | |
121 case 'L': | |
122 listdir = optarg; | |
123 break; | |
389 | 124 case 'm': |
125 typesub = SUB_FILE; | |
126 subfile = "/control/moderators"; | |
127 break; | |
367 | 128 case 'n': |
129 typesub = SUB_NOMAIL; | |
130 break; | |
389 | 131 case 'o': |
132 typesub = SUB_FILE; | |
133 subfile = "/control/owner"; | |
134 break; | |
367 | 135 case 'V': |
136 print_version(argv[0]); | |
137 exit(EXIT_SUCCESS); | |
389 | 138 default: |
139 case 's': | |
140 typesub = SUB_NORMAL; | |
141 break; | |
367 | 142 } |
143 } | |
144 | |
145 if(listdir == NULL) { | |
146 fprintf(stderr, "You have to specify -L\n" | |
147 "%s -h for help\n", argv[0]); | |
148 exit(EXIT_FAILURE); | |
149 } | |
150 | |
151 switch(typesub) { | |
152 default: | |
153 case SUB_NORMAL: | |
389 | 154 subddir = "/subscribers.d/"; |
367 | 155 break; |
156 case SUB_DIGEST: | |
389 | 157 subddir = "/digesters.d/"; |
367 | 158 break; |
159 case SUB_NOMAIL: | |
389 | 160 subddir = "/nomailsubs.d/"; |
161 break; | |
162 case SUB_FILE: | |
163 subddir = NULL; | |
367 | 164 break; |
165 } | |
166 | |
389 | 167 if(subddir) |
168 subdir = concatstr(2, listdir, subddir); | |
169 else | |
170 subdir = NULL; | |
367 | 171 |
389 | 172 if(subdir) { |
173 dirp = opendir(subdir); | |
174 if(dirp == NULL) { | |
175 fprintf(stderr, "Could not opendir(%s);\n", subdir); | |
176 exit(EXIT_FAILURE); | |
177 } | |
178 while((dp = readdir(dirp)) != NULL) { | |
179 if((strcmp(dp->d_name, "..") == 0) || | |
180 (strcmp(dp->d_name, ".") == 0)) | |
181 continue; | |
367 | 182 |
389 | 183 fileiter = concatstr(2, subdir, dp->d_name); |
184 | |
185 if(docount) | |
186 dumpcount(fileiter, &count); | |
187 else | |
188 ret = dumpcount(fileiter, NULL); | |
189 | |
367 | 190 myfree(fileiter); |
191 } | |
389 | 192 myfree(subdir); |
193 closedir(dirp); | |
194 } else { | |
195 tmp = concatstr(2, listdir, subfile); | |
196 if(docount) | |
197 dumpcount(tmp, &count); | |
198 else | |
199 dumpcount(tmp, NULL); | |
367 | 200 } |
201 | |
202 if(docount) | |
203 printf("%d\n", count); | |
204 | |
205 | |
206 return 0; | |
207 } |