nightmaremail

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

qmail-newmrh.c (1826B)


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