14 #define FATAL "condredirect: fatal: "
18 int mywrite(fd
,buf
,len
) int fd
; char *buf
; int len
;
20 qmail_put(&qqt
,buf
,len
);
24 char inbuf
[SUBSTDIO_INSIZE
];
26 substdio ssin
= SUBSTDIO_FDBUF(read
,0,inbuf
,sizeof inbuf
);
27 substdio ssout
= SUBSTDIO_FDBUF(mywrite
,-1,outbuf
,sizeof outbuf
);
41 if (!argv
[1] || !argv
[2])
42 strerr_die1x(100,"condredirect: usage: condredirect newaddress program [ arg ... ]");
46 strerr_die2sys(111,FATAL
,"unable to fork: ");
48 execvp(argv
[2],argv
+ 2);
49 if (error_temp(errno
)) _exit(111);
52 if (wait_pid(&wstat
,pid
) == -1)
53 strerr_die2x(111,FATAL
,"wait failed");
54 if (wait_crashed(wstat
))
55 strerr_die2x(111,FATAL
,"child crashed");
56 switch(wait_exitcode(wstat
)) {
58 case 111: strerr_die2x(111,FATAL
,"temporary child error");
62 if (seek_begin(0) == -1)
63 strerr_die2sys(111,FATAL
,"unable to rewind: ");
66 sender
= env_get("SENDER");
67 if (!sender
) strerr_die2x(100,FATAL
,"SENDER not set");
68 dtline
= env_get("DTLINE");
69 if (!dtline
) strerr_die2x(100,FATAL
,"DTLINE not set");
71 if (qmail_open(&qqt
) == -1)
72 strerr_die2sys(111,FATAL
,"unable to fork: ");
73 qmail_puts(&qqt
,dtline
);
74 if (substdio_copy(&ssout
,&ssin
) != 0)
75 strerr_die2sys(111,FATAL
,"unable to read message: ");
76 substdio_flush(&ssout
);
78 num
[fmt_ulong(num
,qmail_qp(&qqt
))] = 0;
80 qmail_from(&qqt
,sender
);
81 qmail_to(&qqt
,argv
[1]);
82 qqx
= qmail_close(&qqt
);
83 if (*qqx
) strerr_die2x(*qqx
== 'D' ?
100 : 111,FATAL
,qqx
+ 1);
84 strerr_die2x(99,"condredirect: qp ",num
);