test/testloadso.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 07 Nov 2006 15:02:12 +0000
changeset 2068 f16a7d02a176
parent 2067 dcdb175c2829
child 2120 2c835d58faad
permissions -rw-r--r--
Merged r2901:2902 from SDL-1.2 branch to trunk: printf stupidity fixes.
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
icculus@2067
    10
typedef int (*fntype)(const char *);
icculus@2067
    11
icculus@2067
    12
int main(int argc, char *argv[])
icculus@2067
    13
{
icculus@2067
    14
	int retval = 0;
icculus@2067
    15
	int hello = 0;
icculus@2067
    16
	const char *libname = NULL;
icculus@2067
    17
	const char *symname = NULL;
icculus@2067
    18
	void *lib = NULL;
icculus@2067
    19
	fntype fn = NULL;
icculus@2067
    20
icculus@2067
    21
	if (argc != 3) {
icculus@2068
    22
		const char *app = argv[0];
icculus@2068
    23
		fprintf(stderr, "USAGE: %s <library> <functionname>\n", app);
icculus@2068
    24
		fprintf(stderr, "       %s --hello <lib with puts()>\n", app);
icculus@2067
    25
		return 1;
icculus@2067
    26
	}
icculus@2067
    27
icculus@2067
    28
	/* Initialize SDL */
icculus@2067
    29
	if ( SDL_Init(0) < 0 ) {
icculus@2067
    30
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
icculus@2067
    31
		return 2;
icculus@2067
    32
	}
icculus@2067
    33
icculus@2067
    34
	if (strcmp(argv[1], "--hello") == 0) {
icculus@2067
    35
		hello = 1;
icculus@2067
    36
		libname = argv[2];
icculus@2067
    37
		symname = "puts";
icculus@2067
    38
	} else {
icculus@2067
    39
		libname = argv[1];
icculus@2067
    40
		symname = argv[2];
icculus@2067
    41
	}
icculus@2067
    42
icculus@2067
    43
	lib = SDL_LoadObject(libname);
icculus@2067
    44
        if (lib == NULL) {
icculus@2067
    45
		fprintf(stderr, "SDL_LoadObject('%s') failed: %s\n",
icculus@2067
    46
		        libname, SDL_GetError());
icculus@2067
    47
		retval = 3;
icculus@2067
    48
	} else {
icculus@2067
    49
		fn = (fntype) SDL_LoadFunction(lib, symname);
icculus@2067
    50
		if (fn == NULL) {
icculus@2067
    51
			fprintf(stderr, "SDL_LoadFunction('%s') failed: %s\n",
icculus@2067
    52
			        symname, SDL_GetError());
icculus@2067
    53
			retval = 4;
icculus@2067
    54
		} else {
icculus@2068
    55
			printf("Found %s in %s at %p\n", symname, libname, fn);
icculus@2067
    56
			if (hello) {
icculus@2067
    57
				printf("Calling function...\n");
icculus@2067
    58
				fflush(stdout);
icculus@2067
    59
				fn("     HELLO, WORLD!\n");
icculus@2067
    60
				printf("...apparently, we survived.  :)\n");
icculus@2067
    61
				printf("Unloading library...\n");
icculus@2067
    62
				fflush(stdout);
icculus@2067
    63
			}
icculus@2067
    64
		}
icculus@2067
    65
		SDL_UnloadObject(lib);
icculus@2067
    66
	}
icculus@2067
    67
	SDL_Quit();
icculus@2067
    68
	return(0);
icculus@2067
    69
}
icculus@2067
    70
icculus@2067
    71