File: forward.c

package info (click to toggle)
qmail 1.01-5
  • links: PTS
  • area: non-free
  • in suites: hamm
  • size: 2,188 kB
  • ctags: 1,711
  • sloc: ansic: 13,993; makefile: 1,914; perl: 448; sh: 377
file content (58 lines) | stat: -rw-r--r-- 1,499 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "sig.h"
#include "readwrite.h"
#include "exit.h"
#include "env.h"
#include "qmail.h"
#include "stralloc.h"
#include "subfd.h"
#include "substdio.h"

void die_success() { _exit(0); }
void die_perm(s) char *s; { substdio_putsflush(subfderr,s); _exit(100); }
void die_temp(s) char *s; { substdio_putsflush(subfderr,s); _exit(111); }
void die_nomem() { die_temp("forward: fatal: out of memory\n"); }

struct qmail qqt;

int mywrite(fd,buf,len) int fd; char *buf; int len;
{
 qmail_put(&qqt,buf,len);
 return len;
}

substdio ssin;
substdio ssout;
char inbuf[SUBSTDIO_INSIZE];
char outbuf[16];

void main(argc,argv)
int argc;
char **argv;
{
 char *sender;
 char *dtline;

 sig_pipeignore();

 sender = env_get("NEWSENDER");
 if (!sender) die_perm("forward: fatal: NEWSENDER not set\n");
 dtline = env_get("DTLINE");
 if (!dtline) die_perm("forward: fatal: DTLINE not set\n");

 if (qmail_open(&qqt) == -1) die_temp("forward: fatal: unable to fork\n");
 qmail_puts(&qqt,dtline);
 substdio_fdbuf(&ssin,read,0,inbuf,sizeof(inbuf));
 substdio_fdbuf(&ssout,mywrite,-1,outbuf,sizeof(outbuf));
 if (substdio_copy(&ssout,&ssin) != 0)
   die_temp("forward: fatal: error while reading message\n");
 substdio_flush(&ssout);

 qmail_from(&qqt,sender);
 while (*++argv) qmail_to(&qqt,*argv);
 switch(qmail_close(&qqt))
  {
   case 0: die_success();
   case QMAIL_TOOLONG: die_perm("forward: fatal: permanent qmail-queue error\n");
   default: die_temp("forward: fatal: temporary qmail-queue error\n");
  }
}