Debian Codegraph @

test/testloadso.c

raw
1 /*
2 Copyright (C) 1997-2026 Sam Lantinga <[email protected]>
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely.
11 */
12
13 /* Test program to test dynamic loading with the loadso subsystem.
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #include "SDL.h"
21
22 typedef int (*fntype)(const char *);
23
24 int main(int argc, char *argv[])
25 {
26 int retval = 0;
27 int hello = 0;
28 const char *libname = NULL;
29 const char *symname = NULL;
30 void *lib = NULL;
31 fntype fn = NULL;
32
33 if (argc != 3) {
34 const char *app = argv[0];
35 SDL_Log("USAGE: %s <library> <functionname>\n", app);
36 SDL_Log(" %s --hello <lib with puts()>\n", app);
37 return 1;
38 }
39
40 /* Initialize SDL */
41 if (SDL_Init(0) < 0) {
42 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
43 return 2;
44 }
45
46 if (SDL_strcmp(argv[1], "--hello") == 0) {
47 hello = 1;
48 libname = argv[2];
49 symname = "puts";
50 } else {
51 libname = argv[1];
52 symname = argv[2];
53 }
54
55 lib = SDL_LoadObject(libname);
56 if (!lib) {
57 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_LoadObject('%s') failed: %s\n",
58 libname, SDL_GetError());
59 retval = 3;
60 } else {
61 fn = (fntype)SDL_LoadFunction(lib, symname);
62 if (!fn) {
63 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_LoadFunction('%s') failed: %s\n",
64 symname, SDL_GetError());
65 retval = 4;
66 } else {
67 SDL_Log("Found %s in %s at %p\n", symname, libname, fn);
68 if (hello) {
69 SDL_Log("Calling function...\n");
70 fn(" HELLO, WORLD!\n");
71 SDL_Log("...apparently, we survived. :)\n");
72 SDL_Log("Unloading library...\n");
73 }
74 }
75 SDL_UnloadObject(lib);
76 }
77 SDL_Quit();
78 return retval;
79 }