summaryrefslogtreecommitdiffstats
path: root/lib/mprintf.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/mprintf.c')
-rw-r--r--lib/mprintf.c644
1 files changed, 282 insertions, 362 deletions
diff --git a/lib/mprintf.c b/lib/mprintf.c
index 6103953..23070a7 100644
--- a/lib/mprintf.c
+++ b/lib/mprintf.c
@@ -1,17 +1,23 @@
-/****************************************************************************
+/***************************************************************************
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
*
- * $Id$
+ * Copyright (C) 1999 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
*
- *************************************************************************
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at http://curl.haxx.se/docs/copyright.html.
*
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
*
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
- * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
- * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
*
* Purpose:
* A merge of Bjorn Reese's format() function and Daniel's dsprintf()
@@ -29,14 +35,7 @@
* page at http://daniel.haxx.se/trio/
*/
-
-#include "setup.h"
-#include <sys/types.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
-#include <ctype.h>
-#include <string.h>
+#include "curl_setup.h"
#if defined(DJGPP) && (DJGPP_MINOR < 4)
#undef _MPRINTF_REPLACE /* don't use x_was_used() here */
@@ -44,42 +43,64 @@
#include <curl/mprintf.h>
+#include "curl_memory.h"
+/* The last #include file should be: */
+#include "memdebug.h"
+
#ifndef SIZEOF_LONG_DOUBLE
#define SIZEOF_LONG_DOUBLE 0
#endif
+/*
+ * If SIZEOF_SIZE_T has not been defined, default to the size of long.
+ */
+
#ifndef SIZEOF_SIZE_T
-/* default to 4 bytes for size_t unless defined in the config.h */
-#define SIZEOF_SIZE_T 4
+# define SIZEOF_SIZE_T CURL_SIZEOF_LONG
#endif
-#ifdef DPRINTF_DEBUG
-#define HAVE_LONGLONG
-#define LONG_LONG long long
-#define ENABLE_64BIT
+#ifdef HAVE_LONGLONG
+# define LONG_LONG_TYPE long long
+# define HAVE_LONG_LONG_TYPE
+#else
+# if defined(_MSC_VER) && (_MSC_VER >= 900) && (_INTEGRAL_MAX_BITS >= 64)
+# define LONG_LONG_TYPE __int64
+# define HAVE_LONG_LONG_TYPE
+# else
+# undef LONG_LONG_TYPE
+# undef HAVE_LONG_LONG_TYPE
+# endif
#endif
-#include "memory.h"
-/* The last #include file should be: */
-#include "memdebug.h"
+/*
+ * Non-ANSI integer extensions
+ */
-#define BUFFSIZE 256 /* buffer for long-to-str and float-to-str calcs */
-#define MAX_PARAMETERS 128 /* lame static limit */
+#if (defined(__BORLANDC__) && (__BORLANDC__ >= 0x520)) || \
+ (defined(__WATCOMC__) && defined(__386__)) || \
+ (defined(__POCC__) && defined(_MSC_VER)) || \
+ (defined(_WIN32_WCE)) || \
+ (defined(__MINGW32__)) || \
+ (defined(_MSC_VER) && (_MSC_VER >= 900) && (_INTEGRAL_MAX_BITS >= 64))
+# define MP_HAVE_INT_EXTENSIONS
+#endif
-#undef TRUE
-#undef FALSE
-#undef BOOL
-#ifdef __cplusplus
-# define TRUE true
-# define FALSE false
-# define BOOL bool
+/*
+ * Max integer data types that mprintf.c is capable
+ */
+
+#ifdef HAVE_LONG_LONG_TYPE
+# define mp_intmax_t LONG_LONG_TYPE
+# define mp_uintmax_t unsigned LONG_LONG_TYPE
#else
-# define TRUE ((char)(1 == 1))
-# define FALSE ((char)(0 == 1))
-# define BOOL char
+# define mp_intmax_t long
+# define mp_uintmax_t unsigned long
#endif
-#ifdef _AMIGASF
+#define BUFFSIZE 256 /* buffer for long-to-str and float-to-str calcs */
+#define MAX_PARAMETERS 128 /* lame static limit */
+
+#ifdef __AMIGA__
# undef FORMAT_INT
#endif
@@ -95,7 +116,7 @@ static const char upper_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
done++; \
else \
return done; /* return immediately on failure */ \
- } while(0)
+ } WHILE_FALSE
/* Data type to read from the arglist */
typedef enum {
@@ -111,7 +132,7 @@ typedef enum {
FORMAT_WIDTH /* For internal use */
} FormatType;
-/* convertion and display flags */
+/* conversion and display flags */
enum {
FLAGS_NEW = 0,
FLAGS_SPACE = 1<<0,
@@ -144,10 +165,10 @@ typedef struct {
union {
char *str;
void *ptr;
- long num;
-#ifdef ENABLE_64BIT
- LONG_LONG lnum;
-#endif
+ union {
+ mp_intmax_t as_signed;
+ mp_uintmax_t as_unsigned;
+ } num;
double dnum;
} data;
} va_stack_t;
@@ -162,12 +183,10 @@ struct asprintf {
char *buffer; /* allocated buffer */
size_t len; /* length of string */
size_t alloc; /* length of alloc */
- bool fail; /* TRUE if an alloc has failed and thus the output is not
- the complete data */
+ int fail; /* (!= 0) if an alloc has failed and thus
+ the output is not the complete data */
};
-int curl_msprintf(char *buffer, const char *format, ...);
-
static long dprintf_DollarString(char *input, char **end)
{
int number=0;
@@ -183,112 +202,30 @@ static long dprintf_DollarString(char *input, char **end)
return 0;
}
-static BOOL dprintf_IsQualifierNoDollar(char c)
+static bool dprintf_IsQualifierNoDollar(const char *fmt)
{
- switch (c) {
+#if defined(MP_HAVE_INT_EXTENSIONS)
+ if(!strncmp(fmt, "I32", 3) || !strncmp(fmt, "I64", 3)) {
+ return TRUE;
+ }
+#endif
+
+ switch(*fmt) {
case '-': case '+': case ' ': case '#': case '.':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case 'h': case 'l': case 'L': case 'z': case 'q':
case '*': case 'O':
+#if defined(MP_HAVE_INT_EXTENSIONS)
+ case 'I':
+#endif
return TRUE;
+
default:
return FALSE;
}
}
-#ifdef DPRINTF_DEBUG2
-int dprintf_Pass1Report(va_stack_t *vto, int max)
-{
- int i;
- char buffer[128];
- int bit;
- int flags;
-
- for(i=0; i<max; i++) {
- char *type;
- switch(vto[i].type) {
- case FORMAT_UNKNOWN:
- type = "unknown";
- break;
- case FORMAT_STRING:
- type ="string";
- break;
- case FORMAT_PTR:
- type ="pointer";
- break;
- case FORMAT_INT:
- type = "int";
- break;
- case FORMAT_LONG:
- type = "long";
- break;
- case FORMAT_LONGLONG:
- type = "long long";
- break;
- case FORMAT_DOUBLE:
- type = "double";
- break;
- case FORMAT_LONGDOUBLE:
- type = "long double";
- break;
- }
-
-
- buffer[0]=0;
-
- for(bit=0; bit<31; bit++) {
- flags = vto[i].flags & (1<<bit);
-
- if(flags & FLAGS_SPACE)
- strcat(buffer, "space ");
- else if(flags & FLAGS_SHOWSIGN)
- strcat(buffer, "plus ");
- else if(flags & FLAGS_LEFT)
- strcat(buffer, "left ");
- else if(flags & FLAGS_ALT)
- strcat(buffer, "alt ");
- else if(flags & FLAGS_SHORT)
- strcat(buffer, "short ");
- else if(flags & FLAGS_LONG)
- strcat(buffer, "long ");
- else if(flags & FLAGS_LONGLONG)
- strcat(buffer, "longlong ");
- else if(flags & FLAGS_LONGDOUBLE)
- strcat(buffer, "longdouble ");
- else if(flags & FLAGS_PAD_NIL)
- strcat(buffer, "padnil ");
- else if(flags & FLAGS_UNSIGNED)
- strcat(buffer, "unsigned ");
- else if(flags & FLAGS_OCTAL)
- strcat(buffer, "octal ");
- else if(flags & FLAGS_HEX)
- strcat(buffer, "hex ");
- else if(flags & FLAGS_UPPER)
- strcat(buffer, "upper ");
- else if(flags & FLAGS_WIDTH)
- strcat(buffer, "width ");
- else if(flags & FLAGS_WIDTHPARAM)
- strcat(buffer, "widthparam ");
- else if(flags & FLAGS_PREC)
- strcat(buffer, "precision ");
- else if(flags & FLAGS_PRECPARAM)
- strcat(buffer, "precparam ");
- else if(flags & FLAGS_CHAR)
- strcat(buffer, "char ");
- else if(flags & FLAGS_FLOATE)
- strcat(buffer, "floate ");
- else if(flags & FLAGS_FLOATG)
- strcat(buffer, "floatg ");
- }
- printf("REPORT: %d. %s [%s]\n", i, type, buffer);
-
- }
-
-
-}
-#endif
-
/******************************************************************
*
* Pass 1:
@@ -297,10 +234,10 @@ int dprintf_Pass1Report(va_stack_t *vto, int max)
*
******************************************************************/
-static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos,
+static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos,
va_list arglist)
{
- char *fmt = format;
+ char *fmt = (char *)format;
int param_num = 0;
long this_param;
long width;
@@ -309,9 +246,9 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos,
long max_param=0;
long i;
- while (*fmt) {
- if (*fmt++ == '%') {
- if (*fmt == '%') {
+ while(*fmt) {
+ if(*fmt++ == '%') {
+ if(*fmt == '%') {
fmt++;
continue; /* while */
}
@@ -323,11 +260,11 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos,
param_num++;
this_param = dprintf_DollarString(fmt, &fmt);
- if (0 == this_param)
+ if(0 == this_param)
/* we got no positional, get the next counter */
this_param = param_num;
- if (this_param > max_param)
+ if(this_param > max_param)
max_param = this_param;
/*
@@ -341,8 +278,20 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos,
/* Handle the flags */
- while (dprintf_IsQualifierNoDollar(*fmt)) {
- switch (*fmt++) {
+ while(dprintf_IsQualifierNoDollar(fmt)) {
+#if defined(MP_HAVE_INT_EXTENSIONS)
+ if(!strncmp(fmt, "I32", 3)) {
+ flags |= FLAGS_LONG;
+ fmt += 3;
+ }
+ else if(!strncmp(fmt, "I64", 3)) {
+ flags |= FLAGS_LONGLONG;
+ fmt += 3;
+ }
+ else
+#endif
+
+ switch(*fmt++) {
case ' ':
flags |= FLAGS_SPACE;
break;
@@ -358,7 +307,7 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos,
break;
case '.':
flags |= FLAGS_PREC;
- if ('*' == *fmt) {
+ if('*' == *fmt) {
/* The precision is picked from a specified parameter */
flags |= FLAGS_PRECPARAM;
@@ -366,12 +315,12 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos,
param_num++;
i = dprintf_DollarString(fmt, &fmt);
- if (i)
+ if(i)
precision = i;
else
precision = param_num;
- if (precision > max_param)
+ if(precision > max_param)
max_param = precision;
}
else {
@@ -382,8 +331,17 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos,
case 'h':
flags |= FLAGS_SHORT;
break;
+#if defined(MP_HAVE_INT_EXTENSIONS)
+ case 'I':
+#if (CURL_SIZEOF_CURL_OFF_T > CURL_SIZEOF_LONG)
+ flags |= FLAGS_LONGLONG;
+#else
+ flags |= FLAGS_LONG;
+#endif
+ break;
+#endif
case 'l':
- if (flags & FLAGS_LONG)
+ if(flags & FLAGS_LONG)
flags |= FLAGS_LONGLONG;
else
flags |= FLAGS_LONG;
@@ -397,21 +355,21 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos,
case 'z':
/* the code below generates a warning if -Wunreachable-code is
used */
-#if SIZEOF_SIZE_T>4
+#if (SIZEOF_SIZE_T > CURL_SIZEOF_LONG)
flags |= FLAGS_LONGLONG;
#else
flags |= FLAGS_LONG;
#endif
break;
case 'O':
-#if SIZEOF_CURL_OFF_T > 4
+#if (CURL_SIZEOF_CURL_OFF_T > CURL_SIZEOF_LONG)
flags |= FLAGS_LONGLONG;
#else
flags |= FLAGS_LONG;
#endif
break;
case '0':
- if (!(flags & FLAGS_LEFT))
+ if(!(flags & FLAGS_LEFT))
flags |= FLAGS_PAD_NIL;
/* FALLTHROUGH */
case '1': case '2': case '3': case '4':
@@ -466,11 +424,11 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos,
break;
case 'x':
vto[i].type = FORMAT_INT;
- flags |= FLAGS_HEX;
+ flags |= FLAGS_HEX|FLAGS_UNSIGNED;
break;
case 'X':
vto[i].type = FORMAT_INT;
- flags |= FLAGS_HEX|FLAGS_UPPER;
+ flags |= FLAGS_HEX|FLAGS_UPPER|FLAGS_UNSIGNED;
break;
case 'c':
vto[i].type = FORMAT_INT;
@@ -504,7 +462,7 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos,
vto[i].width = width;
vto[i].precision = precision;
- if (flags & FLAGS_WIDTHPARAM) {
+ if(flags & FLAGS_WIDTHPARAM) {
/* we have the width specified from a parameter, so we make that
parameter's info setup properly */
vto[i].width = width - 1;
@@ -514,7 +472,7 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos,
vto[i].precision = vto[i].width = 0; /* can't use width or precision
of width! */
}
- if (flags & FLAGS_PRECPARAM) {
+ if(flags & FLAGS_PRECPARAM) {
/* we have the precision specified from a parameter, so we make that
parameter's info setup properly */
vto[i].precision = precision - 1;
@@ -528,58 +486,66 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos,
}
}
-#ifdef DPRINTF_DEBUG2
- dprintf_Pass1Report(vto, max_param);
-#endif
-
/* Read the arg list parameters into our data list */
- for (i=0; i<max_param; i++) {
- if ((i + 1 < max_param) && (vto[i + 1].type == FORMAT_WIDTH))
- {
- /* Width/precision arguments must be read before the main argument
- * they are attached to
- */
- vto[i + 1].data.num = va_arg(arglist, int);
- }
+ for(i=0; i<max_param; i++) {
+ if((i + 1 < max_param) && (vto[i + 1].type == FORMAT_WIDTH)) {
+ /* Width/precision arguments must be read before the main argument
+ * they are attached to
+ */
+ vto[i + 1].data.num.as_signed = (mp_intmax_t)va_arg(arglist, int);
+ }
- switch (vto[i].type)
- {
- case FORMAT_STRING:
- vto[i].data.str = va_arg(arglist, char *);
- break;
+ switch (vto[i].type) {
+ case FORMAT_STRING:
+ vto[i].data.str = va_arg(arglist, char *);
+ break;
- case FORMAT_INTPTR:
- case FORMAT_UNKNOWN:
- case FORMAT_PTR:
- vto[i].data.ptr = va_arg(arglist, void *);
- break;
+ case FORMAT_INTPTR:
+ case FORMAT_UNKNOWN:
+ case FORMAT_PTR:
+ vto[i].data.ptr = va_arg(arglist, void *);
+ break;
- case FORMAT_INT:
-#ifdef ENABLE_64BIT
- if(vto[i].flags & FLAGS_LONGLONG)
- vto[i].data.lnum = va_arg(arglist, LONG_LONG);
- else
+ case FORMAT_INT:
+#ifdef HAVE_LONG_LONG_TYPE
+ if((vto[i].flags & FLAGS_LONGLONG) && (vto[i].flags & FLAGS_UNSIGNED))
+ vto[i].data.num.as_unsigned =
+ (mp_uintmax_t)va_arg(arglist, mp_uintmax_t);
+ else if(vto[i].flags & FLAGS_LONGLONG)
+ vto[i].data.num.as_signed =
+ (mp_intmax_t)va_arg(arglist, mp_intmax_t);
+ else
#endif
- if(vto[i].flags & FLAGS_LONG)
- vto[i].data.num = va_arg(arglist, long);
+ {
+ if((vto[i].flags & FLAGS_LONG) && (vto[i].flags & FLAGS_UNSIGNED))
+ vto[i].data.num.as_unsigned =
+ (mp_uintmax_t)va_arg(arglist, unsigned long);
+ else if(vto[i].flags & FLAGS_LONG)
+ vto[i].data.num.as_signed =
+ (mp_intmax_t)va_arg(arglist, long);
+ else if(vto[i].flags & FLAGS_UNSIGNED)
+ vto[i].data.num.as_unsigned =
+ (mp_uintmax_t)va_arg(arglist, unsigned int);
else
- vto[i].data.num = va_arg(arglist, int);
- break;
+ vto[i].data.num.as_signed =
+ (mp_intmax_t)va_arg(arglist, int);
+ }
+ break;
- case FORMAT_DOUBLE:
- vto[i].data.dnum = va_arg(arglist, double);
- break;
+ case FORMAT_DOUBLE:
+ vto[i].data.dnum = va_arg(arglist, double);
+ break;
- case FORMAT_WIDTH:
- /* Argument has been read. Silently convert it into an integer
- * for later use
- */
- vto[i].type = FORMAT_INT;
- break;
+ case FORMAT_WIDTH:
+ /* Argument has been read. Silently convert it into an integer
+ * for later use
+ */
+ vto[i].type = FORMAT_INT;
+ break;
- default:
- break;
- }
+ default:
+ break;
+ }
}
return max_param;
@@ -615,15 +581,15 @@ static int dprintf_formatf(
va_stack_t *p;
/* Do the actual %-code parsing */
- dprintf_Pass1((char *)format, vto, endpos, ap_save);
+ dprintf_Pass1(format, vto, endpos, ap_save);
end = &endpos[0]; /* the initial end-position from the list dprintf_Pass1()
created for us */
f = (char *)format;
- while (*f != '\0') {
+ while(*f != '\0') {
/* Format spec modifiers. */
- char alt;
+ int is_alt;
/* Width of a field. */
long width;
@@ -632,20 +598,18 @@ static int dprintf_formatf(
long prec;
/* Decimal integer is negative. */
- char is_neg;
+ int is_neg;
/* Base of a number to be written. */
long base;
/* Integral values to be written. */
-#ifdef ENABLE_64BIT
- unsigned LONG_LONG num;
-#else
- unsigned long num;
-#endif
- long signed_num;
+ mp_uintmax_t num;
+
+ /* Used to convert negative in positive. */
+ mp_intmax_t signed_num;
- if (*f != '%') {
+ if(*f != '%') {
/* This isn't a format spec, so write everything out until the next one
OR end of string is reached. */
do {
@@ -660,13 +624,13 @@ static int dprintf_formatf(
'%' as a conversion specifier, it says "The complete format
specification shall be `%%'," so we can avoid all the width
and precision processing. */
- if (*f == '%') {
+ if(*f == '%') {
++f;
OUTCHAR('%');
continue;
}
- /* If this is a positional parameter, the position must follow imediately
+ /* If this is a positional parameter, the position must follow immediately
after the %, thus create a %<num>$ sequence */
param=dprintf_DollarString(f, &f);
@@ -682,68 +646,67 @@ static int dprintf_formatf(
/* pick up the specified width */
if(p->flags & FLAGS_WIDTHPARAM)
- width = vto[p->width].data.num;
+ width = (long)vto[p->width].data.num.as_signed;
else
width = p->width;
/* pick up the specified precision */
- if(p->flags & FLAGS_PRECPARAM)
- prec = vto[p->precision].data.num;
+ if(p->flags & FLAGS_PRECPARAM) {
+ prec = (long)vto[p->precision].data.num.as_signed;
+ param_num++; /* since the precision is extraced from a parameter, we
+ must skip that to get to the next one properly */
+ }
else if(p->flags & FLAGS_PREC)
prec = p->precision;
else
prec = -1;
- alt = (p->flags & FLAGS_ALT)?TRUE:FALSE;
+ is_alt = (p->flags & FLAGS_ALT) ? 1 : 0;
switch (p->type) {
case FORMAT_INT:
- num = p->data.num;
+ num = p->data.num.as_unsigned;
if(p->flags & FLAGS_CHAR) {
/* Character. */
- if (!(p->flags & FLAGS_LEFT))
- while (--width > 0)
+ if(!(p->flags & FLAGS_LEFT))
+ while(--width > 0)
OUTCHAR(' ');
OUTCHAR((char) num);
- if (p->flags & FLAGS_LEFT)
- while (--width > 0)
+ if(p->flags & FLAGS_LEFT)
+ while(--width > 0)
OUTCHAR(' ');
break;
}
- if(p->flags & FLAGS_UNSIGNED) {
- /* Decimal unsigned integer. */
- base = 10;
- goto unsigned_number;
- }
if(p->flags & FLAGS_OCTAL) {
/* Octal unsigned integer. */
base = 8;
goto unsigned_number;
}
- if(p->flags & FLAGS_HEX) {
+ else if(p->flags & FLAGS_HEX) {
/* Hexadecimal unsigned integer. */
digits = (p->flags & FLAGS_UPPER)? upper_digits : lower_digits;
base = 16;
goto unsigned_number;
}
+ else if(p->flags & FLAGS_UNSIGNED) {
+ /* Decimal unsigned integer. */
+ base = 10;
+ goto unsigned_number;
+ }
/* Decimal integer. */
base = 10;
-#ifdef ENABLE_64BIT
- if(p->flags & FLAGS_LONGLONG) {
- /* long long */
- is_neg = p->data.lnum < 0;
- num = is_neg ? (- p->data.lnum) : p->data.lnum;
- }
- else
-#endif
- {
- signed_num = (long) num;
- is_neg = signed_num < 0;
- num = is_neg ? (- signed_num) : signed_num;
+ is_neg = (p->data.num.as_signed < (mp_intmax_t)0) ? 1 : 0;
+ if(is_neg) {
+ /* signed_num might fail to hold absolute negative minimum by 1 */
+ signed_num = p->data.num.as_signed + (mp_intmax_t)1;
+ signed_num = -signed_num;
+ num = (mp_uintmax_t)signed_num;
+ num += (mp_uintmax_t)1;
}
+
goto number;
unsigned_number:
@@ -757,47 +720,47 @@ static int dprintf_formatf(
char *w;
/* Supply a default precision if none was given. */
- if (prec == -1)
+ if(prec == -1)
prec = 1;
/* Put the number in WORK. */
w = workend;
- while (num > 0) {
+ while(num > 0) {
*w-- = digits[num % base];
num /= base;
}
width -= (long)(workend - w);
prec -= (long)(workend - w);
- if (alt && base == 8 && prec <= 0) {
+ if(is_alt && base == 8 && prec <= 0) {
*w-- = '0';
--width;
}
- if (prec > 0) {
+ if(prec > 0) {
width -= prec;
- while (prec-- > 0)
+ while(prec-- > 0)
*w-- = '0';
}
- if (alt && base == 16)
+ if(is_alt && base == 16)
width -= 2;
- if (is_neg || (p->flags & FLAGS_SHOWSIGN) || (p->flags & FLAGS_SPACE))
+ if(is_neg || (p->flags & FLAGS_SHOWSIGN) || (p->flags & FLAGS_SPACE))
--width;
- if (!(p->flags & FLAGS_LEFT) && !(p->flags & FLAGS_PAD_NIL))
- while (width-- > 0)
+ if(!(p->flags & FLAGS_LEFT) && !(p->flags & FLAGS_PAD_NIL))
+ while(width-- > 0)
OUTCHAR(' ');
- if (is_neg)
+ if(is_neg)
OUTCHAR('-');
- else if (p->flags & FLAGS_SHOWSIGN)
+ else if(p->flags & FLAGS_SHOWSIGN)
OUTCHAR('+');
- else if (p->flags & FLAGS_SPACE)
+ else if(p->flags & FLAGS_SPACE)
OUTCHAR(' ');
- if (alt && base == 16) {
+ if(is_alt && base == 16) {
OUTCHAR('0');
if(p->flags & FLAGS_UPPER)
OUTCHAR('X');
@@ -805,17 +768,17 @@ static int dprintf_formatf(
OUTCHAR('x');
}
- if (!(p->flags & FLAGS_LEFT) && (p->flags & FLAGS_PAD_NIL))
- while (width-- > 0)
+ if(!(p->flags & FLAGS_LEFT) && (p->flags & FLAGS_PAD_NIL))
+ while(width-- > 0)
OUTCHAR('0');
/* Write the number. */
- while (++w <= workend) {
+ while(++w <= workend) {
OUTCHAR(*w);
}
- if (p->flags & FLAGS_LEFT)
- while (width-- > 0)
+ if(p->flags & FLAGS_LEFT)
+ while(width-- > 0)
OUTCHAR(' ');
}
break;
@@ -828,9 +791,9 @@ static int dprintf_formatf(
size_t len;
str = (char *) p->data.str;
- if ( str == NULL) {
+ if(str == NULL) {
/* Write null[] if there's space. */
- if (prec == -1 || prec >= (long) sizeof(null) - 1) {
+ if(prec == -1 || prec >= (long) sizeof(null) - 1) {
str = null;
len = sizeof(null) - 1;
/* Disable quotes around (nil) */
@@ -841,27 +804,27 @@ static int dprintf_formatf(
len = 0;
}
}
+ else if(prec != -1)
+ len = (size_t)prec;
else
len = strlen(str);
- if (prec != -1 && (size_t) prec < len)
- len = prec;
width -= (long)len;
- if (p->flags & FLAGS_ALT)
+ if(p->flags & FLAGS_ALT)
OUTCHAR('"');
- if (!(p->flags&FLAGS_LEFT))
- while (width-- > 0)
+ if(!(p->flags&FLAGS_LEFT))
+ while(width-- > 0)
OUTCHAR(' ');
- while (len-- > 0)
+ while((len-- > 0) && *str)
OUTCHAR(*str++);
- if (p->flags&FLAGS_LEFT)
- while (width-- > 0)
+ if(p->flags&FLAGS_LEFT)
+ while(width-- > 0)
OUTCHAR(' ');
- if (p->flags & FLAGS_ALT)
+ if(p->flags & FLAGS_ALT)
OUTCHAR('"');
}
break;
@@ -871,11 +834,11 @@ static int dprintf_formatf(
{
void *ptr;
ptr = (void *) p->data.ptr;
- if (ptr != NULL) {
+ if(ptr != NULL) {
/* If the pointer is not NULL, write it as a %#x spec. */
base = 16;
digits = (p->flags & FLAGS_UPPER)? upper_digits : lower_digits;
- alt = 1;
+ is_alt = 1;
num = (size_t) ptr;
is_neg = 0;
goto number;
@@ -885,14 +848,14 @@ static int dprintf_formatf(
static const char strnil[] = "(nil)";
const char *point;
- width -= sizeof(strnil) - 1;
- if (p->flags & FLAGS_LEFT)
- while (width-- > 0)
+ width -= (long)(sizeof(strnil) - 1);
+ if(p->flags & FLAGS_LEFT)
+ while(width-- > 0)
OUTCHAR(' ');
- for (point = strnil; *point != '\0'; ++point)
+ for(point = strnil; *point != '\0'; ++point)
OUTCHAR(*point);
- if (! (p->flags & FLAGS_LEFT))
- while (width-- > 0)
+ if(! (p->flags & FLAGS_LEFT))
+ while(width-- > 0)
OUTCHAR(' ');
}
}
@@ -901,32 +864,32 @@ static int dprintf_formatf(
case FORMAT_DOUBLE:
{
char formatbuf[32]="%";
- char *fptr;
+ char *fptr = &formatbuf[1];
size_t left = sizeof(formatbuf)-strlen(formatbuf);
int len;
width = -1;
- if (p->flags & FLAGS_WIDTH)
+ if(p->flags & FLAGS_WIDTH)
width = p->width;
- else if (p->flags & FLAGS_WIDTHPARAM)
- width = vto[p->width].data.num;
+ else if(p->flags & FLAGS_WIDTHPARAM)
+ width = (long)vto[p->width].data.num.as_signed;
prec = -1;
- if (p->flags & FLAGS_PREC)
+ if(p->flags & FLAGS_PREC)
prec = p->precision;
- else if (p->flags & FLAGS_PRECPARAM)
- prec = vto[p->precision].data.num;
+ else if(p->flags & FLAGS_PRECPARAM)
+ prec = (long)vto[p->precision].data.num.as_signed;
- if (p->flags & FLAGS_LEFT)
- strcat(formatbuf, "-");
- if (p->flags & FLAGS_SHOWSIGN)
- strcat(formatbuf, "+");
- if (p->flags & FLAGS_SPACE)
- strcat(formatbuf, " ");
- if (p->flags & FLAGS_ALT)
- strcat(formatbuf, "#");
+ if(p->flags & FLAGS_LEFT)
+ *fptr++ = '-';
+ if(p->flags & FLAGS_SHOWSIGN)
+ *fptr++ = '+';
+ if(p->flags & FLAGS_SPACE)
+ *fptr++ = ' ';
+ if(p->flags & FLAGS_ALT)
+ *fptr++ = '#';
- fptr=&formatbuf[strlen(formatbuf)];
+ *fptr = 0;
if(width >= 0) {
/* RECURSIVE USAGE */
@@ -938,22 +901,21 @@ static int dprintf_formatf(
/* RECURSIVE USAGE */
len = curl_msnprintf(fptr, left, ".%ld", prec);
fptr += len;
- left -= len;
}
- if (p->flags & FLAGS_LONG)
+ if(p->flags & FLAGS_LONG)
*fptr++ = 'l';
- if (p->flags & FLAGS_FLOATE)
- *fptr++ = p->flags&FLAGS_UPPER ? 'E':'e';
- else if (p->flags & FLAGS_FLOATG)
- *fptr++ = p->flags & FLAGS_UPPER ? 'G' : 'g';
+ if(p->flags & FLAGS_FLOATE)
+ *fptr++ = (char)((p->flags & FLAGS_UPPER) ? 'E':'e');
+ else if(p->flags & FLAGS_FLOATG)
+ *fptr++ = (char)((p->flags & FLAGS_UPPER) ? 'G' : 'g');
else
*fptr++ = 'f';
*fptr = 0; /* and a final zero termination */
- /* NOTE NOTE NOTE!! Not all sprintf() implementations returns number
- of output characters */
+ /* NOTE NOTE NOTE!! Not all sprintf implementations return number of
+ output characters */
(sprintf)(work, formatbuf, p->data.dnum);
for(fptr=work; *fptr; fptr++)
@@ -963,14 +925,14 @@ static int dprintf_formatf(
case FORMAT_INTPTR:
/* Answer the count of characters written. */
-#ifdef ENABLE_64BIT
- if (p->flags & FLAGS_LONGLONG)
- *(LONG_LONG *) p->data.ptr = (LONG_LONG)done;
+#ifdef HAVE_LONG_LONG_TYPE
+ if(p->flags & FLAGS_LONGLONG)
+ *(LONG_LONG_TYPE *) p->data.ptr = (LONG_LONG_TYPE)done;
else
#endif
- if (p->flags & FLAGS_LONG)
+ if(p->flags & FLAGS_LONG)
*(long *) p->data.ptr = (long)done;
- else if (!(p->flags & FLAGS_SHORT))
+ else if(!(p->flags & FLAGS_SHORT))
*(int *) p->data.ptr = (int)done;
else
*(short *) p->data.ptr = (short)done;
@@ -1040,9 +1002,9 @@ static int alloc_addbyter(int output, FILE *data)
unsigned char outc = (unsigned char)output;
if(!infop->buffer) {
- infop->buffer=(char *)malloc(32);
+ infop->buffer = malloc(32);
if(!infop->buffer) {
- infop->fail = TRUE;
+ infop->fail = 1;
return -1; /* fail */
}
infop->alloc = 32;
@@ -1051,11 +1013,11 @@ static int alloc_addbyter(int output, FILE *data)
else if(infop->len+1 >= infop->alloc) {
char *newptr;
- newptr = (char *)realloc(infop->buffer, infop->alloc*2);
+ newptr = realloc(infop->buffer, infop->alloc*2);
if(!newptr) {
- infop->fail = TRUE;
- return -1;
+ infop->fail = 1;
+ return -1; /* fail */
}
infop->buffer = newptr;
infop->alloc *= 2;
@@ -1077,7 +1039,7 @@ char *curl_maprintf(const char *format, ...)
info.buffer = NULL;
info.len = 0;
info.alloc = 0;
- info.fail = FALSE;
+ info.fail = 0;
va_start(ap_save, format);
retcode = dprintf_formatf(&info, alloc_addbyter, format, ap_save);
@@ -1103,7 +1065,7 @@ char *curl_mvaprintf(const char *format, va_list ap_save)
info.buffer = NULL;
info.len = 0;
info.alloc = 0;
- info.fail = FALSE;
+ info.fail = 0;
retcode = dprintf_formatf(&info, alloc_addbyter, format, ap_save);
if((-1 == retcode) || info.fail) {
@@ -1178,45 +1140,3 @@ int curl_mvfprintf(FILE *whereto, const char *format, va_list ap_save)
{
return dprintf_formatf(whereto, fputc, format, ap_save);
}
-
-#ifdef DPRINTF_DEBUG
-int main()
-{
- char buffer[129];
- char *ptr;
-#ifdef ENABLE_64BIT
- long long one=99;
- long long two=100;
- long long test = 0x1000000000LL;
- curl_mprintf("%lld %lld %lld\n", one, two, test);
-#endif
-
- curl_mprintf("%3d %5d\n", 10, 1998);
-
- ptr=curl_maprintf("test this then baby %s%s%s%s%s%s %d %d %d loser baby get a hit in yer face now!", "", "pretty long string pretty long string pretty long string pretty long string pretty long string", "/", "/", "/", "pretty long string", 1998, 1999, 2001);
-
- puts(ptr);
-
- memset(ptr, 55, strlen(ptr)+1);
-
- free(ptr);
-
-#if 1
- curl_mprintf(buffer, "%s %s %d", "daniel", "stenberg", 19988);
- puts(buffer);
-
- curl_mfprintf(stderr, "%s %#08x\n", "dummy", 65);
-
- printf("%s %#08x\n", "dummy", 65);
- {
- double tryout = 3.14156592;
- curl_mprintf(buffer, "%.2g %G %f %e %E", tryout, tryout, tryout, tryout, tryout);
- puts(buffer);
- printf("%.2g %G %f %e %E\n", tryout, tryout, tryout, tryout, tryout);
- }
-#endif
-
- return 0;
-}
-
-#endif