1 /*
2  *	Copyright Jan Engelhardt
3  *
4  *	This program is free software; you can redistribute it and/or
5  *	modify it under the terms of the WTF Public License version 2 or
6  *	(at your option) any later version.
7  */
8 #ifdef __cplusplus
9 #	include <cerrno>
10 #	include <cstdlib>
11 #	include <cstring>
12 #else
13 #	include <errno.h>
14 #	include <stdlib.h>
15 #	include <string.h>
16 #endif
17 #include <unistd.h>
18 #include <libHX/init.h>
19 #include <libHX/proc.h>
20 #include <libHX/string.h>
21 
22 static const char *const t_args1[] = {"ls", "ls", "-dl", ".", NULL};
23 static const char *const t_args2[] = {"ls", "ls", "-l", NULL};
24 static const char *const t_args3[] = {"ls", "-l", "/proc/self/fd/", NULL};
25 
t_async1(void)26 static void t_async1(void)
27 {
28 	FILE *fp;
29 	int ret;
30 	unsigned int i = 0;
31 	hxmc_t *line = NULL;
32 	struct HXproc proc;
33 
34 	memset(&proc, 0, sizeof(proc));
35 	proc.p_flags = HXPROC_A0 | HXPROC_STDOUT;
36 
37 	if ((ret = HXproc_run_async(t_args2, &proc)) <= 0) {
38 		fprintf(stderr, "%s\n", strerror(errno));
39 		return;
40 	}
41 	if ((fp = fdopen(proc.p_stdout, "r")) == NULL) {
42 		fprintf(stderr, "%s\n", strerror(errno));
43 		goto out;
44 	}
45 	while (HX_getl(&line, fp) != NULL)
46 		printf("\t#%u\t%s", ++i, line);
47 
48 	fclose(fp);
49  out:
50 	close(proc.p_stdout);
51 	HXproc_wait(&proc);
52 	return;
53 }
54 
main(void)55 int main(void)
56 {
57 	if (HX_init() <= 0)
58 		abort();
59 
60 	/* let it fail - test verbosity */
61 	HXproc_run_sync(t_args1 + 2, HXPROC_VERBOSE);
62 	HXproc_run_sync(t_args1 + 3, HXPROC_VERBOSE);
63 
64 	HXproc_run_sync(t_args1 + 1, 0);
65 
66 	t_async1();
67 	HXproc_run_sync(t_args3, HXPROC_NULL_STDIN);
68 	HX_exit();
69 	return EXIT_SUCCESS;
70 }
71