Blame src/functional/popen.c

nsz 99d408
#include <unistd.h>
nsz 99d408
#include <stdio.h>
nsz 99d408
#include <errno.h>
nsz 99d408
#include <string.h>
nsz 99d408
#include <signal.h>
nsz 99d408
#include "test.h"
nsz 99d408
nsz 99d408
#define TEST(r, f, x, m) ( \
Szabolcs Nagy cfa23c
	((r) = (f)) == (x) || (t_error("%s failed (" m ")\n", #f, r, x), 0) )
nsz 99d408
nsz 99d408
#define TEST_E(f) ( \
nsz 99d408
	(errno = 0), \
Szabolcs Nagy cfa23c
	(f) || (t_error("%s failed (errno = %d)\n", #f, errno), 0) )
nsz 99d408
nsz 99d408
#define TEST_S(s, x, m) ( \
nsz 99d408
	!strcmp((s),(x)) || \
Szabolcs Nagy cfa23c
		(t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
nsz 99d408
nsz 99d408
static sig_atomic_t got_sig;
nsz 99d408
nsz 99d408
static void handler(int sig) {
nsz 99d408
	got_sig = 1;
nsz 99d408
}
nsz 99d408
nsz 462b4f
int main(void)
nsz 462b4f
{
nsz 99d408
	int i;
nsz 99d408
	char foo[6];
nsz 99d408
	char cmd[64];
nsz 99d408
	FILE *f;
nsz 99d408
nsz 99d408
	TEST_E(f = popen("echo hello", "r"));
nsz 99d408
	if (f) {
nsz 99d408
		TEST_E(fgets(foo, sizeof foo, f));
nsz 99d408
		TEST_S(foo, "hello", "child process did not say hello");
nsz 99d408
		TEST(i, pclose(f), 0, "exit status %04x != %04x");
nsz 99d408
	}
nsz 99d408
nsz 99d408
	signal(SIGUSR1, handler);
nsz 99d408
	snprintf(cmd, sizeof cmd, "read a ; test \"x$a\" = xhello && kill -USR1 %d", getpid());
nsz 99d408
	TEST_E(f = popen(cmd, "w"));
nsz 99d408
	if (f) {
nsz 99d408
		TEST_E(fputs("hello", f) >= 0);
nsz 99d408
		TEST(i, pclose(f), 0, "exit status %04x != %04x");
nsz 99d408
		TEST(i, got_sig, 1, "child process did not send signal");
nsz 99d408
	}
nsz 99d408
	signal(SIGUSR1, SIG_DFL);
Szabolcs Nagy cfa23c
	return t_status;
nsz 99d408
}