C: makefile rewritten, howamiused.c.

This commit is contained in:
Philippe Pittoli 2024-12-06 12:53:56 +01:00
parent bb8cd3d410
commit 9aa63b19cb
2 changed files with 53 additions and 22 deletions

View File

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

43
c/howamiused.c Normal file
View File

@ -0,0 +1,43 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#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;
}