build system updates, port libc-testsuit changes
[libc-test] / src / stdio / popen.c
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <signal.h>
6 #include "test.h"
7
8 #define TEST(r, f, x, m) ( \
9         ((r) = (f)) == (x) || (error("%s failed (" m ")\n", #f, r, x), 0) )
10
11 #define TEST_E(f) ( \
12         (errno = 0), \
13         (f) || (error("%s failed (errno = %d)\n", #f, errno), 0) )
14
15 #define TEST_S(s, x, m) ( \
16         !strcmp((s),(x)) || \
17                 (error("[%s] != [%s] (%s)\n", s, x, m), 0) )
18
19 static sig_atomic_t got_sig;
20
21 static void handler(int sig) {
22         got_sig = 1;
23 }
24
25 void test_popen(void) {
26         int i;
27         char foo[6];
28         char cmd[64];
29         FILE *f;
30
31         TEST_E(f = popen("echo hello", "r"));
32         if (f) {
33                 TEST_E(fgets(foo, sizeof foo, f));
34                 TEST_S(foo, "hello", "child process did not say hello");
35                 TEST(i, pclose(f), 0, "exit status %04x != %04x");
36         }
37
38         signal(SIGUSR1, handler);
39         snprintf(cmd, sizeof cmd, "read a ; test \"x$a\" = xhello && kill -USR1 %d", getpid());
40         TEST_E(f = popen(cmd, "w"));
41         if (f) {
42                 TEST_E(fputs("hello", f) >= 0);
43                 TEST(i, pclose(f), 0, "exit status %04x != %04x");
44                 TEST(i, got_sig, 1, "child process did not send signal");
45         }
46         signal(SIGUSR1, SIG_DFL);
47 }