This commit is contained in:
Dan Buch 2016-04-16 15:35:47 -04:00
parent d8c63c261d
commit 1fdab16d96
No known key found for this signature in database
GPG Key ID: FAEF12936DD3E3EC
3 changed files with 85 additions and 3 deletions

View File

@ -1,14 +1,18 @@
SHELL=/bin/bash SHELL = /bin/bash
CFLAGS=-Wall -g -DNDEBUG CFLAGS = -Wall -g -DNDEBUG -fPIC
LDLIBS = -ldl
EXERCISES := $(shell ./list-exercises) EXERCISES := $(shell ./list-exercises)
all: $(EXERCISES) all: $(EXERCISES) libex29.so
ex19: object.o ex19: object.o
ex22_main: ex22.o ex22_main: ex22.o
libex29.so: libex29.o
$(CC) -shared -o $@ $<
clean: clean:
shopt -s nullglob ; \ shopt -s nullglob ; \
$(RM) $(EXERCISES) *.o *.a $(RM) $(EXERCISES) *.o *.a

33
lcthw-remnants-2/ex29.c Normal file
View File

@ -0,0 +1,33 @@
#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 %s function in the 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;
}

View File

@ -0,0 +1,45 @@
#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 problems
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 problems
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;
}