From b3b6f52fadfbab0277153b9142251989e8e635d4 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Thu, 1 Mar 2007 10:41:48 +0100 Subject: Merge in changes from gnulib: * vasnprintf.c: Add a comment explaining why coreutils has its own version of this file. Include . (SIZE_MAX): Remove definition (now, stdint.h covers that). (EOVERFLOW): Remove definition (now done via the eoverflow module). Update some #ifdef to #if. Use HAVE_LONG_LONG_INT, not HAVE_LONG_LONG. * printf-parse.c: Likewise. --- lib/ChangeLog | 12 +++++++++ lib/printf-parse.c | 45 +++++++++++++++++---------------- lib/vasnprintf.c | 74 +++++++++++++++++++++++++++++++++--------------------- 3 files changed, 81 insertions(+), 50 deletions(-) (limited to 'lib') diff --git a/lib/ChangeLog b/lib/ChangeLog index 5e6418687..021190ec8 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,15 @@ +2007-03-01 Jim Meyering + + Merge in changes from gnulib: + * vasnprintf.c: Add a comment explaining why coreutils has its own + version of this file. + Include . + (SIZE_MAX): Remove definition (now, stdint.h covers that). + (EOVERFLOW): Remove definition (now done via the eoverflow module). + Update some #ifdef to #if. + Use HAVE_LONG_LONG_INT, not HAVE_LONG_LONG. + * printf-parse.c: Likewise. + 2007-02-28 Jim Meyering * tsearch.c: Remove unused file. diff --git a/lib/printf-parse.c b/lib/printf-parse.c index f8e0ceda5..9493403eb 100644 --- a/lib/printf-parse.c +++ b/lib/printf-parse.c @@ -1,5 +1,8 @@ /* Formatted output to strings. - Copyright (C) 1999-2000, 2002-2004, 2006 Free Software Foundation, Inc. + This file is intended to provide exactly the same functionality + as the version in gnulib, but without the need for the xsize module. + + Copyright (C) 1999-2000, 2002-2003, 2006-2007 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -27,21 +30,12 @@ /* Get size_t, NULL. */ #include -/* Get intmax_t. */ -#if HAVE_STDINT_H_WITH_UINTMAX -# include -#endif -#if HAVE_INTTYPES_H_WITH_UINTMAX -# include -#endif +/* Get intmax_t, SIZE_MAX. */ +#include /* malloc(), realloc(), free(). */ #include -#ifndef SIZE_MAX -# define SIZE_MAX ((size_t) -1) -#endif - #if WIDE_CHAR_VERSION # define PRINTF_PARSE wprintf_parse # define CHAR_T wchar_t @@ -329,7 +323,7 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a) flags += 8; cp++; } -#ifdef HAVE_INTMAX_T +#if HAVE_INTMAX_T else if (*cp == 'j') { if (sizeof (intmax_t) > sizeof (long)) @@ -385,11 +379,14 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a) switch (c) { case 'd': case 'i': -#ifdef HAVE_LONG_LONG +#if HAVE_LONG_LONG_INT + /* If 'long long' exists and is larger than 'long': */ if (flags >= 16 || (flags & 4)) type = TYPE_LONGLONGINT; else #endif + /* If 'long long' exists and is the same as 'long', we parse + "lld" into TYPE_LONGINT. */ if (flags >= 8) type = TYPE_LONGINT; else if (flags & 2) @@ -400,11 +397,14 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a) type = TYPE_INT; break; case 'o': case 'u': case 'x': case 'X': -#ifdef HAVE_LONG_LONG +#if HAVE_LONG_LONG_INT + /* If 'long long' exists and is larger than 'long': */ if (flags >= 16 || (flags & 4)) type = TYPE_ULONGLONGINT; else #endif + /* If 'unsigned long long' exists and is the same as + 'unsigned long', we parse "llu" into TYPE_ULONGINT. */ if (flags >= 8) type = TYPE_ULONGINT; else if (flags & 2) @@ -416,7 +416,7 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a) break; case 'f': case 'F': case 'e': case 'E': case 'g': case 'G': case 'a': case 'A': -#ifdef HAVE_LONG_DOUBLE +#if HAVE_LONG_DOUBLE if (flags >= 16 || (flags & 4)) type = TYPE_LONGDOUBLE; else @@ -425,7 +425,7 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a) break; case 'c': if (flags >= 8) -#ifdef HAVE_WINT_T +#if HAVE_WINT_T type = TYPE_WIDE_CHAR; #else goto error; @@ -433,7 +433,7 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a) else type = TYPE_CHAR; break; -#ifdef HAVE_WINT_T +#if HAVE_WINT_T case 'C': type = TYPE_WIDE_CHAR; c = 'c'; @@ -441,7 +441,7 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a) #endif case 's': if (flags >= 8) -#ifdef HAVE_WCHAR_T +#if HAVE_WCHAR_T type = TYPE_WIDE_STRING; #else goto error; @@ -449,7 +449,7 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a) else type = TYPE_STRING; break; -#ifdef HAVE_WCHAR_T +#if HAVE_WCHAR_T case 'S': type = TYPE_WIDE_STRING; c = 's'; @@ -459,11 +459,14 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a) type = TYPE_POINTER; break; case 'n': -#ifdef HAVE_LONG_LONG +#if HAVE_LONG_LONG_INT + /* If 'long long' exists and is larger than 'long': */ if (flags >= 16 || (flags & 4)) type = TYPE_COUNT_LONGLONGINT_POINTER; else #endif + /* If 'long long' exists and is the same as 'long', we parse + "lln" into TYPE_COUNT_LONGINT_POINTER. */ if (flags >= 8) type = TYPE_COUNT_LONGINT_POINTER; else if (flags & 2) diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c index 78ead8e3c..d19f7125c 100644 --- a/lib/vasnprintf.c +++ b/lib/vasnprintf.c @@ -1,5 +1,8 @@ /* vsprintf with automatic memory allocation. - Copyright (C) 1999, 2002-2006 Free Software Foundation, Inc. + This file is intended to provide exactly the same functionality + as the version in gnulib, but without the need for the xsize module. + + Copyright (C) 1999, 2002-2007 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -23,7 +26,6 @@ #endif #include - #ifndef IN_LIBINTL # include #endif @@ -37,6 +39,7 @@ #include /* snprintf(), sprintf() */ #include /* abort(), malloc(), realloc(), free() */ +#include /* SIZE_MAX */ #include /* memcpy(), strlen() */ #include /* errno */ #include /* CHAR_BIT, INT_MAX */ @@ -47,17 +50,8 @@ # include "printf-parse.h" #endif -#ifndef SIZE_MAX -# define SIZE_MAX ((size_t) -1) -#endif - -/* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */ -#ifndef EOVERFLOW -# define EOVERFLOW E2BIG -#endif - -#ifdef HAVE_WCHAR_T -# ifdef HAVE_WCSLEN +#if HAVE_WCHAR_T +# if HAVE_WCSLEN # define local_wcslen wcslen # else /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid @@ -254,7 +248,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar case TYPE_COUNT_LONGINT_POINTER: *a.arg[dp->arg_index].a.a_count_longint_pointer = length; break; -#ifdef HAVE_LONG_LONG +#if HAVE_LONG_LONG_INT case TYPE_COUNT_LONGLONGINT_POINTER: *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length; break; @@ -338,7 +332,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar { case 'd': case 'i': case 'u': -# ifdef HAVE_LONG_LONG +# if HAVE_LONG_LONG_INT if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT) tmp_length = (unsigned int) (sizeof (unsigned long long) * CHAR_BIT @@ -369,7 +363,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar break; case 'o': -# ifdef HAVE_LONG_LONG +# if HAVE_LONG_LONG_INT if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT) tmp_length = (unsigned int) (sizeof (unsigned long long) * CHAR_BIT @@ -397,7 +391,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar break; case 'x': case 'X': -# ifdef HAVE_LONG_LONG +# if HAVE_LONG_LONG_INT if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT) tmp_length = (unsigned int) (sizeof (unsigned long long) * CHAR_BIT @@ -426,7 +420,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar break; case 'f': case 'F': -# ifdef HAVE_LONG_DOUBLE +# if HAVE_LONG_DOUBLE if (type == TYPE_LONGDOUBLE) tmp_length = (unsigned int) (LDBL_MAX_EXP @@ -450,7 +444,6 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar break; case 'e': case 'E': case 'g': case 'G': - case 'a': case 'A': tmp_length = 12; /* sign, decimal point, exponent etc. */ tmp_length += precision; @@ -458,8 +451,31 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar goto out_of_memory; break; + case 'a': case 'A': +# if HAVE_LONG_DOUBLE + if (type == TYPE_LONGDOUBLE) + tmp_length = + (unsigned int) (LDBL_DIG + * 0.831 /* decimal -> hexadecimal */ + ) + + 1; /* turn floor into ceil */ + else +# endif + tmp_length = + (unsigned int) (DBL_DIG + * 0.831 /* decimal -> hexadecimal */ + ) + + 1; /* turn floor into ceil */ + if (tmp_length < precision) + tmp_length = precision; + /* Account for sign, decimal point etc. */ + tmp_length += 12 + if (tmp_length < 12) + goto out_of_memory; + break; + case 'c': -# if defined HAVE_WINT_T && !WIDE_CHAR_VERSION +# if HAVE_WINT_T && !WIDE_CHAR_VERSION if (type == TYPE_WIDE_CHAR) tmp_length = MB_CUR_MAX; else @@ -468,7 +484,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar break; case 's': -# ifdef HAVE_WCHAR_T +# if HAVE_WCHAR_T if (type == TYPE_WIDE_STRING) { tmp_length = @@ -551,7 +567,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar switch (type) { -#ifdef HAVE_LONG_LONG +#if HAVE_LONG_LONG_INT case TYPE_LONGLONGINT: case TYPE_ULONGLONGINT: *p++ = 'l'; @@ -559,15 +575,15 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar #endif case TYPE_LONGINT: case TYPE_ULONGINT: -#ifdef HAVE_WINT_T +#if HAVE_WINT_T case TYPE_WIDE_CHAR: #endif -#ifdef HAVE_WCHAR_T +#if HAVE_WCHAR_T case TYPE_WIDE_STRING: #endif *p++ = 'l'; break; -#ifdef HAVE_LONG_DOUBLE +#if HAVE_LONG_DOUBLE case TYPE_LONGDOUBLE: *p++ = 'L'; break; @@ -705,7 +721,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar SNPRINTF_BUF (arg); } break; -#ifdef HAVE_LONG_LONG +#if HAVE_LONG_LONG_INT case TYPE_LONGLONGINT: { long long int arg = a.arg[dp->arg_index].a.a_longlongint; @@ -725,7 +741,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar SNPRINTF_BUF (arg); } break; -#ifdef HAVE_LONG_DOUBLE +#if HAVE_LONG_DOUBLE case TYPE_LONGDOUBLE: { long double arg = a.arg[dp->arg_index].a.a_longdouble; @@ -739,7 +755,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar SNPRINTF_BUF (arg); } break; -#ifdef HAVE_WINT_T +#if HAVE_WINT_T case TYPE_WIDE_CHAR: { wint_t arg = a.arg[dp->arg_index].a.a_wide_char; @@ -753,7 +769,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar SNPRINTF_BUF (arg); } break; -#ifdef HAVE_WCHAR_T +#if HAVE_WCHAR_T case TYPE_WIDE_STRING: { const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string; -- cgit v1.2.3-54-g00ecf