fix the library search code (replaced / with 0, should've kept it and replaced the...
[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         for (i = 0; dllnames[i] != NULL; i++)
50         {
51 #ifdef WIN32
52                 dllhandle = LoadLibrary (dllnames[i]);
53 #else
54                 dllhandle = dlopen (dllnames[i], RTLD_LAZY);
55 #endif
56                 if (dllhandle)
57                         break;
58
59                 Con_Printf ("Can't load \"%s\".\n", dllnames[i]);
60         }
61
62         // No DLL found
63         if (! dllhandle)
64         {
65                 // see if the names can be loaded relative to the executable path
66                 // (this is for Mac OSX which does not check next to the executable)
67                 if (strrchr(com_argv[0], '/'))
68                 {
69                         char path[MAX_OSPATH];
70                         strlcpy(path, com_argv[0], sizeof(path));
71                         strrchr(com_argv[0], '/')[1] = 0;
72                         for (i = 0; dllnames[i] != NULL; i++)
73                         {
74                                 char temp[MAX_OSPATH];
75                                 strlcpy(temp, path, sizeof(temp));
76                                 strlcat(temp, dllnames[i], sizeof(temp));
77 #ifdef WIN32
78                                 dllhandle = LoadLibrary (temp);
79 #else
80                                 dllhandle = dlopen (temp, RTLD_LAZY);
81 #endif
82                                 if (dllhandle)
83                                         break;
84
85                                 Con_Printf ("Can't load \"%s\".\n", temp);
86                         }
87                         if (! dllhandle)
88                                 return false;
89                 }
90                 else
91                         return false;
92         }
93
94         Con_Printf("\"%s\" loaded.\n", dllnames[i]);
95
96         // Get the function adresses
97         for (func = fcts; func && func->name != NULL; func++)
98                 if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name)))
99                 {
100                         Con_Printf ("Missing function \"%s\" - broken library!\n", func->name);
101                         Sys_UnloadLibrary (&dllhandle);
102                         return false;
103                 }
104
105         *handle = dllhandle;
106         return true;
107 }
108
109 void Sys_UnloadLibrary (dllhandle_t* handle)
110 {
111         if (handle == NULL || *handle == NULL)
112                 return;
113
114 #ifdef WIN32
115         FreeLibrary (*handle);
116 #else
117         dlclose (*handle);
118 #endif
119
120         *handle = NULL;
121 }
122
123 void* Sys_GetProcAddress (dllhandle_t handle, const char* name)
124 {
125 #ifdef WIN32
126         return (void *)GetProcAddress (handle, name);
127 #else
128         return (void *)dlsym (handle, name);
129 #endif
130 }
131