--- newaliases.c.orig Tue May 19 13:25:42 1998 +++ newaliases.c Mon Feb 13 09:53:58 2006 @@ -12,6 +12,10 @@ #define FATAL "newaliases: fatal: " +stralloc fnsrc = {0}; +stralloc fncdb = {0}; +stralloc fntmp = {0}; + void nomem() { strerr_die2x(111,FATAL,"out of memory"); @@ -26,11 +30,11 @@ } void writeerr() { - strerr_die2sys(111,FATAL,"unable to write to /etc/aliases.tmp: "); + strerr_die4sys(111,FATAL,"unable to write to ",fntmp.s,": "); } void readerr() { - strerr_die2sys(111,FATAL,"unable to read /etc/aliases: "); + strerr_die4sys(111,FATAL,"unable to read ",fnsrc.s,": "); } void die_control() { @@ -270,19 +274,32 @@ if (cdbmss_add(&cdbmss,key.s,key.len,instr.s,instr.len) == -1) writeerr(); } -void main() +void main(argc,argv) +int argc; +char **argv; { int fd; umask(033); readcontrols(); - fd = open_read("/etc/aliases"); + if (!stralloc_copys(&fnsrc,argv[1] ? argv[1] : "/etc/aliases")) nomem(); + if (!stralloc_0(&fnsrc)) nomem(); + + if (!stralloc_copys(&fncdb,fnsrc.s)) nomem(); + if (!stralloc_cats(&fncdb,".cdb")) nomem(); + if (!stralloc_0(&fncdb)) nomem(); + + if (!stralloc_copys(&fntmp,fnsrc.s)) nomem(); + if (!stralloc_cats(&fntmp,".tmp")) nomem(); + if (!stralloc_0(&fntmp)) nomem(); + + fd = open_read(fnsrc.s); if (fd == -1) readerr(); substdio_fdbuf(&ssin,read,fd,inbuf,sizeof inbuf); - fd = open_trunc("/etc/aliases.tmp"); - if (fd == -1) strerr_die2sys(111,FATAL,"unable to create /etc/aliases.tmp: "); + fd = open_trunc(fntmp.s); + if (fd == -1) strerr_die4sys(111,FATAL,"unable to create ",fntmp.s,": "); if (cdbmss_start(&cdbmss,fd) == -1) writeerr(); if (!stralloc_copys(&line,"")) nomem(); @@ -314,8 +331,8 @@ if (fsync(fd) == -1) writeerr(); if (close(fd) == -1) writeerr(); /* NFS stupidity */ - if (rename("/etc/aliases.tmp","/etc/aliases.cdb") == -1) - strerr_die2sys(111,FATAL,"unable to move /etc/aliases.tmp to /etc/aliases.cdb: "); + if (rename(fntmp.s,fncdb.s) == -1) + strerr_die6sys(111,FATAL,"unable to move ",fntmp.s," to ",fncdb.s,": "); _exit(0); }