Ejemplo n.º 1
0
int main(int argc,char* argv[]) {
  static size_t x;
  x=23;
  atomic_add(&x,3);
  printf("%u\n",x);
  printf("%u\n",atomic_add_return(&x,-3));
  printf("%u\n",compare_and_swap(&x,26,17));
  printf("%u\n",compare_and_swap(&x,23,17));

#if 0
  atomic_add(&x,3); printf("%u\n",x);
  x=23;
  atomic_add(&x,3); assert(x==26);
  atomic_or(&x,1); assert(x==27);
  atomic_and(&x,-2); assert(x==26);
#endif

#if 0
  iarray a;
  char* c;
  iarray_init(&a,sizeof(io_entry));
  printf("15 -> %p\n",c=iarray_allocate(&a,15));
  printf("23 -> %p\n",c=iarray_allocate(&a,23));
  printf("1234567 -> %p\n",c=iarray_allocate(&a,1234567));
  printf("23 -> %p\n",iarray_get(&a,23));
#endif
#if 0
  io_batch* b=iob_new(1234);
  int64 fd=open("t.c",0);
  iob_addbuf(b,"fnord",5);
  iob_addfile_close(b,fd,0,7365);
  iob_write(1,b,writecb);
#endif
#if 0
  char dest[1024];
  unsigned long len;
  scan_urlencoded2("libstdc++.tar.gz",dest,&len);
  buffer_putmflush(buffer_1,dest,"\n");
#endif
#if 0
  static stralloc sa;
  stralloc_copym(&sa,"foo ","bar ","baz.\n");
  write(1,sa.s,sa.len);
#endif
#if 0
  buffer_putmflush(buffer_1,"foo ","bar ","baz.\n");
#endif
#if 0
  char* c="fnord";
  int fd=open_read(c);
  errmsg_iam(argv[0]);
  carp("could not open file `",c,"'");
  diesys(23,"could not open file `",c,"'");
#endif
#if 0
  errmsg_warn("could not open file `",c,"'",0);
  errmsg_warnsys("could not open file `",c,"'",0);
#endif
#if 0
  char buf[100]="/usr/bin/sh";
  int len=str_len(buf);
  assert(byte_rchr(buf,len,'/')==8);
  assert(byte_rchr(buf,len,'@')==len);
  assert(byte_rchr(buf,len,'h')==len-1);
  printf("%d\n",byte_rchr("x",1,'x'));
#endif
#if 0
  char buf[IP6_FMT+100];
  int i;
  char ip[16];
  uint32 scope_id;
  char* s="fec0::1:220:e0ff:fe69:ad92%eth0/64";
  char blubip[16]="\0\0\0\0\0\0\0\0\0\0\xff\xff\x7f\0\0\001";
  i=scan_ip6if(s,ip,&scope_id);
  assert(s[i]=='/');
  buffer_put(buffer_1,buf,fmt_ip6if(buf,ip,scope_id));
  buffer_putnlflush(buffer_1);
  buffer_put(buffer_1,buf,fmt_ip6ifc(buf,blubip,scope_id));
  buffer_putnlflush(buffer_1);
  scan_ip6("2001:7d0:0:f015:0:0:0:1",ip);
  buffer_put(buffer_1,buf,fmt_ip6(buf,ip));
  buffer_putnlflush(buffer_1);
#endif
#if 0
  char buf[100];
  int i;
  printf("%d\n",i=fmt_pad(buf,"fnord",5,7,10));
  buf[i]=0;
  puts(buf);
#endif
#if 0
  char ip[16];
  char buf[32];
  printf("%d (expect 2)\n",scan_ip6("::",ip));
  printf("%d (expect 3)\n",scan_ip6("::1",ip));
  printf("%d (expect 16)\n",scan_ip6("fec0:0:0:ffff::1/0",ip));
  printf("%.*s\n",fmt_ip6(buf,ip),buf);
#endif
#if 0
  static stralloc s,t;
  stralloc_copys(&s,"fnord");
  stralloc_copys(&t,"abc"); printf("%d\n",stralloc_diff(&s,&t));
  stralloc_copys(&t,"fnor"); printf("%d\n",stralloc_diff(&s,&t));
  stralloc_copys(&t,"fnord"); printf("%d\n",stralloc_diff(&s,&t));
  stralloc_copys(&t,"fnordh"); printf("%d\n",stralloc_diff(&s,&t));
  stralloc_copys(&t,"hausen"); printf("%d\n",stralloc_diff(&s,&t));
#endif
#if 0
  static stralloc s;
  stralloc_copys(&s,"fnord");
  printf("%d\n",stralloc_diffs(&s,"abc"));
  printf("%d\n",stralloc_diffs(&s,"fnor"));
  printf("%d\n",stralloc_diffs(&s,"fnord"));
  printf("%d\n",stralloc_diffs(&s,"fnordh"));
  printf("%d\n",stralloc_diffs(&s,"hausen"));
#endif
#if 0
  printf("%d\n",case_starts("fnordhausen","FnOrD"));
  printf("%d\n",case_starts("fnordhausen","blah"));
#endif
#if 0
  char buf[]="FnOrD";
  case_lowers(buf);
  puts(buf);
#endif
#if 0
  char buf[100]="foo bar baz";
  printf("%d (expect 7)\n",byte_rchr(buf,11,' '));
#endif
#if 0
  unsigned long size;
  char* buf=mmap_read(argv[1],&size);
  if (buf) {
    unsigned int x=fmt_yenc(0,buf,size);
    unsigned int y;
    char* tmp=malloc(x+1);
    y=fmt_yenc(tmp,buf,size);
    write(1,tmp,x);
  }
#endif
#if 0
  char buf[100];
  char buf2[100];
  unsigned int len,len2;
  buf[fmt_yenc(buf,"http://localhost/~fefe",22)]=0;
  buffer_puts(buffer_1,buf);
  buffer_putsflush(buffer_1,"\n");
  if ((buf[len2=scan_yenc(buf,buf2,&len)])!='\n') {
    buffer_putsflush(buffer_2,"parse error!\n");
    return 1;
  }
  buffer_put(buffer_1,buf2,len2);
  buffer_putsflush(buffer_1,"\n");
  return 0;
#endif
#if 0
  char buf[100];
  char buf2[100];
  unsigned int len,len2;
  buf[fmt_base64(buf,"foo:bar",7)]=0;
  buffer_puts(buffer_1,buf);
  buffer_putsflush(buffer_1,"\n");
  if ((buf[len2=scan_base64(buf,buf2,&len)])!=0) {
    buffer_putsflush(buffer_2,"parse error!\n");
    return 1;
  }
  buffer_put(buffer_1,buf2,len2);
  buffer_putsflush(buffer_1,"\n");
  return 0;
#endif
#if 0
  unsigned long size;
  char* buf=mmap_read(argv[1],&size);
  if (buf) {
    unsigned int x=fmt_uuencoded(0,buf,size);
    unsigned int y;
    char* tmp=malloc(x+1);
    y=fmt_uuencoded(tmp,buf,size);
    write(1,tmp,x);
  }
#endif
#if 0
  char buf[]="00000000000000000000000000000001";
  char ip[16];
  if (scan_ip6_flat(buf,ip) != str_len(buf))
    buffer_putsflush(buffer_2,"parse error!\n");
#endif
#if 0
  int fd=open_read("t.c");
  buffer b;
  char buf[1024];
  char line[20];
  int i;
  buffer_init(&b,read,fd,buf,1024);
  i=buffer_getline(&b,line,19);
  buffer_puts(buffer_1,"getline returned ");
  buffer_putulong(buffer_1,i);
  buffer_puts(buffer_1,"\n");
  buffer_puts(buffer_1,line);
  buffer_flush(buffer_1);
#endif
#if 0
  buffer_putulong(buffer_1,23);
//  buffer_putspace(buffer_1);
  buffer_putsflush(buffer_1,"\n");
//  buffer_flush(buffer_1);
#endif
#if 0
  long a,b,c;
  char buf[4096];
  char buf2[4096];
  memcpy(buf,buf2,4096);
  byte_copy(buf,4096,buf2);
  rdtscl(a);
  memcpy(buf,buf2,4096);
  rdtscl(b);
  byte_copy(buf,4096,buf2);
  rdtscl(c);
  printf("memcpy: %d - byte_copy: %d\n",b-a,c-b);
#endif
#if 0
  char ip[16];
  int i;
  if ((i=scan_ip6(argv[1],ip))) {
    char buf[128];
    buf[fmt_ip6(buf,ip)]=0;
    puts(buf);
  }
#endif
#if 0
  char buf[100];
  strcpy(buf,"foobarbaz");
  buf[fmt_fill(buf,3,5,100)]=0;
  printf("\"%s\"\n",buf);
#endif
#if 0
  unsigned long len;
  char *c=mmap_read("/etc/passwd",&len);
  printf("got map %p of len %lu\n",c,len);
#endif
#if 0
  char c;
  printf("%d\n",buffer_getc(buffer_0,&c));
  printf("%c\n",c);
#endif
#if 0
  char buf[100]="01234567890123456789012345678901234567890123456789";
  long a,b,c;
#endif
#if 0
  buf[ip4_fmt(buf,ip4loopback)]=0;
  buffer_puts(buffer_1small,buf);
  buffer_flush(buffer_1small);
#endif

#if 0
  buf[0]=0;
  buf[fmt_8long(buf,0)]=0;
  puts(buf);
  rdtscl(a);
  c=str_len(buf);
  rdtscl(b);
  /*byte_zero_djb(buf,j); */
//  printf("\n%lu %d\n",b-a,c);
#endif
#if 0
  buffer_puts(buffer_1small,"hello, world\n");
  buffer_flush(buffer_1small);
#endif
#if 0
  int s=socket_tcp4();
  char ip[4]={127,0,0,1};
  int t=socket_connect4(s,ip,80);
#endif
#if 0
  char buf[100]="foo bar baz fnord   ";
  char buf2[100]="foo braz fnord";
  long a,b,c;
  long i=0,j=0,k=0;
  double d;
  uint32 l,m,n;
  stralloc sa={0};
  stralloc_copys(&sa,"fnord");
  stralloc_catlong0(&sa,-23,5);
  stralloc_append(&sa,"\n");
  printf("%d %d\n",str_equal("fnord","fnord1"),str_equal("fnord1","fnord"));
  write(1,sa.s,sa.len);
  printf("%d %d\n",stralloc_starts(&sa,"fnord"),stralloc_starts(&sa,"fnord\na"));

  l=0xdeadbeef;
  uint32_pack_big((char*)&m,l);
  uint32_unpack_big((char*)&m,&n);
  printf("%x %x %x\n",l,m,n);

  rdtscl(a);
/*  i=scan_double("3.1415",&d); */
  rdtscl(b);
  /*byte_zero_djb(buf,j); */
  rdtscl(c);
  printf("%lu %lu\n",b-a,c-b);
#endif
#if 0
  size_t size;
  char* buf=mmap_read(argv[1],&size);
  if (buf) {
    unsigned int x=fmt_urlencoded2(0,buf,size,"x");
    unsigned int y;
    char* tmp=malloc(x+1);
    y=fmt_urlencoded2(tmp,buf,size,"x");
    write(1,tmp,x);
  }
#endif
#if 0
  printf("%d %d\n",strcmp("foo","bar"),str_diff("foo","bar"));
  printf("%d %d\n",strcmp("foo","üar"),str_diff("foo","üar"));
#endif
#if 0
  {
    int16 a;
    int32 b;
    int64 c;
    assert(imult16(4,10000,&a)==0);
    assert(imult16(-4,10000,&a)==0);
    assert(imult16(5,10,&a)==1 && a==50);
    assert(imult16(-3,10000,&a)==1 && a==-30000);

    assert(imult32(0x40000000,2,&b)==0);
    assert(imult32(0x3fffffff,2,&b)==1 && b==0x7ffffffe);

    assert(imult64(0x4000000000000000ll,2,&c)==0);
    assert(imult64(0x3fffffffffffffffll,2,&c)==1 && c==0x7ffffffffffffffell);
  }
#endif
#if 0
  stralloc a;
  printf("%d\n",stralloc_copym(&a,"fnord",", ","foo"));
#endif

  return 0;
}
Ejemplo n.º 2
0
static int mangleurl(stralloc* tag,const char* baseurl) {
  char* x;
  const char* y;
  static stralloc before,arg,after,tmp;
  int found;
  struct stat ss;
  found=0;
  if (stralloc_istag(tag,"a") || stralloc_istag(tag,"link"))
    found=1;
  else if (stralloc_istag(tag,"img") || stralloc_istag(tag,"frame"))
    found=2;
  if (!found) return 0;
  if (extractparam(tag,found==1?"href":"src",&before,&arg,&after)) {
    if (stralloc_starts(&arg,"/") ||
	stralloc_starts(&arg,"http://") ||
	stralloc_starts(&arg,"https://")) {
      canonicalize(&arg,baseurl);
    } else
      return 0;	/* url was already relative */
    if (stralloc_0(&arg)==0) return -1;
    stralloc_chop(&arg);
    x=arg.s+7; if (*x=='/') ++x;
    y=baseurl+7; if (*y=='/') ++y;

    /* now x is something like
      * "www.spiegel.de/img/0,1020,525770,00.jpg"
      * and baseurl is something like
      * "www.spiegel.de/panorama/0,1518,378421,00.html"
      * and we want to change x into "../img/0,1020,525770,00.jpg" */
    if (stat(x,&ss)!=0) return 0;

    for (;;) {
      int i=str_chr(x,'/');
      int j=str_chr(y,'/');
      if (i>0 && i==j && byte_equal(x,i,y)) {
	x+=i+1;
	y+=i+1;
	while (*x=='/') ++x;
	while (*y=='/') ++y;
      } else
	break;
    }
    stralloc_zero(&tmp);
    for (;;) {
      int i=str_chr(y,'/');
      if (y[i]=='/') {
	y+=i+1;
	while (*y=='/') ++y;
	if (stralloc_cats(&tmp,"../")==0) return -1;
      } else
	break;
    }
    {
      int i,needquote;
      for (i=needquote=0; x[i]; ++i)
	if (!isalnum(x[i]) && x[i]!='/' && x[i]!='_' && x[i]!='.') needquote=1;
      if (needquote) {
	if (stralloc_cats(&before,"\"")==0 ||
	    stralloc_cat(&before,&tmp)==0 ||
	    stralloc_cats(&before,x)==0 ||
	    stralloc_cats(&before,"\"")==0) return -1;
      } else
	if (stralloc_cat(&before,&tmp)==0 ||
	    stralloc_cats(&before,x)==0) return -1;
    }
    if (stralloc_cat(&before,&after)==0) return -1;
    if (stralloc_copy(tag,&before)==0) return -1;
  }
  return 0;
}
Ejemplo n.º 3
0
void main(void)
{
  unsigned int i,j;

  for (;;) {
    get(&line);
    if (line.len == 1) break;
    if (flagdsn) {
      if (line.s[0] == ' ' || line.s[0] == '\t')	/* continuation */
	if (!stralloc_catb(&dsnline,line.s,line.len - 1)) die_nomem();
      continue;
    }
    flagdsn = 0;
    if (stralloc_starts(&line,"Subject: success notice"))
      _exit(99);
    if (stralloc_starts(&line,"Subject: deferral notice"))
      _exit(99);
    if (stralloc_starts(&line,"Precedence: bulk"))
      _exit(99);
    if (stralloc_starts(&line,"Precedence: junk"))
      _exit(99);
    if (stralloc_starts(&line,"Auto-Submitted:"))
      _exit(99);
    /* Mailing list signatures */
    if (stralloc_starts(&line,"Precedence: list"))
      _exit(99);
    if (stralloc_starts(&line,"Mailing-List:"))
      _exit(99);
    if (stralloc_starts(&line,"List-ID:"))
      _exit(99);
    if (stralloc_starts(&line,"X-Mailing-List:"))
      _exit(99);
    if (stralloc_starts(&line,"X-ML-Name:"))
      _exit(99);
    if (stralloc_starts(&line,"List-Help:"))
      _exit(99);
    if (stralloc_starts(&line,"List-Unsubscribe:"))
      _exit(99);
    if (stralloc_starts(&line,"List-Subscribe:"))
      _exit(99);
    if (stralloc_starts(&line,"List-Post:"))
      _exit(99);
    if (stralloc_starts(&line,"List-Owner:"))
      _exit(99);
    if (stralloc_starts(&line,"List-Archive:"))
      _exit(99);
/* for Novell Groupwise */
    if (stralloc_starts(&line,"Subject: Message status - delivered"))
      _exit(99);
    if (stralloc_starts(&line,"Subject: Message status - opened"))
      _exit(99);
    if (stralloc_starts(&line,"Subject: Out of Office AutoReply:"))
      _exit(99);
    /* Other autoresponders */
    if (stralloc_starts(&line,"X-Amazon-Auto-Reply:"))
      _exit(99);
    if (stralloc_starts(&line,"X-Mailer: KANA Response"))
      _exit(99);
    if (stralloc_starts(&line,"Thread-Topic: AutoResponse"))
      _exit(99);
    if (stralloc_starts(&line,"Subject: AutoResponse -"))
      _exit(99);
    
    if (stralloc_starts(&line,"From: Mail Delivery Subsystem <MAILER-DAEMON@"))
      flagmds = 1;
    if (stralloc_starts(&line,"Subject: Warning: could not send message"))
      flagsw = 1;
    if (stralloc_starts(&line,"Subject: Returned mail: warning: cannot send message"))
      flagsr = 1;
    if (stralloc_starts(&line,"Auto-Submitted: auto-generated (warning"))
      flagas = 1;
    if (case_startb(line.s,line.len,"Content-type: multipart/report")) {
      if (!stralloc_copyb(&dsnline,line.s,line.len - 1)) die_nomem();
      flagdsn = 1;
    }
  }			/* end of header */

  if (dsnline.len > 0) {	/* always only one recipient/action */
    flagdsn = 0;	/* will be set for correct report type */
    for (i=0; i < dsnline.len; i += 1+byte_chr(dsnline.s+i,dsnline.len-i,';')) {
      while (dsnline.s[i] == ' ' || dsnline.s[i] == '\t')
	if (++i >= dsnline.len) break;
      if (case_startb(dsnline.s + i,dsnline.len - i,"report-type=")) {
	i += 12;
	while (dsnline.s[i] ==' ' || dsnline.s[i] =='\t' || dsnline.s[i] =='"')
	  if (++i >= dsnline.len) break;
	if (case_startb(dsnline.s + i,dsnline.len - i,"delivery-status"))
	  flagdsn = 1;
      } else if (case_startb(dsnline.s + i,dsnline.len - i,"boundary=")) {
	i += 9;
	while (dsnline.s[i] ==' ' || dsnline.s[i] =='\t')
	  if (++i >= dsnline.len) break;
	if (dsnline.s[i] == '"') {
	  if (++i >= dsnline.len) break;
	  j = i + byte_chr(dsnline.s + i,dsnline.len - i,'"');
	  if (j >= dsnline.len) break;
	} else {
	  j = i;
	  while (dsnline.s[j] !=' ' && dsnline.s[j] !='\t' &&
		dsnline.s[j] !=';')
	    if (++j >= dsnline.len) break;
	}				/* got boundary */
	if (!stralloc_copyb(&boundary,dsnline.s+i,j-i)) die_nomem();
      }
    }
  }
  if (flagdsn && boundary.len) {	/* parse DSN message */
    get(&line);			/* if bad format we exit(0) via get() */
    for (;;) {
      if (isboundary()) {
      if (line.len == boundary.len + 5 && line.s[line.len - 1] == '-'
		&& line.s[line.len - 2] == '-')
        _exit(99);			/* end: not failure report */
        get(&line);			/* Content-type */
        if (case_startb(line.s,line.len,"content-type:")) {
	  i = 13;
	  while (line.s[i] == ' ' || line.s[i] == '\t')
		if (++i >= line.len) break;
	  if (case_startb(line.s+i,line.len-i,"message/delivery-status")) {
	    for (;;) {
	      get(&line);
	      if (isboundary()) break;
	      if (case_startb(line.s,line.len,"action:")) {
	        i = 8;
	        while (line.s[i] == ' ' || line.s[i] == '\t')
		  if (++i >= line.len) break;
	        if (case_startb(line.s + i, line.len - i,"failed"))
		  _exit(0);	/* failure notice */
		else
		  _exit(99);	/* there shouldn't be more than 1 action */
	      }
            }
	  }
        }
      } else
	get(&line);
    }
  }

  get(&line1);
  get(&line2);
  get(&line3);
  get(&line4);
  get(&line5);
  get(&line6);
  get(&line7);
  get(&line8);

  if (stralloc_starts(&line1,"This is a MIME-encapsulated message"))
  if (stralloc_starts(&line3,"--"))
  if (stralloc_starts(&line5,warn1))
  if (stralloc_starts(&line6,warn2))
  if (stralloc_starts(&line7,warn3))
  if (stralloc_starts(&line8,warn4))
    flagbw = 1;

  if (stralloc_starts(&line1,warn1))
  if (stralloc_starts(&line2,warn2))
  if (stralloc_starts(&line3,warn3))
  if (stralloc_starts(&line4,warn4))
    flagbw = 1;

  if (flagmds && flagsw && flagas && flagbw) _exit(99);
  if (flagmds && flagsr && flagbw) _exit(99);

  _exit(0);
}
Ejemplo n.º 4
0
int main()
{
  buffer bin;
  buffer bout;
  struct prioq_elt pe;
  int fdoldmbox;
  int fdnewmbox;
  int fd;
  int match;
  int fdlock;

  umask(077);

  mbox = env_get("MAIL");
  if (!mbox) strerr_die2x(111,FATL,"MAIL not set");
  mboxtmp = env_get("MAILTMP");
  if (!mboxtmp) strerr_die2x(111,FATL,"MAILTMP not set");

  if (maildir_chdir() == -1)
    strerr_die1(111,FATAL,&maildir_chdir_err);
  maildir_clean(&filenames);
  if (maildir_scan(&pq,&filenames,1,1) == -1)
    strerr_die1(111,FATAL,&maildir_scan_err);

  if (!prioq_min(&pq,&pe)) _exit(0); /* nothing new */

  fdlock = open_append(mbox);
  if (fdlock == -1)
    strerr_die4sys(111,FATL,"unable to lock ",mbox,": ");
  if (lock_ex(fdlock) == -1)
    strerr_die4sys(111,FATL,"unable to lock ",mbox,": ");

  fdoldmbox = open_read(mbox);
  if (fdoldmbox == -1)
    strerr_die4sys(111,FATL,"unable to read ",mbox,": ");

  fdnewmbox = open_trunc(mboxtmp);
  if (fdnewmbox == -1)
    strerr_die4sys(111,FATL,"unable to create ",mboxtmp,": ");

  buffer_init(&bin,read,fdoldmbox,inbuf,sizeof(inbuf));
  buffer_init(&bout,write,fdnewmbox,outbuf,sizeof(outbuf));

  switch(buffer_copy(&bout,&bin))
  {
    case -2: strerr_die4sys(111,FATL,"unable to read ",mbox,": ");
    case -3: strerr_die4sys(111,FATL,"unable to write to ",mboxtmp,": ");
  }

  while (prioq_min(&pq,&pe))
  {
    prioq_delmin(&pq);
    if (!prioq_insert(&pq2,&pe)) die_nomem();

    fd = open_read(filenames.s + pe.id);
    if (fd == -1)
      strerr_die4sys(111,FATL,"unable to read $MAILDIR/",filenames.s + pe.id,": ");
    buffer_init(&bin,read,fd,inbuf,sizeof(inbuf));

    if (getln(&bin,&line,&match,'\n') != 0)
      strerr_die4sys(111,FATL,"unable to read $MAILDIR/",filenames.s + pe.id,": ");

    if (!stralloc_copys(&ufline,"From XXX ")) die_nomem();
    if (match)
      if (stralloc_starts(&line,"Return-Path: <"))
      {
        if (line.s[14] == '>')
    {
          if (!stralloc_copys(&ufline,"From MAILER-DAEMON ")) die_nomem();
    }
        else
        {
      int i;
         if (!stralloc_ready(&ufline,line.len)) die_nomem();
         if (!stralloc_copys(&ufline,"From ")) die_nomem();
      for (i = 14;i < line.len - 2;++i)
        if ((line.s[i] == ' ') || (line.s[i] == '\t'))
          ufline.s[ufline.len++] = '-';
        else {
          ufline.s[ufline.len++] = line.s[i];
          if (!stralloc_cats(&ufline," ")) die_nomem();
          }
        }
      }
    if (!stralloc_cats(&ufline,myctime(pe.dt))) die_nomem();
    if (buffer_put(&bout,ufline.s,ufline.len) == -1)
      strerr_die4sys(111,FATAL,"unable to write to ",mboxtmp,": ");

    while (match && line.len)
    {
      if (gfrom(line.s,line.len))
        if (buffer_puts(&bout,">") == -1)
          strerr_die4sys(111,FATL,"unable to write to ",mboxtmp,": ");
      if (buffer_put(&bout,line.s,line.len) == -1)
        strerr_die4sys(111,FATL,"unable to write to ",mboxtmp,": ");
      if (!match)
      {
        if (buffer_puts(&bout,"\n") == -1)
          strerr_die4sys(111,FATL,"unable to write to ",mboxtmp,": ");
        break;
      }
      if (getln(&bin,&line,&match,'\n') != 0)
        strerr_die4sys(111,FATL,"unable to read $MAILDIR/",filenames.s + pe.id,": ");
    }
    if (buffer_puts(&bout,"\n"))
      strerr_die4sys(111,FATL,"unable to write to ",mboxtmp,": ");

   close(fd);
  }

  if (buffer_flush(&bout) == -1)
    strerr_die4sys(111,FATL,"unable to write to ",mboxtmp,": ");
  if (fsync(fdnewmbox) == -1)
    strerr_die4sys(111,FATL,"unable to write to ",mboxtmp,": ");
  if (close(fdnewmbox) == -1) /* NFS dorks */
    strerr_die4sys(111,FATL,"unable to write to ",mboxtmp,": ");
  if (rename(mboxtmp,mbox) == -1)
    strerr_die6(111,FATL,"unable to move ",mboxtmp," to ",mbox,": ",&strerr_sys);

  while (prioq_min(&pq2,&pe))
  {
    prioq_delmin(&pq2);
    if (unlink(filenames.s + pe.id) == -1)
      strerr_warn4(WARNING,"$MAILDIR/",filenames.s + pe.id," will be delivered twice; unable to unlink: ",&strerr_sys);
  }

  return(0);
}
Ejemplo n.º 5
0
int headerbody(buffer *b,void (*dohf)(),void (*hdone)(),void (*dobl)())
//substdio *ss;
//void (*dohf)();
//void (*hdone)();
//void (*dobl)();
{
  int match;
  int flaglineok;
  match = 1;
  flaglineok = 0;
  for (;;)
  {
    switch(getsa(b,&nextline,&match))
    {
      case -1:
        return -1;
      case 0:
        if (flaglineok) dohf(&line);
        hdone();
        /* no message body; could insert blank line here */
        return 0;
    }
    if (flaglineok)
    {
      if ((nextline.s[0] == ' ') || (nextline.s[0] == '\t'))
      {
        if (!stralloc_cat(&line,&nextline)) return -1;
        continue;
      }
      dohf(&line);
    }
    if (nextline.len == 1)
    {
     hdone();
     dobl(&nextline);
     break;
    }
    if (stralloc_starts(&nextline,"From "))
    {
      if (!stralloc_copys(&line,"MBOX-Line: ")) return -1;
      if (!stralloc_cat(&line,&nextline)) return -1;
    }
    else
      if (hfield_valid(nextline.s,nextline.len))
      {
        if (!stralloc_copy(&line,&nextline)) return -1;
      }
      else
      {
        hdone();
        if (!stralloc_copys(&line,"\n")) return -1;
        dobl(&line);
        dobl(&nextline);
        break;
      }
    flaglineok = 1;
  }
  for (;;)
    switch(getsa(b,&nextline,&match))
    {
      case -1: return -1;
      case 0: return 0;
      case 1: dobl(&nextline);
    }
}