File: copy-stream.c

package info (click to toggle)
detachtty 5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 80 kB
  • ctags: 35
  • sloc: ansic: 365; makefile: 56
file content (31 lines) | stat: -rw-r--r-- 795 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
#include "config.h"

static char buf[4097];
static int bytes_in_buf=0;

int copy_a_bit(int in_fd, int out_fd, int dribble_fd, char *message) {
  /* copy whatever's available (max 4k) from in_fd to out_fd (and
     dribble_fd if !=-1)
     Return number of bytes copied. Bail if error happens.  
     Note: not re-entrant */
  
  bytes_in_buf=read(in_fd,buf,4096);
  if(!bytes_in_buf) return 0;
  output_buffer(dribble_fd);
  if(output_buffer(out_fd)==0) {
    perror(message);
    exit(1);
  }
  return bytes_in_buf;
}

int output_buffer(int fd) {
  int bytes_written=0,bytes_to_write=bytes_in_buf;
  if(fd<=0) return;
  while(bytes_to_write>0) {
    bytes_written=write(fd,buf,bytes_to_write);
    if(bytes_written==-1) return 0;
    bytes_to_write-=bytes_written;
  }
  return bytes_in_buf;
}