Shizuka Kamishima avatar Shizuka Kamishima committed 81dfe21 Draft

liblcthw - init

Comments (0)

Files changed (7)

+Copyright (c) 2012, Shizuka Kamishima
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+1. Redistributions of source code must retain the above copyright
+notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+notice, this list of conditions and the following disclaimer in the
+documentation and/or other materials provided with the distribution.
+3. The name of the author may not be used to endorse or promote products
+derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR
+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
+INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.

Empty file added.

Empty file added.

liblcthw/makefile

+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/liblcthw.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

liblcthw/src/lcthw/dbg.h

+/**
+ * Debug Header - Zed Shaw's *Learn C the Hard Way*
+ * Shizuka Kamishima - 2012-09-15
+ *
+ * USAGE:
+ *
+ * debug(msg, printf_args...)
+ *  prints [DBG] level message
+ *  removed with #define NDEBUG before including this
+ *  or with CFLAGS=-DNDEBUG
+ *
+ * log_err(msg, printf_args...)
+ *  prints [ERROR] level msg with errno translation
+ *
+ * log_warn(msg, printf_args...)
+ *  prints [WARN] level msg with errno translation
+ *
+ * log_info(msg, printf_args...)
+ *  prints [INFO] level message
+ *
+ * check(condition, msg, printf_args...)
+ *  if condition is false, prints [ERROR] level msg
+ *  translates errno values
+ *
+ * sentinel(msg, printf_args...)
+ *  prints [ERROR] level msg with errno translation
+ *  used in branches that shouldn't be reached
+ *
+ * check_mem(ptr)
+ *  used to ensure malloc succeeds
+ *  prints [ERROR] level message if ptr doesn't exist
+ *
+ * check_debug(condition, msg, printf_args...)
+ *  if condition is false, prints [DBG] level message
+ *  removed with #define NDEBUG before including this
+ *  or with CFLAGS=-DNDEBUG
+ */
+
+#ifndef __dbg_h__
+#define __dbg_h__
+
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+
+#ifdef NDEBUG
+#define debug(M, ...)
+#else
+#define debug(M, ...) fprintf(stderr, "\x1b[1;32m[DBG]   \x1b[0;32m[%s():%s:%d]\x1b[0m " M "\n", __func__, __FILE__, __LINE__, ##__VA_ARGS__)
+#endif
+
+#define clean_errno() (errno == 0 ? "None" : strerror(errno))
+
+#define log_err(M, ...) fprintf(stderr, "\x1b[1;31m[ERROR] \x1b[0;31m[%s():%s:%d: errno: %s]\x1b[0m " M "\n", __func__, __FILE__,  __LINE__, clean_errno(), ##__VA_ARGS__)
+
+#define log_warn(M, ...) fprintf(stderr, "\x1b[1;33m[WARN]  \x1b[0;33m[%s():%s:%d: errno: %s]\x1b[0m " M "\n", __func__, __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
+
+#define log_info(M, ...) fprintf(stderr, "\x1b[1;36m[INFO]  \x1b[0;36m[%s():%s:%d]\x1b[0m " M "\n", __func__, __FILE__, __LINE__, ##__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

liblcthw/tests/minunit.h

+#undef NDEBUG
+#ifndef _minunit_h
+#define _minunit_h
+
+#include <stdio.h>
+#include <lcthw/dbg.h>
+#include <stdlib.h>
+
+#define mu_suite_start() char *message = NULL
+
+#define mu_assert(test, message) if (!(test)) { log_err(message); return message; }
+#define mu_run_test(test) debug("\n-----%s", " " #test); {\
+    message = test(); tests_run++; if (message) return message;
+
+#define RUN_TESTS(name) int main(int argc char *argv[]) {\
+    argc = 1; \
+    debug("----- RUNNING: %s", argv[0]); \
+    printf("----\nRUNNING: %s\n", argv[0]); \
+    char *result = name(); \
+    if (result != 0) { \
+        printf("FAILED: %s\n", result); \
+    } else { \
+        printf("ALL TESTS PASSED\n"); \
+    } \
+    printf("Tests run: %d\n", tests_run); \
+    exit(result != 0); \
+}
+
+int tests_run;
+
+#endif

liblcthw/tests/runtests.sh

+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 ""
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.