From 45aecbb91a2928f7466a1ea5cdede8b9c05bba9c Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Sun, 8 May 2016 03:22:49 +0200
Subject: [PATCH] designtest: com. between app., redir. std(in|out) to pipes
---
c/design-test/Makefile | 33 ++++++++++++++++++++++++
c/design-test/com-child.c | 15 +++++++++++
c/design-test/com-parent.c | 53 ++++++++++++++++++++++++++++++++++++++
3 files changed, 101 insertions(+)
create mode 100644 c/design-test/Makefile
create mode 100644 c/design-test/com-child.c
create mode 100644 c/design-test/com-parent.c
diff --git a/c/design-test/Makefile b/c/design-test/Makefile
new file mode 100644
index 0000000..817a957
--- /dev/null
+++ b/c/design-test/Makefile
@@ -0,0 +1,33 @@
+#SOURCE= vlc
+#CFLAGS=$(shell pkg-config --libs vlc)
+#
+#all: compilationvlc
+#
+#compilationvlc:
+# $(CC) $(SOURCE).c -o $(SOURCE) $(CFLAGS)
+
+CC=gcc
+CFLAGS=-c -Wall -g
+LDFLAGS=
+CFILES=$(wildcard *.c) # CFILES => recompiles everything on a C file change
+EXEC=$(basename $(wildcard *.c))
+#SOURCES=$(wildcard *.c)
+TESTS=$(addsuffix .test, $(EXEC))
+
+all: $(SOURCES) $(EXEC)
+
+$(EXEC): $(CFILES)
+ $(CC) $(LDFLAGS) $@.c -o $@
+
+.c.o:
+ $(CC) $(CFLAGS) $< -o $@
+
+clean:
+ @-rm $(EXEC)
+
+# to test a binary "prog" : make prog.test
+
+$(TESTS):
+ valgrind --leak-check=full -v --track-origins=yes ./$(basename $@)
+
+test: all $(TESTS)
diff --git a/c/design-test/com-child.c b/c/design-test/com-child.c
new file mode 100644
index 0000000..65ddba7
--- /dev/null
+++ b/c/design-test/com-child.c
@@ -0,0 +1,15 @@
+#include
+#include
+#include
+#include
+
+int main(int argc, char * argv[])
+{
+
+ char buf[BUFSIZ];
+ read (0, buf, BUFSIZ);
+ buf[BUFSIZ-1] = '\0';
+ printf ("%s", buf);
+
+ return EXIT_SUCCESS;
+}
diff --git a/c/design-test/com-parent.c b/c/design-test/com-parent.c
new file mode 100644
index 0000000..89b6149
--- /dev/null
+++ b/c/design-test/com-parent.c
@@ -0,0 +1,53 @@
+#include
+#include
+#include
+#include
+#include
+
+#define CHILDBIN "./com-child"
+
+int main(int argc, char * argv[])
+{
+
+ if (argc < 2) {
+ fprintf (stderr, "usage : %s string\n", argv[0]);
+ exit (EXIT_FAILURE);
+ }
+
+ int in[2], out[2];
+ FILE *in_fp, *out_fp;
+
+ pipe(in);
+ pipe(out);
+
+ if (fork()) { // PARENT
+
+ close(in[0]);
+ close(out[1]);
+
+ write (in[1], argv[1], strlen(argv[1]));
+
+ char buf[BUFSIZ];
+ read (out[0], buf, BUFSIZ);
+ buf[BUFSIZ-1] = '\0';
+
+ printf ("response : %s\n", buf);
+
+ } else { // CHILD
+
+ close(0);
+ close(1);
+
+ dup2(in[0], 0);
+ dup2(out[1], 1);
+
+ execv(CHILDBIN, NULL);
+ }
+
+ int status;
+ wait(&status);
+ // do something with the status
+
+ return EXIT_SUCCESS;
+}
+