File: pipe.c

package info (click to toggle)
lurkftp 1.00-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 324 kB
  • ctags: 375
  • sloc: ansic: 4,551; sh: 186; makefile: 162
file content (35 lines) | stat: -rw-r--r-- 626 bytes parent folder | download | duplicates (2)
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

char buf[4096];

int main(int argc, char **argv)
{
    FILE *f;
    int i;
    char bg = 0;

    if(argc < 2) {
	fprintf(stderr, "pipe: make pipe seekable: %s <cmd>\n", argv[0]);
	exit(1);
    }
    if(argc > 2 && !strcmp(argv[1], "-b")) {
	argv++;
	argc--;
	bg = 1;
    }
    f = tmpfile();
    while((i = fread(buf, 1, 4096, stdin)) > 0)
      if(fwrite(buf, i, 1, f) != 1) {
	  perror("tmp file write");
	  exit(1);
      }
    rewind(f);
    close(0);
    dup2(fileno(f), 0);
    close(fileno(f));
    execvp(argv[1], argv+1);
    perror("exec");
    exit(1);
}