test/testloadso.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 14 Jun 2007 13:21:29 +0000
changeset 2120 2c835d58faad
parent 2068 f16a7d02a176
child 3338 9de326b3099c
permissions -rw-r--r--
make indent
icculus@2067
     1
icculus@2067
     2
/* Test program to test dynamic loading with the loadso subsystem. 
icculus@2067
     3
*/
icculus@2067
     4
icculus@2067
     5
#include <stdio.h>
icculus@2067
     6
#include <stdlib.h>
icculus@2067
     7
icculus@2067
     8
#include "SDL.h"
icculus@2067
     9
slouken@2120
    10
typedef int (*fntype) (const char *);
icculus@2067
    11
slouken@2120
    12
int
slouken@2120
    13
main(int argc, char *argv[])
icculus@2067
    14
{
slouken@2120
    15
    int retval = 0;
slouken@2120
    16
    int hello = 0;
slouken@2120
    17
    const char *libname = NULL;
slouken@2120
    18
    const char *symname = NULL;
slouken@2120
    19
    void *lib = NULL;
slouken@2120
    20
    fntype fn = NULL;
icculus@2067
    21
slouken@2120
    22
    if (argc != 3) {
slouken@2120
    23
        const char *app = argv[0];
slouken@2120
    24
        fprintf(stderr, "USAGE: %s <library> <functionname>\n", app);
slouken@2120
    25
        fprintf(stderr, "       %s --hello <lib with puts()>\n", app);
slouken@2120
    26
        return 1;
slouken@2120
    27
    }
icculus@2067
    28
slouken@2120
    29
    /* Initialize SDL */
slouken@2120
    30
    if (SDL_Init(0) < 0) {
slouken@2120
    31
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@2120
    32
        return 2;
slouken@2120
    33
    }
icculus@2067
    34
slouken@2120
    35
    if (strcmp(argv[1], "--hello") == 0) {
slouken@2120
    36
        hello = 1;
slouken@2120
    37
        libname = argv[2];
slouken@2120
    38
        symname = "puts";
slouken@2120
    39
    } else {
slouken@2120
    40
        libname = argv[1];
slouken@2120
    41
        symname = argv[2];
slouken@2120
    42
    }
icculus@2067
    43
slouken@2120
    44
    lib = SDL_LoadObject(libname);
slouken@2120
    45
    if (lib == NULL) {
slouken@2120
    46
        fprintf(stderr, "SDL_LoadObject('%s') failed: %s\n",
slouken@2120
    47
                libname, SDL_GetError());
slouken@2120
    48
        retval = 3;
slouken@2120
    49
    } else {
slouken@2120
    50
        fn = (fntype) SDL_LoadFunction(lib, symname);
slouken@2120
    51
        if (fn == NULL) {
slouken@2120
    52
            fprintf(stderr, "SDL_LoadFunction('%s') failed: %s\n",
slouken@2120
    53
                    symname, SDL_GetError());
slouken@2120
    54
            retval = 4;
slouken@2120
    55
        } else {
slouken@2120
    56
            printf("Found %s in %s at %p\n", symname, libname, fn);
slouken@2120
    57
            if (hello) {
slouken@2120
    58
                printf("Calling function...\n");
slouken@2120
    59
                fflush(stdout);
slouken@2120
    60
                fn("     HELLO, WORLD!\n");
slouken@2120
    61
                printf("...apparently, we survived.  :)\n");
slouken@2120
    62
                printf("Unloading library...\n");
slouken@2120
    63
                fflush(stdout);
slouken@2120
    64
            }
slouken@2120
    65
        }
slouken@2120
    66
        SDL_UnloadObject(lib);
slouken@2120
    67
    }
slouken@2120
    68
    SDL_Quit();
slouken@2120
    69
    return (0);
icculus@2067
    70
}