add test for strftime
[libc-test.git] / src / functional / popen.c
blob42c32c4102d653bcdc3e2edb085398e025af02ea
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <signal.h>
6 #include "test.h"
8 #define TEST(r, f, x, m) ( \
9 ((r) = (f)) == (x) || (t_error("%s failed (" m ")\n", #f, r, x), 0) )
11 #define TEST_E(f) ( \
12 (errno = 0), \
13 (f) || (t_error("%s failed (errno = %d)\n", #f, errno), 0) )
15 #define TEST_S(s, x, m) ( \
16 !strcmp((s),(x)) || \
17 (t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
19 static sig_atomic_t got_sig;
21 static void handler(int sig) {
22 got_sig = 1;
25 int main(void)
27 int i;
28 char foo[6];
29 char cmd[64];
30 FILE *f;
32 TEST_E(f = popen("echo hello", "r"));
33 if (f) {
34 TEST_E(fgets(foo, sizeof foo, f));
35 TEST_S(foo, "hello", "child process did not say hello");
36 TEST(i, pclose(f), 0, "exit status %04x != %04x");
39 signal(SIGUSR1, handler);
40 snprintf(cmd, sizeof cmd, "read a ; test \"x$a\" = xhello && kill -USR1 %d", getpid());
41 TEST_E(f = popen(cmd, "w"));
42 if (f) {
43 TEST_E(fputs("hello", f) >= 0);
44 TEST(i, pclose(f), 0, "exit status %04x != %04x");
45 TEST(i, got_sig, 1, "child process did not send signal");
47 signal(SIGUSR1, SIG_DFL);
48 return t_status;