designtest: com. between app., redir. std(in|out) to pipes

master
Philippe PITTOLI 2016-05-08 03:22:49 +02:00
parent 124cbfd74e
commit 45aecbb91a
3 changed files with 101 additions and 0 deletions

33
c/design-test/Makefile Normal file
View File

@ -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)

15
c/design-test/com-child.c Normal file
View File

@ -0,0 +1,15 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char * argv[])
{
char buf[BUFSIZ];
read (0, buf, BUFSIZ);
buf[BUFSIZ-1] = '\0';
printf ("%s", buf);
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,53 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#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;
}