]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/base/safe_sprintf.c
ncurses 5.0
[ncurses.git] / ncurses / base / safe_sprintf.c
similarity index 92%
rename from ncurses/safe_sprintf.c
rename to ncurses/base/safe_sprintf.c
index 6dbc8d98b7f6b0e559837f6186e225e1945a6341..e4d525272627b7c153c99d4bdb036b21e6965254 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
 /****************************************************************************
- * Copyright (c) 1998 Free Software Foundation, Inc.                        *
+ * Copyright (c) 1998,1999 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            *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -33,7 +33,7 @@
 #include <curses.priv.h>
 #include <ctype.h>
 
 #include <curses.priv.h>
 #include <ctype.h>
 
-MODULE_ID("$Id: safe_sprintf.c,v 1.5 1998/02/11 12:13:57 tom Exp $")
+MODULE_ID("$Id: safe_sprintf.c,v 1.11 1999/09/11 18:03:27 tom Exp $")
 
 #if USE_SAFE_SPRINTF
 
 
 #if USE_SAFE_SPRINTF
 
@@ -57,17 +57,18 @@ _nc_printf_length(const char *fmt, va_list ap)
 
        if (fmt == 0 || *fmt == '\0')
                return -1;
 
        if (fmt == 0 || *fmt == '\0')
                return -1;
-       if ((format = malloc(strlen(fmt)+1)) == 0)
+       if ((format = typeMalloc(char, strlen(fmt)+1)) == 0)
                return -1;
                return -1;
-       if ((buffer = malloc(length)) == 0) {
+       if ((buffer = typeMalloc(char, length)) == 0) {
                free(format);
                return -1;
        }
 
        while (*fmt != '\0') {
                if (*fmt == '%') {
                free(format);
                return -1;
        }
 
        while (*fmt != '\0') {
                if (*fmt == '%') {
+                       static char dummy[] = "";
                        PRINTF state = Flags;
                        PRINTF state = Flags;
-                       char *pval   = "";
+                       char *pval   = dummy;   /* avoid const-cast */
                        double fval  = 0.0;
                        int done     = FALSE;
                        int ival     = 0;
                        double fval  = 0.0;
                        int done     = FALSE;
                        int ival     = 0;
@@ -111,7 +112,6 @@ _nc_printf_length(const char *fmt, va_list ap)
                                        case 'Z': /* FALLTHRU */
                                        case 'h': /* FALLTHRU */
                                        case 'l': /* FALLTHRU */
                                        case 'Z': /* FALLTHRU */
                                        case 'h': /* FALLTHRU */
                                        case 'l': /* FALLTHRU */
-                                       case 'L': /* FALLTHRU */
                                                done = FALSE;
                                                type = *fmt;
                                                break;
                                                done = FALSE;
                                                type = *fmt;
                                                break;
@@ -133,10 +133,7 @@ _nc_printf_length(const char *fmt, va_list ap)
                                        case 'E': /* FALLTHRU */
                                        case 'g': /* FALLTHRU */
                                        case 'G': /* FALLTHRU */
                                        case 'E': /* FALLTHRU */
                                        case 'g': /* FALLTHRU */
                                        case 'G': /* FALLTHRU */
-                                               if (type == 'L')
-                                                       VA_FLOAT(long double);
-                                               else
-                                                       VA_FLOAT(double);
+                                               VA_FLOAT(double);
                                                used = 'f';
                                                break;
                                        case 'c':
                                                used = 'f';
                                                break;
                                        case 'c':
@@ -149,7 +146,7 @@ _nc_printf_length(const char *fmt, va_list ap)
                                                        prec = strlen(pval);
                                                if (prec > (int)length) {
                                                        length = length + prec;
                                                        prec = strlen(pval);
                                                if (prec > (int)length) {
                                                        length = length + prec;
-                                                       buffer = realloc(buffer, length);
+                                                       buffer = typeRealloc(char, length, buffer);
                                                        if (buffer == 0) {
                                                                free(format);
                                                                return -1;
                                                        if (buffer == 0) {
                                                                free(format);
                                                                return -1;
@@ -211,7 +208,8 @@ _nc_printf_string(const char *fmt, va_list ap)
        int len = _nc_printf_length(fmt, ap);
 
        if (len > 0) {
        int len = _nc_printf_length(fmt, ap);
 
        if (len > 0) {
-               buf = malloc(len+1);
+               if ((buf = typeMalloc(char, len+1)) == 0)
+                       return(0);
                vsprintf(buf, fmt, ap);
        }
 #else
                vsprintf(buf, fmt, ap);
        }
 #else
@@ -223,10 +221,10 @@ _nc_printf_string(const char *fmt, va_list ap)
                if (screen_lines   > rows) rows = screen_lines;
                if (screen_columns > cols) cols = screen_columns;
                len = (rows * (cols + 1)) + 1;
                if (screen_lines   > rows) rows = screen_lines;
                if (screen_columns > cols) cols = screen_columns;
                len = (rows * (cols + 1)) + 1;
-               if (buf == 0)
-                       buf = malloc(len);
-               else
-                       buf = realloc(buf, len);
+               buf = typeRealloc(char, len, buf);
+               if (buf == 0) {
+                       return(0);
+               }
        }
 
        if (buf != 0) {
        }
 
        if (buf != 0) {
@@ -235,7 +233,7 @@ _nc_printf_string(const char *fmt, va_list ap)
 # else
                vsprintf(buf, fmt, ap);         /* ANSI */
 # endif
 # else
                vsprintf(buf, fmt, ap);         /* ANSI */
 # endif
-#endif
        }
        }
+#endif
        return buf;
 }
        return buf;
 }