From 2b81bf74baeaf734185fa4e2116b44b809ec9b74 Mon Sep 17 00:00:00 2001 From: Avril Date: Thu, 10 Mar 2022 12:39:47 +0000 Subject: [PATCH] Added sink program and Makefile MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for sink's current commit: Half blessing − 半吉 --- .gitignore | 3 +++ Makefile | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ sink.c | 43 ++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 sink.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..867ddb9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.o +sink +sink-* diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ba1925c --- /dev/null +++ b/Makefile @@ -0,0 +1,65 @@ +# Sinks all input to /dev/null +PROJECT=sink + +SRC=$(wildcard *.c) +OUTPUT=$(PROJECT) + +COMMON_FLAGS+= -W -Wall -Wextra -Wstrict-aliasing -fno-strict-aliasing + +TARGET_ARCH?=native +ifneq ($(TARGET_ARCH),) + TARGET_ARCH:=$(addprefix -march=,$(TARGET_ARCH)) +endif +RELEASE_CFLAGS+= $(TARGET_ARCH) -fgraphite -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4 \ + -floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \ + -fno-stack-check + +RELEASE_CFLAGS+= -O3 -flto +RELEASE_LDFLAGS+= -Wl,-O3 -Wl,-flto + +DEBUG_CFLAGS+=-Og -g3 -ggdb -gz +DEBUG_LDFLASG+=-Wl,-g + +CFLAGS += $(COMMON_FLAGS) --std=gnu17 +LDFLAGS += + +OBJ = $(SRC:.c=.o) + +STRIP=strip + +# Phonies + +.PHONY: $(OUTPUT) +$(OUTPUT): $(PROJECT)-release + mv $< $@ + $(STRIP) $@ + + +.PHONY: release +release: $(PROJECT)-release + +.PHONY: debug +debug: $(PROJECT)-debug + +# Targets + +%.o: %.c + $(CC) -c $< $(CFLAGS) -o $@ $(LDFLAGS) + +$(PROJECT)-debug: CFLAGS+=$(DEBUG_CFLAGS) +$(PROJECT)-debug: LDFLAGS+=$(DEBUG_LDFLAGS) +$(PROJECT)-debug: $(OBJ) + $(CC) $^ -fwhole-program $(CFLAGS) -o $@ $(LDFLAGS) + + +$(PROJECT)-release: CFLAGS+=$(RELEASE_CFLAGS) +$(PROJECT)-release: LDFLAGS+=$(RELEASE_LDFLAGS) +$(PROJECT)-release: $(OBJ) + $(CC) $^ -fwhole-program $(CFLAGS) -o $@ $(LDFLAGS) + +clean: + rm -f *.o + rm -f $(OUTPUT) $(PROJECT)-* + + + diff --git a/sink.c b/sink.c new file mode 100644 index 0000000..6682eee --- /dev/null +++ b/sink.c @@ -0,0 +1,43 @@ +#include +#include +#include + +#define r_stdin 0 +#define r_stdout 1 +#define r_stderr 2 + +#define LIKELY(e) __builtin_expect(!!(e), 1) +#define UNLIKELY(e) __builtin_expect(!!(e), 0) + +static inline int dupall(int from) +{ + if(UNLIKELY(dup2(from, r_stdin) < 0)) { + perror("failed to dup2() stdin to sink"); + return 1; + } + if(UNLIKELY(dup2(from, r_stdout) < 0)) { + perror("failed to dup2() stdout to sink"); + return 2; + } +#ifdef REPLACE_STDERR + if(UNLIKELY(dup2(from, r_stderr) < 0)) { + perror("failed to dup2() stderr to sink"); +#else + if(UNLIKELY(close(r_stderr) != 0)) { + perror("failed to close stderr"); + +#endif + return 3; + } + return 0; +} + +int main(void) +{ + int null = open("/dev/null", O_RDWR); + if(UNLIKELY(null<0)) { + perror("failed to open /dev/null"); + return 1; + } + return dupall(null); +}