s/qmail 4.2.29a
Next generation secure email transport
Loading...
Searching...
No Matches
setmaillist.c
Go to the documentation of this file.
1#include <unistd.h>
2#include <sys/stat.h>
3#include "buffer.h"
4#include "logmsg.h"
5#include "stralloc.h"
6#include "getln.h"
7#include "open.h"
8#include "byte.h"
9
10#define WHO "setmaillist"
11
12int rename(const char *,const char *); // stdio.h
13
14void usage()
15{
16 logmsg(WHO,100,USAGE,"setmaillist list.bin list.tmp");
17}
18
19stralloc line = {0};
21
22char *fnbin;
23char *fntmp;
24int fd;
25char buf[1024];
26buffer bo;
27
29{
30 logmsg(WHO,111,FATAL,B("unable to write to: ",fntmp));
31}
32
33static void out(char *s,int len)
34{
35 if (buffer_put(&bo,s,len) == -1) writeerr();
36}
37
38int main(int argc,char **argv)
39{
40 umask(033);
41
42 fnbin = argv[1]; if (!fnbin) usage();
43 fntmp = argv[2]; if (!fntmp) usage();
44
45 fd = open_trunc(fntmp);
46 if (fd == -1)
47 logmsg(WHO,111,FATAL,B("unable to create: ",fntmp));
48
49 buffer_init(&bo,write,fd,buf,sizeof(buf));
50
51
52 do {
53 if (getln(buffer_0small,&line,&match,'\n') == -1)
54 logmsg(WHO,111,FATAL,"unable to read input: ");
55
56 while (line.len) {
57 if (line.s[line.len - 1] != '\n')
58 if (line.s[line.len - 1] != ' ')
59 if (line.s[line.len - 1] != '\t')
60 break;
61 --line.len;
62 }
63
64 if (byte_chr(line.s,line.len,'\0') != line.len)
65 logmsg(WHO,111,FATAL,"NUL in input");
66
67 if (line.len)
68 if (line.s[0] != '#') {
69 if ((line.s[0] == '.') || (line.s[0] == '/')) {
70 out(line.s,line.len);
71 out("",1);
72 }
73 else {
74 if (line.len > 800)
75 logmsg(WHO,111,FATAL,"addresses must be under 800 bytes");
76 if (line.s[0] != '&')
77 out("&",1);
78 out(line.s,line.len);
79 out("",1);
80 }
81 }
82
83 } while (match);
84
85 if (buffer_flush(&bo) == -1) writeerr();
86 if (fsync(fd) == -1) writeerr();
87 if (close(fd) == -1) writeerr(); /* NFS stupidity */
88
89 if (rename(fntmp,fnbin) == -1)
90 logmsg(WHO,111,FATAL,B("unable to move ",fntmp," to: ",fnbin));
91
92 _exit(0);
93}
int main()
Definition: chkshsgr.c:6
stralloc out
Definition: dnscname.c:12
void _exit()
char * fntmp
Definition: setmaillist.c:23
void usage()
Definition: setmaillist.c:14
char * fnbin
Definition: setmaillist.c:22
int rename(const char *, const char *)
int fd
Definition: setmaillist.c:24
stralloc line
Definition: setmaillist.c:19
char buf[1024]
Definition: setmaillist.c:25
void writeerr()
Definition: setmaillist.c:28
int match
Definition: setmaillist.c:20
buffer bo
Definition: setmaillist.c:26
#define WHO
Definition: setmaillist.c:10
void write()