h8300: Assembly functions
[uclibc-ng.git] / test / misc / popen.c
blob868b70eedfdd39c71e43d99db7f956946dc5a90e
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <signal.h>
7 #define TEST(r, f, x, m) ( \
8 ((r) = (f)) == (x) || \
9 (printf(__FILE__ ":%d: %s failed (" m ")\n", __LINE__, #f, r, x), err++, 0) )
11 #define TEST_E(f) ( (errno = 0), (f) || \
12 (printf(__FILE__ ":%d: %s failed (errno = %d)\n", __LINE__, #f, errno), err++, 0) )
14 #define TEST_S(s, x, m) ( \
15 !strcmp((s),(x)) || \
16 (printf(__FILE__ ":%d: [%s] != [%s] (%s)\n", __LINE__, s, x, m), err++, 0) )
18 static sig_atomic_t got_sig;
20 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 int err = 0;
31 FILE *f;
33 TEST_E(f = popen("echo hello", "r"));
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");
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 TEST_E(fputs("hello", f) >= 0);
42 TEST(i, pclose(f), 0, "exit status %04x != %04x");
43 signal(SIGUSR1, SIG_DFL);
44 TEST(i, got_sig, 1, "child process did not send signal (%i!=%i)");
46 return err;