From ae1912cb0d494b48d514d937826c9fe83ec96c4d Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 29 Dec 1999 14:20:26 +0000 Subject: Initial revision --- lib/mprintf.c | 1253 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1253 insertions(+) create mode 100644 lib/mprintf.c (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c new file mode 100644 index 000000000..237a21a9d --- /dev/null +++ b/lib/mprintf.c @@ -0,0 +1,1253 @@ +/**************************************************************************** + * + * $Id$ + * + ************************************************************************* + * + * Purpose: + * A merge of Bjorn Reese's format() function and Daniel's dsprintf() + * 1.0. A full blooded printf() clone with full support for $ + * everywhere (parameters, widths and precisions) including variabled + * sized parameters (like doubles, long longs, long doubles and even + * void * in 64-bit architectures). + * + * Current restrictions: + * - Max 128 parameters + * - No 'long double' support. + * + ************************************************************************* + * + * + * 1998/01/10 (v2.8) + * Daniel + * - Updated version number. + * - Corrected a static non-zero prefixed width problem. + * + * 1998/11/17 - Daniel + * Added daprintf() and dvaprintf() for allocated printf() and vprintf(). + * They return an allocated buffer with the result inside. The result must + * be free()ed! + * + * 1998/08/23 - breese + * + * Converted all non-printable (and non-whitespace) characters into + * their decimal ASCII value preceeded by a '\' character + * (this only applies to snprintf family so far) + * + * Added %S (which is the same as %#s) + * + * 1998/05/05 (v2.7) + * + * Fixed precision and width qualifiers (%.*s) + * + * Added support for snprintf() + * + * Quoting (%#s) is disabled for the (nil) pointer + * + * 1997/06/09 (v2.6) + * + * %#s means that the string will be quoted with " + * (I was getting tired of writing \"%s\" all the time) + * + * [ERR] for strings changed to (nil) + * + * v2.5 + * - Added C++ support + * - Prepended all internal functions with dprintf_ + * - Defined the booleans + * + * v2.4 + * - Added dvsprintf(), dvfprintf() and dvprintf(). + * - Made the formatting function available with the name _formatf() to enable + * other *printf()-inspired functions. (I considered adding a dmsprintf() + * that works like sprintf() but allocates the destination string and + * possibly enlarges it itself, but things like that should be done with the + * new _formatf() instead.) + * + * v2.3 + * - Small modifications to make it compile nicely at both Daniel's and + * Bjorn's place. + * + * v2.2 + * - Made it work with text to the right of the last %! + * - Introduced dprintf(), dsprintf() and dfprintf(). + * - Float/double support enabled. This system is currently using the ordinary + * sprintf() function. NOTE that positional parameters, widths and precisions + * will still work like it should since the d-system takes care of that and + * passes that information re-formatted to the old sprintf(). + * + * v2.1 + * - Fixed space padding (i.e %d was extra padded previously) + * - long long output is supported + * - alternate output is done correct like in %#08x + * + ****************************************************************************/ + +static const char rcsid[] = "@(#)$Id$"; + +/* + * To test: + * + * Use WIDTH, PRECISION and NUMBERED ARGUMENT combined. + */ + +#include +#include +#include +#include +#include +#include + + +#define BUFFSIZE 256 /* buffer for long-to-str and float-to-str calcs */ +#define MAX_PARAMETERS 128 /* lame static limit */ + +#undef TRUE +#undef FALSE +#undef BOOL +#ifdef __cplusplus +# define TRUE true +# define FALSE false +# define BOOL bool +#else +# define TRUE ((char)(1 == 1)) +# define FALSE ((char)(0 == 1)) +# define BOOL char +#endif + + +/* Lower-case digits. */ +static const char lower_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; + +/* Upper-case digits. */ +static const char upper_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + +#define OUTCHAR(x) done+=(stream(x, (FILE *)data)==-1?0:1) + +/* Data type to read from the arglist */ +typedef enum { + FORMAT_UNKNOWN = 0, + FORMAT_STRING, + FORMAT_PTR, + FORMAT_INT, + FORMAT_INTPTR, + FORMAT_LONG, + FORMAT_LONGLONG, + FORMAT_DOUBLE, + FORMAT_LONGDOUBLE, + FORMAT_WIDTH /* For internal use */ +} FormatType; + +/* convertion and display flags */ +enum { + FLAGS_NEW = 0, + FLAGS_SPACE = 1<<0, + FLAGS_SHOWSIGN = 1<<1, + FLAGS_LEFT = 1<<2, + FLAGS_ALT = 1<<3, + FLAGS_SHORT = 1<<4, + FLAGS_LONG = 1<<5, + FLAGS_LONGLONG = 1<<6, + FLAGS_LONGDOUBLE = 1<<7, + FLAGS_PAD_NIL = 1<<8, + FLAGS_UNSIGNED = 1<<9, + FLAGS_OCTAL = 1<<10, + FLAGS_HEX = 1<<11, + FLAGS_UPPER = 1<<12, + FLAGS_WIDTH = 1<<13, /* '*' or '*$' used */ + FLAGS_WIDTHPARAM = 1<<14, /* width PARAMETER was specified */ + FLAGS_PREC = 1<<15, /* precision was specified */ + FLAGS_PRECPARAM = 1<<16, /* precision PARAMETER was specified */ + FLAGS_CHAR = 1<<17, /* %c story */ + FLAGS_FLOATE = 1<<18, /* %e or %E */ + FLAGS_FLOATG = 1<<19 /* %g or %G */ +}; + +typedef struct { + FormatType type; + int flags; + int width; /* width OR width parameter number */ + int precision; /* precision OR precision parameter number */ + union { + char *str; + void *ptr; + long num; +#if SIZEOF_LONG_LONG /* if this is non-zero */ + long long lnum; +#endif + double dnum; +#if SIZEOF_LONG_DOUBLE + long double ldnum; +#endif + } data; +} va_stack_t; + +struct nsprintf { + char *buffer; + size_t length; + size_t max; +}; + +struct asprintf { + char *buffer; /* allocated buffer */ + size_t len; /* length of string */ + size_t alloc; /* length of alloc */ +}; + +int msprintf(char *buffer, const char *format, ...); + +static int dprintf_DollarString(char *input, char **end) +{ + int number=0; + while(isdigit((int)*input)) { + number *= 10; + number += *input-'0'; + input++; + } + if(number && ('$'==*input++)) { + *end = input; + return number; + } + return 0; +} + +static BOOL dprintf_IsQualifierNoDollar(char c) +{ + switch (c) { + 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': + 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_param) + max_param = this_param; + + /* + * The parameter with number 'i' should be used. Next, we need + * to get SIZE and TYPE of the parameter. Add the information + * to our array. + */ + + width = 0; + precision = 0; + + /* Handle the flags */ + + while (dprintf_IsQualifierNoDollar(*fmt)) { + switch (*fmt++) { + case ' ': + flags |= FLAGS_SPACE; + break; + case '+': + flags |= FLAGS_SHOWSIGN; + break; + case '-': + flags |= FLAGS_LEFT; + flags &= ~FLAGS_PAD_NIL; + break; + case '#': + flags |= FLAGS_ALT; + break; + case '.': + flags |= FLAGS_PREC; + if ('*' == *fmt) { + /* The precision is picked from a specified parameter */ + + flags |= FLAGS_PRECPARAM; + fmt++; + param_num++; + + i = dprintf_DollarString(fmt, &fmt); + if (i) + precision = i; + else + precision = param_num; + + if (precision > max_param) + max_param = precision; + } + else { + flags |= FLAGS_PREC; + precision = strtol(fmt, &fmt, 10); + } + break; + case 'h': + flags |= FLAGS_SHORT; + break; + case 'l': + if (flags & FLAGS_LONG) + flags |= FLAGS_LONGLONG; + else + flags |= FLAGS_LONG; + break; + case 'L': + flags |= FLAGS_LONGDOUBLE; + break; + case 'q': + flags |= FLAGS_LONGLONG; + break; + case 'Z': + if (sizeof(size_t) > sizeof(unsigned long int)) + flags |= FLAGS_LONGLONG; + if (sizeof(size_t) > sizeof(unsigned int)) + flags |= FLAGS_LONG; + break; + case '0': + if (!(flags & FLAGS_LEFT)) + flags |= FLAGS_PAD_NIL; + /* FALLTHROUGH */ + case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + flags |= FLAGS_WIDTH; + width = strtol(--fmt, &fmt, 10); + break; + case '*': /* Special case */ + flags |= FLAGS_WIDTHPARAM; + param_num++; + + i = dprintf_DollarString(fmt, &fmt); + if(i) + width = i; + else + width = param_num; + if(width > max_param) + max_param=width; + break; + default: + break; + } + } /* switch */ + + /* Handle the specifier */ + + i = this_param - 1; + + switch (*fmt) { + case 'S': + flags |= FLAGS_ALT; + /* FALLTHROUGH */ + case 's': + vto[i].type = FORMAT_STRING; + break; + case 'n': + vto[i].type = FORMAT_INTPTR; + break; + case 'p': + vto[i].type = FORMAT_PTR; + break; + case 'd': case 'i': + vto[i].type = FORMAT_INT; + break; + case 'u': + vto[i].type = FORMAT_INT; + flags |= FLAGS_UNSIGNED; + break; + case 'o': + vto[i].type = FORMAT_INT; + flags |= FLAGS_OCTAL; + break; + case 'x': + vto[i].type = FORMAT_INT; + flags |= FLAGS_HEX; + break; + case 'X': + vto[i].type = FORMAT_INT; + flags |= FLAGS_HEX|FLAGS_UPPER; + break; + case 'c': + vto[i].type = FORMAT_INT; + flags |= FLAGS_CHAR; + break; + case 'f': + vto[i].type = FORMAT_DOUBLE; + break; + case 'e': case 'E': + vto[i].type = FORMAT_DOUBLE; + flags |= FLAGS_FLOATE| (('E' == *fmt)?FLAGS_UPPER:0); + break; + case 'g': case 'G': + vto[i].type = FORMAT_DOUBLE; + flags |= FLAGS_FLOATG| (('G' == *fmt)?FLAGS_UPPER:0); + break; + default: + vto[i].type = FORMAT_UNKNOWN; + break; + } /* switch */ + + vto[i].flags = flags; + vto[i].width = width; + vto[i].precision = precision; + + 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; + i = width - 1; + vto[i].type = FORMAT_WIDTH; + vto[i].flags = FLAGS_NEW; + vto[i].precision = vto[i].width = 0; /* can't use width or precision + of width! */ + } + 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; + i = precision - 1; + vto[i].type = FORMAT_WIDTH; + vto[i].flags = FLAGS_NEW; + vto[i].precision = vto[i].width = 0; /* can't use width or precision + of width! */ + } + *endpos++ = fmt + 1; /* end of this sequence */ + } + } + +#ifdef DPRINTF_DEBUG2 + dprintf_Pass1Report(vto, max_param); +#endif + + /* Read the arg list parameters into our data list */ + for (i=0; i$ sequence */ + param=dprintf_DollarString(f, &f); + + if(!param) + param = param_num; + else + --param; + + param_num++; /* increase this always to allow "%2$s %1$s %s" and then the + third %s will pick the 3rd argument */ + + p = &vto[param]; + + /* pick up the specified width */ + if(p->flags & FLAGS_WIDTHPARAM) + width = vto[p->width].data.num; + else + width = p->width; + + /* pick up the specified precision */ + if(p->flags & FLAGS_PRECPARAM) + prec = vto[p->precision].data.num; + else if(p->flags & FLAGS_PREC) + prec = p->precision; + else + prec = -1; + + alt = p->flags & FLAGS_ALT; + + switch (p->type) { + case FORMAT_INT: + num = p->data.num; + if(p->flags & FLAGS_CHAR) { + /* Character. */ + if (!(p->flags & FLAGS_LEFT)) + while (--width > 0) + OUTCHAR(' '); + OUTCHAR((char) num); + 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) { + /* Hexadecimal unsigned integer. */ + + digits = (p->flags & FLAGS_UPPER)? upper_digits : lower_digits; + base = 16; + goto unsigned_number; + } + + /* Decimal integer. */ + base = 10; + +#if SIZEOF_LONG_LONG + if(p->flags & FLAGS_LONGLONG) { + /* long long */ + num = p->data.lnum; + is_neg = num < 0; + num = is_neg ? (- num) : num; + } + else +#endif + { + signed_num = (long) num; + + is_neg = signed_num < 0; + num = is_neg ? (- signed_num) : signed_num; + } + goto number; + + unsigned_number:; + /* Unsigned number of base BASE. */ + is_neg = 0; + + number:; + /* Number of base BASE. */ + { + char *workend = &work[sizeof(work) - 1]; + register char *w; + + /* Supply a default precision if none was given. */ + if (prec == -1) + prec = 1; + + /* Put the number in WORK. */ + w = workend; + while (num > 0) { + *w-- = digits[num % base]; + num /= base; + } + width -= workend - w; + prec -= workend - w; + + if (alt && base == 8 && prec <= 0) { + *w-- = '0'; + --width; + } + + if (prec > 0) { + width -= prec; + while (prec-- > 0) + *w-- = '0'; + } + + if (alt && base == 16) + width -= 2; + + 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) + OUTCHAR(' '); + + if (is_neg) + OUTCHAR('-'); + else if (p->flags & FLAGS_SHOWSIGN) + OUTCHAR('+'); + else if (p->flags & FLAGS_SPACE) + OUTCHAR(' '); + + if (alt && base == 16) { + OUTCHAR('0'); + if(p->flags & FLAGS_UPPER) + OUTCHAR('X'); + else + OUTCHAR('x'); + } + + if (!(p->flags & FLAGS_LEFT) && (p->flags & FLAGS_PAD_NIL)) + while (width-- > 0) + OUTCHAR('0'); + + /* Write the number. */ + while (++w <= workend) { + OUTCHAR(*w); + } + + if (p->flags & FLAGS_LEFT) + while (width-- > 0) + OUTCHAR(' '); + } + break; + + case FORMAT_STRING: + /* String. */ + { + static char null[] = "(nil)"; + char *str; + size_t len; + + str = (char *) p->data.str; + if ( str == NULL) { + /* Write null[] if there's space. */ + if (prec == -1 || prec >= (long) sizeof(null) - 1) { + str = null; + len = sizeof(null) - 1; + /* Disable quotes around (nil) */ + p->flags &= (~FLAGS_ALT); + } + else { + str = ""; + len = 0; + } + } + else + len = strlen(str); + + if (prec != -1 && (size_t) prec < len) + len = prec; + width -= len; + + if (p->flags & FLAGS_ALT) + OUTCHAR('"'); + + if (!(p->flags&FLAGS_LEFT)) + while (width-- > 0) + OUTCHAR(' '); + + while (len-- > 0) + OUTCHAR(*str++); + if (p->flags&FLAGS_LEFT) + while (width-- > 0) + OUTCHAR(' '); + + if (p->flags & FLAGS_ALT) + OUTCHAR('"'); + } + break; + + case FORMAT_PTR: + /* Generic pointer. */ + { + void *ptr; + ptr = (void *) p->data.ptr; + 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; + num = (unsigned long) ptr; + is_neg = 0; + goto number; + } + else { + /* Write "(nil)" for a nil pointer. */ + static char nil[] = "(nil)"; + register char *point; + + width -= sizeof(nil) - 1; + if (p->flags & FLAGS_LEFT) + while (width-- > 0) + OUTCHAR(' '); + for (point = nil; *point != '\0'; ++point) + OUTCHAR(*point); + if (! (p->flags & FLAGS_LEFT)) + while (width-- > 0) + OUTCHAR(' '); + } + } + break; + + case FORMAT_DOUBLE: + { + char formatbuf[32]="%"; + char *fptr; + + width = -1; + if (p->flags & FLAGS_WIDTH) + width = p->width; + else if (p->flags & FLAGS_WIDTHPARAM) + width = vto[p->width].data.num; + + prec = -1; + if (p->flags & FLAGS_PREC) + prec = p->precision; + else if (p->flags & FLAGS_PRECPARAM) + prec = vto[p->precision].data.num; + + 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, "#"); + + fptr=&formatbuf[strlen(formatbuf)]; + + if(width >= 0) { + /* RECURSIVE USAGE */ + fptr += msprintf(fptr, "%d", width); + } + if(prec >= 0) { + /* RECURSIVE USAGE */ + fptr += msprintf(fptr, ".%d", prec); + } + if (p->flags & FLAGS_LONG) + strcat(fptr, "l"); + + if (p->flags & FLAGS_FLOATE) + strcat(fptr, p->flags&FLAGS_UPPER?"E":"e"); + else if (p->flags & FLAGS_FLOATG) + strcat(fptr, (p->flags & FLAGS_UPPER) ? "G" : "g"); + else + strcat(fptr, "f"); + + /* NOTE NOTE NOTE!! Not all sprintf() implementations returns number + of output characters */ +#if SIZEOF_LONG_DOUBLE + if (p->flags & FLAGS_LONG) + /* This is for support of the 'long double' type */ + (sprintf)(work, formatbuf, p->data.ldnum); + else +#endif + (sprintf)(work, formatbuf, p->data.dnum); + + for(fptr=work; *fptr; fptr++) + OUTCHAR(*fptr); + } + break; + + case FORMAT_INTPTR: + /* Answer the count of characters written. */ +#if SIZEOF_LONG_LONG + if (p->flags & FLAGS_LONGLONG) + *(long long int *) p->data.ptr = done; + else +#endif + if (p->flags & FLAGS_LONG) + *(long int *) p->data.ptr = done; + else if (!(p->flags & FLAGS_SHORT)) + *(int *) p->data.ptr = done; + else + *(short int *) p->data.ptr = done; + break; + + default: + break; + } + f = *end++; /* goto end of %-code */ + + } + return done; +} + +static int StoreNonPrintable(int output, struct nsprintf *infop) +{ + /* If the character isn't printable then we convert it */ + char work[64], *w; + int num = output; + + w = &work[sizeof(work)]; + *(--w) = (char)0; + for(; num > 0; num /= 10) { + *(--w) = lower_digits[num % 10]; + } + if (infop->length + strlen(w) + 1 < infop->max) + { + infop->buffer[0] = '\\'; + infop->buffer++; + infop->length++; + for (; *w; w++) + { + infop->buffer[0] = *w; + infop->buffer++; + infop->length++; + } + return output; + } + return -1; +} + +/* fputc() look-alike */ +static int addbyter(int output, FILE *data) +{ + struct nsprintf *infop=(struct nsprintf *)data; + + if(infop->length < infop->max) { + /* only do this if we haven't reached max length yet */ + if (isprint(output) || isspace(output)) + { + infop->buffer[0] = (char)output; /* store */ + infop->buffer++; /* increase pointer */ + infop->length++; /* we are now one byte larger */ + } + else + { + return StoreNonPrintable(output, infop); + } + return output; /* fputc() returns like this on success */ + } + return -1; +} + +int msnprintf(char *buffer, size_t maxlength, const char *format, ...) +{ + va_list ap_save; /* argument pointer */ + int retcode; + struct nsprintf info; + + info.buffer = buffer; + info.length = 0; + info.max = maxlength; + + va_start(ap_save, format); + retcode = dprintf_formatf(&info, addbyter, format, ap_save); + va_end(ap_save); + info.buffer[0] = 0; /* we terminate this with a zero byte */ + + /* we could even return things like */ + + return retcode; +} + +int mvsnprintf(char *buffer, size_t maxlength, const char *format, va_list ap_save) +{ + int retcode; + struct nsprintf info; + + info.buffer = buffer; + info.length = 0; + info.max = maxlength; + + retcode = dprintf_formatf(&info, addbyter, format, ap_save); + info.buffer[0] = 0; /* we terminate this with a zero byte */ + return retcode; +} + + +/* fputc() look-alike */ +static int alloc_addbyter(int output, FILE *data) +{ + struct asprintf *infop=(struct asprintf *)data; + + if(!infop->buffer) { + infop->buffer=(char *)malloc(32); + if(!infop->buffer) + return -1; /* fail */ + infop->alloc = 32; + infop->len =0; + } + else if(infop->len+1 >= infop->alloc) { + char *newptr; + + newptr = (char *)realloc(infop->buffer, infop->alloc*2); + + if(!newptr) { + return -1; + } + infop->buffer = newptr; + infop->alloc *= 2; + } + + infop->buffer[ infop->len ] = output; + + infop->len++; + + return output; /* fputc() returns like this on success */ + +} + +char *maprintf(const char *format, ...) +{ + va_list ap_save; /* argument pointer */ + int retcode; + struct asprintf info; + + info.buffer = NULL; + info.len = 0; + info.alloc = 0; + + va_start(ap_save, format); + retcode = dprintf_formatf(&info, alloc_addbyter, format, ap_save); + va_end(ap_save); + if(info.len) { + info.buffer[info.len] = 0; /* we terminate this with a zero byte */ + return info.buffer; + } + else + return NULL; +} + +char *mvaprintf(const char *format, va_list ap_save) +{ + int retcode; + struct asprintf info; + + info.buffer = NULL; + info.len = 0; + info.alloc = 0; + + retcode = dprintf_formatf(&info, alloc_addbyter, format, ap_save); + info.buffer[info.len] = 0; /* we terminate this with a zero byte */ + if(info.len) { + info.buffer[info.len] = 0; /* we terminate this with a zero byte */ + return info.buffer; + } + else + return NULL; +} + +static int storebuffer(int output, FILE *data) +{ + char **buffer = (char **)data; + **buffer = (char)output; + (*buffer)++; + return output; /* act like fputc() ! */ +} + +int msprintf(char *buffer, const char *format, ...) +{ + va_list ap_save; /* argument pointer */ + int retcode; + va_start(ap_save, format); + retcode = dprintf_formatf(&buffer, storebuffer, format, ap_save); + va_end(ap_save); + *buffer=0; /* we terminate this with a zero byte */ + return retcode; +} + +extern int fputc(int, FILE *); + +int mprintf(const char *format, ...) +{ + int retcode; + va_list ap_save; /* argument pointer */ + va_start(ap_save, format); + retcode = dprintf_formatf(stdout, fputc, format, ap_save); + va_end(ap_save); + return retcode; +} + +int mfprintf(FILE *whereto, const char *format, ...) +{ + int retcode; + va_list ap_save; /* argument pointer */ + va_start(ap_save, format); + retcode = dprintf_formatf(whereto, fputc, format, ap_save); + va_end(ap_save); + return retcode; +} + +int mvsprintf(char *buffer, const char *format, va_list ap_save) +{ + int retcode; + retcode = dprintf_formatf(&buffer, storebuffer, format, ap_save); + *buffer=0; /* we terminate this with a zero byte */ + return retcode; +} + +int mvprintf(const char *format, va_list ap_save) +{ + return dprintf_formatf(stdout, fputc, format, ap_save); +} + +int 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 SIZEOF_LONG_LONG + long long hullo; + dprintf("%3$12s %1$s %2$qd %4$d\n", "daniel", hullo, "stenberg", 65); +#endif + + mprintf("%3d %5d\n", 10, 1998); + + ptr=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 + mprintf(buffer, "%s %s %d", "daniel", "stenberg", 19988); + puts(buffer); + + mfprintf(stderr, "%s %#08x\n", "dummy", 65); + + printf("%s %#08x\n", "dummy", 65); + { + double tryout = 3.14156592; + 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 -- cgit v1.2.1 From 0f8facb49b45a711fa7832c68260a5b45b362922 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 9 Oct 2000 11:12:34 +0000 Subject: added memory debugging include file --- lib/mprintf.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 237a21a9d..7ccbcbf81 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -98,6 +98,10 @@ static const char rcsid[] = "@(#)$Id$"; #include #include +/* The last #include file should be: */ +#ifdef MALLOCDEBUG +#include "memdebug.h" +#endif #define BUFFSIZE 256 /* buffer for long-to-str and float-to-str calcs */ #define MAX_PARAMETERS 128 /* lame static limit */ -- cgit v1.2.1 From 5a07305dc837f7c226fe18142653f012e07a3270 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 15 Nov 2000 15:36:41 +0000 Subject: not printf()ing %s normally for character that weren't isprint() made things go weird, had to remove this. I should use trio soon for all the *printf() stuff as this is too broken --- lib/mprintf.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 7ccbcbf81..64d2360d2 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -1035,16 +1035,9 @@ static int addbyter(int output, FILE *data) if(infop->length < infop->max) { /* only do this if we haven't reached max length yet */ - if (isprint(output) || isspace(output)) - { - infop->buffer[0] = (char)output; /* store */ - infop->buffer++; /* increase pointer */ - infop->length++; /* we are now one byte larger */ - } - else - { - return StoreNonPrintable(output, infop); - } + infop->buffer[0] = (char)output; /* store */ + infop->buffer++; /* increase pointer */ + infop->length++; /* we are now one byte larger */ return output; /* fputc() returns like this on success */ } return -1; -- cgit v1.2.1 From 56ac132401bf514374388fc1e2b193fc58429e2f Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 21 Nov 2000 13:18:30 +0000 Subject: removed the storenonprintable function as it isn't used anymore --- lib/mprintf.c | 27 --------------------------- 1 file changed, 27 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 64d2360d2..1ac98cae1 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -1001,33 +1001,6 @@ static int dprintf_formatf( return done; } -static int StoreNonPrintable(int output, struct nsprintf *infop) -{ - /* If the character isn't printable then we convert it */ - char work[64], *w; - int num = output; - - w = &work[sizeof(work)]; - *(--w) = (char)0; - for(; num > 0; num /= 10) { - *(--w) = lower_digits[num % 10]; - } - if (infop->length + strlen(w) + 1 < infop->max) - { - infop->buffer[0] = '\\'; - infop->buffer++; - infop->length++; - for (; *w; w++) - { - infop->buffer[0] = *w; - infop->buffer++; - infop->length++; - } - return output; - } - return -1; -} - /* fputc() look-alike */ static int addbyter(int output, FILE *data) { -- cgit v1.2.1 From ed8dbf4ac24f08c69d0cf2020a9c3ea09203baba Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 4 Jan 2001 12:27:04 +0000 Subject: updated license text in headers --- lib/mprintf.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 1ac98cae1..a14e56ea2 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -4,6 +4,15 @@ * ************************************************************************* * + * 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. + * + * 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() * 1.0. A full blooded printf() clone with full support for $ -- cgit v1.2.1 From 4031104404c6ceed5e57134125dcdb6cac51c564 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 5 Jan 2001 10:11:41 +0000 Subject: Internal symbols that aren't static are now prefixed with 'Curl_' --- lib/mprintf.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index a14e56ea2..dd6dbabad 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -207,7 +207,7 @@ struct asprintf { size_t alloc; /* length of alloc */ }; -int msprintf(char *buffer, const char *format, ...); +int Curl_msprintf(char *buffer, const char *format, ...); static int dprintf_DollarString(char *input, char **end) { @@ -955,11 +955,11 @@ static int dprintf_formatf( if(width >= 0) { /* RECURSIVE USAGE */ - fptr += msprintf(fptr, "%d", width); + fptr += Curl_msprintf(fptr, "%d", width); } if(prec >= 0) { /* RECURSIVE USAGE */ - fptr += msprintf(fptr, ".%d", prec); + fptr += Curl_msprintf(fptr, ".%d", prec); } if (p->flags & FLAGS_LONG) strcat(fptr, "l"); @@ -1025,7 +1025,7 @@ static int addbyter(int output, FILE *data) return -1; } -int msnprintf(char *buffer, size_t maxlength, const char *format, ...) +int Curl_msnprintf(char *buffer, size_t maxlength, const char *format, ...) { va_list ap_save; /* argument pointer */ int retcode; @@ -1045,7 +1045,7 @@ int msnprintf(char *buffer, size_t maxlength, const char *format, ...) return retcode; } -int mvsnprintf(char *buffer, size_t maxlength, const char *format, va_list ap_save) +int Curl_mvsnprintf(char *buffer, size_t maxlength, const char *format, va_list ap_save) { int retcode; struct nsprintf info; @@ -1092,7 +1092,7 @@ static int alloc_addbyter(int output, FILE *data) } -char *maprintf(const char *format, ...) +char *Curl_maprintf(const char *format, ...) { va_list ap_save; /* argument pointer */ int retcode; @@ -1113,7 +1113,7 @@ char *maprintf(const char *format, ...) return NULL; } -char *mvaprintf(const char *format, va_list ap_save) +char *Curl_mvaprintf(const char *format, va_list ap_save) { int retcode; struct asprintf info; @@ -1140,7 +1140,7 @@ static int storebuffer(int output, FILE *data) return output; /* act like fputc() ! */ } -int msprintf(char *buffer, const char *format, ...) +int Curl_msprintf(char *buffer, const char *format, ...) { va_list ap_save; /* argument pointer */ int retcode; @@ -1153,7 +1153,7 @@ int msprintf(char *buffer, const char *format, ...) extern int fputc(int, FILE *); -int mprintf(const char *format, ...) +int Curl_mprintf(const char *format, ...) { int retcode; va_list ap_save; /* argument pointer */ @@ -1163,7 +1163,7 @@ int mprintf(const char *format, ...) return retcode; } -int mfprintf(FILE *whereto, const char *format, ...) +int Curl_mfprintf(FILE *whereto, const char *format, ...) { int retcode; va_list ap_save; /* argument pointer */ @@ -1173,7 +1173,7 @@ int mfprintf(FILE *whereto, const char *format, ...) return retcode; } -int mvsprintf(char *buffer, const char *format, va_list ap_save) +int Curl_mvsprintf(char *buffer, const char *format, va_list ap_save) { int retcode; retcode = dprintf_formatf(&buffer, storebuffer, format, ap_save); @@ -1181,12 +1181,12 @@ int mvsprintf(char *buffer, const char *format, va_list ap_save) return retcode; } -int mvprintf(const char *format, va_list ap_save) +int Curl_mvprintf(const char *format, va_list ap_save) { return dprintf_formatf(stdout, fputc, format, ap_save); } -int mvfprintf(FILE *whereto, const char *format, va_list ap_save) +int Curl_mvfprintf(FILE *whereto, const char *format, va_list ap_save) { return dprintf_formatf(whereto, fputc, format, ap_save); } -- cgit v1.2.1 From 6403257886fffe9d320099dfa019620c6c1f8dee Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 5 Jan 2001 12:19:42 +0000 Subject: renamed Curl_ to curl_ for the printf() prefixes --- lib/mprintf.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index dd6dbabad..1bf9a6e72 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -207,7 +207,7 @@ struct asprintf { size_t alloc; /* length of alloc */ }; -int Curl_msprintf(char *buffer, const char *format, ...); +int curl_msprintf(char *buffer, const char *format, ...); static int dprintf_DollarString(char *input, char **end) { @@ -955,11 +955,11 @@ static int dprintf_formatf( if(width >= 0) { /* RECURSIVE USAGE */ - fptr += Curl_msprintf(fptr, "%d", width); + fptr += curl_msprintf(fptr, "%d", width); } if(prec >= 0) { /* RECURSIVE USAGE */ - fptr += Curl_msprintf(fptr, ".%d", prec); + fptr += curl_msprintf(fptr, ".%d", prec); } if (p->flags & FLAGS_LONG) strcat(fptr, "l"); @@ -1025,7 +1025,7 @@ static int addbyter(int output, FILE *data) return -1; } -int Curl_msnprintf(char *buffer, size_t maxlength, const char *format, ...) +int curl_msnprintf(char *buffer, size_t maxlength, const char *format, ...) { va_list ap_save; /* argument pointer */ int retcode; @@ -1045,7 +1045,7 @@ int Curl_msnprintf(char *buffer, size_t maxlength, const char *format, ...) return retcode; } -int Curl_mvsnprintf(char *buffer, size_t maxlength, const char *format, va_list ap_save) +int curl_mvsnprintf(char *buffer, size_t maxlength, const char *format, va_list ap_save) { int retcode; struct nsprintf info; @@ -1092,7 +1092,7 @@ static int alloc_addbyter(int output, FILE *data) } -char *Curl_maprintf(const char *format, ...) +char *curl_maprintf(const char *format, ...) { va_list ap_save; /* argument pointer */ int retcode; @@ -1113,7 +1113,7 @@ char *Curl_maprintf(const char *format, ...) return NULL; } -char *Curl_mvaprintf(const char *format, va_list ap_save) +char *curl_mvaprintf(const char *format, va_list ap_save) { int retcode; struct asprintf info; @@ -1140,7 +1140,7 @@ static int storebuffer(int output, FILE *data) return output; /* act like fputc() ! */ } -int Curl_msprintf(char *buffer, const char *format, ...) +int curl_msprintf(char *buffer, const char *format, ...) { va_list ap_save; /* argument pointer */ int retcode; @@ -1153,7 +1153,7 @@ int Curl_msprintf(char *buffer, const char *format, ...) extern int fputc(int, FILE *); -int Curl_mprintf(const char *format, ...) +int curl_mprintf(const char *format, ...) { int retcode; va_list ap_save; /* argument pointer */ @@ -1163,7 +1163,7 @@ int Curl_mprintf(const char *format, ...) return retcode; } -int Curl_mfprintf(FILE *whereto, const char *format, ...) +int curl_mfprintf(FILE *whereto, const char *format, ...) { int retcode; va_list ap_save; /* argument pointer */ @@ -1173,7 +1173,7 @@ int Curl_mfprintf(FILE *whereto, const char *format, ...) return retcode; } -int Curl_mvsprintf(char *buffer, const char *format, va_list ap_save) +int curl_mvsprintf(char *buffer, const char *format, va_list ap_save) { int retcode; retcode = dprintf_formatf(&buffer, storebuffer, format, ap_save); @@ -1181,12 +1181,12 @@ int Curl_mvsprintf(char *buffer, const char *format, va_list ap_save) return retcode; } -int Curl_mvprintf(const char *format, va_list ap_save) +int curl_mvprintf(const char *format, va_list ap_save) { return dprintf_formatf(stdout, fputc, format, ap_save); } -int Curl_mvfprintf(FILE *whereto, const char *format, va_list ap_save) +int curl_mvfprintf(FILE *whereto, const char *format, va_list ap_save) { return dprintf_formatf(whereto, fputc, format, ap_save); } -- cgit v1.2.1 From f35b6e90f55dd072af42eaa0f003be04328fe1d9 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 14 Aug 2001 06:06:15 +0000 Subject: corrected dubious use of the same variable twice in a function call, gcc 3.0 warned about it --- lib/mprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 1bf9a6e72..9d446070c 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -446,7 +446,7 @@ static int dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, va_list a case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': flags |= FLAGS_WIDTH; - width = strtol(--fmt, &fmt, 10); + width = strtol(fmt-1, &fmt, 10); break; case '*': /* Special case */ flags |= FLAGS_WIDTHPARAM; -- cgit v1.2.1 From 95e7e551f65723f9c632c40b8cccc625a6d289ec Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 14 Aug 2001 08:32:03 +0000 Subject: added const char * => char * typecast --- lib/mprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 9d446070c..91f10fcb0 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -864,7 +864,7 @@ static int dprintf_formatf( p->flags &= (~FLAGS_ALT); } else { - str = ""; + str = (char *)""; len = 0; } } -- cgit v1.2.1 From aace68c91bc687345e4baa32814bce2d0f1e9f60 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 24 Aug 2001 07:39:15 +0000 Subject: extern declarations no longer done on windows (T. Bharath's patch) --- lib/mprintf.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 91f10fcb0..6e28d2b32 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -1151,7 +1151,9 @@ int curl_msprintf(char *buffer, const char *format, ...) return retcode; } +#ifndef WIN32 /* not needed on win32 */ extern int fputc(int, FILE *); +#endif int curl_mprintf(const char *format, ...) { -- cgit v1.2.1 From 6147879837a53d22c9be04e7a4fc315a297ba2b3 Mon Sep 17 00:00:00 2001 From: Sterling Hughes Date: Fri, 7 Sep 2001 04:01:32 +0000 Subject: Added formatting sections for emacs and vim --- lib/mprintf.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 6e28d2b32..375f4b3e0 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -1232,3 +1232,11 @@ int main() } #endif + +/* + * local variables: + * eval: (load-file "../curl-mode.el") + * end: + * vim600: et sw=2 ts=2 sts=2 tw=78 fdm=marker + * vim<600: et sw=2 ts=2 sts=2 tw=78 + */ -- cgit v1.2.1 From 8e91d5de8e4e17ce3d4936cc91171d09726e7bb3 Mon Sep 17 00:00:00 2001 From: Sterling Hughes Date: Thu, 11 Oct 2001 09:32:19 +0000 Subject: looks nicer and is better compatible with older vim versions --- lib/mprintf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 375f4b3e0..4897b4001 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -1237,6 +1237,6 @@ int main() * local variables: * eval: (load-file "../curl-mode.el") * end: - * vim600: et sw=2 ts=2 sts=2 tw=78 fdm=marker - * vim<600: et sw=2 ts=2 sts=2 tw=78 + * vim600: fdm=marker + * vim: et sw=2 ts=2 sts=2 tw=78 */ -- cgit v1.2.1 From a18d41a463b51b2ed6b9c4efcaf26cb0489128a8 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 12 Nov 2001 10:19:36 +0000 Subject: include setup.h --- lib/mprintf.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 4897b4001..7c42fcae1 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -92,13 +92,12 @@ * ****************************************************************************/ -static const char rcsid[] = "@(#)$Id$"; - -/* +* * To test: * * Use WIDTH, PRECISION and NUMBERED ARGUMENT combined. */ +#include "setup.h" #include #include -- cgit v1.2.1 From 05f3ca880fa092e4fd8684d39cb52401fedc9db7 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 12 Nov 2001 14:08:41 +0000 Subject: made CURLOPT_HTTPPROXYTUNNEL work for plain HTTP as well --- lib/mprintf.c | 6 ------ 1 file changed, 6 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 7c42fcae1..7259a9ade 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -92,13 +92,7 @@ * ****************************************************************************/ -* - * To test: - * - * Use WIDTH, PRECISION and NUMBERED ARGUMENT combined. - */ #include "setup.h" - #include #include #include -- cgit v1.2.1 From dccc77a32595f39c01198ccdd9e2c7794588c556 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 27 Nov 2001 07:27:32 +0000 Subject: Eric Lavigne updates --- lib/mprintf.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 7259a9ade..427d5b9f1 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -902,14 +902,14 @@ static int dprintf_formatf( } else { /* Write "(nil)" for a nil pointer. */ - static char nil[] = "(nil)"; + static char strnil[] = "(nil)"; register char *point; - width -= sizeof(nil) - 1; + width -= sizeof(strnil) - 1; if (p->flags & FLAGS_LEFT) while (width-- > 0) OUTCHAR(' '); - for (point = nil; *point != '\0'; ++point) + for (point = strnil; *point != '\0'; ++point) OUTCHAR(*point); if (! (p->flags & FLAGS_LEFT)) while (width-- > 0) -- cgit v1.2.1 From fd307bfe29ab61f9533d01082674c0f5e4b46267 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 18 Jan 2002 10:45:03 +0000 Subject: cut off a big piece of comment and added a pointer to the Trio web page should anyone ever want a good printf() clone --- lib/mprintf.c | 72 +++++------------------------------------------------------ 1 file changed, 5 insertions(+), 67 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 427d5b9f1..6ca74f3fb 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -24,73 +24,11 @@ * - Max 128 parameters * - No 'long double' support. * - ************************************************************************* - * - * - * 1998/01/10 (v2.8) - * Daniel - * - Updated version number. - * - Corrected a static non-zero prefixed width problem. - * - * 1998/11/17 - Daniel - * Added daprintf() and dvaprintf() for allocated printf() and vprintf(). - * They return an allocated buffer with the result inside. The result must - * be free()ed! - * - * 1998/08/23 - breese - * - * Converted all non-printable (and non-whitespace) characters into - * their decimal ASCII value preceeded by a '\' character - * (this only applies to snprintf family so far) - * - * Added %S (which is the same as %#s) - * - * 1998/05/05 (v2.7) - * - * Fixed precision and width qualifiers (%.*s) - * - * Added support for snprintf() - * - * Quoting (%#s) is disabled for the (nil) pointer - * - * 1997/06/09 (v2.6) - * - * %#s means that the string will be quoted with " - * (I was getting tired of writing \"%s\" all the time) - * - * [ERR] for strings changed to (nil) - * - * v2.5 - * - Added C++ support - * - Prepended all internal functions with dprintf_ - * - Defined the booleans - * - * v2.4 - * - Added dvsprintf(), dvfprintf() and dvprintf(). - * - Made the formatting function available with the name _formatf() to enable - * other *printf()-inspired functions. (I considered adding a dmsprintf() - * that works like sprintf() but allocates the destination string and - * possibly enlarges it itself, but things like that should be done with the - * new _formatf() instead.) - * - * v2.3 - * - Small modifications to make it compile nicely at both Daniel's and - * Bjorn's place. - * - * v2.2 - * - Made it work with text to the right of the last %! - * - Introduced dprintf(), dsprintf() and dfprintf(). - * - Float/double support enabled. This system is currently using the ordinary - * sprintf() function. NOTE that positional parameters, widths and precisions - * will still work like it should since the d-system takes care of that and - * passes that information re-formatted to the old sprintf(). - * - * v2.1 - * - Fixed space padding (i.e %d was extra padded previously) - * - long long output is supported - * - alternate output is done correct like in %#08x - * - ****************************************************************************/ + * If you ever want truly portable and good *printf() clones, the project that + * took on from here is named 'Trio' and you find more details on the trio web + * page at http://daniel.haxx.se/trio/ + */ + #include "setup.h" #include -- cgit v1.2.1 From 1dc5bf4f73703f7a7ba0f846b183660b44130d50 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 18 Jan 2002 12:53:05 +0000 Subject: #ifndef and #define magic to prevent compiler warnings when doing #if BLA where BLA is undefined --- lib/mprintf.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 6ca74f3fb..ffe645108 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -38,6 +38,15 @@ #include #include +#ifndef SIZEOF_LONG_LONG +/* prevents warnings on picky compilers */ +#define SIZEOF_LONG_LONG 0 +#endif +#ifndef SIZEOF_LONG_DOUBLE +#define SIZEOF_LONG_DOUBLE 0 +#endif + + /* The last #include file should be: */ #ifdef MALLOCDEBUG #include "memdebug.h" @@ -1129,7 +1138,7 @@ int main() { char buffer[129]; char *ptr; -#ifdef SIZEOF_LONG_LONG +#if SIZEOF_LONG_LONG>0 long long hullo; dprintf("%3$12s %1$s %2$qd %4$d\n", "daniel", hullo, "stenberg", 65); #endif -- cgit v1.2.1 From c795123cd575be3d593900984146efda0302e78e Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 18 Feb 2002 23:32:45 +0000 Subject: fixed a long long mistake --- lib/mprintf.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index ffe645108..37332b2ce 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -699,9 +699,8 @@ static int dprintf_formatf( #if SIZEOF_LONG_LONG if(p->flags & FLAGS_LONGLONG) { /* long long */ - num = p->data.lnum; - is_neg = num < 0; - num = is_neg ? (- num) : num; + is_neg = p->data.lnum < 0; + num = is_neg ? (- p->data.lnum) : p->data.lnum; } else #endif -- cgit v1.2.1 From c7b03d6479f59ca3330ebc0794c0e9f4fdb5aaea Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 21 May 2002 17:59:57 +0000 Subject: maprintf() and vmaprintf() now work better when printfing "%s" with an empty string --- lib/mprintf.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 37332b2ce..1a908b3f2 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -1028,7 +1028,6 @@ static int alloc_addbyter(int output, FILE *data) infop->len++; return output; /* fputc() returns like this on success */ - } char *curl_maprintf(const char *format, ...) @@ -1044,12 +1043,17 @@ char *curl_maprintf(const char *format, ...) va_start(ap_save, format); retcode = dprintf_formatf(&info, alloc_addbyter, format, ap_save); va_end(ap_save); - if(info.len) { + if(-1 == retcode) { + if(info.alloc) + free(info.buffer); + return NULL; + } + if(info.alloc) { info.buffer[info.len] = 0; /* we terminate this with a zero byte */ return info.buffer; } else - return NULL; + return strdup(""); } char *curl_mvaprintf(const char *format, va_list ap_save) @@ -1062,13 +1066,18 @@ char *curl_mvaprintf(const char *format, va_list ap_save) info.alloc = 0; retcode = dprintf_formatf(&info, alloc_addbyter, format, ap_save); - info.buffer[info.len] = 0; /* we terminate this with a zero byte */ - if(info.len) { + if(-1 == retcode) { + if(info.alloc) + free(info.buffer); + return NULL; + } + + if(info.alloc) { info.buffer[info.len] = 0; /* we terminate this with a zero byte */ return info.buffer; } else - return NULL; + return strdup(""); } static int storebuffer(int output, FILE *data) -- cgit v1.2.1 From f26a338a54e04d0a6907f5d2479d8b0fa9daf297 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 16 Jan 2003 21:08:12 +0000 Subject: copyright year update in the source header --- lib/mprintf.c | 292 ++++------------------------------------------------------ 1 file changed, 18 insertions(+), 274 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 1a908b3f2..dd9a42f81 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -1,281 +1,25 @@ -/**************************************************************************** +/*************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| * - * $Id$ - * - ************************************************************************* - * - * 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. + * Copyright (C) 1998 - 2003, Daniel Stenberg, , et al. * - * 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. + * 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. + * + * 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. * - * Purpose: - * A merge of Bjorn Reese's format() function and Daniel's dsprintf() - * 1.0. A full blooded printf() clone with full support for $ - * everywhere (parameters, widths and precisions) including variabled - * sized parameters (like doubles, long longs, long doubles and even - * void * in 64-bit architectures). + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. * - * Current restrictions: - * - Max 128 parameters - * - No 'long double' support. - * - * If you ever want truly portable and good *printf() clones, the project that - * took on from here is named 'Trio' and you find more details on the trio web - * page at http://daniel.haxx.se/trio/ - */ - - -#include "setup.h" -#include -#include -#include -#include -#include -#include - -#ifndef SIZEOF_LONG_LONG -/* prevents warnings on picky compilers */ -#define SIZEOF_LONG_LONG 0 -#endif -#ifndef SIZEOF_LONG_DOUBLE -#define SIZEOF_LONG_DOUBLE 0 -#endif - - -/* The last #include file should be: */ -#ifdef MALLOCDEBUG -#include "memdebug.h" -#endif - -#define BUFFSIZE 256 /* buffer for long-to-str and float-to-str calcs */ -#define MAX_PARAMETERS 128 /* lame static limit */ - -#undef TRUE -#undef FALSE -#undef BOOL -#ifdef __cplusplus -# define TRUE true -# define FALSE false -# define BOOL bool -#else -# define TRUE ((char)(1 == 1)) -# define FALSE ((char)(0 == 1)) -# define BOOL char -#endif - - -/* Lower-case digits. */ -static const char lower_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; - -/* Upper-case digits. */ -static const char upper_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - -#define OUTCHAR(x) done+=(stream(x, (FILE *)data)==-1?0:1) - -/* Data type to read from the arglist */ -typedef enum { - FORMAT_UNKNOWN = 0, - FORMAT_STRING, - FORMAT_PTR, - FORMAT_INT, - FORMAT_INTPTR, - FORMAT_LONG, - FORMAT_LONGLONG, - FORMAT_DOUBLE, - FORMAT_LONGDOUBLE, - FORMAT_WIDTH /* For internal use */ -} FormatType; - -/* convertion and display flags */ -enum { - FLAGS_NEW = 0, - FLAGS_SPACE = 1<<0, - FLAGS_SHOWSIGN = 1<<1, - FLAGS_LEFT = 1<<2, - FLAGS_ALT = 1<<3, - FLAGS_SHORT = 1<<4, - FLAGS_LONG = 1<<5, - FLAGS_LONGLONG = 1<<6, - FLAGS_LONGDOUBLE = 1<<7, - FLAGS_PAD_NIL = 1<<8, - FLAGS_UNSIGNED = 1<<9, - FLAGS_OCTAL = 1<<10, - FLAGS_HEX = 1<<11, - FLAGS_UPPER = 1<<12, - FLAGS_WIDTH = 1<<13, /* '*' or '*$' used */ - FLAGS_WIDTHPARAM = 1<<14, /* width PARAMETER was specified */ - FLAGS_PREC = 1<<15, /* precision was specified */ - FLAGS_PRECPARAM = 1<<16, /* precision PARAMETER was specified */ - FLAGS_CHAR = 1<<17, /* %c story */ - FLAGS_FLOATE = 1<<18, /* %e or %E */ - FLAGS_FLOATG = 1<<19 /* %g or %G */ -}; - -typedef struct { - FormatType type; - int flags; - int width; /* width OR width parameter number */ - int precision; /* precision OR precision parameter number */ - union { - char *str; - void *ptr; - long num; -#if SIZEOF_LONG_LONG /* if this is non-zero */ - long long lnum; -#endif - double dnum; -#if SIZEOF_LONG_DOUBLE - long double ldnum; -#endif - } data; -} va_stack_t; - -struct nsprintf { - char *buffer; - size_t length; - size_t max; -}; - -struct asprintf { - char *buffer; /* allocated buffer */ - size_t len; /* length of string */ - size_t alloc; /* length of alloc */ -}; - -int curl_msprintf(char *buffer, const char *format, ...); - -static int dprintf_DollarString(char *input, char **end) -{ - int number=0; - while(isdigit((int)*input)) { - number *= 10; - number += *input-'0'; - input++; - } - if(number && ('$'==*input++)) { - *end = input; - return number; - } - return 0; -} - -static BOOL dprintf_IsQualifierNoDollar(char c) -{ - switch (c) { - 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': - 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 Date: Thu, 16 Jan 2003 21:10:10 +0000 Subject: reverted bad header replacement --- lib/mprintf.c | 292 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 274 insertions(+), 18 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index dd9a42f81..1a908b3f2 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -1,25 +1,281 @@ -/*************************************************************************** - * _ _ ____ _ - * Project ___| | | | _ \| | - * / __| | | | |_) | | - * | (__| |_| | _ <| |___ - * \___|\___/|_| \_\_____| +/**************************************************************************** * - * Copyright (C) 1998 - 2003, Daniel Stenberg, , et al. + * $Id$ * - * 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. - * - * 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. + * 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. * - * $Id$ - ***************************************************************************/ + * 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() + * 1.0. A full blooded printf() clone with full support for $ + * everywhere (parameters, widths and precisions) including variabled + * sized parameters (like doubles, long longs, long doubles and even + * void * in 64-bit architectures). + * + * Current restrictions: + * - Max 128 parameters + * - No 'long double' support. + * + * If you ever want truly portable and good *printf() clones, the project that + * took on from here is named 'Trio' and you find more details on the trio web + * page at http://daniel.haxx.se/trio/ + */ + + +#include "setup.h" +#include +#include +#include +#include +#include +#include + +#ifndef SIZEOF_LONG_LONG +/* prevents warnings on picky compilers */ +#define SIZEOF_LONG_LONG 0 +#endif +#ifndef SIZEOF_LONG_DOUBLE +#define SIZEOF_LONG_DOUBLE 0 +#endif + + +/* The last #include file should be: */ +#ifdef MALLOCDEBUG +#include "memdebug.h" +#endif + +#define BUFFSIZE 256 /* buffer for long-to-str and float-to-str calcs */ +#define MAX_PARAMETERS 128 /* lame static limit */ + +#undef TRUE +#undef FALSE +#undef BOOL +#ifdef __cplusplus +# define TRUE true +# define FALSE false +# define BOOL bool +#else +# define TRUE ((char)(1 == 1)) +# define FALSE ((char)(0 == 1)) +# define BOOL char +#endif + + +/* Lower-case digits. */ +static const char lower_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; + +/* Upper-case digits. */ +static const char upper_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + +#define OUTCHAR(x) done+=(stream(x, (FILE *)data)==-1?0:1) + +/* Data type to read from the arglist */ +typedef enum { + FORMAT_UNKNOWN = 0, + FORMAT_STRING, + FORMAT_PTR, + FORMAT_INT, + FORMAT_INTPTR, + FORMAT_LONG, + FORMAT_LONGLONG, + FORMAT_DOUBLE, + FORMAT_LONGDOUBLE, + FORMAT_WIDTH /* For internal use */ +} FormatType; + +/* convertion and display flags */ +enum { + FLAGS_NEW = 0, + FLAGS_SPACE = 1<<0, + FLAGS_SHOWSIGN = 1<<1, + FLAGS_LEFT = 1<<2, + FLAGS_ALT = 1<<3, + FLAGS_SHORT = 1<<4, + FLAGS_LONG = 1<<5, + FLAGS_LONGLONG = 1<<6, + FLAGS_LONGDOUBLE = 1<<7, + FLAGS_PAD_NIL = 1<<8, + FLAGS_UNSIGNED = 1<<9, + FLAGS_OCTAL = 1<<10, + FLAGS_HEX = 1<<11, + FLAGS_UPPER = 1<<12, + FLAGS_WIDTH = 1<<13, /* '*' or '*$' used */ + FLAGS_WIDTHPARAM = 1<<14, /* width PARAMETER was specified */ + FLAGS_PREC = 1<<15, /* precision was specified */ + FLAGS_PRECPARAM = 1<<16, /* precision PARAMETER was specified */ + FLAGS_CHAR = 1<<17, /* %c story */ + FLAGS_FLOATE = 1<<18, /* %e or %E */ + FLAGS_FLOATG = 1<<19 /* %g or %G */ +}; + +typedef struct { + FormatType type; + int flags; + int width; /* width OR width parameter number */ + int precision; /* precision OR precision parameter number */ + union { + char *str; + void *ptr; + long num; +#if SIZEOF_LONG_LONG /* if this is non-zero */ + long long lnum; +#endif + double dnum; +#if SIZEOF_LONG_DOUBLE + long double ldnum; +#endif + } data; +} va_stack_t; + +struct nsprintf { + char *buffer; + size_t length; + size_t max; +}; + +struct asprintf { + char *buffer; /* allocated buffer */ + size_t len; /* length of string */ + size_t alloc; /* length of alloc */ +}; + +int curl_msprintf(char *buffer, const char *format, ...); + +static int dprintf_DollarString(char *input, char **end) +{ + int number=0; + while(isdigit((int)*input)) { + number *= 10; + number += *input-'0'; + input++; + } + if(number && ('$'==*input++)) { + *end = input; + return number; + } + return 0; +} + +static BOOL dprintf_IsQualifierNoDollar(char c) +{ + switch (c) { + 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': + 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 Date: Wed, 29 Jan 2003 10:14:20 +0000 Subject: removed the local variables for emacs and vim, use the new sample.emacs way for emacs, and vim users should provide a similar non-polluting style --- lib/mprintf.c | 8 -------- 1 file changed, 8 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 1a908b3f2..08a98c796 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -1180,11 +1180,3 @@ int main() } #endif - -/* - * local variables: - * eval: (load-file "../curl-mode.el") - * end: - * vim600: fdm=marker - * vim: et sw=2 ts=2 sts=2 tw=78 - */ -- cgit v1.2.1 From 308bc9d919d57388f269c473778ea7f6a331d1c5 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 26 Jun 2003 11:22:12 +0000 Subject: use CURLDEBUG instead of MALLOCDEBUG for preprocessor conditions --- lib/mprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 08a98c796..b51e883f1 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -48,7 +48,7 @@ /* The last #include file should be: */ -#ifdef MALLOCDEBUG +#ifdef CURLDEBUG #include "memdebug.h" #endif -- cgit v1.2.1 From bbe23945e438ec74002df18ffd01b182c98175c5 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 6 Aug 2003 22:32:47 +0000 Subject: fix the treatment of the variable width specifier '*', which caused a bug in the urlglobbing just now, fixed in the debian bug tracker as Bug#203827 --- lib/mprintf.c | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index b51e883f1..23e12c8ac 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -171,6 +171,7 @@ static BOOL dprintf_IsQualifierNoDollar(char c) 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 '*': return TRUE; default: return FALSE; -- cgit v1.2.1 From fb3eee5f0bf02585b337268612f788acfe729598 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 19 Aug 2003 15:37:07 +0000 Subject: Respect HAVE_LONGLONG to support 'long long' --- lib/mprintf.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 23e12c8ac..af2104170 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -38,10 +38,6 @@ #include #include -#ifndef SIZEOF_LONG_LONG -/* prevents warnings on picky compilers */ -#define SIZEOF_LONG_LONG 0 -#endif #ifndef SIZEOF_LONG_DOUBLE #define SIZEOF_LONG_DOUBLE 0 #endif @@ -125,7 +121,7 @@ typedef struct { char *str; void *ptr; long num; -#if SIZEOF_LONG_LONG /* if this is non-zero */ +#ifdef HAVE_LONGLONG long long lnum; #endif double dnum; @@ -517,7 +513,7 @@ static int dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, va_list a break; case FORMAT_INT: -#if SIZEOF_LONG_LONG +#ifdef HAVE_LONGLONG if(vto[i].flags & FLAGS_LONGLONG) vto[i].data.lnum = va_arg(arglist, long long); else @@ -604,7 +600,7 @@ static int dprintf_formatf( long base; /* Integral values to be written. */ -#if SIZEOF_LONG_LONG +#ifdef HAVE_LONGLONG unsigned long long num; #else unsigned long num; @@ -697,9 +693,9 @@ static int dprintf_formatf( /* Decimal integer. */ base = 10; -#if SIZEOF_LONG_LONG +#ifdef HAVE_LONGLONG if(p->flags & FLAGS_LONGLONG) { - /* long long */ + /* long long */ is_neg = p->data.lnum < 0; num = is_neg ? (- p->data.lnum) : p->data.lnum; } @@ -928,7 +924,7 @@ static int dprintf_formatf( case FORMAT_INTPTR: /* Answer the count of characters written. */ -#if SIZEOF_LONG_LONG +#ifdef HAVE_LONGLONG if (p->flags & FLAGS_LONGLONG) *(long long int *) p->data.ptr = done; else @@ -1147,7 +1143,7 @@ int main() { char buffer[129]; char *ptr; -#if SIZEOF_LONG_LONG>0 +#ifdef HAVE_LONGLONG long long hullo; dprintf("%3$12s %1$s %2$qd %4$d\n", "daniel", hullo, "stenberg", 65); #endif -- cgit v1.2.1 From 4cccceb0342c59441c6ae693612c557dbebcc03a Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Sun, 26 Oct 2003 15:37:45 +0000 Subject: snprintf() made a single-byte buffer overflow, as it could write a zero outside its given buffer. Discovered and reported by James Bursa. --- lib/mprintf.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index af2104170..6cb345a58 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -961,9 +961,9 @@ static int addbyter(int output, FILE *data) return -1; } -int curl_msnprintf(char *buffer, size_t maxlength, const char *format, ...) +int curl_mvsnprintf(char *buffer, size_t maxlength, const char *format, + va_list ap_save) { - va_list ap_save; /* argument pointer */ int retcode; struct nsprintf info; @@ -971,31 +971,28 @@ int curl_msnprintf(char *buffer, size_t maxlength, const char *format, ...) info.length = 0; info.max = maxlength; - va_start(ap_save, format); retcode = dprintf_formatf(&info, addbyter, format, ap_save); - va_end(ap_save); - info.buffer[0] = 0; /* we terminate this with a zero byte */ - - /* we could even return things like */ - + if(info.max) { + /* we terminate this with a zero byte */ + if(info.max == info.length) + /* we're at maximum, scrap the last letter */ + info.buffer[-1] = 0; + else + info.buffer[0] = 0; + } return retcode; } -int curl_mvsnprintf(char *buffer, size_t maxlength, const char *format, va_list ap_save) +int curl_msnprintf(char *buffer, size_t maxlength, const char *format, ...) { int retcode; - struct nsprintf info; - - info.buffer = buffer; - info.length = 0; - info.max = maxlength; - - retcode = dprintf_formatf(&info, addbyter, format, ap_save); - info.buffer[0] = 0; /* we terminate this with a zero byte */ + va_list ap_save; /* argument pointer */ + va_start(ap_save, format); + retcode = curl_mvsnprintf(buffer, maxlength, format, ap_save); + va_end(ap_save); return retcode; } - /* fputc() look-alike */ static int alloc_addbyter(int output, FILE *data) { -- cgit v1.2.1 From b60e0fa97ed7ddc66d0ad6d00dfd78319bb6ad36 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 5 Jan 2004 22:29:29 +0000 Subject: David J Meyer's large file support. --- lib/mprintf.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 6cb345a58..2b1356af7 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -167,7 +167,7 @@ static BOOL dprintf_IsQualifierNoDollar(char c) 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 '*': case 'O': return TRUE; default: return FALSE; @@ -376,6 +376,13 @@ static int dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, va_list a if (sizeof(size_t) > sizeof(unsigned int)) flags |= FLAGS_LONG; break; + case 'O': + if (sizeof(off_t) > sizeof(unsigned long int)) { + flags |= FLAGS_LONGLONG; + } else if (sizeof(off_t) > sizeof(unsigned int)) { + flags |= FLAGS_LONG; + } + break; case '0': if (!(flags & FLAGS_LEFT)) flags |= FLAGS_PAD_NIL; -- cgit v1.2.1 From b791e158f0e04a518dea19fdaf0bfbf71b343c64 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 22 Jan 2004 12:45:50 +0000 Subject: use curl_off_t instead of off_t! --- lib/mprintf.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 2b1356af7..a323bf554 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -38,6 +38,8 @@ #include #include +#include /* for the curl_off_t type */ + #ifndef SIZEOF_LONG_DOUBLE #define SIZEOF_LONG_DOUBLE 0 #endif @@ -377,9 +379,10 @@ static int dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, va_list a flags |= FLAGS_LONG; break; case 'O': - if (sizeof(off_t) > sizeof(unsigned long int)) { + if (sizeof(curl_off_t) > sizeof(unsigned long int)) { flags |= FLAGS_LONGLONG; - } else if (sizeof(off_t) > sizeof(unsigned int)) { + } + else if (sizeof(curl_off_t) > sizeof(unsigned int)) { flags |= FLAGS_LONG; } break; -- cgit v1.2.1 From 4d17d6876e4b2f08380812c4ec113073b0a14639 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 29 Jan 2004 13:56:45 +0000 Subject: Dan Fandrich's cleanup patch to make pedantic compiler options cause less warnings. Minor edits by me. --- lib/mprintf.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index a323bf554..42199cb8f 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -40,6 +40,8 @@ #include /* for the curl_off_t type */ +#include + #ifndef SIZEOF_LONG_DOUBLE #define SIZEOF_LONG_DOUBLE 0 #endif -- cgit v1.2.1 From b2e1bf7e7d617bb9437c750d4ad4e392e331adbb Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 20 Feb 2004 15:16:31 +0000 Subject: No longer support Z as a flag to print size_t, it isn't used by libcurl and I doubt anyone else uses it. Better preprocessor magic for the O flag (for curl_off_t printing) to prevent compiler warnings. --- lib/mprintf.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 42199cb8f..60fe81fe8 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -374,19 +374,20 @@ static int dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, va_list a case 'q': flags |= FLAGS_LONGLONG; break; +#if 0 case 'Z': if (sizeof(size_t) > sizeof(unsigned long int)) flags |= FLAGS_LONGLONG; if (sizeof(size_t) > sizeof(unsigned int)) flags |= FLAGS_LONG; break; +#endif case 'O': - if (sizeof(curl_off_t) > sizeof(unsigned long int)) { - flags |= FLAGS_LONGLONG; - } - else if (sizeof(curl_off_t) > sizeof(unsigned int)) { - flags |= FLAGS_LONG; - } +#if SIZEOF_CURL_OFF_T > 4 + flags |= FLAGS_LONGLONG; +#else + flags |= FLAGS_LONG; +#endif break; case '0': if (!(flags & FLAGS_LEFT)) -- cgit v1.2.1 From 4bde770169c05653726956c9bf866aec19ddfe1c Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Sat, 21 Feb 2004 15:05:46 +0000 Subject: added some extra typecasts to prevent compiler warnings when converting int to various types --- lib/mprintf.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 60fe81fe8..f30db759e 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -939,15 +939,15 @@ static int dprintf_formatf( /* Answer the count of characters written. */ #ifdef HAVE_LONGLONG if (p->flags & FLAGS_LONGLONG) - *(long long int *) p->data.ptr = done; + *(long long *) p->data.ptr = (long long)done; else #endif if (p->flags & FLAGS_LONG) - *(long int *) p->data.ptr = done; + *(long *) p->data.ptr = (long)done; else if (!(p->flags & FLAGS_SHORT)) - *(int *) p->data.ptr = done; + *(int *) p->data.ptr = (int)done; else - *(short int *) p->data.ptr = done; + *(short *) p->data.ptr = (short)done; break; default: -- cgit v1.2.1 From df94c7aedcc0b115fc38578d7b9de95cfcb5c5ef Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 25 Feb 2004 14:15:38 +0000 Subject: Based on a patch by Greg Hewgill I modified how long long is used, as we can use a 64bit type with MSVC that is a long long equivalent. --- lib/mprintf.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index f30db759e..50a396963 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -46,6 +46,15 @@ #define SIZEOF_LONG_DOUBLE 0 #endif +#ifdef HAVE_LONGLONG +#define LONG_LONG long long +#define ENABLE_64BIT +#else +#ifdef _MSC_VER +#define LONG_LONG __int64 +#define ENABLE_64BIT +#endif +#endif /* HAVE_LONGLONG */ /* The last #include file should be: */ #ifdef CURLDEBUG @@ -125,11 +134,11 @@ typedef struct { char *str; void *ptr; long num; -#ifdef HAVE_LONGLONG - long long lnum; +#ifdef ENABLE_64BIT + LONG_LONG lnum; #endif double dnum; -#if SIZEOF_LONG_DOUBLE +#if 0 /*SIZEOF_LONG_DOUBLE */ long double ldnum; #endif } data; @@ -526,9 +535,9 @@ static int dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, va_list a break; case FORMAT_INT: -#ifdef HAVE_LONGLONG +#ifdef ENABLE_64BIT if(vto[i].flags & FLAGS_LONGLONG) - vto[i].data.lnum = va_arg(arglist, long long); + vto[i].data.lnum = va_arg(arglist, LONG_LONG); else #endif if(vto[i].flags & FLAGS_LONG) @@ -538,7 +547,7 @@ static int dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, va_list a break; case FORMAT_DOUBLE: -#if SIZEOF_LONG_DOUBLE +#if 0 /*SIZEOF_LONG_DOUBLE */ if(vto[i].flags & FLAGS_LONG) vto[i].data.ldnum = va_arg(arglist, long double); else @@ -613,8 +622,8 @@ static int dprintf_formatf( long base; /* Integral values to be written. */ -#ifdef HAVE_LONGLONG - unsigned long long num; +#ifdef ENABLE_64BIT + unsigned LONG_LONG num; #else unsigned long num; #endif @@ -706,7 +715,7 @@ static int dprintf_formatf( /* Decimal integer. */ base = 10; -#ifdef HAVE_LONGLONG +#ifdef ENABLE_64BIT if(p->flags & FLAGS_LONGLONG) { /* long long */ is_neg = p->data.lnum < 0; @@ -937,9 +946,9 @@ static int dprintf_formatf( case FORMAT_INTPTR: /* Answer the count of characters written. */ -#ifdef HAVE_LONGLONG +#ifdef ENABLE_64BIT if (p->flags & FLAGS_LONGLONG) - *(long long *) p->data.ptr = (long long)done; + *(LONG_LONG *) p->data.ptr = (LONG_LONG)done; else #endif if (p->flags & FLAGS_LONG) @@ -1153,8 +1162,8 @@ int main() { char buffer[129]; char *ptr; -#ifdef HAVE_LONGLONG - long long hullo; +#ifdef ENABLE_64BIT + LONG_LONG hullo; dprintf("%3$12s %1$s %2$qd %4$d\n", "daniel", hullo, "stenberg", 65); #endif -- cgit v1.2.1 From f9b2b7940e1498413e341a04fac6989538d465dd Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 25 Feb 2004 15:34:05 +0000 Subject: disable the use of long double, we don't use it --- lib/mprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 50a396963..d56ef0301 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -931,7 +931,7 @@ static int dprintf_formatf( /* NOTE NOTE NOTE!! Not all sprintf() implementations returns number of output characters */ -#if SIZEOF_LONG_DOUBLE +#if 0 /*SIZEOF_LONG_DOUBLE*/ if (p->flags & FLAGS_LONG) /* This is for support of the 'long double' type */ (sprintf)(work, formatbuf, p->data.ldnum); -- cgit v1.2.1 From d687eed33e33019eb6c0d534b5c097c78c4b1731 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 26 Feb 2004 12:32:29 +0000 Subject: use %ld when printf()ing long variables (and removed use of 'register') --- lib/mprintf.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index d56ef0301..8956cb6ca 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -611,7 +611,8 @@ static int dprintf_formatf( char alt; /* Width of a field. */ - register long width; + long width; + /* Precision of a field. */ long prec; @@ -913,11 +914,11 @@ static int dprintf_formatf( if(width >= 0) { /* RECURSIVE USAGE */ - fptr += curl_msprintf(fptr, "%d", width); + fptr += curl_msprintf(fptr, "%ld", width); } if(prec >= 0) { /* RECURSIVE USAGE */ - fptr += curl_msprintf(fptr, ".%d", prec); + fptr += curl_msprintf(fptr, ".%ld", prec); } if (p->flags & FLAGS_LONG) strcat(fptr, "l"); -- cgit v1.2.1 From 5eeaff823506ab8a97801d3d7f9cf7f668681b58 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 1 Mar 2004 12:44:07 +0000 Subject: Support 'z' for size_t-sized integer printing, as in %zd or %zx. --- lib/mprintf.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 8956cb6ca..c653425f9 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -179,7 +179,7 @@ static BOOL dprintf_IsQualifierNoDollar(char c) 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 'h': case 'l': case 'L': case 'z': case 'q': case '*': case 'O': return TRUE; default: @@ -383,14 +383,15 @@ static int dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, va_list a case 'q': flags |= FLAGS_LONGLONG; break; -#if 0 - case 'Z': - if (sizeof(size_t) > sizeof(unsigned long int)) + case 'z': + /* the code below generates a warning if -Wunreachable-code is + used */ + if (sizeof(size_t) > sizeof(unsigned long)) flags |= FLAGS_LONGLONG; if (sizeof(size_t) > sizeof(unsigned int)) flags |= FLAGS_LONG; break; -#endif + case 'O': #if SIZEOF_CURL_OFF_T > 4 flags |= FLAGS_LONGLONG; -- cgit v1.2.1 From 485122035c2139dc6da23e77bb2ac021d8e998ee Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 1 Mar 2004 16:22:17 +0000 Subject: fixed the test code to work --- lib/mprintf.c | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index c653425f9..60584b4e6 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -38,23 +38,17 @@ #include #include -#include /* for the curl_off_t type */ - #include #ifndef SIZEOF_LONG_DOUBLE #define SIZEOF_LONG_DOUBLE 0 #endif -#ifdef HAVE_LONGLONG +#ifdef DPRINTF_DEBUG +#define HAVE_LONGLONG #define LONG_LONG long long #define ENABLE_64BIT -#else -#ifdef _MSC_VER -#define LONG_LONG __int64 -#define ENABLE_64BIT #endif -#endif /* HAVE_LONGLONG */ /* The last #include file should be: */ #ifdef CURLDEBUG @@ -391,14 +385,13 @@ static int dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, va_list a if (sizeof(size_t) > sizeof(unsigned int)) flags |= FLAGS_LONG; break; - - case 'O': + case 'O': #if SIZEOF_CURL_OFF_T > 4 flags |= FLAGS_LONGLONG; #else flags |= FLAGS_LONG; #endif - break; + break; case '0': if (!(flags & FLAGS_LEFT)) flags |= FLAGS_PAD_NIL; @@ -1165,13 +1158,15 @@ int main() char buffer[129]; char *ptr; #ifdef ENABLE_64BIT - LONG_LONG hullo; - dprintf("%3$12s %1$s %2$qd %4$d\n", "daniel", hullo, "stenberg", 65); + long long one=99; + long long two=100; + long long test = 0x1000000000LL; + curl_mprintf("%lld %lld %lld\n", one, two, test); #endif - mprintf("%3d %5d\n", 10, 1998); + curl_mprintf("%3d %5d\n", 10, 1998); - ptr=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); + 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); @@ -1180,15 +1175,15 @@ int main() free(ptr); #if 1 - mprintf(buffer, "%s %s %d", "daniel", "stenberg", 19988); + curl_mprintf(buffer, "%s %s %d", "daniel", "stenberg", 19988); puts(buffer); - mfprintf(stderr, "%s %#08x\n", "dummy", 65); + curl_mfprintf(stderr, "%s %#08x\n", "dummy", 65); printf("%s %#08x\n", "dummy", 65); { double tryout = 3.14156592; - mprintf(buffer, "%.2g %G %f %e %E", tryout, tryout, tryout, tryout, tryout); + 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); } -- cgit v1.2.1 From d9ffd2f54435d92dca3d7b689f2b18a19acb6a4c Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 8 Mar 2004 11:28:14 +0000 Subject: don't use 'register' make strtol() returns get stored in long variables don't mix size_t with int --- lib/mprintf.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 60584b4e6..3b06ab5ab 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -152,7 +152,7 @@ struct asprintf { int curl_msprintf(char *buffer, const char *format, ...); -static int dprintf_DollarString(char *input, char **end) +static long dprintf_DollarString(char *input, char **end) { int number=0; while(isdigit((int)*input)) { @@ -285,12 +285,12 @@ static int dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, va_list a { char *fmt = format; int param_num = 0; - int this_param; - int width; - int precision; + long this_param; + long width; + long precision; int flags; - int max_param=0; - int i; + long max_param=0; + long i; while (*fmt) { if (*fmt++ == '%') { @@ -461,13 +461,21 @@ static int dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, va_list a case 'f': vto[i].type = FORMAT_DOUBLE; break; - case 'e': case 'E': + case 'e': + vto[i].type = FORMAT_DOUBLE; + flags |= FLAGS_FLOATE; + break; + case 'E': vto[i].type = FORMAT_DOUBLE; - flags |= FLAGS_FLOATE| (('E' == *fmt)?FLAGS_UPPER:0); + flags |= FLAGS_FLOATE|FLAGS_UPPER; break; - case 'g': case 'G': + case 'g': + vto[i].type = FORMAT_DOUBLE; + flags |= FLAGS_FLOATG; + break; + case 'G': vto[i].type = FORMAT_DOUBLE; - flags |= FLAGS_FLOATG| (('G' == *fmt)?FLAGS_UPPER:0); + flags |= FLAGS_FLOATG|FLAGS_UPPER; break; default: vto[i].type = FORMAT_UNKNOWN; @@ -580,7 +588,7 @@ static int dprintf_formatf( char *f; /* Number of characters written. */ - register size_t done = 0; + int done = 0; long param; /* current parameter to read */ long param_num=0; /* parameter counter */ @@ -734,7 +742,7 @@ static int dprintf_formatf( /* Number of base BASE. */ { char *workend = &work[sizeof(work) - 1]; - register char *w; + char *w; /* Supply a default precision if none was given. */ if (prec == -1) @@ -863,7 +871,7 @@ static int dprintf_formatf( else { /* Write "(nil)" for a nil pointer. */ static char strnil[] = "(nil)"; - register char *point; + char *point; width -= sizeof(strnil) - 1; if (p->flags & FLAGS_LEFT) -- cgit v1.2.1 From f8426a2c441a63b5d207e92a8509428dfb7fb1dc Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 23 Mar 2004 15:25:54 +0000 Subject: stricter variable type usage --- lib/mprintf.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 3b06ab5ab..ddf34edf8 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -122,8 +122,8 @@ enum { typedef struct { FormatType type; int flags; - int width; /* width OR width parameter number */ - int precision; /* precision OR precision parameter number */ + long width; /* width OR width parameter number */ + long precision; /* precision OR precision parameter number */ union { char *str; void *ptr; @@ -281,7 +281,8 @@ int dprintf_Pass1Report(va_stack_t *vto, int max) * ******************************************************************/ -static int dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, va_list arglist) +static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, + va_list arglist) { char *fmt = format; int param_num = 0; @@ -681,7 +682,7 @@ static int dprintf_formatf( else prec = -1; - alt = p->flags & FLAGS_ALT; + alt = (p->flags & FLAGS_ALT)?TRUE:FALSE; switch (p->type) { case FORMAT_INT: @@ -1042,7 +1043,7 @@ static int alloc_addbyter(int output, FILE *data) infop->alloc *= 2; } - infop->buffer[ infop->len ] = output; + infop->buffer[ infop->len ] = (char)output; infop->len++; -- cgit v1.2.1 From 4d9517f0b4092b62dc791125823d6ce581d0c26c Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 5 May 2004 06:57:26 +0000 Subject: prevent warnings when using the gcc option -Wunreachable-code --- lib/mprintf.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index ddf34edf8..e8613839f 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -44,6 +44,11 @@ #define SIZEOF_LONG_DOUBLE 0 #endif +#ifndef SIZEOF_SIZE_T +/* default to 4 bytes for size_t unless defined in the config.h */ +#define SIZEOF_SIZE_T 4 +#endif + #ifdef DPRINTF_DEBUG #define HAVE_LONGLONG #define LONG_LONG long long @@ -381,11 +386,12 @@ 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) > sizeof(unsigned long)) - flags |= FLAGS_LONGLONG; - if (sizeof(size_t) > sizeof(unsigned int)) - flags |= FLAGS_LONG; - break; +#if SIZEOF_SIZE_T>4 + flags |= FLAGS_LONGLONG; +#else + flags |= FLAGS_LONG; +#endif + break; case 'O': #if SIZEOF_CURL_OFF_T > 4 flags |= FLAGS_LONGLONG; -- cgit v1.2.1 From a2ecdf42497682072b61eed4e8a6002b2e54da1b Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 10 May 2004 10:50:43 +0000 Subject: the aprintf() versions now return NULL if _any_ alloc along the way failed, previously they could return a piece of the string, making it impossible for the caller to detect errors. --- lib/mprintf.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index e8613839f..a9c375778 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -153,6 +153,8 @@ 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 curl_msprintf(char *buffer, const char *format, ...); @@ -1032,8 +1034,10 @@ static int alloc_addbyter(int output, FILE *data) if(!infop->buffer) { infop->buffer=(char *)malloc(32); - if(!infop->buffer) + if(!infop->buffer) { + infop->fail = TRUE; return -1; /* fail */ + } infop->alloc = 32; infop->len =0; } @@ -1043,6 +1047,7 @@ static int alloc_addbyter(int output, FILE *data) newptr = (char *)realloc(infop->buffer, infop->alloc*2); if(!newptr) { + infop->fail = TRUE; return -1; } infop->buffer = newptr; @@ -1065,11 +1070,12 @@ char *curl_maprintf(const char *format, ...) info.buffer = NULL; info.len = 0; info.alloc = 0; + info.fail = FALSE; va_start(ap_save, format); retcode = dprintf_formatf(&info, alloc_addbyter, format, ap_save); va_end(ap_save); - if(-1 == retcode) { + if((-1 == retcode) || info.fail) { if(info.alloc) free(info.buffer); return NULL; @@ -1090,9 +1096,10 @@ char *curl_mvaprintf(const char *format, va_list ap_save) info.buffer = NULL; info.len = 0; info.alloc = 0; + info.fail = FALSE; retcode = dprintf_formatf(&info, alloc_addbyter, format, ap_save); - if(-1 == retcode) { + if((-1 == retcode) || info.fail) { if(info.alloc) free(info.buffer); return NULL; -- cgit v1.2.1 From bbafb2eb27954c34967f91c705e74cc0c186970d Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 11 May 2004 11:30:23 +0000 Subject: curl_global_init_mem() allows the memory functions to be replaced. memory.h is included everywhere for this. --- lib/mprintf.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index a9c375778..c9264e2cd 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -55,10 +55,9 @@ #define ENABLE_64BIT #endif +#include "memory.h" /* The last #include file should be: */ -#ifdef CURLDEBUG #include "memdebug.h" -#endif #define BUFFSIZE 256 /* buffer for long-to-str and float-to-str calcs */ #define MAX_PARAMETERS 128 /* lame static limit */ -- cgit v1.2.1 From 1d7ce36791f32039383236e4940994c41cb2a0bf Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 12 May 2004 12:05:13 +0000 Subject: return faster when we "hit a wall" while printfing --- lib/mprintf.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index c9264e2cd..cd096c599 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -82,7 +82,13 @@ static const char lower_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; /* Upper-case digits. */ static const char upper_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; -#define OUTCHAR(x) done+=(stream(x, (FILE *)data)==-1?0:1) +#define OUTCHAR(x) \ + do{ \ + if(stream((unsigned char)(x), (FILE *)data) != -1) \ + done++; \ + else \ + return done; /* return immediately on failure */ \ + } while(0) /* Data type to read from the arglist */ typedef enum { @@ -582,12 +588,12 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, } static int dprintf_formatf( - void *data, /* untouched by format(), just sent to the - stream() function in the first argument */ - int (*stream)(int, FILE *), /* function pointer called for each - output character */ - const char *format, /* %-formatted string */ - va_list ap_save) /* list of parameters */ + void *data, /* untouched by format(), just sent to the stream() function in + the second argument */ + /* function pointer called for each output character */ + int (*stream)(int, FILE *), + const char *format, /* %-formatted string */ + va_list ap_save) /* list of parameters */ { /* Base-36 digits for numbers. */ const char *digits = lower_digits; @@ -983,13 +989,14 @@ static int dprintf_formatf( static int addbyter(int output, FILE *data) { struct nsprintf *infop=(struct nsprintf *)data; + unsigned char outc = (unsigned char)output; if(infop->length < infop->max) { /* only do this if we haven't reached max length yet */ - infop->buffer[0] = (char)output; /* store */ + infop->buffer[0] = outc; /* store */ infop->buffer++; /* increase pointer */ infop->length++; /* we are now one byte larger */ - return output; /* fputc() returns like this on success */ + return outc; /* fputc() returns like this on success */ } return -1; } @@ -1030,6 +1037,7 @@ int curl_msnprintf(char *buffer, size_t maxlength, const char *format, ...) static int alloc_addbyter(int output, FILE *data) { struct asprintf *infop=(struct asprintf *)data; + unsigned char outc = (unsigned char)output; if(!infop->buffer) { infop->buffer=(char *)malloc(32); @@ -1053,11 +1061,11 @@ static int alloc_addbyter(int output, FILE *data) infop->alloc *= 2; } - infop->buffer[ infop->len ] = (char)output; + infop->buffer[ infop->len ] = outc; infop->len++; - return output; /* fputc() returns like this on success */ + return outc; /* fputc() returns like this on success */ } char *curl_maprintf(const char *format, ...) @@ -1115,9 +1123,10 @@ char *curl_mvaprintf(const char *format, va_list ap_save) static int storebuffer(int output, FILE *data) { char **buffer = (char **)data; - **buffer = (char)output; + unsigned char outc = (unsigned char)output; + **buffer = outc; (*buffer)++; - return output; /* act like fputc() ! */ + return outc; /* act like fputc() ! */ } int curl_msprintf(char *buffer, const char *format, ...) -- cgit v1.2.1 From c39858aac0584716282dcb097ce9d972b43dbcb2 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 24 Jun 2004 07:43:48 +0000 Subject: Source cleanups. The major one being that we now _always_ use a Curl_addrinfo linked list for name resolved data, even on hosts/systems with only IPv4 stacks as this simplifies a lot of code. --- lib/mprintf.c | 110 +++++++++++++++++++++++++--------------------------------- 1 file changed, 48 insertions(+), 62 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index cd096c599..6e77075c2 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -142,9 +142,6 @@ typedef struct { LONG_LONG lnum; #endif double dnum; -#if 0 /*SIZEOF_LONG_DOUBLE */ - long double ldnum; -#endif } data; } va_stack_t; @@ -227,7 +224,7 @@ int dprintf_Pass1Report(va_stack_t *vto, int max) break; case FORMAT_LONGDOUBLE: type = "long double"; - break; + break; } @@ -317,7 +314,7 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, /* Handle the positional case (N$) */ param_num++; - + this_param = dprintf_DollarString(fmt, &fmt); if (0 == this_param) /* we got no positional, get the next counter */ @@ -418,7 +415,7 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, case '*': /* Special case */ flags |= FLAGS_WIDTHPARAM; param_num++; - + i = dprintf_DollarString(fmt, &fmt); if(i) width = i; @@ -471,7 +468,7 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, case 'c': vto[i].type = FORMAT_INT; flags |= FLAGS_CHAR; - break; + break; case 'f': vto[i].type = FORMAT_DOUBLE; break; @@ -486,11 +483,11 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, case 'g': vto[i].type = FORMAT_DOUBLE; flags |= FLAGS_FLOATG; - break; + break; case 'G': vto[i].type = FORMAT_DOUBLE; flags |= FLAGS_FLOATG|FLAGS_UPPER; - break; + break; default: vto[i].type = FORMAT_UNKNOWN; break; @@ -499,7 +496,7 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, vto[i].flags = flags; vto[i].width = width; vto[i].precision = precision; - + if (flags & FLAGS_WIDTHPARAM) { /* we have the width specified from a parameter, so we make that parameter's info setup properly */ @@ -508,7 +505,7 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, vto[i].type = FORMAT_WIDTH; vto[i].flags = FLAGS_NEW; vto[i].precision = vto[i].width = 0; /* can't use width or precision - of width! */ + of width! */ } if (flags & FLAGS_PRECPARAM) { /* we have the precision specified from a parameter, so we make that @@ -543,13 +540,13 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, 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_INT: #ifdef ENABLE_64BIT if(vto[i].flags & FLAGS_LONGLONG) @@ -561,23 +558,18 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, else vto[i].data.num = va_arg(arglist, int); break; - + case FORMAT_DOUBLE: -#if 0 /*SIZEOF_LONG_DOUBLE */ - if(vto[i].flags & FLAGS_LONG) - vto[i].data.ldnum = va_arg(arglist, long double); - else -#endif - vto[i].data.dnum = va_arg(arglist, 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; - + default: break; } @@ -620,21 +612,21 @@ static int dprintf_formatf( end = &endpos[0]; /* the initial end-position from the list dprintf_Pass1() created for us */ - + f = (char *)format; while (*f != '\0') { /* Format spec modifiers. */ char alt; - + /* Width of a field. */ long width; /* Precision of a field. */ long prec; - + /* Decimal integer is negative. */ char is_neg; - + /* Base of a number to be written. */ long base; @@ -645,7 +637,7 @@ static int dprintf_formatf( unsigned long num; #endif long signed_num; - + if (*f != '%') { /* This isn't a format spec, so write everything out until the next one OR end of string is reached. */ @@ -654,9 +646,9 @@ static int dprintf_formatf( } while(*++f && ('%' != *f)); continue; } - + ++f; - + /* Check for "%%". Note that although the ANSI standard lists '%' as a conversion specifier, it says "The complete format specification shall be `%%'," so we can avoid all the width @@ -675,7 +667,7 @@ static int dprintf_formatf( param = param_num; else --param; - + param_num++; /* increase this always to allow "%2$s %1$s %s" and then the third %s will pick the 3rd argument */ @@ -696,7 +688,7 @@ static int dprintf_formatf( prec = -1; alt = (p->flags & FLAGS_ALT)?TRUE:FALSE; - + switch (p->type) { case FORMAT_INT: num = p->data.num; @@ -742,26 +734,26 @@ static int dprintf_formatf( #endif { signed_num = (long) num; - + is_neg = signed_num < 0; num = is_neg ? (- signed_num) : signed_num; } goto number; - + unsigned_number:; /* Unsigned number of base BASE. */ is_neg = 0; - + number:; /* Number of base BASE. */ { char *workend = &work[sizeof(work) - 1]; char *w; - + /* Supply a default precision if none was given. */ if (prec == -1) prec = 1; - + /* Put the number in WORK. */ w = workend; while (num > 0) { @@ -770,35 +762,35 @@ static int dprintf_formatf( } width -= workend - w; prec -= workend - w; - + if (alt && base == 8 && prec <= 0) { *w-- = '0'; --width; } - + if (prec > 0) { width -= prec; while (prec-- > 0) *w-- = '0'; } - + if (alt && base == 16) width -= 2; - + 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) OUTCHAR(' '); - + if (is_neg) OUTCHAR('-'); else if (p->flags & FLAGS_SHOWSIGN) OUTCHAR('+'); else if (p->flags & FLAGS_SPACE) OUTCHAR(' '); - + if (alt && base == 16) { OUTCHAR('0'); if(p->flags & FLAGS_UPPER) @@ -810,25 +802,25 @@ static int dprintf_formatf( if (!(p->flags & FLAGS_LEFT) && (p->flags & FLAGS_PAD_NIL)) while (width-- > 0) OUTCHAR('0'); - + /* Write the number. */ while (++w <= workend) { OUTCHAR(*w); } - + if (p->flags & FLAGS_LEFT) while (width-- > 0) OUTCHAR(' '); } break; - + case FORMAT_STRING: /* String. */ { static char null[] = "(nil)"; char *str; size_t len; - + str = (char *) p->data.str; if ( str == NULL) { /* Write null[] if there's space. */ @@ -845,7 +837,7 @@ static int dprintf_formatf( } else len = strlen(str); - + if (prec != -1 && (size_t) prec < len) len = prec; width -= len; @@ -856,7 +848,7 @@ static int dprintf_formatf( if (!(p->flags&FLAGS_LEFT)) while (width-- > 0) OUTCHAR(' '); - + while (len-- > 0) OUTCHAR(*str++); if (p->flags&FLAGS_LEFT) @@ -867,7 +859,7 @@ static int dprintf_formatf( OUTCHAR('"'); } break; - + case FORMAT_PTR: /* Generic pointer. */ { @@ -886,7 +878,7 @@ static int dprintf_formatf( /* Write "(nil)" for a nil pointer. */ static char strnil[] = "(nil)"; char *point; - + width -= sizeof(strnil) - 1; if (p->flags & FLAGS_LEFT) while (width-- > 0) @@ -904,7 +896,7 @@ static int dprintf_formatf( { char formatbuf[32]="%"; char *fptr; - + width = -1; if (p->flags & FLAGS_WIDTH) width = p->width; @@ -948,13 +940,7 @@ static int dprintf_formatf( /* NOTE NOTE NOTE!! Not all sprintf() implementations returns number of output characters */ -#if 0 /*SIZEOF_LONG_DOUBLE*/ - if (p->flags & FLAGS_LONG) - /* This is for support of the 'long double' type */ - (sprintf)(work, formatbuf, p->data.ldnum); - else -#endif - (sprintf)(work, formatbuf, p->data.dnum); + (sprintf)(work, formatbuf, p->data.dnum); for(fptr=work; *fptr; fptr++) OUTCHAR(*fptr); @@ -990,7 +976,7 @@ static int addbyter(int output, FILE *data) { struct nsprintf *infop=(struct nsprintf *)data; unsigned char outc = (unsigned char)output; - + if(infop->length < infop->max) { /* only do this if we haven't reached max length yet */ infop->buffer[0] = outc; /* store */ @@ -1038,7 +1024,7 @@ static int alloc_addbyter(int output, FILE *data) { struct asprintf *infop=(struct asprintf *)data; unsigned char outc = (unsigned char)output; - + if(!infop->buffer) { infop->buffer=(char *)malloc(32); if(!infop->buffer) { @@ -1195,7 +1181,7 @@ int main() #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); -- cgit v1.2.1 From feb2dd283533f842c9b6e4cc2fcc7fd35638d5a0 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 24 Jun 2004 11:54:11 +0000 Subject: Replaced all uses of sprintf() with the safer snprintf(). It is just a precaution to prevent mistakes to lead to buffer overflows. --- lib/mprintf.c | 801 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 404 insertions(+), 397 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 6e77075c2..9135a1308 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -82,7 +82,7 @@ static const char lower_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; /* Upper-case digits. */ static const char upper_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; -#define OUTCHAR(x) \ +#define OUTCHAR(x) \ do{ \ if(stream((unsigned char)(x), (FILE *)data) != -1) \ done++; \ @@ -234,45 +234,45 @@ int dprintf_Pass1Report(va_stack_t *vto, int max) flags = vto[i].flags & (1< max_param) - max_param = this_param; + max_param = this_param; /* * The parameter with number 'i' should be used. Next, we need @@ -335,59 +335,59 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, /* Handle the flags */ while (dprintf_IsQualifierNoDollar(*fmt)) { - switch (*fmt++) { - case ' ': - flags |= FLAGS_SPACE; - break; - case '+': - flags |= FLAGS_SHOWSIGN; - break; - case '-': - flags |= FLAGS_LEFT; - flags &= ~FLAGS_PAD_NIL; - break; - case '#': - flags |= FLAGS_ALT; - break; - case '.': - flags |= FLAGS_PREC; - if ('*' == *fmt) { - /* The precision is picked from a specified parameter */ - - flags |= FLAGS_PRECPARAM; - fmt++; - param_num++; - - i = dprintf_DollarString(fmt, &fmt); - if (i) - precision = i; - else - precision = param_num; - - if (precision > max_param) - max_param = precision; - } - else { - flags |= FLAGS_PREC; - precision = strtol(fmt, &fmt, 10); - } - break; - case 'h': - flags |= FLAGS_SHORT; - break; - case 'l': - if (flags & FLAGS_LONG) - flags |= FLAGS_LONGLONG; - else - flags |= FLAGS_LONG; - break; - case 'L': - flags |= FLAGS_LONGDOUBLE; - break; - case 'q': - flags |= FLAGS_LONGLONG; - break; - case 'z': + switch (*fmt++) { + case ' ': + flags |= FLAGS_SPACE; + break; + case '+': + flags |= FLAGS_SHOWSIGN; + break; + case '-': + flags |= FLAGS_LEFT; + flags &= ~FLAGS_PAD_NIL; + break; + case '#': + flags |= FLAGS_ALT; + break; + case '.': + flags |= FLAGS_PREC; + if ('*' == *fmt) { + /* The precision is picked from a specified parameter */ + + flags |= FLAGS_PRECPARAM; + fmt++; + param_num++; + + i = dprintf_DollarString(fmt, &fmt); + if (i) + precision = i; + else + precision = param_num; + + if (precision > max_param) + max_param = precision; + } + else { + flags |= FLAGS_PREC; + precision = strtol(fmt, &fmt, 10); + } + break; + case 'h': + flags |= FLAGS_SHORT; + break; + case 'l': + if (flags & FLAGS_LONG) + flags |= FLAGS_LONGLONG; + else + flags |= FLAGS_LONG; + break; + case 'L': + flags |= FLAGS_LONGDOUBLE; + break; + case 'q': + flags |= FLAGS_LONGLONG; + break; + case 'z': /* the code below generates a warning if -Wunreachable-code is used */ #if SIZEOF_SIZE_T>4 @@ -403,30 +403,30 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, flags |= FLAGS_LONG; #endif break; - case '0': - if (!(flags & FLAGS_LEFT)) - flags |= FLAGS_PAD_NIL; - /* FALLTHROUGH */ - case '1': case '2': case '3': case '4': - case '5': case '6': case '7': case '8': case '9': - flags |= FLAGS_WIDTH; - width = strtol(fmt-1, &fmt, 10); - break; - case '*': /* Special case */ - flags |= FLAGS_WIDTHPARAM; - param_num++; - - i = dprintf_DollarString(fmt, &fmt); - if(i) - width = i; - else - width = param_num; - if(width > max_param) - max_param=width; - break; - default: - break; - } + case '0': + if (!(flags & FLAGS_LEFT)) + flags |= FLAGS_PAD_NIL; + /* FALLTHROUGH */ + case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + flags |= FLAGS_WIDTH; + width = strtol(fmt-1, &fmt, 10); + break; + case '*': /* Special case */ + flags |= FLAGS_WIDTHPARAM; + param_num++; + + i = dprintf_DollarString(fmt, &fmt); + if(i) + width = i; + else + width = param_num; + if(width > max_param) + max_param=width; + break; + default: + break; + } } /* switch */ /* Handle the specifier */ @@ -435,62 +435,62 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, switch (*fmt) { case 'S': - flags |= FLAGS_ALT; - /* FALLTHROUGH */ + flags |= FLAGS_ALT; + /* FALLTHROUGH */ case 's': - vto[i].type = FORMAT_STRING; - break; + vto[i].type = FORMAT_STRING; + break; case 'n': - vto[i].type = FORMAT_INTPTR; - break; + vto[i].type = FORMAT_INTPTR; + break; case 'p': - vto[i].type = FORMAT_PTR; - break; + vto[i].type = FORMAT_PTR; + break; case 'd': case 'i': - vto[i].type = FORMAT_INT; - break; + vto[i].type = FORMAT_INT; + break; case 'u': - vto[i].type = FORMAT_INT; - flags |= FLAGS_UNSIGNED; - break; + vto[i].type = FORMAT_INT; + flags |= FLAGS_UNSIGNED; + break; case 'o': - vto[i].type = FORMAT_INT; - flags |= FLAGS_OCTAL; - break; + vto[i].type = FORMAT_INT; + flags |= FLAGS_OCTAL; + break; case 'x': - vto[i].type = FORMAT_INT; - flags |= FLAGS_HEX; - break; + vto[i].type = FORMAT_INT; + flags |= FLAGS_HEX; + break; case 'X': - vto[i].type = FORMAT_INT; - flags |= FLAGS_HEX|FLAGS_UPPER; - break; + vto[i].type = FORMAT_INT; + flags |= FLAGS_HEX|FLAGS_UPPER; + break; case 'c': - vto[i].type = FORMAT_INT; - flags |= FLAGS_CHAR; - break; + vto[i].type = FORMAT_INT; + flags |= FLAGS_CHAR; + break; case 'f': - vto[i].type = FORMAT_DOUBLE; - break; + vto[i].type = FORMAT_DOUBLE; + break; case 'e': - vto[i].type = FORMAT_DOUBLE; - flags |= FLAGS_FLOATE; - break; + vto[i].type = FORMAT_DOUBLE; + flags |= FLAGS_FLOATE; + break; case 'E': - vto[i].type = FORMAT_DOUBLE; - flags |= FLAGS_FLOATE|FLAGS_UPPER; - break; + vto[i].type = FORMAT_DOUBLE; + flags |= FLAGS_FLOATE|FLAGS_UPPER; + break; case 'g': - vto[i].type = FORMAT_DOUBLE; - flags |= FLAGS_FLOATG; - break; + vto[i].type = FORMAT_DOUBLE; + flags |= FLAGS_FLOATG; + break; case 'G': - vto[i].type = FORMAT_DOUBLE; - flags |= FLAGS_FLOATG|FLAGS_UPPER; - break; + vto[i].type = FORMAT_DOUBLE; + flags |= FLAGS_FLOATG|FLAGS_UPPER; + break; default: - vto[i].type = FORMAT_UNKNOWN; - break; + vto[i].type = FORMAT_UNKNOWN; + break; } /* switch */ vto[i].flags = flags; @@ -498,24 +498,24 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, vto[i].precision = precision; 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; - i = width - 1; - vto[i].type = FORMAT_WIDTH; - vto[i].flags = FLAGS_NEW; - vto[i].precision = vto[i].width = 0; /* can't use width or precision - of width! */ + /* we have the width specified from a parameter, so we make that + parameter's info setup properly */ + vto[i].width = width - 1; + i = width - 1; + vto[i].type = FORMAT_WIDTH; + vto[i].flags = FLAGS_NEW; + vto[i].precision = vto[i].width = 0; /* can't use width or precision + of width! */ } 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; - i = precision - 1; - vto[i].type = FORMAT_WIDTH; - vto[i].flags = FLAGS_NEW; - vto[i].precision = vto[i].width = 0; /* can't use width or precision - of width! */ + /* we have the precision specified from a parameter, so we make that + parameter's info setup properly */ + vto[i].precision = precision - 1; + i = precision - 1; + vto[i].type = FORMAT_WIDTH; + vto[i].flags = FLAGS_NEW; + vto[i].precision = vto[i].width = 0; /* can't use width or precision + of width! */ } *endpos++ = fmt + 1; /* end of this sequence */ } @@ -529,49 +529,49 @@ static long dprintf_Pass1(char *format, va_stack_t *vto, char **endpos, for (i=0; idata.num; if(p->flags & FLAGS_CHAR) { - /* Character. */ - if (!(p->flags & FLAGS_LEFT)) - while (--width > 0) - OUTCHAR(' '); - OUTCHAR((char) num); - if (p->flags & FLAGS_LEFT) - while (--width > 0) - OUTCHAR(' '); - break; + /* Character. */ + if (!(p->flags & FLAGS_LEFT)) + while (--width > 0) + OUTCHAR(' '); + OUTCHAR((char) num); + if (p->flags & FLAGS_LEFT) + while (--width > 0) + OUTCHAR(' '); + break; } if(p->flags & FLAGS_UNSIGNED) { - /* Decimal unsigned integer. */ - base = 10; - goto unsigned_number; + /* Decimal unsigned integer. */ + base = 10; + goto unsigned_number; } if(p->flags & FLAGS_OCTAL) { - /* Octal unsigned integer. */ - base = 8; - goto unsigned_number; + /* Octal unsigned integer. */ + base = 8; + goto unsigned_number; } if(p->flags & FLAGS_HEX) { - /* Hexadecimal unsigned integer. */ + /* Hexadecimal unsigned integer. */ - digits = (p->flags & FLAGS_UPPER)? upper_digits : lower_digits; - base = 16; - goto unsigned_number; + digits = (p->flags & FLAGS_UPPER)? upper_digits : lower_digits; + base = 16; + goto unsigned_number; } /* Decimal integer. */ @@ -727,223 +727,230 @@ static int dprintf_formatf( #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; + 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; + signed_num = (long) num; + is_neg = signed_num < 0; + num = is_neg ? (- signed_num) : signed_num; } goto number; - unsigned_number:; + unsigned_number: /* Unsigned number of base BASE. */ is_neg = 0; - number:; + number: /* Number of base BASE. */ { - char *workend = &work[sizeof(work) - 1]; - char *w; - - /* Supply a default precision if none was given. */ - if (prec == -1) - prec = 1; - - /* Put the number in WORK. */ - w = workend; - while (num > 0) { - *w-- = digits[num % base]; - num /= base; - } - width -= workend - w; - prec -= workend - w; - - if (alt && base == 8 && prec <= 0) { - *w-- = '0'; - --width; - } - - if (prec > 0) { - width -= prec; - while (prec-- > 0) - *w-- = '0'; - } - - if (alt && base == 16) - width -= 2; - - 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) - OUTCHAR(' '); - - if (is_neg) - OUTCHAR('-'); - else if (p->flags & FLAGS_SHOWSIGN) - OUTCHAR('+'); - else if (p->flags & FLAGS_SPACE) - OUTCHAR(' '); - - if (alt && base == 16) { - OUTCHAR('0'); - if(p->flags & FLAGS_UPPER) - OUTCHAR('X'); - else - OUTCHAR('x'); - } - - if (!(p->flags & FLAGS_LEFT) && (p->flags & FLAGS_PAD_NIL)) - while (width-- > 0) - OUTCHAR('0'); - - /* Write the number. */ - while (++w <= workend) { - OUTCHAR(*w); - } - - if (p->flags & FLAGS_LEFT) - while (width-- > 0) - OUTCHAR(' '); + char *workend = &work[sizeof(work) - 1]; + char *w; + + /* Supply a default precision if none was given. */ + if (prec == -1) + prec = 1; + + /* Put the number in WORK. */ + w = workend; + while (num > 0) { + *w-- = digits[num % base]; + num /= base; + } + width -= workend - w; + prec -= workend - w; + + if (alt && base == 8 && prec <= 0) { + *w-- = '0'; + --width; + } + + if (prec > 0) { + width -= prec; + while (prec-- > 0) + *w-- = '0'; + } + + if (alt && base == 16) + width -= 2; + + 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) + OUTCHAR(' '); + + if (is_neg) + OUTCHAR('-'); + else if (p->flags & FLAGS_SHOWSIGN) + OUTCHAR('+'); + else if (p->flags & FLAGS_SPACE) + OUTCHAR(' '); + + if (alt && base == 16) { + OUTCHAR('0'); + if(p->flags & FLAGS_UPPER) + OUTCHAR('X'); + else + OUTCHAR('x'); + } + + if (!(p->flags & FLAGS_LEFT) && (p->flags & FLAGS_PAD_NIL)) + while (width-- > 0) + OUTCHAR('0'); + + /* Write the number. */ + while (++w <= workend) { + OUTCHAR(*w); + } + + if (p->flags & FLAGS_LEFT) + while (width-- > 0) + OUTCHAR(' '); } break; case FORMAT_STRING: - /* String. */ + /* String. */ { - static char null[] = "(nil)"; - char *str; - size_t len; - - str = (char *) p->data.str; - if ( str == NULL) { - /* Write null[] if there's space. */ - if (prec == -1 || prec >= (long) sizeof(null) - 1) { - str = null; - len = sizeof(null) - 1; - /* Disable quotes around (nil) */ - p->flags &= (~FLAGS_ALT); - } - else { - str = (char *)""; - len = 0; - } - } - else - len = strlen(str); - - if (prec != -1 && (size_t) prec < len) - len = prec; - width -= len; - - if (p->flags & FLAGS_ALT) - OUTCHAR('"'); - - if (!(p->flags&FLAGS_LEFT)) - while (width-- > 0) - OUTCHAR(' '); - - while (len-- > 0) - OUTCHAR(*str++); - if (p->flags&FLAGS_LEFT) - while (width-- > 0) - OUTCHAR(' '); - - if (p->flags & FLAGS_ALT) - OUTCHAR('"'); + static char null[] = "(nil)"; + char *str; + size_t len; + + str = (char *) p->data.str; + if ( str == NULL) { + /* Write null[] if there's space. */ + if (prec == -1 || prec >= (long) sizeof(null) - 1) { + str = null; + len = sizeof(null) - 1; + /* Disable quotes around (nil) */ + p->flags &= (~FLAGS_ALT); + } + else { + str = (char *)""; + len = 0; + } + } + else + len = strlen(str); + + if (prec != -1 && (size_t) prec < len) + len = prec; + width -= len; + + if (p->flags & FLAGS_ALT) + OUTCHAR('"'); + + if (!(p->flags&FLAGS_LEFT)) + while (width-- > 0) + OUTCHAR(' '); + + while (len-- > 0) + OUTCHAR(*str++); + if (p->flags&FLAGS_LEFT) + while (width-- > 0) + OUTCHAR(' '); + + if (p->flags & FLAGS_ALT) + OUTCHAR('"'); } break; case FORMAT_PTR: /* Generic pointer. */ { - void *ptr; - ptr = (void *) p->data.ptr; - 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; - num = (unsigned long) ptr; - is_neg = 0; - goto number; - } - else { - /* Write "(nil)" for a nil pointer. */ - static char strnil[] = "(nil)"; - char *point; - - width -= sizeof(strnil) - 1; - if (p->flags & FLAGS_LEFT) - while (width-- > 0) - OUTCHAR(' '); - for (point = strnil; *point != '\0'; ++point) - OUTCHAR(*point); - if (! (p->flags & FLAGS_LEFT)) - while (width-- > 0) - OUTCHAR(' '); - } + void *ptr; + ptr = (void *) p->data.ptr; + 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; + num = (unsigned long) ptr; + is_neg = 0; + goto number; + } + else { + /* Write "(nil)" for a nil pointer. */ + static char strnil[] = "(nil)"; + char *point; + + width -= sizeof(strnil) - 1; + if (p->flags & FLAGS_LEFT) + while (width-- > 0) + OUTCHAR(' '); + for (point = strnil; *point != '\0'; ++point) + OUTCHAR(*point); + if (! (p->flags & FLAGS_LEFT)) + while (width-- > 0) + OUTCHAR(' '); + } } break; case FORMAT_DOUBLE: { - char formatbuf[32]="%"; - char *fptr; - - width = -1; - if (p->flags & FLAGS_WIDTH) - width = p->width; - else if (p->flags & FLAGS_WIDTHPARAM) - width = vto[p->width].data.num; - - prec = -1; - if (p->flags & FLAGS_PREC) - prec = p->precision; - else if (p->flags & FLAGS_PRECPARAM) - prec = vto[p->precision].data.num; - - 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, "#"); - - fptr=&formatbuf[strlen(formatbuf)]; - - if(width >= 0) { - /* RECURSIVE USAGE */ - fptr += curl_msprintf(fptr, "%ld", width); - } - if(prec >= 0) { - /* RECURSIVE USAGE */ - fptr += curl_msprintf(fptr, ".%ld", prec); - } - if (p->flags & FLAGS_LONG) - strcat(fptr, "l"); - - if (p->flags & FLAGS_FLOATE) - strcat(fptr, p->flags&FLAGS_UPPER?"E":"e"); - else if (p->flags & FLAGS_FLOATG) - strcat(fptr, (p->flags & FLAGS_UPPER) ? "G" : "g"); - else - strcat(fptr, "f"); - - /* NOTE NOTE NOTE!! Not all sprintf() implementations returns number - of output characters */ + char formatbuf[32]="%"; + char *fptr; + size_t left = sizeof(formatbuf)-strlen(formatbuf); + int len; + + width = -1; + if (p->flags & FLAGS_WIDTH) + width = p->width; + else if (p->flags & FLAGS_WIDTHPARAM) + width = vto[p->width].data.num; + + prec = -1; + if (p->flags & FLAGS_PREC) + prec = p->precision; + else if (p->flags & FLAGS_PRECPARAM) + prec = vto[p->precision].data.num; + + 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, "#"); + + fptr=&formatbuf[strlen(formatbuf)]; + + if(width >= 0) { + /* RECURSIVE USAGE */ + len = curl_msnprintf(fptr, left, "%ld", width); + fptr += len; + left -= len; + } + if(prec >= 0) { + /* RECURSIVE USAGE */ + len = curl_msnprintf(fptr, left, ".%ld", prec); + fptr += len; + left -= len; + } + 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'; + else + *fptr++ = 'f'; + + *fptr = 0; /* and a final zero termination */ + + /* NOTE NOTE NOTE!! Not all sprintf() implementations returns number + of output characters */ (sprintf)(work, formatbuf, p->data.dnum); - for(fptr=work; *fptr; fptr++) - OUTCHAR(*fptr); + for(fptr=work; *fptr; fptr++) + OUTCHAR(*fptr); } break; @@ -951,15 +958,15 @@ static int dprintf_formatf( /* Answer the count of characters written. */ #ifdef ENABLE_64BIT if (p->flags & FLAGS_LONGLONG) - *(LONG_LONG *) p->data.ptr = (LONG_LONG)done; + *(LONG_LONG *) p->data.ptr = (LONG_LONG)done; else #endif - if (p->flags & FLAGS_LONG) - *(long *) p->data.ptr = (long)done; + if (p->flags & FLAGS_LONG) + *(long *) p->data.ptr = (long)done; else if (!(p->flags & FLAGS_SHORT)) - *(int *) p->data.ptr = (int)done; + *(int *) p->data.ptr = (int)done; else - *(short *) p->data.ptr = (short)done; + *(short *) p->data.ptr = (short)done; break; default: -- cgit v1.2.1 From 24d47a6e07304cf0921f2d30734b3c64360773c3 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 2 Nov 2004 10:12:22 +0000 Subject: Paul Nolan fix to make libcurl build nicely on Windows CE --- lib/mprintf.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 9135a1308..629a9e07c 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -40,6 +40,12 @@ #include +#ifdef _WIN32_WCE +#define CURL_CDECL __cdecl +#else +#define CURL_CDECL +#endif + #ifndef SIZEOF_LONG_DOUBLE #define SIZEOF_LONG_DOUBLE 0 #endif @@ -583,7 +589,12 @@ static int dprintf_formatf( void *data, /* untouched by format(), just sent to the stream() function in the second argument */ /* function pointer called for each output character */ + +#if _WIN32_WCE + int (__cdecl *stream) (int, FILE *), +#else int (*stream)(int, FILE *), +#endif const char *format, /* %-formatted string */ va_list ap_save) /* list of parameters */ { @@ -979,7 +990,7 @@ static int dprintf_formatf( } /* fputc() look-alike */ -static int addbyter(int output, FILE *data) +static int CURL_CDECL addbyter(int output, FILE *data) { struct nsprintf *infop=(struct nsprintf *)data; unsigned char outc = (unsigned char)output; @@ -1027,7 +1038,7 @@ int curl_msnprintf(char *buffer, size_t maxlength, const char *format, ...) } /* fputc() look-alike */ -static int alloc_addbyter(int output, FILE *data) +static int CURL_CDECL alloc_addbyter(int output, FILE *data) { struct asprintf *infop=(struct asprintf *)data; unsigned char outc = (unsigned char)output; @@ -1113,7 +1124,7 @@ char *curl_mvaprintf(const char *format, va_list ap_save) return strdup(""); } -static int storebuffer(int output, FILE *data) +static int CURL_CDECL storebuffer(int output, FILE *data) { char **buffer = (char **)data; unsigned char outc = (unsigned char)output; @@ -1142,6 +1153,7 @@ int curl_mprintf(const char *format, ...) int retcode; va_list ap_save; /* argument pointer */ va_start(ap_save, format); + retcode = dprintf_formatf(stdout, fputc, format, ap_save); va_end(ap_save); return retcode; -- cgit v1.2.1 From 4cd5220d27883537669ed29b762406e08d50d1b6 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 2 Nov 2004 14:02:29 +0000 Subject: use ifdef not if --- lib/mprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 629a9e07c..4650961d1 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -590,7 +590,7 @@ static int dprintf_formatf( the second argument */ /* function pointer called for each output character */ -#if _WIN32_WCE +#ifdef _WIN32_WCE int (__cdecl *stream) (int, FILE *), #else int (*stream)(int, FILE *), -- cgit v1.2.1 From f471a293ea995aa071e016d25388e9e26e43b6c0 Mon Sep 17 00:00:00 2001 From: Gisle Vanem Date: Mon, 8 Nov 2004 14:20:14 +0000 Subject: Un-do changes for WinCE; cdecl decoration is not needed. Confirmed by Paul Nolan. --- lib/mprintf.c | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 4650961d1..a9b601c9c 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -40,12 +40,6 @@ #include -#ifdef _WIN32_WCE -#define CURL_CDECL __cdecl -#else -#define CURL_CDECL -#endif - #ifndef SIZEOF_LONG_DOUBLE #define SIZEOF_LONG_DOUBLE 0 #endif @@ -589,12 +583,7 @@ static int dprintf_formatf( void *data, /* untouched by format(), just sent to the stream() function in the second argument */ /* function pointer called for each output character */ - -#ifdef _WIN32_WCE - int (__cdecl *stream) (int, FILE *), -#else int (*stream)(int, FILE *), -#endif const char *format, /* %-formatted string */ va_list ap_save) /* list of parameters */ { @@ -990,7 +979,7 @@ static int dprintf_formatf( } /* fputc() look-alike */ -static int CURL_CDECL addbyter(int output, FILE *data) +static int addbyter(int output, FILE *data) { struct nsprintf *infop=(struct nsprintf *)data; unsigned char outc = (unsigned char)output; @@ -1038,7 +1027,7 @@ int curl_msnprintf(char *buffer, size_t maxlength, const char *format, ...) } /* fputc() look-alike */ -static int CURL_CDECL alloc_addbyter(int output, FILE *data) +static int alloc_addbyter(int output, FILE *data) { struct asprintf *infop=(struct asprintf *)data; unsigned char outc = (unsigned char)output; @@ -1124,7 +1113,7 @@ char *curl_mvaprintf(const char *format, va_list ap_save) return strdup(""); } -static int CURL_CDECL storebuffer(int output, FILE *data) +static int storebuffer(int output, FILE *data) { char **buffer = (char **)data; unsigned char outc = (unsigned char)output; -- cgit v1.2.1 From 358e08b95d57a1f688995b3b30a607645f01f80a Mon Sep 17 00:00:00 2001 From: Dan Fandrich Date: Tue, 14 Dec 2004 20:17:58 +0000 Subject: Removed fputc() prototype since it's already in stdio.h --- lib/mprintf.c | 4 ---- 1 file changed, 4 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index a9b601c9c..56bede316 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -1133,10 +1133,6 @@ int curl_msprintf(char *buffer, const char *format, ...) return retcode; } -#ifndef WIN32 /* not needed on win32 */ -extern int fputc(int, FILE *); -#endif - int curl_mprintf(const char *format, ...) { int retcode; -- cgit v1.2.1 From 1ba47e7af9edfa682faba73df8bf0dc240facb19 Mon Sep 17 00:00:00 2001 From: Dan Fandrich Date: Wed, 15 Dec 2004 01:38:25 +0000 Subject: Add 'const' to immutable arrays. --- lib/mprintf.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 56bede316..30f0f7aa5 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -816,8 +816,8 @@ static int dprintf_formatf( case FORMAT_STRING: /* String. */ { - static char null[] = "(nil)"; - char *str; + static const char null[] = "(nil)"; + const char *str; size_t len; str = (char *) p->data.str; @@ -830,7 +830,7 @@ static int dprintf_formatf( p->flags &= (~FLAGS_ALT); } else { - str = (char *)""; + str = ""; len = 0; } } @@ -875,8 +875,8 @@ static int dprintf_formatf( } else { /* Write "(nil)" for a nil pointer. */ - static char strnil[] = "(nil)"; - char *point; + static const char strnil[] = "(nil)"; + const char *point; width -= sizeof(strnil) - 1; if (p->flags & FLAGS_LEFT) -- cgit v1.2.1 From 6b1220b61d5ed2481dbf31714a68be6ef6eed3da Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 26 Apr 2005 13:08:49 +0000 Subject: Cory Nelson's work on nuking compiler warnings when building on x64 with VS2005. --- lib/mprintf.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 30f0f7aa5..b9fe238f9 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -759,8 +759,8 @@ static int dprintf_formatf( *w-- = digits[num % base]; num /= base; } - width -= workend - w; - prec -= workend - w; + width -= (long)(workend - w); + prec -= (long)(workend - w); if (alt && base == 8 && prec <= 0) { *w-- = '0'; @@ -839,7 +839,7 @@ static int dprintf_formatf( if (prec != -1 && (size_t) prec < len) len = prec; - width -= len; + width -= (long)len; if (p->flags & FLAGS_ALT) OUTCHAR('"'); @@ -869,7 +869,7 @@ static int dprintf_formatf( base = 16; digits = (p->flags & FLAGS_UPPER)? upper_digits : lower_digits; alt = 1; - num = (unsigned long) ptr; + num = (size_t) ptr; is_neg = 0; goto number; } -- cgit v1.2.1 From 16bbd13af7244cc0967d9f12327f63ffcaeac4bd Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 13 Jul 2005 18:06:40 +0000 Subject: Diego Casorran patches to make (lib)curl build fine on Amiga again --- lib/mprintf.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index b9fe238f9..598ea7626 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -75,6 +75,9 @@ # define BOOL char #endif +#ifdef _AMIGASF +# undef FORMAT_INT +#endif /* Lower-case digits. */ static const char lower_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; -- cgit v1.2.1 From c6ae0ebcbf3c7b47ab444195613c6e14843fa248 Mon Sep 17 00:00:00 2001 From: Gisle Vanem Date: Wed, 5 Jul 2006 14:23:09 +0000 Subject: Cludge fix for djgpp 2.03 or older; it doesn't have snprintf() etc. So avoid using x_was_used(). --- lib/mprintf.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 598ea7626..475afb770 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -38,6 +38,10 @@ #include #include +#if defined(DJGPP) && (DJGPP_MINOR < 4) +#undef CURLDEBUG /* don't use x_was_used() here */ +#endif + #include #ifndef SIZEOF_LONG_DOUBLE -- cgit v1.2.1 From 4f012ad703f6ed8704365a7a12cf9d50bb0b085c Mon Sep 17 00:00:00 2001 From: Gisle Vanem Date: Thu, 6 Jul 2006 13:33:56 +0000 Subject: Undefine correct symbol. --- lib/mprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 475afb770..543a39f15 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -39,7 +39,7 @@ #include #if defined(DJGPP) && (DJGPP_MINOR < 4) -#undef CURLDEBUG /* don't use x_was_used() here */ +#undef _MPRINTF_REPLACE /* don't use x_was_used() here */ #endif #include -- cgit v1.2.1 From 44d84ac1646cf04ccc2c1a736f3c9d1644ccacec Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 17 Oct 2006 21:32:56 +0000 Subject: Avoid typecasting a signed char to an int when using is*() functions, as that could very well cause a negate number get passed in and thus cause reading outside of the array usually used for this purpose. We avoid this by using the uppercase macro versions introduced just now that does some extra crazy typecasts to avoid byte codes > 127 to cause negative int values. --- lib/mprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 543a39f15..610395318 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -171,7 +171,7 @@ int curl_msprintf(char *buffer, const char *format, ...); static long dprintf_DollarString(char *input, char **end) { int number=0; - while(isdigit((int)*input)) { + while(ISDIGIT(*input)) { number *= 10; number += *input-'0'; input++; -- cgit v1.2.1 From 54db98c220255737456b1311cb9534c9e95215c6 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Thu, 1 Feb 2007 01:42:13 +0000 Subject: compiler warning fix --- lib/mprintf.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 610395318..3224521b0 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -694,7 +694,7 @@ static int dprintf_formatf( else prec = -1; - alt = (p->flags & FLAGS_ALT)?TRUE:FALSE; + alt = (char)((p->flags & FLAGS_ALT)?TRUE:FALSE); switch (p->type) { case FORMAT_INT: @@ -734,14 +734,14 @@ static int dprintf_formatf( #ifdef ENABLE_64BIT if(p->flags & FLAGS_LONGLONG) { /* long long */ - is_neg = p->data.lnum < 0; + is_neg = (char)(p->data.lnum < 0); num = is_neg ? (- p->data.lnum) : p->data.lnum; } else #endif { signed_num = (long) num; - is_neg = signed_num < 0; + is_neg = (char)(signed_num < 0); num = is_neg ? (- signed_num) : signed_num; } goto number; @@ -944,9 +944,9 @@ static int dprintf_formatf( *fptr++ = 'l'; if (p->flags & FLAGS_FLOATE) - *fptr++ = p->flags&FLAGS_UPPER ? 'E':'e'; + *fptr++ = (char)((p->flags & FLAGS_UPPER) ? 'E':'e'); else if (p->flags & FLAGS_FLOATG) - *fptr++ = p->flags & FLAGS_UPPER ? 'G' : 'g'; + *fptr++ = (char)((p->flags & FLAGS_UPPER) ? 'G' : 'g'); else *fptr++ = 'f'; -- cgit v1.2.1 From c514a2a89aa1c1e06b70405eedb4e1f70b27fd10 Mon Sep 17 00:00:00 2001 From: Gisle Vanem Date: Mon, 26 Feb 2007 04:24:26 +0000 Subject: Removed inclusion of and in .c-files since they're already included through "setup.h". --- lib/mprintf.c | 1 - 1 file changed, 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 3224521b0..55a6f6295 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -31,7 +31,6 @@ #include "setup.h" -#include #include #include #include -- cgit v1.2.1 From be8a5d0aef8eef4236d7cd6ce2cd7eabf74006f4 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Wed, 28 Feb 2007 14:45:48 +0000 Subject: proper symbol definition check for all AmigaOS flavours --- lib/mprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 55a6f6295..10a8ad940 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -78,7 +78,7 @@ # define BOOL char #endif -#ifdef _AMIGASF +#ifdef __AMIGA__ # undef FORMAT_INT #endif -- cgit v1.2.1 From 3f62bfb61d776d8f9095c13f4cd004dd32cb3cca Mon Sep 17 00:00:00 2001 From: Gunter Knauf Date: Thu, 9 Aug 2007 21:05:05 +0000 Subject: fixed a warning which MingW gcc 4.2.1. --- lib/mprintf.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 10a8ad940..f0a383210 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -296,10 +296,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; @@ -614,7 +614,7 @@ 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 */ -- cgit v1.2.1 From ad6e28073c985a42e8b15d2234baa7ef67ffcb35 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 5 Nov 2007 09:45:09 +0000 Subject: removed space after if and while before the parenthesis for better source code consistency --- lib/mprintf.c | 136 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 68 insertions(+), 68 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index f0a383210..b3fc33922 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -308,9 +308,9 @@ static long dprintf_Pass1(const 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 */ } @@ -322,11 +322,11 @@ static long dprintf_Pass1(const 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; /* @@ -340,7 +340,7 @@ static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos, /* Handle the flags */ - while (dprintf_IsQualifierNoDollar(*fmt)) { + while(dprintf_IsQualifierNoDollar(*fmt)) { switch (*fmt++) { case ' ': flags |= FLAGS_SPACE; @@ -357,7 +357,7 @@ static long dprintf_Pass1(const 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; @@ -365,12 +365,12 @@ static long dprintf_Pass1(const 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,7 +382,7 @@ static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos, flags |= FLAGS_SHORT; break; case 'l': - if (flags & FLAGS_LONG) + if(flags & FLAGS_LONG) flags |= FLAGS_LONGLONG; else flags |= FLAGS_LONG; @@ -410,7 +410,7 @@ static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos, #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': @@ -503,7 +503,7 @@ static long dprintf_Pass1(const 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; @@ -513,7 +513,7 @@ static long dprintf_Pass1(const 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; @@ -533,7 +533,7 @@ static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos, /* Read the arg list parameters into our data list */ for (i=0; idata.num; 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; } @@ -756,47 +756,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(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(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(alt && base == 16) { OUTCHAR('0'); if(p->flags & FLAGS_UPPER) OUTCHAR('X'); @@ -804,17 +804,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; @@ -827,9 +827,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) */ @@ -843,24 +843,24 @@ static int dprintf_formatf( else len = strlen(str); - if (prec != -1 && (size_t) prec < len) + 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) 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; @@ -870,7 +870,7 @@ 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; @@ -885,13 +885,13 @@ static int dprintf_formatf( const char *point; width -= sizeof(strnil) - 1; - if (p->flags & FLAGS_LEFT) - while (width-- > 0) + if(p->flags & FLAGS_LEFT) + while(width-- > 0) OUTCHAR(' '); 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(' '); } } @@ -905,24 +905,24 @@ static int dprintf_formatf( int len; width = -1; - if (p->flags & FLAGS_WIDTH) + if(p->flags & FLAGS_WIDTH) width = p->width; - else if (p->flags & FLAGS_WIDTHPARAM) + else if(p->flags & FLAGS_WIDTHPARAM) width = vto[p->width].data.num; prec = -1; - if (p->flags & FLAGS_PREC) + if(p->flags & FLAGS_PREC) prec = p->precision; - else if (p->flags & FLAGS_PRECPARAM) + else if(p->flags & FLAGS_PRECPARAM) prec = vto[p->precision].data.num; - if (p->flags & FLAGS_LEFT) + if(p->flags & FLAGS_LEFT) strcat(formatbuf, "-"); - if (p->flags & FLAGS_SHOWSIGN) + if(p->flags & FLAGS_SHOWSIGN) strcat(formatbuf, "+"); - if (p->flags & FLAGS_SPACE) + if(p->flags & FLAGS_SPACE) strcat(formatbuf, " "); - if (p->flags & FLAGS_ALT) + if(p->flags & FLAGS_ALT) strcat(formatbuf, "#"); fptr=&formatbuf[strlen(formatbuf)]; @@ -939,12 +939,12 @@ static int dprintf_formatf( fptr += len; left -= len; } - if (p->flags & FLAGS_LONG) + if(p->flags & FLAGS_LONG) *fptr++ = 'l'; - if (p->flags & FLAGS_FLOATE) + if(p->flags & FLAGS_FLOATE) *fptr++ = (char)((p->flags & FLAGS_UPPER) ? 'E':'e'); - else if (p->flags & FLAGS_FLOATG) + else if(p->flags & FLAGS_FLOATG) *fptr++ = (char)((p->flags & FLAGS_UPPER) ? 'G' : 'g'); else *fptr++ = 'f'; @@ -963,13 +963,13 @@ static int dprintf_formatf( case FORMAT_INTPTR: /* Answer the count of characters written. */ #ifdef ENABLE_64BIT - if (p->flags & FLAGS_LONGLONG) + if(p->flags & FLAGS_LONGLONG) *(LONG_LONG *) p->data.ptr = (LONG_LONG)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; -- cgit v1.2.1 From 2f928797cf18d41d4ae0114958cfedac3fcd6c13 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 20 Nov 2007 10:03:33 +0000 Subject: fix the treatment of the parameter-based precision, as in "%.*s%s" as previously the second %s would wrongly get the numerical argument that is used for the variable precision for the first %s... --- lib/mprintf.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index b3fc33922..bba1a6481 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -686,8 +686,11 @@ static int dprintf_formatf( width = p->width; /* pick up the specified precision */ - if(p->flags & FLAGS_PRECPARAM) + if(p->flags & FLAGS_PRECPARAM) { prec = vto[p->precision].data.num; + 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 -- cgit v1.2.1 From 69aac49f79999214d2423140258728f8b70e0047 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 30 Jun 2008 12:58:15 +0000 Subject: made %llu work for printing unsigned long longs, added the generic curl source header --- lib/mprintf.c | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index bba1a6481..25a4196ae 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -1,17 +1,24 @@ -/**************************************************************************** +/*************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| * - * $Id$ + * Copyright (C) 1999 - 2008, Daniel Stenberg, , 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. * - ************************************************************************* + * 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. * - * 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. + * 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. + * $Id$ * * Purpose: * A merge of Bjorn Reese's format() function and Daniel's dsprintf() @@ -29,7 +36,6 @@ * page at http://daniel.haxx.se/trio/ */ - #include "setup.h" #include #include @@ -559,10 +565,12 @@ static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos, vto[i].data.lnum = va_arg(arglist, LONG_LONG); else #endif + { if(vto[i].flags & FLAGS_LONG) vto[i].data.num = va_arg(arglist, long); - else - vto[i].data.num = va_arg(arglist, int); + else + vto[i].data.num = va_arg(arglist, int); + } break; case FORMAT_DOUBLE: @@ -700,7 +708,12 @@ static int dprintf_formatf( switch (p->type) { case FORMAT_INT: - num = p->data.num; +#ifdef ENABLE_64BIT + if(p->flags & FLAGS_LONGLONG) + num = p->data.lnum; + else +#endif + num = p->data.num; if(p->flags & FLAGS_CHAR) { /* Character. */ if(!(p->flags & FLAGS_LEFT)) -- cgit v1.2.1 From 24b189071035e5b987c137b491532597158889b9 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Mon, 11 Aug 2008 01:22:57 +0000 Subject: s/SIZEOF_CURL_OFF_T/CURL_SIZEOF_CURL_OFF_T/g --- lib/mprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 25a4196ae..475bdded5 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -409,7 +409,7 @@ static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos, #endif break; case 'O': -#if SIZEOF_CURL_OFF_T > 4 +#if CURL_SIZEOF_CURL_OFF_T > 4 flags |= FLAGS_LONGLONG; #else flags |= FLAGS_LONG; -- cgit v1.2.1 From ceb49d3742e4c27fdd86f9a6b043bebf7f42deaa Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Thu, 21 Aug 2008 00:06:15 +0000 Subject: Get rid of ENABLE_64BIT symbol definition and usage. Improve HAVE_LONGLONG symbol description. --- lib/mprintf.c | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 475bdded5..f139c8da1 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -58,10 +58,17 @@ #define SIZEOF_SIZE_T 4 #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) +# 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" @@ -150,8 +157,8 @@ typedef struct { char *str; void *ptr; long num; -#ifdef ENABLE_64BIT - LONG_LONG lnum; +#ifdef HAVE_LONG_LONG_TYPE + LONG_LONG_TYPE lnum; #endif double dnum; } data; @@ -560,9 +567,9 @@ static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos, break; case FORMAT_INT: -#ifdef ENABLE_64BIT +#ifdef HAVE_LONG_LONG_TYPE if(vto[i].flags & FLAGS_LONGLONG) - vto[i].data.lnum = va_arg(arglist, LONG_LONG); + vto[i].data.lnum = va_arg(arglist, LONG_LONG_TYPE); else #endif { @@ -645,8 +652,8 @@ static int dprintf_formatf( long base; /* Integral values to be written. */ -#ifdef ENABLE_64BIT - unsigned LONG_LONG num; +#ifdef HAVE_LONG_LONG_TYPE + unsigned LONG_LONG_TYPE num; #else unsigned long num; #endif @@ -708,7 +715,7 @@ static int dprintf_formatf( switch (p->type) { case FORMAT_INT: -#ifdef ENABLE_64BIT +#ifdef HAVE_LONG_LONG_TYPE if(p->flags & FLAGS_LONGLONG) num = p->data.lnum; else @@ -746,7 +753,7 @@ static int dprintf_formatf( /* Decimal integer. */ base = 10; -#ifdef ENABLE_64BIT +#ifdef HAVE_LONG_LONG_TYPE if(p->flags & FLAGS_LONGLONG) { /* long long */ is_neg = (char)(p->data.lnum < 0); @@ -978,9 +985,9 @@ static int dprintf_formatf( case FORMAT_INTPTR: /* Answer the count of characters written. */ -#ifdef ENABLE_64BIT +#ifdef HAVE_LONG_LONG_TYPE if(p->flags & FLAGS_LONGLONG) - *(LONG_LONG *) p->data.ptr = (LONG_LONG)done; + *(LONG_LONG_TYPE *) p->data.ptr = (LONG_LONG_TYPE)done; else #endif if(p->flags & FLAGS_LONG) @@ -1199,10 +1206,10 @@ int main() { char buffer[129]; char *ptr; -#ifdef ENABLE_64BIT - long long one=99; - long long two=100; - long long test = 0x1000000000LL; +#ifdef HAVE_LONG_LONG_TYPE + LONG_LONG_TYPE one=99; + LONG_LONG_TYPE two=100; + LONG_LONG_TYPE test = 0x1000000000LL; curl_mprintf("%lld %lld %lld\n", one, two, test); #endif -- cgit v1.2.1 From 5794ffe4bd75b4d0086a11c222884405bdc35839 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Thu, 21 Aug 2008 00:10:27 +0000 Subject: Some data type size adjustments. --- lib/mprintf.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index f139c8da1..f9e0c8764 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -53,9 +53,12 @@ #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 HAVE_LONGLONG @@ -409,14 +412,14 @@ static long dprintf_Pass1(const 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 CURL_SIZEOF_CURL_OFF_T > 4 +#if (CURL_SIZEOF_CURL_OFF_T > CURL_SIZEOF_LONG) flags |= FLAGS_LONGLONG; #else flags |= FLAGS_LONG; -- cgit v1.2.1 From f209a4804b812970a82275216a2c448c5232906a Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Thu, 21 Aug 2008 00:12:03 +0000 Subject: Fix one bug detected thanks to test case 557. --- lib/mprintf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index f9e0c8764..41cb6777f 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -720,10 +720,10 @@ static int dprintf_formatf( case FORMAT_INT: #ifdef HAVE_LONG_LONG_TYPE if(p->flags & FLAGS_LONGLONG) - num = p->data.lnum; + num = (unsigned LONG_LONG_TYPE)p->data.lnum; else #endif - num = p->data.num; + num = (unsigned long)p->data.num; if(p->flags & FLAGS_CHAR) { /* Character. */ if(!(p->flags & FLAGS_LEFT)) -- cgit v1.2.1 From afe7bb4b3309d260e9537af47a02f1a7dc6eee6a Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Thu, 21 Aug 2008 01:49:19 +0000 Subject: Fix a LONG_MIN and LLONG_MIN related bug in internal m*printf() --- lib/mprintf.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 41cb6777f..a358cc10a 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -213,10 +213,10 @@ static BOOL dprintf_IsQualifierNoDollar(char c) } #ifdef DPRINTF_DEBUG2 -int dprintf_Pass1Report(va_stack_t *vto, int max) +static void dprintf_Pass1Report(va_stack_t *vto, int max) { int i; - char buffer[128]; + char buffer[256]; int bit; int flags; @@ -235,6 +235,9 @@ int dprintf_Pass1Report(va_stack_t *vto, int max) case FORMAT_INT: type = "int"; break; + case FORMAT_INTPTR: + type = "intptr"; + break; case FORMAT_LONG: type = "long"; break; @@ -649,7 +652,7 @@ 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; @@ -657,10 +660,11 @@ static int dprintf_formatf( /* Integral values to be written. */ #ifdef HAVE_LONG_LONG_TYPE unsigned LONG_LONG_TYPE num; + LONG_LONG_TYPE signed_num; #else unsigned long num; -#endif long signed_num; +#endif if(*f != '%') { /* This isn't a format spec, so write everything out until the next one @@ -759,15 +763,28 @@ static int dprintf_formatf( #ifdef HAVE_LONG_LONG_TYPE if(p->flags & FLAGS_LONGLONG) { /* long long */ - is_neg = (char)(p->data.lnum < 0); - num = is_neg ? (- p->data.lnum) : p->data.lnum; + is_neg = (p->data.lnum < 0); + if(is_neg) { + /* signed long long might fail to hold absolute LLONG_MIN by 1 */ + signed_num = p->data.lnum + (LONG_LONG_TYPE)1; + num = (unsigned LONG_LONG_TYPE)-signed_num; + num += (unsigned LONG_LONG_TYPE)1; + } + else + num = p->data.lnum; } else #endif { - signed_num = (long) num; - is_neg = (char)(signed_num < 0); - num = is_neg ? (- signed_num) : signed_num; + is_neg = (p->data.num < 0); + if(is_neg) { + /* signed long might fail to hold absolute LONG_MIN by 1 */ + signed_num = p->data.num + (long)1; + num = (unsigned long)-signed_num; + num += (unsigned long)1; + } + else + num = p->data.num; } goto number; -- cgit v1.2.1 From 4dbfc91e2b69497ac4dfc78e88e76c4f98633ad2 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Thu, 21 Aug 2008 06:58:12 +0000 Subject: MSVC's __int64 data type is only available when _INTEGRAL_MAX_BITS >= 64 --- lib/mprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index a358cc10a..93982a5b1 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -65,7 +65,7 @@ # define LONG_LONG_TYPE long long # define HAVE_LONG_LONG_TYPE #else -# if defined(_MSC_VER) && (_MSC_VER >= 900) +# if defined(_MSC_VER) && (_MSC_VER >= 900) && (_INTEGRAL_MAX_BITS >= 64) # define LONG_LONG_TYPE __int64 # define HAVE_LONG_LONG_TYPE # else -- cgit v1.2.1 From f07c3171e31b56fd8f116d57b5178ba8a6a2b74f Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Fri, 22 Aug 2008 06:53:01 +0000 Subject: cleanup the BOOL usage --- lib/mprintf.c | 45 ++++++++++++++++----------------------------- 1 file changed, 16 insertions(+), 29 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 93982a5b1..0623aaea9 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -81,19 +81,6 @@ #define BUFFSIZE 256 /* buffer for long-to-str and float-to-str calcs */ #define MAX_PARAMETERS 128 /* lame static limit */ -#undef TRUE -#undef FALSE -#undef BOOL -#ifdef __cplusplus -# define TRUE true -# define FALSE false -# define BOOL bool -#else -# define TRUE ((char)(1 == 1)) -# define FALSE ((char)(0 == 1)) -# define BOOL char -#endif - #ifdef __AMIGA__ # undef FORMAT_INT #endif @@ -177,8 +164,8 @@ 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, ...); @@ -198,7 +185,7 @@ static long dprintf_DollarString(char *input, char **end) return 0; } -static BOOL dprintf_IsQualifierNoDollar(char c) +static int dprintf_IsQualifierNoDollar(char c) { switch (c) { case '-': case '+': case ' ': case '#': case '.': @@ -206,9 +193,9 @@ static BOOL dprintf_IsQualifierNoDollar(char c) case '5': case '6': case '7': case '8': case '9': case 'h': case 'l': case 'L': case 'z': case 'q': case '*': case 'O': - return TRUE; + return 1; /* true */ default: - return FALSE; + return 0; /* false */ } } @@ -643,7 +630,7 @@ static int dprintf_formatf( f = (char *)format; while(*f != '\0') { /* Format spec modifiers. */ - char alt; + int is_alt; /* Width of a field. */ long width; @@ -718,7 +705,7 @@ static int dprintf_formatf( else prec = -1; - alt = (char)((p->flags & FLAGS_ALT)?TRUE:FALSE); + is_alt = p->flags & FLAGS_ALT; switch (p->type) { case FORMAT_INT: @@ -811,7 +798,7 @@ static int dprintf_formatf( width -= (long)(workend - w); prec -= (long)(workend - w); - if(alt && base == 8 && prec <= 0) { + if(is_alt && base == 8 && prec <= 0) { *w-- = '0'; --width; } @@ -822,7 +809,7 @@ static int dprintf_formatf( *w-- = '0'; } - if(alt && base == 16) + if(is_alt && base == 16) width -= 2; if(is_neg || (p->flags & FLAGS_SHOWSIGN) || (p->flags & FLAGS_SPACE)) @@ -839,7 +826,7 @@ static int dprintf_formatf( 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'); @@ -917,7 +904,7 @@ static int dprintf_formatf( /* 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; @@ -1084,7 +1071,7 @@ static int alloc_addbyter(int output, FILE *data) if(!infop->buffer) { infop->buffer=(char *)malloc(32); if(!infop->buffer) { - infop->fail = TRUE; + infop->fail = 1; return -1; /* fail */ } infop->alloc = 32; @@ -1096,8 +1083,8 @@ static int alloc_addbyter(int output, FILE *data) newptr = (char *)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; @@ -1119,7 +1106,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); @@ -1145,7 +1132,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) { -- cgit v1.2.1 From 9bb5da968c7ab38434d426f9928bcd69cf4ebb02 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Fri, 22 Aug 2008 11:11:33 +0000 Subject: Improved curl_m*printf() integral data type size and signedness handling --- lib/mprintf.c | 108 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 53 insertions(+), 55 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 0623aaea9..2f7f31e29 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -49,6 +49,10 @@ #include +#include "memory.h" +/* The last #include file should be: */ +#include "memdebug.h" + #ifndef SIZEOF_LONG_DOUBLE #define SIZEOF_LONG_DOUBLE 0 #endif @@ -74,9 +78,17 @@ # endif #endif -#include "memory.h" -/* The last #include file should be: */ -#include "memdebug.h" +/* + * 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 mp_intmax_t long +# define mp_uintmax_t unsigned long +#endif #define BUFFSIZE 256 /* buffer for long-to-str and float-to-str calcs */ #define MAX_PARAMETERS 128 /* lame static limit */ @@ -146,10 +158,10 @@ typedef struct { union { char *str; void *ptr; - long num; -#ifdef HAVE_LONG_LONG_TYPE - LONG_LONG_TYPE lnum; -#endif + union { + mp_intmax_t as_signed; + mp_uintmax_t as_unsigned; + } num; double dnum; } data; } va_stack_t; @@ -544,7 +556,7 @@ static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos, /* Width/precision arguments must be read before the main argument * they are attached to */ - vto[i + 1].data.num = va_arg(arglist, int); + vto[i + 1].data.num.as_signed = (mp_intmax_t)va_arg(arglist, int); } switch (vto[i].type) @@ -561,15 +573,27 @@ static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos, case FORMAT_INT: #ifdef HAVE_LONG_LONG_TYPE - if(vto[i].flags & FLAGS_LONGLONG) - vto[i].data.lnum = va_arg(arglist, 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, unsigned LONG_LONG_TYPE); + else if(vto[i].flags & FLAGS_LONGLONG) + vto[i].data.num.as_signed = + (mp_intmax_t)va_arg(arglist, LONG_LONG_TYPE); 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); + vto[i].data.num.as_signed = + (mp_intmax_t)va_arg(arglist, int); } break; @@ -645,13 +669,10 @@ static int dprintf_formatf( long base; /* Integral values to be written. */ -#ifdef HAVE_LONG_LONG_TYPE - unsigned LONG_LONG_TYPE num; - LONG_LONG_TYPE signed_num; -#else - unsigned long num; - long signed_num; -#endif + mp_uintmax_t num; + + /* Used to convert negative in positive. */ + mp_intmax_t signed_num; if(*f != '%') { /* This isn't a format spec, so write everything out until the next one @@ -690,13 +711,13 @@ 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; + 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 */ } @@ -709,12 +730,7 @@ static int dprintf_formatf( switch (p->type) { case FORMAT_INT: -#ifdef HAVE_LONG_LONG_TYPE - if(p->flags & FLAGS_LONGLONG) - num = (unsigned LONG_LONG_TYPE)p->data.lnum; - else -#endif - num = (unsigned long)p->data.num; + num = p->data.num.as_unsigned; if(p->flags & FLAGS_CHAR) { /* Character. */ if(!(p->flags & FLAGS_LEFT)) @@ -747,32 +763,14 @@ static int dprintf_formatf( /* Decimal integer. */ base = 10; -#ifdef HAVE_LONG_LONG_TYPE - if(p->flags & FLAGS_LONGLONG) { - /* long long */ - is_neg = (p->data.lnum < 0); - if(is_neg) { - /* signed long long might fail to hold absolute LLONG_MIN by 1 */ - signed_num = p->data.lnum + (LONG_LONG_TYPE)1; - num = (unsigned LONG_LONG_TYPE)-signed_num; - num += (unsigned LONG_LONG_TYPE)1; - } - else - num = p->data.lnum; - } - else -#endif - { - is_neg = (p->data.num < 0); - if(is_neg) { - /* signed long might fail to hold absolute LONG_MIN by 1 */ - signed_num = p->data.num + (long)1; - num = (unsigned long)-signed_num; - num += (unsigned long)1; - } - else - num = p->data.num; + is_neg = p->data.num.as_signed < 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; + num = (mp_uintmax_t)-signed_num; + num += (mp_uintmax_t)1; } + goto number; unsigned_number: @@ -938,13 +936,13 @@ static int dprintf_formatf( if(p->flags & FLAGS_WIDTH) width = p->width; else if(p->flags & FLAGS_WIDTHPARAM) - width = vto[p->width].data.num; + width = (long)vto[p->width].data.num.as_signed; prec = -1; if(p->flags & FLAGS_PREC) prec = p->precision; else if(p->flags & FLAGS_PRECPARAM) - prec = vto[p->precision].data.num; + prec = (long)vto[p->precision].data.num.as_signed; if(p->flags & FLAGS_LEFT) strcat(formatbuf, "-"); -- cgit v1.2.1 From 578f42d588a5698f6488a11dd880acb592ca524c Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sat, 23 Aug 2008 02:04:55 +0000 Subject: typecast constant in comparison --- lib/mprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 2f7f31e29..3f74f26f5 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -763,7 +763,7 @@ static int dprintf_formatf( /* Decimal integer. */ base = 10; - is_neg = p->data.num.as_signed < 0; + is_neg = (p->data.num.as_signed < (mp_intmax_t)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; -- cgit v1.2.1 From 70b1cd798f4e64ab18f117114599089e383f75c1 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sat, 23 Aug 2008 02:35:16 +0000 Subject: explicit value assignment for comparison result --- lib/mprintf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 3f74f26f5..be6832fa1 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -726,7 +726,7 @@ static int dprintf_formatf( else prec = -1; - is_alt = p->flags & FLAGS_ALT; + is_alt = (p->flags & FLAGS_ALT) ? 1 : 0; switch (p->type) { case FORMAT_INT: @@ -763,7 +763,7 @@ static int dprintf_formatf( /* Decimal integer. */ base = 10; - is_neg = (p->data.num.as_signed < (mp_intmax_t)0); + 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; -- cgit v1.2.1 From 79cbe508944029672ec2de1db9d559473ec02110 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sun, 24 Aug 2008 00:15:59 +0000 Subject: Test if type casting a 'signed int' to a 'signed long long' fails to do sign extension on x86_64. --- lib/mprintf.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index be6832fa1..3a83cf001 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -325,6 +325,7 @@ static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos, int flags; long max_param=0; long i; + int aux_signed_int; while(*fmt) { if(*fmt++ == '%') { @@ -591,9 +592,17 @@ static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos, else if(vto[i].flags & FLAGS_UNSIGNED) vto[i].data.num.as_unsigned = (mp_uintmax_t)va_arg(arglist, unsigned int); - else + else { + /* vto[i].data.num.as_signed = (mp_intmax_t)va_arg(arglist, int); + */ + aux_signed_int = va_arg(arglist, int); + if(sizeof(mp_intmax_t) > sizeof(long)) + vto[i].data.num.as_signed = (mp_intmax_t)aux_signed_int; + else + vto[i].data.num.as_signed = (long)aux_signed_int; + } } break; -- cgit v1.2.1 From 13f035b905610abf9ac84686f1c5bc47a4d3b007 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sun, 24 Aug 2008 03:59:43 +0000 Subject: x86_64 fixes --- lib/mprintf.c | 59 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 27 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 3a83cf001..1e5235769 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -325,7 +325,13 @@ static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos, int flags; long max_param=0; long i; - int aux_signed_int; + + int va_signed_int; + unsigned int va_unsigned_int; + long va_signed_long; + unsigned long va_unsigned_long; + mp_intmax_t va_signed_long_long; + mp_uintmax_t va_unsigned_long_long; while(*fmt) { if(*fmt++ == '%') { @@ -574,34 +580,32 @@ static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos, 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, unsigned LONG_LONG_TYPE); - else if(vto[i].flags & FLAGS_LONGLONG) - vto[i].data.num.as_signed = - (mp_intmax_t)va_arg(arglist, LONG_LONG_TYPE); + if((vto[i].flags & FLAGS_LONGLONG) && (vto[i].flags & FLAGS_UNSIGNED)) { + va_unsigned_long_long = va_arg(arglist, mp_uintmax_t); + vto[i].data.num.as_unsigned = (mp_uintmax_t)va_unsigned_long_long; + } + else if(vto[i].flags & FLAGS_LONGLONG) { + va_signed_long_long = va_arg(arglist, mp_intmax_t); + vto[i].data.num.as_signed = (mp_intmax_t)va_signed_long_long; + } else #endif { - 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); + if((vto[i].flags & FLAGS_LONG) && (vto[i].flags & FLAGS_UNSIGNED)) { + va_unsigned_long = va_arg(arglist, unsigned long); + vto[i].data.num.as_unsigned = (mp_uintmax_t)va_unsigned_long; + } + else if(vto[i].flags & FLAGS_LONG) { + va_signed_long = va_arg(arglist, long); + vto[i].data.num.as_signed = (mp_intmax_t)va_signed_long; + } + else if(vto[i].flags & FLAGS_UNSIGNED) { + va_unsigned_int = va_arg(arglist, unsigned int); + vto[i].data.num.as_unsigned = (mp_uintmax_t)va_unsigned_int; + } else { - /* - vto[i].data.num.as_signed = - (mp_intmax_t)va_arg(arglist, int); - */ - aux_signed_int = va_arg(arglist, int); - if(sizeof(mp_intmax_t) > sizeof(long)) - vto[i].data.num.as_signed = (mp_intmax_t)aux_signed_int; - else - vto[i].data.num.as_signed = (long)aux_signed_int; + va_signed_int = va_arg(arglist, int); + vto[i].data.num.as_signed = (mp_intmax_t)va_signed_int; } } break; @@ -776,8 +780,9 @@ static int dprintf_formatf( 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; - num = (mp_uintmax_t)-signed_num; - num += (mp_uintmax_t)1; + signed_num = -signed_num; + num = (mp_uintmax_t)signed_num; + num += (mp_uintmax_t)0x1; } goto number; -- cgit v1.2.1 From bc69e46ad112ff3680a18f6c5361894d74194e80 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sun, 24 Aug 2008 10:40:38 +0000 Subject: x86_64 fixes --- lib/mprintf.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 1e5235769..1bbf53ade 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -776,10 +776,11 @@ static int dprintf_formatf( /* Decimal integer. */ base = 10; - is_neg = (p->data.num.as_signed < (mp_intmax_t)0) ? 1 : 0; + signed_num = p->data.num.as_signed; + is_neg = (signed_num < (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 += (mp_intmax_t)1; signed_num = -signed_num; num = (mp_uintmax_t)signed_num; num += (mp_uintmax_t)0x1; -- cgit v1.2.1 From cbc04a7d40895d731d833c34d1c55e31b3fe4b79 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sun, 24 Aug 2008 16:01:15 +0000 Subject: Debug trace curl_mprintf() on x86_64 and ia64 systems. --- lib/mprintf.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 1bbf53ade..fea8e5198 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -182,6 +182,19 @@ struct asprintf { int curl_msprintf(char *buffer, const char *format, ...); +static int trace_this(void) +{ + const char * host = OS; + const char * arc1 = "x86_64"; + const char * arc2 = "ia64"; + + if(0 == memcmp(host, arc1, strlen(arc1))) + return 1; + if(0 == memcmp(host, arc2, strlen(arc2))) + return 1; + return 0; +} + static long dprintf_DollarString(char *input, char **end) { int number=0; @@ -582,30 +595,66 @@ static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos, #ifdef HAVE_LONG_LONG_TYPE if((vto[i].flags & FLAGS_LONGLONG) && (vto[i].flags & FLAGS_UNSIGNED)) { va_unsigned_long_long = va_arg(arglist, mp_uintmax_t); + if(trace_this()) + printf("va_unsigned_long_long = %llu\n", + va_unsigned_long_long); vto[i].data.num.as_unsigned = (mp_uintmax_t)va_unsigned_long_long; + if(trace_this()) + printf("vto[i].data.num.as_unsigned = %llu\n", + vto[i].data.num.as_unsigned); } else if(vto[i].flags & FLAGS_LONGLONG) { va_signed_long_long = va_arg(arglist, mp_intmax_t); + if(trace_this()) + printf("va_signed_long_long = %lld\n", + va_signed_long_long); vto[i].data.num.as_signed = (mp_intmax_t)va_signed_long_long; + if(trace_this()) + printf("vto[i].data.num.as_signed = %lld\n", + vto[i].data.num.as_signed); } else #endif { if((vto[i].flags & FLAGS_LONG) && (vto[i].flags & FLAGS_UNSIGNED)) { va_unsigned_long = va_arg(arglist, unsigned long); + if(trace_this()) + printf("va_unsigned_long = %lu\n", + va_unsigned_long); vto[i].data.num.as_unsigned = (mp_uintmax_t)va_unsigned_long; + if(trace_this()) + printf("vto[i].data.num.as_unsigned = %llu\n", + vto[i].data.num.as_unsigned); } else if(vto[i].flags & FLAGS_LONG) { va_signed_long = va_arg(arglist, long); + if(trace_this()) + printf("va_signed_long = %ld\n", + va_signed_long); vto[i].data.num.as_signed = (mp_intmax_t)va_signed_long; + if(trace_this()) + printf("vto[i].data.num.as_signed = %lld\n", + vto[i].data.num.as_signed); } else if(vto[i].flags & FLAGS_UNSIGNED) { va_unsigned_int = va_arg(arglist, unsigned int); + if(trace_this()) + printf("va_unsigned_int = %u\n", + va_unsigned_int); vto[i].data.num.as_unsigned = (mp_uintmax_t)va_unsigned_int; + if(trace_this()) + printf("vto[i].data.num.as_unsigned = %llu\n", + vto[i].data.num.as_unsigned); } else { va_signed_int = va_arg(arglist, int); + if(trace_this()) + printf("va_signed_int = %d\n", + va_signed_int); vto[i].data.num.as_signed = (mp_intmax_t)va_signed_int; + if(trace_this()) + printf("vto[i].data.num.as_signed = %lld\n", + vto[i].data.num.as_signed); } } break; @@ -777,13 +826,25 @@ static int dprintf_formatf( base = 10; signed_num = p->data.num.as_signed; + if(trace_this()) + printf("1 signed_num = %lld\n", signed_num); is_neg = (signed_num < (mp_intmax_t)0) ? 1 : 0; + if(trace_this()) + printf("is_neg = %d\n", is_neg); if(is_neg) { /* signed_num might fail to hold absolute negative minimum by 1 */ signed_num += (mp_intmax_t)1; + if(trace_this()) + printf("2 signed_num = %lld\n", signed_num); signed_num = -signed_num; + if(trace_this()) + printf("3 signed_num = %lld\n", signed_num); num = (mp_uintmax_t)signed_num; + if(trace_this()) + printf("4 num = %llu\n", num); num += (mp_uintmax_t)0x1; + if(trace_this()) + printf("5 num = %llu\n", num); } goto number; -- cgit v1.2.1 From c9f2c54c4924feb3bc150db7bc065f45c4c74794 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sun, 24 Aug 2008 23:21:46 +0000 Subject: Remove debug tracing and nearly all changes introduced since revision 1.72 The effective result of this commit is revision 1.72 plus two changed lines. These can be viewed in http://cool.haxx.se/cvs.cgi/curl/lib/mprintf.c.diff?r1=1.72&r2=1.77 --- lib/mprintf.c | 117 +++++++++++----------------------------------------------- 1 file changed, 21 insertions(+), 96 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index fea8e5198..02a25f6de 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -182,19 +182,6 @@ struct asprintf { int curl_msprintf(char *buffer, const char *format, ...); -static int trace_this(void) -{ - const char * host = OS; - const char * arc1 = "x86_64"; - const char * arc2 = "ia64"; - - if(0 == memcmp(host, arc1, strlen(arc1))) - return 1; - if(0 == memcmp(host, arc2, strlen(arc2))) - return 1; - return 0; -} - static long dprintf_DollarString(char *input, char **end) { int number=0; @@ -339,13 +326,6 @@ static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos, long max_param=0; long i; - int va_signed_int; - unsigned int va_unsigned_int; - long va_signed_long; - unsigned long va_unsigned_long; - mp_intmax_t va_signed_long_long; - mp_uintmax_t va_unsigned_long_long; - while(*fmt) { if(*fmt++ == '%') { if(*fmt == '%') { @@ -593,69 +573,27 @@ static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos, case FORMAT_INT: #ifdef HAVE_LONG_LONG_TYPE - if((vto[i].flags & FLAGS_LONGLONG) && (vto[i].flags & FLAGS_UNSIGNED)) { - va_unsigned_long_long = va_arg(arglist, mp_uintmax_t); - if(trace_this()) - printf("va_unsigned_long_long = %llu\n", - va_unsigned_long_long); - vto[i].data.num.as_unsigned = (mp_uintmax_t)va_unsigned_long_long; - if(trace_this()) - printf("vto[i].data.num.as_unsigned = %llu\n", - vto[i].data.num.as_unsigned); - } - else if(vto[i].flags & FLAGS_LONGLONG) { - va_signed_long_long = va_arg(arglist, mp_intmax_t); - if(trace_this()) - printf("va_signed_long_long = %lld\n", - va_signed_long_long); - vto[i].data.num.as_signed = (mp_intmax_t)va_signed_long_long; - if(trace_this()) - printf("vto[i].data.num.as_signed = %lld\n", - vto[i].data.num.as_signed); - } + 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].flags & FLAGS_UNSIGNED)) { - va_unsigned_long = va_arg(arglist, unsigned long); - if(trace_this()) - printf("va_unsigned_long = %lu\n", - va_unsigned_long); - vto[i].data.num.as_unsigned = (mp_uintmax_t)va_unsigned_long; - if(trace_this()) - printf("vto[i].data.num.as_unsigned = %llu\n", - vto[i].data.num.as_unsigned); - } - else if(vto[i].flags & FLAGS_LONG) { - va_signed_long = va_arg(arglist, long); - if(trace_this()) - printf("va_signed_long = %ld\n", - va_signed_long); - vto[i].data.num.as_signed = (mp_intmax_t)va_signed_long; - if(trace_this()) - printf("vto[i].data.num.as_signed = %lld\n", - vto[i].data.num.as_signed); - } - else if(vto[i].flags & FLAGS_UNSIGNED) { - va_unsigned_int = va_arg(arglist, unsigned int); - if(trace_this()) - printf("va_unsigned_int = %u\n", - va_unsigned_int); - vto[i].data.num.as_unsigned = (mp_uintmax_t)va_unsigned_int; - if(trace_this()) - printf("vto[i].data.num.as_unsigned = %llu\n", - vto[i].data.num.as_unsigned); - } - else { - va_signed_int = va_arg(arglist, int); - if(trace_this()) - printf("va_signed_int = %d\n", - va_signed_int); - vto[i].data.num.as_signed = (mp_intmax_t)va_signed_int; - if(trace_this()) - printf("vto[i].data.num.as_signed = %lld\n", - vto[i].data.num.as_signed); - } + 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.as_signed = + (mp_intmax_t)va_arg(arglist, int); } break; @@ -825,26 +763,13 @@ static int dprintf_formatf( /* Decimal integer. */ base = 10; - signed_num = p->data.num.as_signed; - if(trace_this()) - printf("1 signed_num = %lld\n", signed_num); - is_neg = (signed_num < (mp_intmax_t)0) ? 1 : 0; - if(trace_this()) - printf("is_neg = %d\n", is_neg); + 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 += (mp_intmax_t)1; - if(trace_this()) - printf("2 signed_num = %lld\n", signed_num); + signed_num = p->data.num.as_signed + (mp_intmax_t)1; signed_num = -signed_num; - if(trace_this()) - printf("3 signed_num = %lld\n", signed_num); num = (mp_uintmax_t)signed_num; - if(trace_this()) - printf("4 num = %llu\n", num); - num += (mp_uintmax_t)0x1; - if(trace_this()) - printf("5 num = %llu\n", num); + num += (mp_uintmax_t)1; } goto number; -- cgit v1.2.1 From 861b647e7b1da564b831a5b07312a30feb7b6c58 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sat, 6 Sep 2008 04:28:43 +0000 Subject: remove unnecessary typecasting of realloc() --- lib/mprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 02a25f6de..255eb11df 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -1079,7 +1079,7 @@ 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 = 1; -- cgit v1.2.1 From 59e378f48fed849e8e41f0bc6a10bf7a1732ae8a Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sat, 6 Sep 2008 05:29:05 +0000 Subject: remove unnecessary typecasting of malloc() --- lib/mprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 255eb11df..46438d47d 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -1068,7 +1068,7 @@ 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 = 1; return -1; /* fail */ -- cgit v1.2.1 From 651dad0cc16adfb2f7d31ba78b914270baf0ea68 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sat, 13 Sep 2008 16:37:16 +0000 Subject: fix compiler warning: external declaration in primary source file --- lib/mprintf.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 46438d47d..a09450306 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -180,8 +180,6 @@ struct asprintf { 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; -- cgit v1.2.1 From 33a3753c3f41d546ebf3350685eb7201d25783f4 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Tue, 21 Apr 2009 11:46:16 +0000 Subject: libcurl's memory.h renamed to curl_memory.h --- lib/mprintf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index a09450306..ceafb5197 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1999 - 2008, Daniel Stenberg, , et al. + * Copyright (C) 1999 - 2009, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -49,7 +49,7 @@ #include -#include "memory.h" +#include "curl_memory.h" /* The last #include file should be: */ #include "memdebug.h" -- cgit v1.2.1 From 7aef172a347a0422a0968fde9c487639ff673383 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Thu, 4 Feb 2010 19:44:31 +0000 Subject: fix printf-style format strings --- lib/mprintf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index ceafb5197..b0f1a9267 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1999 - 2009, Daniel Stenberg, , et al. + * Copyright (C) 1999 - 2010, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -1219,7 +1219,7 @@ int main() 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); + ptr=curl_maprintf("test this then baby %s%s%s%s%s%s %d %d %d loser baby get a kiss 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); -- cgit v1.2.1 From 2179ef9fa914c916b3f6e35519091d3e72b75d2c Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sat, 20 Feb 2010 01:15:10 +0000 Subject: fix compiler warning --- lib/mprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index b0f1a9267..3d65e9ff7 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -871,7 +871,7 @@ static int dprintf_formatf( len = strlen(str); if(prec != -1 && (size_t) prec < len) - len = prec; + len = (size_t)prec; width -= (long)len; if(p->flags & FLAGS_ALT) -- cgit v1.2.1 From a6fb6b70c7d08b9243aecff1fa059758ddf39e52 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sat, 20 Feb 2010 11:58:26 +0000 Subject: fix compiler warning --- lib/mprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 3d65e9ff7..f5daa39fc 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -911,7 +911,7 @@ static int dprintf_formatf( static const char strnil[] = "(nil)"; const char *point; - width -= sizeof(strnil) - 1; + width -= (long)(sizeof(strnil) - 1); if(p->flags & FLAGS_LEFT) while(width-- > 0) OUTCHAR(' '); -- cgit v1.2.1 From 2309b4e330b96bc2e1f8e36b6184015e59544037 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 24 Mar 2010 11:02:54 +0100 Subject: remove the CVSish $Id$ lines --- lib/mprintf.c | 1 - 1 file changed, 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index f5daa39fc..1d93c5828 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -18,7 +18,6 @@ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * - * $Id$ * * Purpose: * A merge of Bjorn Reese's format() function and Daniel's dsprintf() -- cgit v1.2.1 From dfad8a6dad9f42516a3228d10c44806606fd1c79 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 16 Apr 2010 23:50:39 +0200 Subject: dprintf_formatf: Value stored to 'left' is never read --- lib/mprintf.c | 1 - 1 file changed, 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 1d93c5828..536c0c247 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -963,7 +963,6 @@ static int dprintf_formatf( /* RECURSIVE USAGE */ len = curl_msnprintf(fptr, left, ".%ld", prec); fptr += len; - left -= len; } if(p->flags & FLAGS_LONG) *fptr++ = 'l'; -- cgit v1.2.1 From 1702a2c08d3a0ed5945f34e6cd38436611f65164 Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Tue, 19 Apr 2011 15:54:13 +0200 Subject: Fix a couple of spelling errors in lib/ Found with codespell. --- lib/mprintf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 536c0c247..b02b50253 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -124,7 +124,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, @@ -692,7 +692,7 @@ static int dprintf_formatf( 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 %$ sequence */ param=dprintf_DollarString(f, &f); -- cgit v1.2.1 From b903186fa0189ff241d756d25d07fdfe9885ae49 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 20 Apr 2011 15:17:42 +0200 Subject: source cleanup: unify look, style and indent levels By the use of a the new lib/checksrc.pl script that checks that our basic source style rules are followed. --- lib/mprintf.c | 146 ++++++++++++++++++++-------------------------------------- 1 file changed, 51 insertions(+), 95 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index b02b50253..ca8e7ff2a 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1999 - 2010, Daniel Stenberg, , et al. + * Copyright (C) 1999 - 2011, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -547,67 +547,65 @@ static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos, #endif /* Read the arg list parameters into our data list */ - for (i=0; iflags & 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) @@ -1202,45 +1200,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 HAVE_LONG_LONG_TYPE - LONG_LONG_TYPE one=99; - LONG_LONG_TYPE two=100; - LONG_LONG_TYPE 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 kiss 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 -- cgit v1.2.1 From 889d1e973fb718a77c5000141d724ce03863af23 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 22 Apr 2011 23:01:30 +0200 Subject: whitespace cleanup: no space first in conditionals "if(a)" is our style, not "if( a )" --- lib/mprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index ca8e7ff2a..dd7a1e09b 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -851,7 +851,7 @@ 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) { str = null; -- cgit v1.2.1 From f1586cb4775681810afd8e6626e7842d459f3b85 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Tue, 26 Jul 2011 17:23:27 +0200 Subject: stdio.h, stdlib.h, string.h, stdarg.h and ctype.h inclusion done in setup_once.h --- lib/mprintf.c | 5 ----- 1 file changed, 5 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index dd7a1e09b..38732e099 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -36,11 +36,6 @@ */ #include "setup.h" -#include -#include -#include -#include -#include #if defined(DJGPP) && (DJGPP_MINOR < 4) #undef _MPRINTF_REPLACE /* don't use x_was_used() here */ -- cgit v1.2.1 From 9194e1700312f27c6e2abdfb1f604e130497e887 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Fri, 2 Sep 2011 19:40:53 +0200 Subject: MemoryTracking: fix logging of free() calls done where Curl_safefree is called Just internal stuff... Curl_safefree is now a macro defined in memdebug.h instead of a function prototyped in url.h and implemented in url.c, so inclusion of url.h is no longer required in order to simply use Curl_safefree. Provide definition of macro WHILE_FALSE in setup_once.h in order to allow other macros such as DEBUGF and DEBUGASSERT, and code using it, to compile without 'conditional expression is constant' warnings. The WHILE_FALSE stuff fixes 150+ MSVC compiler warnings. --- lib/mprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index 38732e099..0990f4024 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -103,7 +103,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 { -- cgit v1.2.1