if (fd == -1)
strerr_die5x(111,"Unable to open ",fn,":; ",error_str(errno),". (#4.2.1)");
//时间信号,30秒
sig_alarmcatch(temp_slowlock);
alarm(30);
//锁定
flaglocked = (lock_ex(fd) != -1);
alarm(0);
sig_alarmdefault();
//结尾
seek_end(fd);
//pos实际指向文件尾(文件大小)
pos = seek_cur(fd);
substdio_fdbuf(&ss,read,0,buf,sizeof(buf));
substdio_fdbuf(&ssout,write,fd,outbuf,sizeof(outbuf));
//dtline:;邮件头Delivered-To部分
//rpline:;邮件头返回路径
//ufline:;mbox 是标准的UNIX邮箱格式, 多个邮件存放在一个独立文件内, 每个邮件由一个"From" 的行开始.
//这一行看起来像一个标题字段, 不过那不是标题, 那仅仅是传送代理添加的, 便于邮件读出器可以找到每个邮件开始部分的一个标记.
if (substdio_put(&ssout,ufline.s,ufline.len)) goto writeerrs;
if (substdio_put(&ssout,rpline.s,rpline.len)) goto writeerrs;
if (substdio_put(&ssout,dtline.s,dtline.len)) goto writeerrs;
for (;;)
{//读一行
if (getln(&ss,&messline,&match,'\n') != 0)
{
strerr_warn3("Unable to read message:; ",error_str(errno),". (#4.3.0)",0);
if (flaglocked) seek_trunc(fd,pos); close(fd);
_exit(111);
}
if (!match && !messline.len) break;
if (gfrom(messline.s,messline.len))
if (substdio_bput(&ssout,">",1)) goto writeerrs;
if (substdio_bput(&ssout,messline.s,messline.len)) goto writeerrs;
if (!match)
{
if (substdio_bputs(&ssout,"\n")) goto writeerrs;
break;
}
}
if (substdio_bputs(&ssout,"\n")) goto writeerrs;
if (substdio_flush(&ssout)) goto writeerrs;
//由缓冲区写回磁盘
if (fsync(fd) == -1) goto writeerrs;
close(fd);
return;
writeerrs:;
strerr_warn5("Unable to write ",fn,":; ",error_str(errno),". (#4.3.0)",0);
if (flaglocked) seek_trunc(fd,pos);
close(fd);
_exit(111);
}
void mailprogram(prog)
char *prog;
{
int child;
char *(args[4]);
int wstat;
if (seek_begin(0) == -1) temp_rewind();
switch(child = fork())
{
case -1:;
temp_fork();
case 0:;
args[0] = "/bin/sh"; args[1] = "-c"; args[2] = prog; args[3] = 0;
sig_pipedefault();
execv(*args,args);
strerr_die3x(111,"Unable to run /bin/sh:; ",error_str(errno),". (#4.3.0)");
}
wait_pid(&wstat,child);
if (wait_crashed(wstat))
temp_childcrashed();
switch(wait_exitcode(wstat))
{
case 100:;
case 64:; case 65:; case 70:; case 76:; case 77:; case 78:; case 112:; _exit(100);
case 0:; break;
case 99:; flag99 = 1; break;
default:; _exit(111);
}
}