changed Host_Init to execute configs only once
[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                 return false;
65
66         Con_Printf("\"%s\" loaded.\n", dllnames[i]);
67
68         // Get the function adresses
69         for (func = fcts; func && func->name != NULL; func++)
70                 if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name)))
71                 {
72                         Con_Printf ("Missing function \"%s\" - broken library!\n", func->name);
73                         Sys_UnloadLibrary (&dllhandle);
74                         return false;
75                 }
76
77         *handle = dllhandle;
78         return true;
79 }
80
81 void Sys_UnloadLibrary (dllhandle_t* handle)
82 {
83         if (handle == NULL || *handle == NULL)
84                 return;
85
86 #ifdef WIN32
87         FreeLibrary (*handle);
88 #else
89         dlclose (*handle);
90 #endif
91
92         *handle = NULL;
93 }
94
95 void* Sys_GetProcAddress (dllhandle_t handle, const char* name)
96 {
97 #ifdef WIN32
98         return (void *)GetProcAddress (handle, name);
99 #else
100         return (void *)dlsym (handle, name);
101 #endif
102 }
103