File: writefile.c

package info (click to toggle)
safecat 1.8-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 516 kB
  • ctags: 250
  • sloc: ansic: 1,920; makefile: 371; sh: 282
file content (45 lines) | stat: -rw-r--r-- 1,259 bytes parent folder | download | duplicates (6)
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
/* Copyright (c) 2000, Len Budney. See COPYING for details. */
#include "env.h"
#include "stralloc.h"
#include "strerr.h"
#include "subfd.h"
#include "substdio.h"
#include "writefile.h"
#include <errno.h>
#include <unistd.h>

extern stralloc tmppath;

/* ****************************************************************** */
void writefile(int fd) {
  char inbuf[512];
  char outbuf[512];
  char *dtline;
  char *rpline;
  substdio ssin;
  substdio ssout;

  /* Prepare substdio buffers for reading and writing. */
  substdio_fdbuf(&ssin,read,0,inbuf,sizeof(inbuf));
  substdio_fdbuf(&ssout,write,fd,outbuf,sizeof(outbuf));

  /* Print DTLINE and RPLINE, if supplied. */
  dtline = env_get("DTLINE");
  rpline = env_get("RPLINE");
  if (dtline && rpline) {
    if(substdio_puts(&ssout,rpline) == -1) goto fail;
    if(substdio_puts(&ssout,dtline) == -1) goto fail;
  }

  /* Copy stdin to the output file, watching the return values each time. */
  if (substdio_copy(&ssout,&ssin) < 0) goto fail;
  if (substdio_flush(&ssout) == -1) goto fail;

  /* The file is copied. */
  return;

 fail:
  unlink(tmppath.s);
  strerr_die2x(111,"safecat: fatal: ","unable to copy standard input");
}
/* ****************************************************************** */