aboutsummaryrefslogtreecommitdiff
blob: 0ab151c598e7c17edcd1e1d8e445fd32e5f00d0d (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <string.h>
#include <unistd.h>

static int
do_test (void)
{
  int fd = dup (fileno (stdout));
  if (fd <= 1)
    {
      puts ("dup failed");
      return 1;
    }

  FILE *f1 = fdopen (fd, "w");
  if (f1 == NULL)
    {
      printf ("fdopen failed: %m\n");
      return 1;
    }

  fclose (stdout);

  FILE *f2 = popen ("echo test1", "r");
  if (f2 == NULL)
    {
      fprintf (f1, "1st popen failed: %m\n");
      return 1;
    }
  FILE *f3 = popen ("echo test2", "r");
  if (f2 == NULL || f3 == NULL)
    {
      fprintf (f1, "2nd popen failed: %m\n");
      return 1;
    }

  char *line = NULL;
  size_t len = 0;
  int result = 0;
  if (getline (&line, &len, f2) != 6)
    {
      fputs ("could not read line from 1st popen\n", f1);
      result = 1;
    }
  else if (strcmp (line, "test1\n") != 0)
    {
      fprintf (f1, "read \"%s\"\n", line);
      result = 1;
    }

  if (getline (&line, &len, f2) != -1)
    {
      fputs ("second getline did not return -1\n", f1);
      result = 1;
    }

  if (getline (&line, &len, f3) != 6)
    {
      fputs ("could not read line from 2nd popen\n", f1);
      result = 1;
    }
  else if (strcmp (line, "test2\n") != 0)
    {
      fprintf (f1, "read \"%s\"\n", line);
      result = 1;
    }

  if (getline (&line, &len, f3) != -1)
    {
      fputs ("second getline did not return -1\n", f1);
      result = 1;
    }

  int ret = pclose (f2);
  if (ret != 0)
    {
      fprintf (f1, "1st pclose returned %d\n", ret);
      result = 1;
    }

  ret = pclose (f3);
  if (ret != 0)
    {
      fprintf (f1, "2nd pclose returned %d\n", ret);
      result = 1;
    }

  return result;
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"