added explicit casts for agl functions when calling GL_GetProcAddress
[divverent/darkplaces.git] / sys_shared.c
1
2 #include "quakedef.h"
3 # include <time.h>
4 #ifndef WIN32
5 # include <unistd.h>
6 # include <fcntl.h>
7 # include <dlfcn.h>
8 #endif
9
10 static char sys_timestring[128];
11 char *Sys_TimeString(const char *timeformat)
12 {
13         time_t mytime = time(NULL);
14         strftime(sys_timestring, sizeof(sys_timestring), timeformat, localtime(&mytime));
15         return sys_timestring;
16 }
17
18
19 extern qboolean host_shuttingdown;
20 void Sys_Quit (void)
21 {
22         host_shuttingdown = true;
23         Host_Shutdown();
24         exit(0);
25 }
26
27 /*
28 ===============================================================================
29
30 DLL MANAGEMENT
31
32 ===============================================================================
33 */
34
35 qboolean Sys_LoadLibrary (const char** dllnames, dllhandle_t* handle, const dllfunction_t *fcts)
36 {
37         const dllfunction_t *func;
38         dllhandle_t dllhandle = 0;
39         unsigned int i;
40
41         if (handle == NULL)
42                 return false;
43
44         // Initializations
45         for (func = fcts; func && func->name != NULL; func++)
46                 *func->funcvariable = NULL;
47
48         // Try every possible name
49         Con_Printf ("Trying to load library...");
50         for (i = 0; dllnames[i] != NULL; i++)
51         {
52                 Con_Printf (" \"%s\"", dllnames[i]);
53 #ifdef WIN32
54                 dllhandle = LoadLibrary (dllnames[i]);
55 #else
56                 dllhandle = dlopen (dllnames[i], RTLD_LAZY | RTLD_GLOBAL);
57 #endif
58                 if (dllhandle)
59                         break;
60         }
61
62         // see if the names can be loaded relative to the executable path
63         // (this is for Mac OSX which does not check next to the executable)
64         if (!dllhandle && strrchr(com_argv[0], '/'))
65         {
66                 char path[MAX_OSPATH];
67                 strlcpy(path, com_argv[0], sizeof(path));
68                 strrchr(path, '/')[1] = 0;
69                 for (i = 0; dllnames[i] != NULL; i++)
70                 {
71                         char temp[MAX_OSPATH];
72                         strlcpy(temp, path, sizeof(temp));
73                         strlcat(temp, dllnames[i], sizeof(temp));
74                         Con_Printf (" \"%s\"", temp);
75 #ifdef WIN32
76                         dllhandle = LoadLibrary (temp);
77 #else
78                         dllhandle = dlopen (temp, RTLD_LAZY | RTLD_GLOBAL);
79 #endif
80                         if (dllhandle)
81                                 break;
82                 }
83         }
84
85         // No DLL found
86         if (! dllhandle)
87         {
88                 Con_Printf(" - failed.\n");
89                 return false;
90         }
91
92         Con_Printf(" - loaded.\n");
93
94         // Get the function adresses
95         for (func = fcts; func && func->name != NULL; func++)
96                 if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name)))
97                 {
98                         Con_Printf ("Missing function \"%s\" - broken library!\n", func->name);
99                         Sys_UnloadLibrary (&dllhandle);
100                         return false;
101                 }
102
103         *handle = dllhandle;
104         return true;
105 }
106
107 void Sys_UnloadLibrary (dllhandle_t* handle)
108 {
109         if (handle == NULL || *handle == NULL)
110                 return;
111
112 #ifdef WIN32
113         FreeLibrary (*handle);
114 #else
115         dlclose (*handle);
116 #endif
117
118         *handle = NULL;
119 }
120
121 void* Sys_GetProcAddress (dllhandle_t handle, const char* name)
122 {
123 #ifdef WIN32
124         return (void *)GetProcAddress (handle, name);
125 #else
126         return (void *)dlsym (handle, name);
127 #endif
128 }
129