test/testloadso.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Oct 2009 20:44:04 +0000
changeset 3342 3da232c5980a
parent 3338 9de326b3099c
child 5535 96594ac5fd1a
permissions -rw-r--r--
Split acinclude.m4 into its constituent parts for easy upgrading
icculus@2067
     1
slouken@3338
     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>
slouken@3338
     7
#include <string.h>
icculus@2067
     8
icculus@2067
     9
#include "SDL.h"
icculus@2067
    10
slouken@2120
    11
typedef int (*fntype) (const char *);
icculus@2067
    12
slouken@2120
    13
int
slouken@2120
    14
main(int argc, char *argv[])
icculus@2067
    15
{
slouken@2120
    16
    int retval = 0;
slouken@2120
    17
    int hello = 0;
slouken@2120
    18
    const char *libname = NULL;
slouken@2120
    19
    const char *symname = NULL;
slouken@2120
    20
    void *lib = NULL;
slouken@2120
    21
    fntype fn = NULL;
icculus@2067
    22
slouken@2120
    23
    if (argc != 3) {
slouken@2120
    24
        const char *app = argv[0];
slouken@2120
    25
        fprintf(stderr, "USAGE: %s <library> <functionname>\n", app);
slouken@2120
    26
        fprintf(stderr, "       %s --hello <lib with puts()>\n", app);
slouken@2120
    27
        return 1;
slouken@2120
    28
    }
icculus@2067
    29
slouken@2120
    30
    /* Initialize SDL */
slouken@2120
    31
    if (SDL_Init(0) < 0) {
slouken@2120
    32
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@2120
    33
        return 2;
slouken@2120
    34
    }
icculus@2067
    35
slouken@2120
    36
    if (strcmp(argv[1], "--hello") == 0) {
slouken@2120
    37
        hello = 1;
slouken@2120
    38
        libname = argv[2];
slouken@2120
    39
        symname = "puts";
slouken@2120
    40
    } else {
slouken@2120
    41
        libname = argv[1];
slouken@2120
    42
        symname = argv[2];
slouken@2120
    43
    }
icculus@2067
    44
slouken@2120
    45
    lib = SDL_LoadObject(libname);
slouken@2120
    46
    if (lib == NULL) {
slouken@2120
    47
        fprintf(stderr, "SDL_LoadObject('%s') failed: %s\n",
slouken@2120
    48
                libname, SDL_GetError());
slouken@2120
    49
        retval = 3;
slouken@2120
    50
    } else {
slouken@2120
    51
        fn = (fntype) SDL_LoadFunction(lib, symname);
slouken@2120
    52
        if (fn == NULL) {
slouken@2120
    53
            fprintf(stderr, "SDL_LoadFunction('%s') failed: %s\n",
slouken@2120
    54
                    symname, SDL_GetError());
slouken@2120
    55
            retval = 4;
slouken@2120
    56
        } else {
slouken@2120
    57
            printf("Found %s in %s at %p\n", symname, libname, fn);
slouken@2120
    58
            if (hello) {
slouken@2120
    59
                printf("Calling function...\n");
slouken@2120
    60
                fflush(stdout);
slouken@2120
    61
                fn("     HELLO, WORLD!\n");
slouken@2120
    62
                printf("...apparently, we survived.  :)\n");
slouken@2120
    63
                printf("Unloading library...\n");
slouken@2120
    64
                fflush(stdout);
slouken@2120
    65
            }
slouken@2120
    66
        }
slouken@2120
    67
        SDL_UnloadObject(lib);
slouken@2120
    68
    }
slouken@2120
    69
    SDL_Quit();
slouken@2120
    70
    return (0);
icculus@2067
    71
}