19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Test program to test dynamic loading with the loadso subsystem.
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <stdio.h>
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <stdlib.h>
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL.h"
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef int (*fntype)(const char *);
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint main(int argc, char *argv[])
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval = 0;
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int hello = 0;
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *libname = NULL;
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *symname = NULL;
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	void *lib = NULL;
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fntype fn = NULL;
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (argc != 3) {
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		const char *app = argv[0];
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fprintf(stderr, "USAGE: %s <library> <functionname>\n", app);
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fprintf(stderr, "       %s --hello <lib with puts()>\n", app);
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 1;
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize SDL */
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_Init(0) < 0 ) {
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 2;
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (strcmp(argv[1], "--hello") == 0) {
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		hello = 1;
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		libname = argv[2];
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		symname = "puts";
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		libname = argv[1];
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		symname = argv[2];
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	lib = SDL_LoadObject(libname);
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (lib == NULL) {
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fprintf(stderr, "SDL_LoadObject('%s') failed: %s\n",
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		        libname, SDL_GetError());
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = 3;
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fn = (fntype) SDL_LoadFunction(lib, symname);
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (fn == NULL) {
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			fprintf(stderr, "SDL_LoadFunction('%s') failed: %s\n",
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			        symname, SDL_GetError());
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			retval = 4;
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			printf("Found %s in %s at %p\n", symname, libname, fn);
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (hello) {
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				printf("Calling function...\n");
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				fflush(stdout);
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				fn("     HELLO, WORLD!\n");
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				printf("...apparently, we survived.  :)\n");
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				printf("Unloading library...\n");
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				fflush(stdout);
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_UnloadObject(lib);
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Quit();
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
72