From 1aafac69cbffc96a2dfc751e27c619668f6ed84d Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 16 Apr 2016 13:39:22 -0400 Subject: [PATCH] ex28 --- lcthw-remnants-2/c-skeleton/LICENSE | 0 lcthw-remnants-2/c-skeleton/Makefile | 57 +++++++++++++++++++ lcthw-remnants-2/c-skeleton/README.md | 0 lcthw-remnants-2/c-skeleton/src/dbg.h | 30 ++++++++++ lcthw-remnants-2/c-skeleton/tests/runtests.sh | 19 +++++++ 5 files changed, 106 insertions(+) create mode 100644 lcthw-remnants-2/c-skeleton/LICENSE create mode 100644 lcthw-remnants-2/c-skeleton/Makefile create mode 100644 lcthw-remnants-2/c-skeleton/README.md create mode 100644 lcthw-remnants-2/c-skeleton/src/dbg.h create mode 100644 lcthw-remnants-2/c-skeleton/tests/runtests.sh diff --git a/lcthw-remnants-2/c-skeleton/LICENSE b/lcthw-remnants-2/c-skeleton/LICENSE new file mode 100644 index 0000000..e69de29 diff --git a/lcthw-remnants-2/c-skeleton/Makefile b/lcthw-remnants-2/c-skeleton/Makefile new file mode 100644 index 0000000..5bca129 --- /dev/null +++ b/lcthw-remnants-2/c-skeleton/Makefile @@ -0,0 +1,57 @@ +CFLAGS = -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG $(OPTFLAGS) +LIBS = -ldl $(OPTLIBS) +PREFIX ?= /usr/local + +SOURCES = $(wildcard src/**/*.c src/*.c) +OBJECTS = $(patsubst %.c,%.o,$(SOURCES)) + +TEST_SRC = $(wildcard tests/*_tests.c) +TESTS = $(patsubst %.c,%,$(TEST_SRC)) + +TARGET = build/libYOUR_LIBRARY.a +SO_TARGET = $(patsubst %.a,%.so,$(TARGET)) + +# The Target Build +all: $(TARGET) $(SO_TARGET) tests + +dev: CFLAGS = -g -Wall -Isrc -Wall -Wextra $(OPTFLAGS) +dev: all + +$(TARGET): CFLAGS += -fPIC +$(TARGET): build $(OBJECTS) + ar rcs $@ $(OBJECTS) + ranlib $@ + +$(SO_TARGET): $(TARGET) $(OBJECTS) + $(CC) -shared -o $@ $(OBJECTS) + +build: + @mkdir -p build + @mkdir -p bin + +# The Unit Tests +.PHONY: tests +tests: CFLAGS += $(TARGET) +tests: $(TESTS) + sh ./tests/runtests.sh + +valgrind: + VALGRIND="valgrind --log-file=/tmp/valgrind-%p.log" $(MAKE) + +# The Cleaner +clean: + rm -rf build $(OBJECTS) $(TESTS) + rm -f tests/tests.log + find . -name "*.gc*" -exec rm {} \; + rm -rf `find . -name "*.dSYM" -print` + +# The Install +install: all + install -d $(DESTDIR)/$(PREFIX)/lib/ + install $(TARGET) $(DESTDIR)/$(PREFIX)/lib/ + +# The Checker +BADFUNCS='[^_.>a-zA-Z0-9](str(n?cpy|n?cat|xfrm|n?dup|str|pbrk|tok|_)|stpn?cpy|a?sn?printf|byte_)' +check: + @echo Files with potentially dangerous functions + @egrep $(BADFUNCS) $(SOURCES) || true diff --git a/lcthw-remnants-2/c-skeleton/README.md b/lcthw-remnants-2/c-skeleton/README.md new file mode 100644 index 0000000..e69de29 diff --git a/lcthw-remnants-2/c-skeleton/src/dbg.h b/lcthw-remnants-2/c-skeleton/src/dbg.h new file mode 100644 index 0000000..a7aaa6a --- /dev/null +++ b/lcthw-remnants-2/c-skeleton/src/dbg.h @@ -0,0 +1,30 @@ +#ifndef __dbg_h__ +#define __dbg_h__ + +#include +#include +#include + +#ifdef NDEBUG +#define debug(M, ...) +#else +#define debug(M, ...) fprintf(stderr, "DEBUG %s:%d:%s: " M "\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__) +#endif + +#define clean_errno() (errno == 0 ? "None" : strerror(errno)) + +#define log_err(M, ...) fprintf(stderr, "[ERROR] (%s:%d:%s: errno: %s) " M "\n", __FILE__, __LINE__, __func__, clean_errno(), ##__VA_ARGS__) + +#define log_warn(M, ...) fprintf(stderr, "[WARN] (%s:%d:%s: errno: %s) " M "\n", __FILE__, __LINE__, __func__, clean_errno(), ##__VA_ARGS__) + +#define log_info(M, ...) fprintf(stderr, "[INFO] (%s:%d:%s) " M "\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__) + +#define check(A, M, ...) if(!(A)) { log_err(M, ##__VA_ARGS__); errno=0; goto error; } + +#define sentinel(M, ...) { log_err(M, ##__VA_ARGS__); errno=0; goto error; } + +#define check_mem(A) check((A), "Out of memory.") + +#define check_debug(A, M, ...) if(!(A)) { debug(M, ##__VA_ARGS__); errno=0; goto error; } + +#endif diff --git a/lcthw-remnants-2/c-skeleton/tests/runtests.sh b/lcthw-remnants-2/c-skeleton/tests/runtests.sh new file mode 100644 index 0000000..2976086 --- /dev/null +++ b/lcthw-remnants-2/c-skeleton/tests/runtests.sh @@ -0,0 +1,19 @@ +echo "Running unit tests:" + +for i in tests/*_tests +do + if test -f $i + then + if $VALGRIND ./$i 2>> tests/tests.log + then + echo $i PASS + else + echo "ERROR in test $i: here's tests/tests.log" + echo "------" + tail tests/tests.log + exit 1 + fi + fi +done + +echo ""