File: popen1.c

package info (click to toggle)
dmtcp 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,496 kB
  • sloc: cpp: 33,592; ansic: 28,099; sh: 6,735; makefile: 1,950; perl: 1,690; python: 1,241; asm: 138; java: 13
file content (44 lines) | stat: -rw-r--r-- 746 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
#define _POSIX_C_SOURCE 2
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

int main(int argc, char* argv[])
{
  FILE *fr;
  FILE *fw;
  int ch;

  while (1)
  {
    fr = popen("ls *", "r");
    if (fr == NULL) {
      perror("popen");
      exit(1);
    }
    int fd1 = open("/dev/zero", O_RDONLY);

    fw = popen("sort > /dev/null", "w");
    if (fw == NULL) {
      perror("popen");
      exit(1);
    }
    int fd2 = open("/dev/null", O_RDONLY);

    while ((ch = fgetc(fr)) != EOF) {
      if (fputc(ch, fw) == EOF) {
        perror("fputc");
        exit(1);
      }
      putchar(ch);
    }
    pclose(fr);
    close(fd1);
    pclose(fw);
    sleep(2);
    close(fd2);
  }
  return 0;
}