]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - sys_shared.c
strncpy -> {strlcpy,memcpy}. Replaced the "forceloop" boolean in "channel_t" by a...
[xonotic/darkplaces.git] / sys_shared.c
index d1bc7fe60c90029f8bbdcb2e35b4b644fdecdb47..a72125f244940f5046f2111e45624a3d64756c7f 100644 (file)
@@ -1,9 +1,10 @@
 
 #include "quakedef.h"
-#include <time.h>
+# include <time.h>
 #ifndef WIN32
-#include <unistd.h>
-#include <fcntl.h>
+# include <unistd.h>
+# include <fcntl.h>
+# include <dlfcn.h>
 #endif
 
 extern cvar_t  timestamps;
@@ -48,14 +49,20 @@ static char qfont_table[256] = {
        'x',  'y',  'z',  '{',  '|',  '}',  '~',  '<'
 };
 
+static char sys_timestring[128];
+char *Sys_TimeString(const char *timeformat)
+{
+       time_t mytime = time(NULL);
+       strftime(sys_timestring, sizeof(sys_timestring), timeformat, localtime(&mytime));
+       return sys_timestring;
+}
+
 
 #define MAXPRINTMSG 16384
 
 void Sys_Print(const char *msg)
 {
        unsigned char *p;
-       // Time stamp
-       char stamp[128];
        // String we print
        char final[MAXPRINTMSG];
 
@@ -63,13 +70,9 @@ void Sys_Print(const char *msg)
                return;
 
        if (timestamps.integer)
-       {
-               time_t mytime = time(NULL);
-               strftime(stamp, sizeof(stamp), timeformat.string, localtime(&mytime));
-               snprintf(final, sizeof(final), "%s%s", stamp, msg);
-       }
+               snprintf(final, sizeof(final), "%s%s", Sys_TimeString(timeformat.string), msg);
        else
-               strncpy(final, msg, sizeof(final));
+               strlcpy (final, msg, sizeof (final));
 
        // LordHavoc: make sure the string is terminated
        final[MAXPRINTMSG-1] = 0;
@@ -133,26 +136,56 @@ DLL MANAGEMENT
 ===============================================================================
 */
 
-#ifndef WIN32
-#include <dlfcn.h>
-#endif
-
-dllhandle_t Sys_LoadLibrary (const char* name)
+qboolean Sys_LoadLibrary (const char* dllname, dllhandle_t* handle, const dllfunction_t *fcts)
 {
+       const dllfunction_t *func;
+       dllhandle_t dllhandle;
+
+       if (handle == NULL)
+               return false;
+
+       // Initializations
+       for (func = fcts; func && func->name != NULL; func++)
+               *func->funcvariable = NULL;
+
+       // Load the DLL
 #ifdef WIN32
-       return LoadLibrary (name);
+       dllhandle = LoadLibrary (dllname);
 #else
-       return dlopen (name, RTLD_LAZY);
+       dllhandle = dlopen (dllname, RTLD_LAZY);
 #endif
+       if (! dllhandle)
+       {
+               Con_Printf ("Can't load \"%s\".\n", dllname);
+               return false;
+       }
+
+       // Get the function adresses
+       for (func = fcts; func && func->name != NULL; func++)
+               if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name)))
+               {
+                       Con_Printf ("Missing function \"%s\" - broken library!\n", func->name);
+                       Sys_UnloadLibrary (&dllhandle);
+                       return false;
+               }
+
+       *handle = dllhandle;
+       Con_DPrintf("\"%s\" loaded.\n", dllname);
+       return true;
 }
 
-void Sys_UnloadLibrary (dllhandle_t handle)
+void Sys_UnloadLibrary (dllhandle_t* handle)
 {
+       if (handle == NULL || *handle == NULL)
+               return;
+
 #ifdef WIN32
-       FreeLibrary (handle);
+       FreeLibrary (*handle);
 #else
-       dlclose (handle);
+       dlclose (*handle);
 #endif
+
+       *handle = NULL;
 }
 
 void* Sys_GetProcAddress (dllhandle_t handle, const char* name)