From 9aa63b19cb52220d22cc039b4c051d7f2c0b9fdf Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Fri, 6 Dec 2024 12:53:56 +0100 Subject: [PATCH] C: makefile rewritten, howamiused.c. --- c/Makefile | 32 ++++++++++---------------------- c/howamiused.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 c/howamiused.c diff --git a/c/Makefile b/c/Makefile index 3a0de70..6b43baa 100644 --- a/c/Makefile +++ b/c/Makefile @@ -1,33 +1,21 @@ -#SOURCE= vlc -#CFLAGS=$(shell pkg-config --libs vlc) -# -#all: compilationvlc -# -#compilationvlc: -# $(CC) $(SOURCE).c -o $(SOURCE) $(CFLAGS) +# Directory to store compiled binaries. +BINDIR ?= /tmp/bin/ CC=clang 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) +CFILES=$(wildcard *.c) +EXEC=$(patsubst %.c,$(BINDIR)%,$(CFILES)) +TESTS=$(addsuffix .test, $(patsubst %.c,%,$(CFILES))) -$(EXEC): $(CFILES) - $(CC) $(LDFLAGS) $@.c -o $@ +all: $(BINDIR) $(EXEC) -.c.o: - $(CC) $(CFLAGS) $< -o $@ - -clean: - @-rm $(EXEC) +$(BINDIR):; -mkdir -p $(BINDIR) +$(EXEC): $(CFILES); $(CC) $(LDFLAGS) $< -o $@ +clean:; @-rm $(EXEC) # to test a binary "prog" : make prog.test - -$(TESTS): - valgrind --leak-check=full -v --track-origins=yes ./$(basename $@) +$(TESTS):; valgrind --leak-check=full -v --track-origins=yes $(BINDIR)$(basename $@) test: all $(TESTS) diff --git a/c/howamiused.c b/c/howamiused.c new file mode 100644 index 0000000..7665a00 --- /dev/null +++ b/c/howamiused.c @@ -0,0 +1,43 @@ +#include +#include +#include + +#define TMPDIR "/tmp/" + +// When invoked, this application writes its input, parameters and +// environment variables in differents files: last-(input|parameters|env) in TMPDIR. +// +// Useful for debugging purposes. + +int main(int argc, char **argv, char **env) { + int fd = open(TMPDIR "last-input", O_APPEND | O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); + if (fd < 0) { return -1; } + + size_t nb = 0; + char buffer[BUFSIZ]; + while((nb = read(0, buffer, BUFSIZ-1))) { + dprintf(fd, "%*s", (int)nb, buffer); + } + + close(fd); + fd = open(TMPDIR "last-parameters", O_APPEND | O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); + if (fd < 0) { return -1; } + + for (size_t i = 0 ; i < argc ; i++) { + dprintf(fd, "%s\n", argv[i]); + } + + close(fd); + fd = open(TMPDIR "last-env", O_APPEND | O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); + if (fd < 0) { return -1; } + + while (env[0] != NULL) { + dprintf(fd, "%s\n", env[0]); + printf("env: %s\n", env[0]); + env++; + } + + close(fd); + + return 0; +}