]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - common.c
Change directory text colour in console from blue to light cyan (#197)
[xonotic/darkplaces.git] / common.c
index 06afb60fbda94023beffced85838aa80f6291c7d..0870a4547d26035cf25edf054282728c28cd8b1c 100644 (file)
--- a/common.c
+++ b/common.c
@@ -29,8 +29,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include "quakedef.h"
 #include "utf8lib.h"
 
-cvar_t registered = {CF_CLIENT | CF_SERVER, "registered","0", "indicates if this is running registered quake (whether gfx/pop.lmp was found)"};
-cvar_t cmdline = {CF_CLIENT | CF_SERVER, "cmdline","0", "contains commandline the engine was launched with"};
+cvar_t registered = {CF_CLIENT | CF_SERVER | CF_READONLY, "registered","0", "indicates if this is running registered quake (whether gfx/pop.lmp was found)"};
+cvar_t cmdline = {CF_CLIENT | CF_SERVER | CF_READONLY, "cmdline","0", "contains commandline the engine was launched with"};
 
 // FIXME: Find a better place for these.
 cvar_t cl_playermodel = {CF_CLIENT | CF_SERVER | CF_USERINFO | CF_ARCHIVE, "playermodel", "", "current player model in Nexuiz/Xonotic"};
@@ -1018,12 +1018,27 @@ int dpvsnprintf (char *buffer, size_t buffersize, const char *format, va_list ar
 #endif
        if (result < 0 || (size_t)result >= buffersize)
        {
-               buffer[buffersize - 1] = '\0';
-               // we could be inside Con_Printf
+               // _vsnprintf_s returns -1 on error and on truncation (indistinguishable),
+               // vsnprintf returns negative on error and the desired strlen on truncation.
+               buffer[buffersize - 1] = '\0'; // should be unnecessary, but just in case
+               // Basic stdout only: we could be inside Con_Printf, Sys_Printf calls dpvsnprintf,
+               // Windows console doesn't support colours.
                if (result < 0)
-                       Sys_Printf("dpvsnprintf: output error, buffer size %zu\n", buffersize);
+                       Sys_Print("dpvsnprintf: output error!\n", 27);
                else
-                       Sys_Printf("dpvsnprintf: truncated to %zu bytes: \"%s\"\n", buffersize - 1, buffer);
+               {
+                       char msg[MAX_INPUTLINE];
+#if _MSC_VER >= 1400
+                       result = _snprintf_s(msg, sizeof(msg), _TRUNCATE, "dpvsnprintf: truncated to %lu bytes: \"%s\"\n", (unsigned long)buffersize - 1, buffer);
+#else
+                       result = snprintf(msg, sizeof(msg), "dpvsnprintf: truncated to %lu bytes: \"%s\"\n", (unsigned long)buffersize - 1, buffer);
+#endif
+                       if (result > 0)
+                       {
+                               msg[sizeof(msg) - 1] = '\n'; // may have been lost in truncation
+                               Sys_Print(msg, min((size_t)result, sizeof(msg) - 1));
+                       }
+               }
                return -1;
        }
 
@@ -1345,7 +1360,8 @@ copy one byte at a time (even at -O3) and its advantage increases with string le
 #ifdef WIN32
        // memccpy() is standard in POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD, C23.
        // Microsoft supports it, but apparently complains if we use it.
-       #pragma warning(disable : 4996)
+       #undef memccpy
+       #define memccpy _memccpy
 #endif
 
 /** Chain-copies a string with truncation and efficiency (compared to strlcat()).
@@ -1361,7 +1377,7 @@ char *dp_stpecpy(char *dst, char *end, const char *src)
        if (p)
                return p - 1;
        end[-1] = '\0';
-       Con_Printf(CON_WARN "%s: src string unterminated or truncated to %zu bytes: \"%s\"\n", __func__, dst == end ? 0 : (end - dst) - 1, dst);
+       Con_Printf(CON_WARN "%s: src string unterminated or truncated to %lu bytes: \"%s\"\n", __func__, (unsigned long)(dst == end ? 0 : (end - dst) - 1), dst);
        return end;
 }
 
@@ -1374,7 +1390,7 @@ char *dp_ustr2stp(char *dst, size_t dsize, const char *src, size_t slen)
        if (slen >= dsize)
        {
                slen = dsize - 1;
-               Con_Printf(CON_WARN "%s: src string truncated to %zu bytes: \"%.*s\"\n", __func__, slen, (int)slen, src);
+               Con_Printf(CON_WARN "%s: src string truncated to %lu bytes: \"%.*s\"\n", __func__, (unsigned long)slen, (int)slen, src);
        }
        memcpy(dst, src, slen);
        dst[slen] = '\0';
@@ -1394,7 +1410,7 @@ size_t dp__strlcpy(char *dst, const char *src, size_t dsize, const char *func, u
        if (p)
                return (p - 1) - dst;
        dst[dsize - 1] = '\0';
-       Con_Printf(CON_WARN "%s:%u: src string unterminated or truncated to %zu bytes: \"%s\"\n", func, line, dsize - 1, dst);
+       Con_Printf(CON_WARN "%s:%u: src string unterminated or truncated to %lu bytes: \"%s\"\n", func, line, (unsigned long)dsize - 1, dst);
        return dsize - 1;
 }
 
@@ -1406,9 +1422,12 @@ size_t dp__strlcpy(char *dst, const char *src, size_t dsize, const char *func, u
  */
 size_t dp__strlcat(char *dst, const char *src, size_t dsize, const char *func, unsigned line)
 {
-       char *p = (char *)memchr(dst, '\0', dsize) ?: dst;
-       size_t offset = p - dst;
+       size_t offset;
+       char *p = (char *)memchr(dst, '\0', dsize);
 
+       if (!p)
+               p = dst;
+       offset = p - dst;
        return dp__strlcpy(p, src, dsize - offset, func, line) + offset;
 }