All pastes #229839 Raw Edit

Miscellany

public c v1 · immutable
#229839 ·published 2006-10-30 23:32 UTC
rendered paste body
/* Compile and link with: *  * gcc -rdynamic -ldl -o dltest dltest.c */#include <stdio.h>#include <dlfcn.h>#include <stdlib.h>int foo_11 = 16;int foo_8  = 19;int foo_5  = 20;int foo_14 = 22;int foo_10 = 32;int foo_2  = 42;int foo_6  = 50;int foo_15 = 51;int foo_1  = 57;int foo_3  = 68;int foo_0  = 76;int foo_12 = 80;int foo_4  = 82;int foo_9  = 84;int foo_13 = 88;int foo_7  = 98;int main(int argc, char **argv) {    void *dlhand;    int *foo = NULL; /* Our dynamically loaded stuff */    int userin = -1;    char symname[100];    dlhand = dlopen(NULL, RTLD_LAZY|RTLD_LOCAL);    if(dlhand == NULL) { /* ack fail */        fprintf(stderr, "%s\n", dlerror());        exit(EXIT_FAILURE);    }    dlerror(); /* clear error flag */    while (1) {        printf("Enter a number between 0 and 15, -1 to quit: ");        scanf(" %i", &userin);        if(userin == -1) {            break;        }        else if(userin > 15 || userin < 0) {            continue;        }        snprintf(symname, 100, "foo_%i", userin);        foo = dlsym(dlhand, symname);        if(foo == NULL) {            fprintf(stderr, "%s\n", dlerror());            exit(EXIT_FAILURE);        }        printf("Your magically-obtained result is: %d\n", *foo);    }    dlclose(dlhand);    return 0;}