14 void die_success() { _exit(0); }
15 void die_99() { _exit(99); }
16 void die_perm(s
) char *s
; { substdio_putsflush(subfderr
,s
); _exit(100); }
17 void die_temp(s
) char *s
; { substdio_putsflush(subfderr
,s
); _exit(111); }
18 void die_nomem() { die_temp("condredirect: fatal: out of memory\n"); }
22 int mywrite(fd
,buf
,len
) int fd
; char *buf
; int len
;
24 qmail_put(&qqt
,buf
,len
);
30 char inbuf
[SUBSTDIO_INSIZE
];
42 if (!argv
[1] || !argv
[2])
43 die_perm("condredirect: usage: condredirect newaddress program arg ...\n");
47 case -1: die_temp("condredirect: fatal: unable to fork\n");
49 execvp(argv
[2],argv
+ 2);
50 if (error_temp(errno
)) _exit(111);
53 if (wait_pid(&wstat
,pid
) != pid
)
54 die_perm("condredirect: fatal: internal bug\n");
55 if (wait_crashed(wstat
)) die_temp("condredirect: fatal: child crashed\n");
56 switch(wait_exitcode(wstat
))
59 case 111: die_temp("condredirect: fatal: temporary child error\n");
60 default: die_success();
63 if (seek_begin(0) == -1) die_temp("condredirect: fatal: unable to rewind\n");
66 sender
= env_get("SENDER");
67 if (!sender
) die_perm("condredirect: fatal: SENDER not set\n");
68 dtline
= env_get("DTLINE");
69 if (!dtline
) die_perm("condredirect: fatal: DTLINE not set\n");
71 if (qmail_open(&qqt
) == -1) die_temp("condredirect: fatal: unable to fork\n");
72 qmail_puts(&qqt
,dtline
);
73 substdio_fdbuf(&ssin
,read
,0,inbuf
,sizeof(inbuf
));
74 substdio_fdbuf(&ssout
,mywrite
,-1,outbuf
,sizeof(outbuf
));
75 if (substdio_copy(&ssout
,&ssin
) != 0)
76 die_temp("condredirect: fatal: error while reading message\n");
77 substdio_flush(&ssout
);
79 qmail_from(&qqt
,sender
);
80 qmail_to(&qqt
,argv
[1]);
81 switch(qmail_close(&qqt
))
84 case QMAIL_TOOLONG
: die_perm("condredirect: fatal: permanent qmail-queue error\n");
85 default: die_temp("condredirect: fatal: temporary qmail-queue error\n");