Added sink program and Makefile

Fortune for sink's current commit: Half blessing − 半吉
master
Avril 2 years ago
commit 2b81bf74ba
Signed by: flanchan
GPG Key ID: 284488987C31F630

3
.gitignore vendored

@ -0,0 +1,3 @@
*.o
sink
sink-*

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

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