lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | #include <unistd.h> |
| 2 | #include <stdio.h> |
| 3 | #include <errno.h> |
| 4 | #include <string.h> |
| 5 | #include <signal.h> |
| 6 | |
| 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) ) |
| 10 | |
| 11 | #define TEST_E(f) ( (errno = 0), (f) || \ |
| 12 | (printf(__FILE__ ":%d: %s failed (errno = %d)\n", __LINE__, #f, errno), err++, 0) ) |
| 13 | |
| 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) ) |
| 17 | |
| 18 | static sig_atomic_t got_sig; |
| 19 | |
| 20 | static void handler(int sig) |
| 21 | { |
| 22 | got_sig = 1; |
| 23 | } |
| 24 | |
| 25 | int main(void) |
| 26 | { |
| 27 | int i; |
| 28 | char foo[6]; |
| 29 | char cmd[64]; |
| 30 | int err = 0; |
| 31 | FILE *f; |
| 32 | |
| 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"); |
| 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 | 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)"); |
| 45 | |
| 46 | return err; |
| 47 | } |