chiark / gitweb /
Import ezmlm 0.53
[ezmlm] / ezmlm-list.c
1 #include "stralloc.h"
2 #include "substdio.h"
3 #include "getln.h"
4 #include "strerr.h"
5 #include "error.h"
6 #include "readwrite.h"
7 #include "exit.h"
8 #include "open.h"
9
10 #define FATAL "ezmlm-list: fatal: "
11 void die_write()
12 {
13   strerr_die2sys(111,FATAL,"unable to write: ");
14 }
15
16 char outbuf[1024];
17 substdio out = SUBSTDIO_FDBUF(write,1,outbuf,sizeof(outbuf));
18 char inbuf[1024];
19 substdio in;
20
21 stralloc line = {0};
22
23 char fn[14] = "subscribers/?";
24
25 void main(argc,argv)
26 int argc;
27 char **argv;
28 {
29   char *dir;
30   int fd;
31   int match;
32
33   dir = argv[1];
34   if (!dir) strerr_die1x(100,"ezmlm-list: usage: ezmlm-list dir");
35
36   if (chdir(dir) == -1)
37     strerr_die4sys(111,FATAL,"unable to switch to ",dir,": ");
38
39   for (fn[12] = 64;fn[12] < 64 + 53;++fn[12]) {
40     fd = open_read(fn);
41     if (fd == -1) {
42       if (errno != error_noent)
43         strerr_die4sys(111,FATAL,"unable to open ",fn,": ");
44     }
45     else {
46       substdio_fdbuf(&in,read,fd,inbuf,sizeof(inbuf));
47       for (;;) {
48         if (getln(&in,&line,&match,'\0') == -1)
49           strerr_die4sys(111,FATAL,"unable to read ",fn,": ");
50         if (!match) break;
51         if (line.s[str_chr(line.s,'\n')])
52           strerr_die3x(111,FATAL,"newline in ",fn);
53         if (substdio_puts(&out,line.s + 1)) die_write();
54         if (substdio_put(&out,"\n",1) == -1) die_write();
55       }
56     }
57
58   }
59
60   if (substdio_flush(&out) == -1) die_write();
61   _exit(0);
62 }