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