fixed all VS2005 deprecated function warnings
[divverent/darkplaces.git] / sys_shared.c
1 #include "quakedef.h"
2 # include <time.h>
3 #ifndef WIN32
4 # include <unistd.h>
5 # include <fcntl.h>
6 # include <dlfcn.h>
7 #endif
8
9 static char sys_timestring[128];
10 char *Sys_TimeString(const char *timeformat)
11 {
12         time_t mytime = time(NULL);
13 #if _MSC_VER >= 1400
14         struct tm mytm;
15         localtime_s(&mytm, &mytime);
16         strftime(sys_timestring, sizeof(sys_timestring), timeformat, &mytm);
17 #else
18         strftime(sys_timestring, sizeof(sys_timestring), timeformat, localtime(&mytime));
19 #endif
20         return sys_timestring;
21 }
22
23
24 extern qboolean host_shuttingdown;
25 void Sys_Quit (int returnvalue)
26 {
27         if (COM_CheckParm("-profilegameonly"))
28                 Sys_AllowProfiling(false);
29         host_shuttingdown = true;
30         Host_Shutdown();
31         exit(returnvalue);
32 }
33
34 void Sys_AllowProfiling(qboolean enable)
35 {
36 #if defined(__linux__) || defined(__FreeBSD__)
37 int moncontrol(int);
38         moncontrol(enable);
39 #endif
40 }
41
42
43 /*
44 ===============================================================================
45
46 DLL MANAGEMENT
47
48 ===============================================================================
49 */
50
51 qboolean Sys_LoadLibrary (const char** dllnames, dllhandle_t* handle, const dllfunction_t *fcts)
52 {
53         const dllfunction_t *func;
54         dllhandle_t dllhandle = 0;
55         unsigned int i;
56
57         if (handle == NULL)
58                 return false;
59
60 #ifndef WIN32
61 #ifdef PREFER_PRELOAD
62         dllhandle = dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL);
63         if(dllhandle)
64         {
65                 for (func = fcts; func && func->name != NULL; func++)
66                         if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name)))
67                         {
68                                 dlclose(dllhandle);
69                                 goto notfound;
70                         }
71                 Con_Printf ("All of %s's functions were already linked in! Not loading dynamically...\n", dllnames[0]);
72                 *handle = dllhandle;
73                 return true;
74         }
75 notfound:
76 #endif
77 #endif
78
79         // Initializations
80         for (func = fcts; func && func->name != NULL; func++)
81                 *func->funcvariable = NULL;
82
83         // Try every possible name
84         Con_Printf ("Trying to load library...");
85         for (i = 0; dllnames[i] != NULL; i++)
86         {
87                 Con_Printf (" \"%s\"", dllnames[i]);
88 #ifdef WIN32
89                 dllhandle = LoadLibrary (dllnames[i]);
90 #else
91                 dllhandle = dlopen (dllnames[i], RTLD_LAZY | RTLD_GLOBAL);
92 #endif
93                 if (dllhandle)
94                         break;
95         }
96
97         // see if the names can be loaded relative to the executable path
98         // (this is for Mac OSX which does not check next to the executable)
99         if (!dllhandle && strrchr(com_argv[0], '/'))
100         {
101                 char path[MAX_OSPATH];
102                 strlcpy(path, com_argv[0], sizeof(path));
103                 strrchr(path, '/')[1] = 0;
104                 for (i = 0; dllnames[i] != NULL; i++)
105                 {
106                         char temp[MAX_OSPATH];
107                         strlcpy(temp, path, sizeof(temp));
108                         strlcat(temp, dllnames[i], sizeof(temp));
109                         Con_Printf (" \"%s\"", temp);
110 #ifdef WIN32
111                         dllhandle = LoadLibrary (temp);
112 #else
113                         dllhandle = dlopen (temp, RTLD_LAZY | RTLD_GLOBAL);
114 #endif
115                         if (dllhandle)
116                                 break;
117                 }
118         }
119
120         // No DLL found
121         if (! dllhandle)
122         {
123                 Con_Printf(" - failed.\n");
124                 return false;
125         }
126
127         Con_Printf(" - loaded.\n");
128
129         // Get the function adresses
130         for (func = fcts; func && func->name != NULL; func++)
131                 if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name)))
132                 {
133                         Con_Printf ("Missing function \"%s\" - broken library!\n", func->name);
134                         Sys_UnloadLibrary (&dllhandle);
135                         return false;
136                 }
137
138         *handle = dllhandle;
139         return true;
140 }
141
142 void Sys_UnloadLibrary (dllhandle_t* handle)
143 {
144         if (handle == NULL || *handle == NULL)
145                 return;
146
147 #ifdef WIN32
148         FreeLibrary (*handle);
149 #else
150         dlclose (*handle);
151 #endif
152
153         *handle = NULL;
154 }
155
156 void* Sys_GetProcAddress (dllhandle_t handle, const char* name)
157 {
158 #ifdef WIN32
159         return (void *)GetProcAddress (handle, name);
160 #else
161         return (void *)dlsym (handle, name);
162 #endif
163 }
164