ncurses 5.9 - patch 20121026
[ncurses.git] / ncurses / trace / varargs.c
index 0b3c28c4b61e0b4dd47fa073dff8548aa3730527..66252d88db210834c12a1fcc972592e3b0370472 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2001,2002 Free Software Foundation, Inc.                   *
+ * Copyright (c) 2001-2008,2012 Free Software Foundation, Inc.              *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -34,7 +34,7 @@
 
 #include <ctype.h>
 
-MODULE_ID("$Id: varargs.c,v 1.3 2002/10/05 23:04:17 tom Exp $")
+MODULE_ID("$Id: varargs.c,v 1.11 2012/10/27 21:03:28 tom Exp $")
 
 #ifdef TRACE
 
@@ -44,11 +44,14 @@ typedef enum {
     atUnknown = 0, atInteger, atFloat, atPoint, atString
 } ARGTYPE;
 
-#define VA_INT(type) ival = va_arg(ap, type)
+#define VA_INT(type) ival = (int) va_arg(ap, type)
 #define VA_FLT(type) fval = va_arg(ap, type)
-#define VA_PTR(type) pval = (void *)va_arg(ap, type)
+#define VA_PTR(type) pval = (char *)va_arg(ap, type)
 #define VA_STR(type) sval = va_arg(ap, type)
 
+#define MyBuffer _nc_globals.tracearg_buf
+#define MyLength _nc_globals.tracearg_used
+
 /*
  * Returns a string that represents the parameter list of a printf-style call.
  */
@@ -56,8 +59,6 @@ NCURSES_EXPORT(char *)
 _nc_varargs(const char *fmt, va_list ap)
 {
     static char dummy[] = "";
-    static char *result_buf;
-    static size_t result_len;
 
     char buffer[BUFSIZ];
     const char *param;
@@ -65,11 +66,11 @@ _nc_varargs(const char *fmt, va_list ap)
 
     if (fmt == 0 || *fmt == '\0')
        return dummy;
-    if (result_len == 0)
-       result_buf = typeMalloc(char, result_len = BUFSIZ);
-    if (result_buf == 0)
+    if (MyLength == 0)
+       MyBuffer = typeMalloc(char, MyLength = BUFSIZ);
+    if (MyBuffer == 0)
        return dummy;
-    *result_buf = '\0';
+    *MyBuffer = '\0';
 
     while (*fmt != '\0') {
        if (*fmt == '%') {
@@ -148,24 +149,32 @@ _nc_varargs(const char *fmt, va_list ap)
                        param = buffer;
                        switch (used) {
                        case atInteger:
-                           sprintf(buffer, "%d", ival);
+                           _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
+                                       "%d", ival);
                            break;
                        case atFloat:
-                           sprintf(buffer, "%f", fval);
+                           _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
+                                       "%f", fval);
                            break;
                        case atPoint:
-                           sprintf(buffer, "%p", pval);
+                           _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
+                                       "%p", pval);
                            break;
                        case atString:
                            param = _nc_visbuf2(1, sval);
                            break;
+                       case atUnknown:
                        default:
-                           strcpy(buffer, "?");
+                           _nc_STRCPY(buffer, "?", sizeof(buffer));
                            break;
                        }
-                       result_len += strlen(param) + 2;
-                       result_buf = typeRealloc(char, result_len, result_buf);
-                       sprintf(result_buf + strlen(result_buf), ", %s", param);
+                       MyLength += strlen(param) + 2;
+                       MyBuffer = typeRealloc(char, MyLength, MyBuffer);
+                       if (MyBuffer != 0) {
+                           _nc_SPRINTF(MyBuffer + strlen(MyBuffer),
+                                       _nc_SLIMIT(MyLength - strlen(MyBuffer))
+                                       ", %s", param);
+                       }
                    }
                }
                used = atUnknown;
@@ -175,8 +184,8 @@ _nc_varargs(const char *fmt, va_list ap)
        }
     }
 
-    return (result_buf);
+    return (MyBuffer ? MyBuffer : dummy);
 }
 #else
-empty_module(_nc_varargs)
+EMPTY_MODULE(_nc_varargs)
 #endif