Commits

Shizuka Kamishima committed 2ebef8c Draft

ex29 - dynamic linking sucks

  • Participants
  • Parent commits 7719287

Comments (0)

Files changed (3)

+/**
+ * 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
+#include <stdio.h>
+#include "dbg.h"
+#include <dlfcn.h>
+
+typedef int (*lib_function)(const char *data);
+
+int main(int argc, char *argv[]) {
+    int rc = 0;
+    check(argc == 4, "USAGE: ex29 libex29.so function data");
+
+    char *lib_file = argv[1];
+    char *func_to_run = argv[2];
+    char *data = argv[3];
+
+    void *lib = dlopen(lib_file, RTLD_NOW);
+    check(lib != NULL, "Failed to open the library %s: %s", lib_file, dlerror());
+
+    lib_function func = dlsym(lib, func_to_run);
+
+    check(func != NULL, "Did not find function %s() in library %s: %s", func_to_run, lib_file, dlerror());
+
+    rc = func(data);
+    check(rc == 0, "Function %s() return %d for data: %s", func_to_run, rc, data);
+
+    rc = dlclose(lib);
+    check(rc == 0, "Failed to close %s", lib_file);
+
+    return 0;
+
+error:
+    return 1;
+}

File ex29/libex29.c

+#include <stdio.h>
+#include <ctype.h>
+#include "dbg.h"
+
+int print_a_message(const char *msg) {
+    printf("A STRING: %s\n", msg);
+    return 0;
+}
+
+int uppercase(const char *msg) {
+    int i = 0;
+
+    // BUG: \0 termination
+    for (i = 0; msg[i] != '\0'; i++) {
+        printf("%c", toupper(msg[i]));
+    }
+    printf("\n");
+    return 0;
+}
+
+int lowercase(const char *msg) {
+    int i = 0;
+
+    //BUG: \0 termination
+    for (i = 0; msg[i] != '\0'; i++) {
+        printf("%c", tolower(msg[i]));
+    }
+    printf("\n");
+    return 0;
+}
+
+int fail_on_purpose(const char *msg) {
+    return 1;
+}