Replaced snprintf and vnsprintf calls by dpsnprintf and dpvsnprintf calls, to ensure...
[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 qboolean sys_nostdout = false;
11
12 static char sys_timestring[128];
13 char *Sys_TimeString(const char *timeformat)
14 {
15         time_t mytime = time(NULL);
16         strftime(sys_timestring, sizeof(sys_timestring), timeformat, localtime(&mytime));
17         return sys_timestring;
18 }
19
20
21 extern qboolean host_shuttingdown;
22 void Sys_Quit (void)
23 {
24         host_shuttingdown = true;
25         Host_Shutdown();
26         exit(0);
27 }
28
29 char engineversion[128];
30
31 void Sys_Shared_EarlyInit(void)
32 {
33         const char* os;
34
35         Memory_Init ();
36         Log_Init ();
37
38         COM_InitArgv();
39         COM_InitGameType();
40
41 #if defined(__linux__)
42         os = "Linux";
43 #elif defined(WIN32)
44         os = "Windows";
45 #elif defined(__FreeBSD__)
46         os = "FreeBSD";
47 #elif defined(__NetBSD__)
48         os = "NetBSD";
49 #elif defined(__OpenBSD__)
50         os = "OpenBSD";
51 #elif defined(MACOSX)
52         os = "Mac OS X";
53 #else
54         os = "Unknown";
55 #endif
56         dpsnprintf (engineversion, sizeof (engineversion), "%s %s %s", gamename, os, buildstring);
57
58 // COMMANDLINEOPTION: Console: -nostdout disables text output to the terminal the game was launched from
59         if (COM_CheckParm("-nostdout"))
60                 sys_nostdout = 1;
61         else
62                 Con_Printf("%s\n", engineversion);
63 }
64
65 void Sys_Shared_LateInit(void)
66 {
67 }
68
69 /*
70 ===============================================================================
71
72 DLL MANAGEMENT
73
74 ===============================================================================
75 */
76
77 qboolean Sys_LoadLibrary (const char** dllnames, dllhandle_t* handle, const dllfunction_t *fcts)
78 {
79         const dllfunction_t *func;
80         dllhandle_t dllhandle = 0;
81         unsigned int i;
82
83         if (handle == NULL)
84                 return false;
85
86         // Initializations
87         for (func = fcts; func && func->name != NULL; func++)
88                 *func->funcvariable = NULL;
89
90         // Try every possible name
91         for (i = 0; dllnames[i] != NULL; i++)
92         {
93 #ifdef WIN32
94                 dllhandle = LoadLibrary (dllnames[i]);
95 #else
96                 dllhandle = dlopen (dllnames[i], RTLD_LAZY);
97 #endif
98                 if (dllhandle)
99                         break;
100
101                 Con_Printf ("Can't load \"%s\".\n", dllnames[i]);
102         }
103
104         // No DLL found
105         if (! dllhandle)
106                 return false;
107
108         Con_Printf("\"%s\" loaded.\n", dllnames[i]);
109
110         // Get the function adresses
111         for (func = fcts; func && func->name != NULL; func++)
112                 if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name)))
113                 {
114                         Con_Printf ("Missing function \"%s\" - broken library!\n", func->name);
115                         Sys_UnloadLibrary (&dllhandle);
116                         return false;
117                 }
118
119         *handle = dllhandle;
120         return true;
121 }
122
123 void Sys_UnloadLibrary (dllhandle_t* handle)
124 {
125         if (handle == NULL || *handle == NULL)
126                 return;
127
128 #ifdef WIN32
129         FreeLibrary (*handle);
130 #else
131         dlclose (*handle);
132 #endif
133
134         *handle = NULL;
135 }
136
137 void* Sys_GetProcAddress (dllhandle_t handle, const char* name)
138 {
139 #ifdef WIN32
140         return (void *)GetProcAddress (handle, name);
141 #else
142         return (void *)dlsym (handle, name);
143 #endif
144 }
145