#include #include #include #include #include int main(int argc, char **argv) { void *handle; char buf[512], *error; long int (*convert)(char *); /* check for argument */ if (argc < 2) { fprintf(stderr, "usage: %s \n", argv[0]); exit(1); } /* load shared library function */ if (!getcwd(buf, sizeof(buf) - 1)) { fprintf(stderr, "failed to obtain current directory"); exit(1); } strncat(buf, "/libconvert.so", sizeof(buf) - (strlen(buf) + 1)); fprintf(stdout, "using: %s\n", buf); handle = dlopen (buf, RTLD_NOW); if (!handle) { fprintf(stderr, "falling back to default lib ...\n"); if (!(handle = dlopen("/usr/local/lib/libconvert.so", RTLD_NOW))) { fputs(dlerror(), stderr); exit(1); } } convert = dlsym(handle, "atoi"); if ((error = dlerror()) != NULL) { fputs(error, stderr); exit(1); } /* convert first argument to integer and multiply by two */ fprintf(stdout, "%ld\n", 2 * (*convert)(argv[1])); /* cleanup */ dlclose(handle); exit(0); }