| 1 | #include "substdio.h" |
| 2 | #include "subfd.h" |
| 3 | #include "strerr.h" |
| 4 | #include "stralloc.h" |
| 5 | #include "getln.h" |
| 6 | #include "open.h" |
| 7 | #include "readwrite.h" |
| 8 | |
| 9 | #define FATAL "setmaillist: fatal: " |
| 10 | |
| 11 | void usage() |
| 12 | { |
| 13 | strerr_die1x(100,"setmaillist: usage: setmaillist list.bin list.tmp"); |
| 14 | } |
| 15 | |
| 16 | stralloc line = {0}; |
| 17 | int match; |
| 18 | |
| 19 | char *fnbin; |
| 20 | char *fntmp; |
| 21 | int fd; |
| 22 | substdio ss; |
| 23 | char buf[1024]; |
| 24 | |
| 25 | void writeerr() |
| 26 | { |
| 27 | strerr_die4sys(111,FATAL,"unable to write to ",fntmp,": "); |
| 28 | } |
| 29 | |
| 30 | void out(s,len) |
| 31 | char *s; |
| 32 | int len; |
| 33 | { |
| 34 | if (substdio_put(&ss,s,len) == -1) writeerr(); |
| 35 | } |
| 36 | |
| 37 | void main(argc,argv) |
| 38 | int argc; |
| 39 | char **argv; |
| 40 | { |
| 41 | umask(033); |
| 42 | |
| 43 | fnbin = argv[1]; if (!fnbin) usage(); |
| 44 | fntmp = argv[2]; if (!fntmp) usage(); |
| 45 | |
| 46 | fd = open_trunc(fntmp); |
| 47 | if (fd == -1) |
| 48 | strerr_die4sys(111,FATAL,"unable to create ",fntmp,": "); |
| 49 | |
| 50 | substdio_fdbuf(&ss,write,fd,buf,sizeof buf); |
| 51 | |
| 52 | do { |
| 53 | if (getln(subfdinsmall,&line,&match,'\n') == -1) |
| 54 | strerr_die2sys(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 | strerr_die2x(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 | strerr_die2x(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 | } |
| 84 | while (match); |
| 85 | |
| 86 | if (substdio_flush(&ss) == -1) writeerr(); |
| 87 | if (fsync(fd) == -1) writeerr(); |
| 88 | if (close(fd) == -1) writeerr(); /* NFS stupidity */ |
| 89 | |
| 90 | if (rename(fntmp,fnbin) == -1) |
| 91 | strerr_die6sys(111,FATAL,"unable to move ",fntmp," to ",fnbin,": "); |
| 92 | |
| 93 | _exit(0); |
| 94 | } |