popen from libc-testsuit
authornsz <nsz@port70.net>
Wed, 3 Aug 2011 06:21:51 +0000 (08:21 +0200)
committernsz <nsz@port70.net>
Wed, 3 Aug 2011 06:21:51 +0000 (08:21 +0200)
src/stdio/popen.c [new file with mode: 0644]

diff --git a/src/stdio/popen.c b/src/stdio/popen.c
new file mode 100644 (file)
index 0000000..a373df6
--- /dev/null
@@ -0,0 +1,48 @@
+#define _POSIX_C_SOURCE 200809L
+#include <unistd.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <signal.h>
+#include "test.h"
+
+#define TEST(r, f, x, m) ( \
+       ((r) = (f)) == (x) || (error("%s failed (" m ")\n", #f, r, x), 0) )
+
+#define TEST_E(f) ( \
+       (errno = 0), \
+       (f) || (error("%s failed (errno = %d)\n", #f, errno), 0) )
+
+#define TEST_S(s, x, m) ( \
+       !strcmp((s),(x)) || \
+               (error("[%s] != [%s] (%s)\n", s, x, m), 0) )
+
+static sig_atomic_t got_sig;
+
+static void handler(int sig) {
+       got_sig = 1;
+}
+
+void test_popen(void) {
+       int i;
+       char foo[6];
+       char cmd[64];
+       FILE *f;
+
+       TEST_E(f = popen("echo hello", "r"));
+       if (f) {
+               TEST_E(fgets(foo, sizeof foo, f));
+               TEST_S(foo, "hello", "child process did not say hello");
+               TEST(i, pclose(f), 0, "exit status %04x != %04x");
+       }
+
+       signal(SIGUSR1, handler);
+       snprintf(cmd, sizeof cmd, "read a ; test \"x$a\" = xhello && kill -USR1 %d", getpid());
+       TEST_E(f = popen(cmd, "w"));
+       if (f) {
+               TEST_E(fputs("hello", f) >= 0);
+               TEST(i, pclose(f), 0, "exit status %04x != %04x");
+               TEST(i, got_sig, 1, "child process did not send signal");
+       }
+       signal(SIGUSR1, SIG_DFL);
+}