[Tizen] Support Autoconf 2.71
[platform/upstream/SDL.git] / test / testloadso.c
1 /*
2   Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
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 int
24 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 (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 == NULL) {
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 == NULL) {
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                 fflush(stdout);
71                 fn("     HELLO, WORLD!\n");
72                 SDL_Log("...apparently, we survived.  :)\n");
73                 SDL_Log("Unloading library...\n");
74                 fflush(stdout);
75             }
76         }
77         SDL_UnloadObject(lib);
78     }
79     SDL_Quit();
80     return retval;
81 }