debian/rules: Use `git' potty wrapper.
[qmail] / qmail-newmrh.c
1 #include "strerr.h"
2 #include "stralloc.h"
3 #include "substdio.h"
4 #include "getln.h"
5 #include "exit.h"
6 #include "readwrite.h"
7 #include "open.h"
8 #include "auto_qmail.h"
9 #include "cdbmss.h"
10
11 #define FATAL "qmail-newmrh: fatal: "
12
13 void die_read()
14 {
15 strerr_die2sys(111,FATAL,"unable to read control/morercpthosts: ");
16 }
17 void die_write()
18 {
19 strerr_die2sys(111,FATAL,"unable to write to control/morercpthosts.tmp: ");
20 }
21
22 char inbuf[1024];
23 substdio ssin;
24
25 int fd;
26 int fdtemp;
27
28 struct cdbmss cdbmss;
29 stralloc line = {0};
30 int match;
31
32 void main()
33 {
34 umask(033);
35 if (chdir(auto_qmail) == -1)
36 strerr_die4sys(111,FATAL,"unable to chdir to ",auto_qmail,": ");
37
38 fd = open_read("control/morercpthosts");
39 if (fd == -1) die_read();
40
41 substdio_fdbuf(&ssin,read,fd,inbuf,sizeof inbuf);
42
43 fdtemp = open_trunc("control/morercpthosts.tmp");
44 if (fdtemp == -1) die_write();
45
46 if (cdbmss_start(&cdbmss,fdtemp) == -1) die_write();
47
48 for (;;) {
49 if (getln(&ssin,&line,&match,'\n') != 0) die_read();
50 case_lowerb(line.s,line.len);
51 while (line.len) {
52 if (line.s[line.len - 1] == ' ') { --line.len; continue; }
53 if (line.s[line.len - 1] == '\n') { --line.len; continue; }
54 if (line.s[line.len - 1] == '\t') { --line.len; continue; }
55 if (line.s[0] != '#')
56 if (cdbmss_add(&cdbmss,line.s,line.len,"",0) == -1)
57 die_write();
58 break;
59 }
60 if (!match) break;
61 }
62
63 if (cdbmss_finish(&cdbmss) == -1) die_write();
64 if (fsync(fdtemp) == -1) die_write();
65 if (close(fdtemp) == -1) die_write(); /* NFS stupidity */
66 if (rename("control/morercpthosts.tmp","control/morercpthosts.cdb") == -1)
67 strerr_die2sys(111,FATAL,"unable to move control/morercpthosts.tmp to control/morercpthosts.cdb");
68
69 _exit(0);
70 }