summaryrefslogtreecommitdiff
path: root/src/tc-proc.c
blob: 7ec982fea3a3221a5a1765b3ad31a90a34308ec2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 *	Copyright Jan Engelhardt
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the WTF Public License version 2 or
 *	(at your option) any later version.
 */
#ifdef __cplusplus
#	include <cerrno>
#	include <cstdlib>
#	include <cstring>
#else
#	include <errno.h>
#	include <stdlib.h>
#	include <string.h>
#endif
#include <unistd.h>
#include <libHX/init.h>
#include <libHX/proc.h>
#include <libHX/string.h>

static const char *const t_args1[] = {"ls", "ls", "-dl", ".", NULL};
static const char *const t_args2[] = {"ls", "ls", "-l", NULL};
static const char *const t_args3[] = {"ls", "-l", "/proc/self/fd/", NULL};

static void t_async1(void)
{
	FILE *fp;
	int ret;
	unsigned int i = 0;
	hxmc_t *line = NULL;
	struct HXproc proc;

	memset(&proc, 0, sizeof(proc));
	proc.p_flags = HXPROC_A0 | HXPROC_STDOUT;

	if ((ret = HXproc_run_async(t_args2, &proc)) <= 0) {
		fprintf(stderr, "%s\n", strerror(errno));
		return;
	}
	if ((fp = fdopen(proc.p_stdout, "r")) == NULL) {
		fprintf(stderr, "%s\n", strerror(errno));
		goto out;
	}
	while (HX_getl(&line, fp) != NULL)
		printf("\t#%u\t%s", ++i, line);

	fclose(fp);
 out:
	close(proc.p_stdout);
	HXproc_wait(&proc);
	return;
}

int main(void)
{
	if (HX_init() <= 0)
		abort();

	/* let it fail - test verbosity */
	HXproc_run_sync(t_args1 + 2, HXPROC_VERBOSE);
	HXproc_run_sync(t_args1 + 3, HXPROC_VERBOSE);

	HXproc_run_sync(t_args1 + 1, 0);

	t_async1();
	HXproc_run_sync(t_args3, HXPROC_NULL_STDIN);
	HX_exit();
	return EXIT_SUCCESS;
}