Blame SOURCES/xsane-0.999-snprintf-update.patch

0aa144
diff -up xsane-0.999/lib/snprintf.c.OLD xsane-0.999/lib/snprintf.c
0aa144
--- xsane-0.999/lib/snprintf.c.OLD	2014-05-29 10:42:26.795419830 -0400
0aa144
+++ xsane-0.999/lib/snprintf.c	2014-05-29 10:48:58.544431002 -0400
0aa144
@@ -2,180 +2,616 @@
0aa144
 
0aa144
 #ifndef HAVE_SNPRINTF
0aa144
 
0aa144
-/***************************************************************************
0aa144
- * LPRng - An Extended Print Spooler System
0aa144
- *
0aa144
- * Copyright 1988-1997, Patrick Powell, San Diego, CA
0aa144
- *     papowell@sdsu.edu
0aa144
- * See below for conditions of use.
0aa144
- *
0aa144
- ***************************************************************************
0aa144
- * MODULE: snprintf.c
0aa144
- * PURPOSE: LPRng version of printf - absolutely bombproof (hopefully!)
0aa144
+/**************************************************************************
0aa144
+ * Copyright 1994-2003 Patrick Powell, San Diego, CA <papowell@lprng.com>
0aa144
  **************************************************************************/
0aa144
-#if 0
0aa144
-
0aa144
-			 The "Artistic License"
0aa144
-
0aa144
-				Preamble
0aa144
-
0aa144
-The intent of this document is to state the conditions under which a
0aa144
-Package may be copied, such that the Copyright Holder maintains some
0aa144
-semblance of artistic control over the development of the package,
0aa144
-while giving the users of the package the right to use and distribute
0aa144
-the Package in a more-or-less customary fashion, plus the right to make
0aa144
-reasonable modifications.
0aa144
-
0aa144
-Definitions:
0aa144
-
0aa144
-	"Package" refers to the collection of files distributed by the
0aa144
-	Copyright Holder, and derivatives of that collection of files
0aa144
-	created through textual modification.
0aa144
-
0aa144
-	"Standard Version" refers to such a Package if it has not been
0aa144
-	modified, or has been modified in accordance with the wishes
0aa144
-	of the Copyright Holder as specified below.
0aa144
-
0aa144
-	"Copyright Holder" is whoever is named in the copyright or
0aa144
-	copyrights for the package.
0aa144
-
0aa144
-	"You" is you, if you are thinking about copying or distributing
0aa144
-	this Package.
0aa144
-
0aa144
-	"Reasonable copying fee" is whatever you can justify on the
0aa144
-	basis of media cost, duplication charges, time of people involved,
0aa144
-	and so on.  (You will not be required to justify it to the
0aa144
-	Copyright Holder, but only to the computing community at large
0aa144
-	as a market that must bear the fee.)
0aa144
 
0aa144
-	"Freely Available" means that no fee is charged for the item
0aa144
-	itself, though there may be fees involved in handling the item.
0aa144
-	It also means that recipients of the item may redistribute it
0aa144
-	under the same conditions they received it.
0aa144
-
0aa144
-1. You may make and give away verbatim copies of the source form of the
0aa144
-Standard Version of this Package without restriction, provided that you
0aa144
-duplicate all of the original copyright notices and associated disclaimers.
0aa144
-
0aa144
-2. You may apply bug fixes, portability fixes and other modifications
0aa144
-derived from the Public Domain or from the Copyright Holder.  A Package
0aa144
-modified in such a way shall still be considered the Standard Version.
0aa144
-
0aa144
-3. You may otherwise modify your copy of this Package in any way, provided
0aa144
-that you insert a prominent notice in each changed file stating how and
0aa144
-when you changed that file, and provided that you do at least ONE of the
0aa144
-following:
0aa144
-
0aa144
-    a) place your modifications in the Public Domain or otherwise make them
0aa144
-    Freely Available, such as by posting said modifications to Usenet or
0aa144
-    an equivalent medium, or placing the modifications on a major archive
0aa144
-    site such as uunet.uu.net, or by allowing the Copyright Holder to include
0aa144
-    your modifications in the Standard Version of the Package.
0aa144
-
0aa144
-    b) use the modified Package only within your corporation or organization.
0aa144
-
0aa144
-    c) rename any non-standard executables so the names do not conflict
0aa144
-    with standard executables, which must also be provided, and provide
0aa144
-    a separate manual page for each non-standard executable that clearly
0aa144
-    documents how it differs from the Standard Version.
0aa144
-
0aa144
-    d) make other distribution arrangements with the Copyright Holder.
0aa144
-
0aa144
-4. You may distribute the programs of this Package in object code or
0aa144
-executable form, provided that you do at least ONE of the following:
0aa144
+/*
0aa144
+   Overview:
0aa144
 
0aa144
-    a) distribute a Standard Version of the executables and library files,
0aa144
-    together with instructions (in the manual page or equivalent) on where
0aa144
-    to get the Standard Version.
0aa144
+   snprintf( char *buffer, int len, const char *format,...)
0aa144
+   plp_unsafe_snprintf( char *buffer, int len, const char *format,...)
0aa144
+     its horribly unsafe companion that does NOT protect you from
0aa144
+     the printing of evil control characters,  but may be necessary
0aa144
+     See the man page documentation below
0aa144
+   
0aa144
+   This version of snprintf was developed originally for printing
0aa144
+   on a motley collection of specialized hardware that had NO IO
0aa144
+   library.  Due to contractual restrictions,  a clean room implementation
0aa144
+   of the printf() code had to be developed.
0aa144
+   
0aa144
+   The method chosen for printf was to be as paranoid as possible,
0aa144
+   as these platforms had NO memory protection,  and very small
0aa144
+   address spaces.  This made it possible to try to print
0aa144
+   very long strings, i.e. - all of memory, very easily.  To guard
0aa144
+   against this,  all printing was done via a buffer, generous enough
0aa144
+   to hold strings,  but small enough to protect against overruns,
0aa144
+   etc.
0aa144
+   
0aa144
+   Strangely enough,  this proved to be of immense importance when
0aa144
+   SPRINTFing to a buffer on a stack...  The rest,  of course,  is
0aa144
+   well known,  as buffer overruns in the stack are a common way to
0aa144
+   do horrible things to operating systems, security, etc etc.
0aa144
+   
0aa144
+   This version of snprintf is VERY limited by modern standards.
0aa144
+
0aa144
+   Revision History:
0aa144
+   First Released Version - 1994.  This version had NO comments.
0aa144
+   First Released Version - 1994.  This version had NO comments.
0aa144
+   Second Major Released Version - Tue May 23 10:43:44 PDT 2000
0aa144
+    Configuration and other items changed.  Read this doc.
0aa144
+    Treat this as a new version.
0aa144
+   Minor Revision - Mon Apr  1 09:41:28 PST 2002
0aa144
+     - fixed up some constants and casts 
0aa144
+   
0aa144
+   COPYRIGHT AND TERMS OF USE:
0aa144
+   
0aa144
+   You may use, copy, distribute, or otherwise incorporate this software
0aa144
+   and documentation into any product or other item,  provided that
0aa144
+   the copyright in the documentation and source code as well as the
0aa144
+   source code generated constant strings in the object, executable
0aa144
+   or other code remain in place and are present in executable modules
0aa144
+   or objects.
0aa144
+   
0aa144
+   You may modify this code as appropriate to your usage; however the
0aa144
+   modified version must be identified by changing the various source
0aa144
+   and object code identification strings as is appropriately noted
0aa144
+   in the source code.
0aa144
+   
0aa144
+   You can use this with the GNU CONFIGURE utility.
0aa144
+   This should define the following macros appropriately:
0aa144
+   
0aa144
+   HAVE_STDARG_H - if the <stdargs.h> include file is available
0aa144
+   HAVE_VARARG_H - if the <varargs.h> include file is available
0aa144
+   
0aa144
+   HAVE_STRERROR - if the strerror() routine is available.
0aa144
+     If it is not available, then examine the lines containing
0aa144
+     the tests below.
0aa144
+
0aa144
+   HAVE_SYS_ERRLIST  - have sys_errlist available
0aa144
+   HAVE_DECL_SYS_ERRLIST - sys_errlist declaration in include files
0aa144
+   HAVE_SYS_NERR - have sys_nerr available
0aa144
+   HAVE_DECL_SYS_NERR -  sys_nerr declaration in include files
0aa144
+   
0aa144
+   HAVE_QUAD_T      - if the quad_t type is defined
0aa144
+   HAVE_LONG_LONG   - if the long long type is defined
0aa144
+   HAVE_LONG_DOUBLE - if the long double type is defined
0aa144
+   
0aa144
+     If you are using the GNU configure (autoconf) facility, add the
0aa144
+     following line to the configure.in file, to force checking for the
0aa144
+     quad_t and long long  data types:
0aa144
+
0aa144
+
0aa144
+    AC_CHECK_HEADERS(stdlib.h,stdio.h,unistd.h,errno.h)
0aa144
+	AC_CHECK_FUNCS(strerror)
0aa144
+	AC_CACHE_CHECK(for errno,
0aa144
+	ac_cv_errno,
0aa144
+	[
0aa144
+	AC_TRY_LINK(,[extern int errno; return (errno);],
0aa144
+		ac_cv_errno=yes, ac_cv_errno=no)
0aa144
+	])
0aa144
+	if test "$ac_cv_errno" = yes; then
0aa144
+		AC_DEFINE(HAVE_ERRNO)
0aa144
+		AC_CACHE_CHECK(for errno declaration,
0aa144
+		ac_cv_decl_errno,
0aa144
+		[
0aa144
+		AC_TRY_COMPILE([
0aa144
+		#include <stdio.h>
0aa144
+		#ifdef HAVE_STDLIB_H
0aa144
+		#include <stdlib.h>
0aa144
+		#endif
0aa144
+		#ifdef HAVE_UNISTD_H
0aa144
+		#include <unistd.h>
0aa144
+		#endif
0aa144
+		#ifdef HAVE_ERRNO_H
0aa144
+		#include <errno.h>
0aa144
+		],[return(sys_nerr);],
0aa144
+			ac_cv_decl_errno=yes, ac_cv_decl_errno=no)
0aa144
+		])
0aa144
+		if test "$ac_cv_decl_errno" = yes; then
0aa144
+			AC_DEFINE(HAVE_DECL_ERRNO)
0aa144
+		fi;
0aa144
+	fi
0aa144
+
0aa144
+	AC_CACHE_CHECK(for sys_nerr,
0aa144
+	ac_cv_sys_nerr,
0aa144
+	[
0aa144
+	AC_TRY_LINK(,[extern int sys_nerr; return (sys_nerr);],
0aa144
+		ac_cv_sys_nerr=yes, ac_cv_sys_nerr=no)
0aa144
+	])
0aa144
+	if test "$ac_cv_sys_nerr" = yes; then
0aa144
+		AC_DEFINE(HAVE_SYS_NERR)
0aa144
+		AC_CACHE_CHECK(for sys_nerr declaration,
0aa144
+		ac_cv_decl_sys_nerr,
0aa144
+		[
0aa144
+		AC_TRY_COMPILE([
0aa144
+		#include <stdio.h>
0aa144
+		#ifdef HAVE_STDLIB_H
0aa144
+		#include <stdlib.h>
0aa144
+		#endif
0aa144
+		#ifdef HAVE_UNISTD_H
0aa144
+		#include <unistd.h>
0aa144
+		#endif],[return(sys_nerr);],
0aa144
+		ac_cv_decl_sys_nerr_def=yes, ac_cv_decl_sys_nerr_def=no)
0aa144
+		])
0aa144
+		if test "$ac_cv_decl_sys_nerr" = yes; then
0aa144
+			AC_DEFINE(HAVE_DECL_SYS_NERR)
0aa144
+		fi
0aa144
+	fi
0aa144
+
0aa144
+
0aa144
+	AC_CACHE_CHECK(for sys_errlist array,
0aa144
+	ac_cv_sys_errlist,
0aa144
+	[AC_TRY_LINK(,[extern char *sys_errlist[];
0aa144
+		sys_errlist[0];],
0aa144
+		ac_cv_sys_errlist=yes, ac_cv_sys_errlist=no)
0aa144
+	])
0aa144
+	if test "$ac_cv_sys_errlist" = yes; then
0aa144
+		AC_DEFINE(HAVE_SYS_ERRLIST)
0aa144
+		AC_CACHE_CHECK(for sys_errlist declaration,
0aa144
+		ac_cv_sys_errlist_def,
0aa144
+		[AC_TRY_COMPILE([
0aa144
+		#include <stdio.h>
0aa144
+		#include <errno.h>
0aa144
+		#ifdef HAVE_STDLIB_H
0aa144
+		#include <stdlib.h>
0aa144
+		#endif
0aa144
+		#ifdef HAVE_UNISTD_H
0aa144
+		#include <unistd.h>
0aa144
+		#endif],[char *s = sys_errlist[0]; return(*s);],
0aa144
+		ac_cv_decl_sys_errlist=yes, ac_cv_decl_sys_errlist=no)
0aa144
+		])
0aa144
+		if test "$ac_cv_decl_sys_errlist" = yes; then
0aa144
+			AC_DEFINE(HAVE_DECL_SYS_ERRLIST)
0aa144
+		fi
0aa144
+	fi
0aa144
+
0aa144
+
0aa144
+
0aa144
+	AC_CACHE_CHECK(checking for long long,
0aa144
+	ac_cv_long_long,
0aa144
+	[
0aa144
+	AC_TRY_COMPILE([
0aa144
+	#include <stdio.h>
0aa144
+	#include <sys/types.h>
0aa144
+	], [printf("%d",sizeof(long long));],
0aa144
+	ac_cv_long_long=yes, ac_cv_long_long=no)
0aa144
+	])
0aa144
+	if test $ac_cv_long_long = yes; then
0aa144
+	  AC_DEFINE(HAVE_LONG_LONG)
0aa144
+	fi
0aa144
+
0aa144
+	AC_CACHE_CHECK(checking for long double,
0aa144
+	ac_cv_long_double,
0aa144
+	[
0aa144
+	AC_TRY_COMPILE([
0aa144
+	#include <stdio.h>
0aa144
+	#include <sys/types.h>
0aa144
+	], [printf("%d",sizeof(long double));],
0aa144
+	ac_cv_long_double=yes, ac_cv_long_double=no)
0aa144
+	])
0aa144
+	if test $ac_cv_long_double = yes; then
0aa144
+	  AC_DEFINE(HAVE_LONG_DOUBLE)
0aa144
+	fi
0aa144
+
0aa144
+	AC_CACHE_CHECK(checking for quad_t,
0aa144
+	ac_cv_quad_t,
0aa144
+	[
0aa144
+	AC_TRY_COMPILE([
0aa144
+	#include <stdio.h>
0aa144
+	#include <sys/types.h>
0aa144
+	], [printf("%d",sizeof(quad_t));],
0aa144
+	ac_cv_quad_t=yes, ac_cv_quad_t=no)
0aa144
+	])
0aa144
+	if test $ac_cv_quad_t = yes; then
0aa144
+	  AC_DEFINE(HAVE_QUAD_T)
0aa144
+	fi
0aa144
+
0aa144
+
0aa144
+
0aa144
+ NAME
0aa144
+     snprintf, plp_vsnprintf - formatted output conversion
0aa144
+
0aa144
+ SYNOPSIS
0aa144
+     #include <stdio.h>
0aa144
+     #include <stdarg.h>
0aa144
+
0aa144
+     int
0aa144
+     snprintf(const char *format, size_t size, va_list ap);
0aa144
+     int
0aa144
+     plp_unsafe_snprintf(const char *format, size_t size, va_list ap);
0aa144
+
0aa144
+     AKA snprintf and unsafe_snprintf in the documentation below
0aa144
+
0aa144
+     int
0aa144
+     vsnprintf(char *str, size_t size, const char *format, va_list ap);
0aa144
+     int
0aa144
+     unsafe_vsnprintf(char *str, size_t size, const char *format, va_list ap);
0aa144
+
0aa144
+     AKA vsnprintf and unsafe_vsnprintf in the documentation below
0aa144
+
0aa144
+     (Multithreaded Safe)
0aa144
+
0aa144
+ DESCRIPTION
0aa144
+     The printf() family of functions produces output according to
0aa144
+     a format as described below.  Snprintf(), and vsnprintf()
0aa144
+     write to the character string str. These functions write the
0aa144
+     output under the control of a format string that specifies
0aa144
+     how subsequent arguments (or arguments accessed via the
0aa144
+     variable-length argument facilities of stdarg(3))  are converted
0aa144
+     for output.  These functions return the number of characters
0aa144
+     printed (not including the trailing `\0' used to end output
0aa144
+     to strings).  Snprintf() and vsnprintf() will write at most
0aa144
+     size-1 of the characters printed into the output string (the
0aa144
+     size'th character then gets the terminating `\0'); if the
0aa144
+     return value is greater than or equal to the size argument,
0aa144
+     the string was too short and some of the printed characters
0aa144
+     were discarded.  The size or str may be given as zero to find
0aa144
+     out how many characters are needed; in this case, the str
0aa144
+     argument is ignored.
0aa144
+
0aa144
+     By default, the snprintf function will not format control
0aa144
+     characters (except new line and tab) in strings.  This is a
0aa144
+     safety feature that has proven to be extremely critical when
0aa144
+     using snprintf for secure applications and when debugging.
0aa144
+     If you MUST have control characters formatted or printed,
0aa144
+     then use the unsafe_snprintf() and unsafe_vsnprintf() and on
0aa144
+     your own head be the consequences.  You have been warned.
0aa144
+
0aa144
+     There is one exception to the comments above, and that is
0aa144
+     the "%c" (character) format.  It brutally assumes that the
0aa144
+     user will have performed the necessary 'isprint()' or other
0aa144
+     checks and uses the integer value as a character.
0aa144
+
0aa144
+     The format string is composed of zero or more directives:
0aa144
+     ordinary characters (not %), which are copied unchanged to
0aa144
+     the output stream; and conversion specifications, each
0aa144
+     of which results in fetching zero or more subsequent arguments.
0aa144
+     Each conversion specification is introduced by the character
0aa144
+     %. The arguments must correspond properly (after type promotion)
0aa144
+     with the conversion specifier.  After the %, the following
0aa144
+     appear in sequence:
0aa144
+
0aa144
+     o   Zero or more of the following flags:
0aa144
+
0aa144
+	   -   A zero `0' character specifying zero padding.  For
0aa144
+	     all conversions except n, the converted value is padded
0aa144
+	     on the left with zeros rather than blanks.  If a
0aa144
+	     precision is given with a numeric conversion (d, i,
0aa144
+	     o, u, i, x, and X), the `0' flag is ignored.
0aa144
+
0aa144
+       -   A negative field width flag `-' indicates the converted
0aa144
+	     value is to be left adjusted on the field boundary.  Except
0aa144
+	     for n conversions, the converted value is padded on
0aa144
+	     the right with blanks, rather than on the left with
0aa144
+	     blanks or zeros.  A `-' overrides a `0' if both are
0aa144
+	     given.
0aa144
+
0aa144
+	   -   A space, specifying that a blank should be left before
0aa144
+	     a positive number produced by a signed conversion (d, e, E, f,
0aa144
+	     g, G, or i).
0aa144
+
0aa144
+	   -   A `+' character specifying that a sign always be placed
0aa144
+	     before a number produced by a signed conversion.  A `+' overrides
0aa144
+	     a space if both are used.
0aa144
+
0aa144
+     o   An optional decimal digit string specifying a minimum
0aa144
+		 field width.  If the converted value has fewer
0aa144
+		 characters than the field width, it will be padded
0aa144
+		 with spaces on the left (or right, if the
0aa144
+		 left-adjustment flag has been given) to fill out
0aa144
+		 the field width.
0aa144
+
0aa144
+     o   An optional precision, in the form of a period `.' followed
0aa144
+		 by an optional digit string.  If the digit string
0aa144
+		 is omitted, the precision is taken as zero.  This
0aa144
+		 gives the minimum number of digits to appear for
0aa144
+		 d, i, o, u, x, and X conversions, the number of
0aa144
+		 digits to appear after the decimal-point for e,
0aa144
+		 E, and f conversions, the maximum number of
0aa144
+		 significant digits for g and G conversions, or
0aa144
+		 the maximum number of characters to be printed
0aa144
+		 from a string for s conversions.
0aa144
+
0aa144
+     o   The optional character h, specifying that a following d,
0aa144
+		 i, o, u, x, or X conversion corresponds to a short
0aa144
+		 int or unsigned short int argument, or that a
0aa144
+		 following n conversion corresponds to a pointer
0aa144
+		 to a short int argument.
0aa144
+
0aa144
+     o   The optional character l (ell) specifying that a following
0aa144
+		 d, i, o, u, x, or X conversion applies to a pointer
0aa144
+		 to a long int or unsigned long int argument, or
0aa144
+		 that a following n conversion corresponds to a
0aa144
+		 pointer to a long int argument.
0aa144
+
0aa144
+     o   The optional character q, specifying that a following d,
0aa144
+		 i, o, u, x, or X conversion corresponds to a quad_t
0aa144
+		 or u_quad_t argument, or that a following n
0aa144
+		 conversion corresponds to a quad_t argument.
0aa144
+         This value is always printed in HEX notation.  Tough.
0aa144
+         quad_t's are an OS system implementation, and should
0aa144
+         not be allowed.
0aa144
+
0aa144
+     o   The character L specifying that a following e, E, f, g,
0aa144
+		 or G conversion corresponds to a long double
0aa144
+		 argument.
0aa144
+
0aa144
+     o   A character that specifies the type of conversion to be applied.
0aa144
+
0aa144
+
0aa144
+     A field width or precision, or both, may be indicated by an asterisk `*'
0aa144
+     instead of a digit string.  In this case, an int argument supplies the
0aa144
+     field width or precision.  A negative field width is treated as a left
0aa144
+     adjustment flag followed by a positive field width; a negative precision
0aa144
+     is treated as though it were missing.
0aa144
+
0aa144
+     The conversion specifiers and their meanings are:
0aa144
+
0aa144
+     diouxX  The int (or appropriate variant) argument is converted to signed
0aa144
+			 decimal (d and i), unsigned octal (o), unsigned decimal
0aa144
+			 (u), or unsigned hexadecimal (x and X) notation.  The
0aa144
+			 letters abcdef are used for x conversions; the letters
0aa144
+			 ABCDEF are used for X conversions.  The precision, if
0aa144
+			 any, gives the minimum number of digits that must
0aa144
+			 appear; if the converted value requires fewer digits,
0aa144
+			 it is padded on the left with zeros.
0aa144
+
0aa144
+     eE      The double argument is rounded and converted in the style
0aa144
+             [-]d.ddde+-dd where there is one digit before the decimal-point
0aa144
+			 character and the number of digits after it is equal
0aa144
+			 to the precision; if the precision is missing, it is
0aa144
+			 taken as 6; if the precision is zero, no decimal-point
0aa144
+			 character appears.  An E conversion uses the letter
0aa144
+			 E (rather than e) to introduce the exponent.
0aa144
+			 The exponent always contains at least two digits; if
0aa144
+			 the value is zero, the exponent is 00.
0aa144
+
0aa144
+     f       The double argument is rounded and converted to decimal notation
0aa144
+             in the style [-]ddd.ddd, where the number of digits after the
0aa144
+             decimal-point character is equal to the precision specification.
0aa144
+             If the precision is missing, it is taken as 6; if the precision
0aa144
+             is explicitly zero, no decimal-point character appears.  If a
0aa144
+             decimal point appears, at least one digit appears before it.
0aa144
+
0aa144
+     g       The double argument is converted in style f or e (or
0aa144
+			 E for G conversions).  The precision specifies the
0aa144
+			 number of significant digits.  If the precision is
0aa144
+			 missing, 6 digits are given; if the precision is zero,
0aa144
+			 it is treated as 1.  Style e is used if the exponent
0aa144
+			 from its conversion is less than -4 or greater than
0aa144
+			 or equal to the precision.  Trailing zeros are removed
0aa144
+			 from the fractional part of the result; a decimal
0aa144
+			 point appears only if it is followed by at least one
0aa144
+			 digit.
0aa144
+
0aa144
+     c       The int argument is converted to an unsigned char,
0aa144
+             and the resulting character is written.
0aa144
+
0aa144
+     s       The ``char *'' argument is expected to be a pointer to an array
0aa144
+			 of character type (pointer to a string).  Characters
0aa144
+			 from the array are written up to (but not including)
0aa144
+			 a terminating NUL character; if a precision is
0aa144
+			 specified, no more than the number specified are
0aa144
+			 written.  If a precision is given, no null character
0aa144
+			 need be present; if the precision is not specified,
0aa144
+			 or is greater than the size of the array, the array
0aa144
+			 must contain a terminating NUL character.
0aa144
+
0aa144
+     %       A `%' is written. No argument is converted. The complete
0aa144
+             conversion specification is `%%'.
0aa144
+
0aa144
+     In no case does a non-existent or small field width cause truncation of a
0aa144
+     field; if the result of a conversion is wider than the field width, the
0aa144
+     field is expanded to contain the conversion result.
0aa144
+
0aa144
+ EXAMPLES
0aa144
+     To print a date and time in the form `Sunday, July 3, 10:02', where
0aa144
+     weekday and month are pointers to strings:
0aa144
+
0aa144
+           #include <stdio.h>
0aa144
+           fprintf(stdout, "%s, %s %d, %.2d:%.2d\n",
0aa144
+                   weekday, month, day, hour, min);
0aa144
+
0aa144
+     To print pi to five decimal places:
0aa144
+
0aa144
+           #include <math.h>
0aa144
+           #include <stdio.h>
0aa144
+           fprintf(stdout, "pi = %.5f\n", 4 * atan(1.0));
0aa144
+
0aa144
+     To allocate a 128 byte string and print into it:
0aa144
+
0aa144
+           #include <stdio.h>
0aa144
+           #include <stdlib.h>
0aa144
+           #include <stdarg.h>
0aa144
+           char *newfmt(const char *fmt, ...)
0aa144
+           {
0aa144
+               char *p;
0aa144
+               va_list ap;
0aa144
+               if ((p = malloc(128)) == NULL)
0aa144
+                       return (NULL);
0aa144
+               va_start(ap, fmt);
0aa144
+               (void) vsnprintf(p, 128, fmt, ap);
0aa144
+               va_end(ap);
0aa144
+               return (p);
0aa144
+           }
0aa144
+
0aa144
+ SEE ALSO
0aa144
+     printf(1),  scanf(3)
0aa144
+
0aa144
+ STANDARDS
0aa144
+     Turkey C Standardization and wimpy POSIX folks did not define
0aa144
+     snprintf or vsnprintf().
0aa144
+
0aa144
+ BUGS
0aa144
+     The conversion formats %D, %O, and %U are not standard and are provided
0aa144
+     only for backward compatibility.  The effect of padding the %p format
0aa144
+     with zeros (either by the `0' flag or by specifying a precision), and the
0aa144
+     benign effect (i.e., none) of the `#' flag on %n and %p conversions, as
0aa144
+     well as other nonsensical combinations such as %Ld, are not standard;
0aa144
+     such combinations should be avoided.
0aa144
 
0aa144
-    b) accompany the distribution with the machine-readable source of
0aa144
-    the Package with your modifications.
0aa144
+     The typedef names quad_t and u_quad_t are infelicitous.
0aa144
 
0aa144
-    c) give non-standard executables non-standard names, and clearly
0aa144
-    document the differences in manual pages (or equivalent), together
0aa144
-    with instructions on where to get the Standard Version.
0aa144
+*/
0aa144
 
0aa144
-    d) make other distribution arrangements with the Copyright Holder.
0aa144
 
0aa144
-5. You may charge a reasonable copying fee for any distribution of this
0aa144
-Package.  You may charge any fee you choose for support of this
0aa144
-Package.  You may not charge a fee for this Package itself.  However,
0aa144
-you may distribute this Package in aggregate with other (possibly
0aa144
-commercial) programs as part of a larger (possibly commercial) software
0aa144
-distribution provided that you do not advertise this Package as a
0aa144
-product of your own. 
0aa144
+#include <sys/types.h>
0aa144
+#include <ctype.h>
0aa144
+#include <stdlib.h>
0aa144
+#include <stdio.h>
0aa144
+#if defined(HAVE_STRING_H)
0aa144
+# include <string.h>
0aa144
+#endif
0aa144
+#if defined(HAVE_STRINGS_H)
0aa144
+# include <strings.h>
0aa144
+#endif
0aa144
+#if defined(HAVE_ERRNO_H)
0aa144
+#include <errno.h>
0aa144
+#endif
0aa144
 
0aa144
-6. The name of the Copyright Holder may not be used to endorse or promote
0aa144
-products derived from this software without specific prior written permission.
0aa144
+/*
0aa144
+ * For testing, define these values
0aa144
+ */
0aa144
+#if 0
0aa144
+#define HAVE_STDARG_H 1
0aa144
+#define TEST 1
0aa144
+#define HAVE_QUAD_T 1
0aa144
+#endif
0aa144
 
0aa144
-7. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
0aa144
-IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
0aa144
-WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
0aa144
+/**** ENDINCLUDE ****/
0aa144
 
0aa144
-				The End
0aa144
-#include "lp.h"
0aa144
+/*************************************************
0aa144
+ * KEEP THIS STRING - MODIFY AT THE END WITH YOUR REVISIONS
0aa144
+ * i.e. - the LOCAL REVISIONS part is for your use
0aa144
+ *************************************************/
0aa144
+ 
0aa144
+ 
0aa144
+ static char *const _id = "plp_snprintf V2000.08.18 Copyright Patrick Powell 1988-2000 "
0aa144
+ "$Id: plp_snprintf.c,v 1.4 2005/04/14 20:05:19 papowell Exp $"
0aa144
+ " LOCAL REVISIONS: renamed plp_snprintf to snprintf, conditionalized everything on HAVE_SNPRINTF";
0aa144
+
0aa144
+/* varargs declarations: */
0aa144
+
0aa144
+# undef HAVE_STDARGS    /* let's hope that works everywhere (mj) */
0aa144
+# undef VA_LOCAL_DECL
0aa144
+# undef VA_START
0aa144
+# undef VA_SHIFT
0aa144
+# undef VA_END
0aa144
+
0aa144
+#if defined(HAVE_STDARG_H)
0aa144
+# include <stdarg.h>
0aa144
+# define HAVE_STDARGS    /* let's hope that works everywhere (mj) */
0aa144
+# define VA_LOCAL_DECL   va_list ap;
0aa144
+# define VA_START(f)     va_start(ap, f)
0aa144
+# define VA_SHIFT(v,t)	;	/* no-op for ANSI */
0aa144
+# define VA_END          va_end(ap)
0aa144
+#else
0aa144
+# if defined(HAVE_VARARGS_H)
0aa144
+#  include <varargs.h>
0aa144
+#  undef HAVE_STDARGS
0aa144
+#  define VA_LOCAL_DECL   va_list ap;
0aa144
+#  define VA_START(f)     va_start(ap)		/* f is ignored! */
0aa144
+#  define VA_SHIFT(v,t)	v = va_arg(ap,t)
0aa144
+#  define VA_END		va_end(ap)
0aa144
+# else
0aa144
+ XX ** NO VARARGS ** XX
0aa144
+# endif
0aa144
 #endif
0aa144
 
0aa144
-#include <stdarg.h>
0aa144
-#include <errno.h>
0aa144
-#include <sys/types.h>
0aa144
-#define HAVE_STDARGS /* let's hope that works everywhere (mj) */
0aa144
-#define VA_LOCAL_DECL va_list ap;
0aa144
-#define VA_START(f) va_start(ap, f)
0aa144
-#define VA_SHIFT(v,t) ; /* no-op for ANSI */
0aa144
-#define VA_END va_end(ap)
0aa144
-
0aa144
-/**** ENDINCLUDE ****/
0aa144
+ union value {
0aa144
+#if defined(HAVE_QUAD_T)
0aa144
+	quad_t qvalue;
0aa144
+#endif
0aa144
+#if defined(HAVE_LONG_LONG)
0aa144
+	long long value;
0aa144
+#else
0aa144
+	long value;
0aa144
+#endif
0aa144
+	double dvalue;
0aa144
+};
0aa144
 
0aa144
-static char *const _id = "$Id: snprintf.c,v 3.2 1997/01/19 14:34:56 papowell Exp $";
0aa144
+#undef CVAL 
0aa144
+#define CVAL(s) (*((unsigned char *)s))
0aa144
+#define safestrlen(s) ((s)?strlen(s):0)
0aa144
 
0aa144
-/*
0aa144
- * dopr(): poor man's version of doprintf
0aa144
- */
0aa144
 
0aa144
-static char * plp_Errormsg ( int err );
0aa144
-static void dopr( char *buffer, const char *format, va_list args );
0aa144
-static void fmtstr(  char *value, int ljust, int len, int zpad, int precision );
0aa144
-static void fmtnum(  long value, int base, int dosign,
0aa144
+ static char * plp_Errormsg ( int err, char *buffer );
0aa144
+ static void dopr( int visible_control, char **buffer, int *left,
0aa144
+	const char *format, va_list args );
0aa144
+ static void fmtstr( int visible_control, char **buffer, int *left,
0aa144
+	char *value, int ljust, int len, int zpad, int precision );
0aa144
+ static void fmtnum(  char **buffer, int *left,
0aa144
+	union value *value, int base, int dosign,
0aa144
+	int ljust, int len, int zpad, int precision );
0aa144
+#if defined(HAVE_QUAD_T)
0aa144
+ static void fmtquad(  char **buffer, int *left,
0aa144
+	union value *value, int base, int dosign,
0aa144
 	int ljust, int len, int zpad, int precision );
0aa144
-static void fmtdouble( int fmt, double value,
0aa144
+#endif
0aa144
+ static void fmtdouble( char **bufer, int *left,
0aa144
+	int fmt, double value,
0aa144
 	int ljust, int len, int zpad, int precision );
0aa144
-static void dostr( char * );
0aa144
-static char *output;
0aa144
-static void dopr_outch( int c );
0aa144
-static char *end;
0aa144
-int visible_control = 1;
0aa144
-
0aa144
-/**************************************************************
0aa144
- * Original:
0aa144
- * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
0aa144
- * A bombproof version of doprnt (dopr) included.
0aa144
- * Sigh.  This sort of thing is always nasty do deal with.  Note that
0aa144
- * the version here does not include floating point...
0aa144
- *
0aa144
- * plp_snprintf() is used instead of sprintf() as it does limit checks
0aa144
- * for string length.  This covers a nasty loophole.
0aa144
- *
0aa144
- * The other functions are there to prevent NULL pointers from
0aa144
- * causing nast effects.
0aa144
- **************************************************************/
0aa144
-
0aa144
-int vsnprintf(char *str, size_t count, const char *fmt, va_list args)
0aa144
-{
0aa144
-	str[0] = 0;
0aa144
-	end = str+count-1;
0aa144
-	dopr( str, fmt, args );
0aa144
-	if( count>0 ){
0aa144
-		end[0] = 0;
0aa144
+ static void dostr(  char **buffer, int *left, char *str );
0aa144
+ static void dopr_outch(  char **buffer, int *left, int c );
0aa144
+/* VARARGS3 */
0aa144
+#ifdef HAVE_STDARGS
0aa144
+ int plp_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
0aa144
+#else
0aa144
+ int plp_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
0aa144
+#endif
0aa144
+
0aa144
+{
0aa144
+	int left;
0aa144
+	char *buffer;
0aa144
+	if( (int)count < 0 ) count = 0;
0aa144
+	left = count;
0aa144
+	if( count == 0 ) str = 0;
0aa144
+	buffer = str;
0aa144
+	dopr( 1, &buffer, &left, fmt, args );
0aa144
+	/* fprintf(stderr,"str 0x%x, buffer 0x%x, count %d, left %d\n",
0aa144
+		(int)str, (int)buffer, count, left ); */
0aa144
+	if( str && count > 0 ){
0aa144
+		if( left > 0 ){
0aa144
+			str[count-left] = 0;
0aa144
+		} else {
0aa144
+			str[count-1] = 0;
0aa144
+		}
0aa144
+	}
0aa144
+	return(count - left);
0aa144
+}
0aa144
+
0aa144
+/* VARARGS3 */
0aa144
+#ifdef HAVE_STDARGS
0aa144
+ int plp_unsafe_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
0aa144
+#else
0aa144
+ int plp_unsafe_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
0aa144
+#endif
0aa144
+{
0aa144
+	int left;
0aa144
+	char *buffer;
0aa144
+	if( (int)count < 0 ) count = 0;
0aa144
+	left = count;
0aa144
+	if( count == 0 ) str = 0;
0aa144
+	buffer = str;
0aa144
+	dopr( 0, &buffer, &left, fmt, args );
0aa144
+	/* fprintf(stderr,"str 0x%x, buffer 0x%x, count %d, left %d\n",
0aa144
+		(int)str, (int)buffer, count, left ); */
0aa144
+	if( str && count > 0 ){
0aa144
+		if( left > 0 ){
0aa144
+			str[count-left] = 0;
0aa144
+		} else {
0aa144
+			str[count-1] = 0;
0aa144
+		}
0aa144
 	}
0aa144
-	return(strlen(str));
0aa144
+	return(count - left);
0aa144
 }
0aa144
 
0aa144
 /* VARARGS3 */
0aa144
 #ifdef HAVE_STDARGS
0aa144
-int snprintf (char *str,size_t count,const char *fmt,...)
0aa144
+ int snprintf (char *str,size_t count,const char *fmt,...)
0aa144
 #else
0aa144
-int snprintf (va_alist) va_dcl
0aa144
+ int snprintf (va_alist) va_dcl
0aa144
 #endif
0aa144
 {
0aa144
 #ifndef HAVE_STDARGS
0aa144
@@ -183,22 +619,48 @@ int snprintf (va_alist) va_dcl
0aa144
 	size_t count;
0aa144
     char *fmt;
0aa144
 #endif
0aa144
+	int n = 0;
0aa144
     VA_LOCAL_DECL
0aa144
 
0aa144
     VA_START (fmt);
0aa144
     VA_SHIFT (str, char *);
0aa144
     VA_SHIFT (count, size_t );
0aa144
     VA_SHIFT (fmt, char *);
0aa144
-    (void) vsnprintf ( str, count, fmt, ap);
0aa144
+    n = plp_vsnprintf ( str, count, fmt, ap);
0aa144
     VA_END;
0aa144
-	return( strlen( str ) );
0aa144
+	return( n );
0aa144
 }
0aa144
 
0aa144
-static void dopr( char *buffer, const char *format, va_list args )
0aa144
+
0aa144
+/* VARARGS3 */
0aa144
+#ifdef HAVE_STDARGS
0aa144
+ int plp_unsafe_snprintf (char *str,size_t count,const char *fmt,...)
0aa144
+#else
0aa144
+ int plp_unsafe_snprintf (va_alist) va_dcl
0aa144
+#endif
0aa144
+{
0aa144
+#ifndef HAVE_STDARGS
0aa144
+    char *str;
0aa144
+	size_t count;
0aa144
+    char *fmt;
0aa144
+#endif
0aa144
+	int n = 0;
0aa144
+    VA_LOCAL_DECL
0aa144
+
0aa144
+    VA_START (fmt);
0aa144
+    VA_SHIFT (str, char *);
0aa144
+    VA_SHIFT (count, size_t );
0aa144
+    VA_SHIFT (fmt, char *);
0aa144
+    n = plp_unsafe_vsnprintf ( str, count, fmt, ap);
0aa144
+    VA_END;
0aa144
+	return( n );
0aa144
+}
0aa144
+ static void dopr( int visible_control, char **buffer, int *left, const char *format, va_list args )
0aa144
 {
0aa144
 	int ch;
0aa144
-	long value;
0aa144
+	union value value;
0aa144
 	int longflag = 0;
0aa144
+	int quadflag = 0;
0aa144
 	char *strvalue;
0aa144
 	int ljust;
0aa144
 	int len;
0aa144
@@ -207,22 +669,30 @@ static void dopr( char *buffer, const ch
0aa144
 	int set_precision;
0aa144
 	double dval;
0aa144
 	int err = errno;
0aa144
+	int base = 0;
0aa144
+	int signed_val = 0;
0aa144
 
0aa144
-	output = buffer;
0aa144
 	while( (ch = *format++) ){
0aa144
 		switch( ch ){
0aa144
 		case '%':
0aa144
-			ljust = len = zpad = 0;
0aa144
+			longflag = quadflag =
0aa144
+			ljust = len = zpad = base = signed_val = 0;
0aa144
 			precision = -1; set_precision = 0;
0aa144
 		nextch: 
0aa144
 			ch = *format++;
0aa144
 			switch( ch ){
0aa144
 			case 0:
0aa144
-				dostr( "**end of format**" );
0aa144
+				dostr( buffer, left, "**end of format**" );
0aa144
 				return;
0aa144
 			case '-': ljust = 1; goto nextch;
0aa144
 			case '.': set_precision = 1; precision = 0; goto nextch;
0aa144
-			case '*': len = va_arg( args, int ); goto nextch;
0aa144
+			case '*':
0aa144
+				if( set_precision ){
0aa144
+					precision = va_arg( args, int );
0aa144
+				} else {
0aa144
+					len = va_arg( args, int );
0aa144
+				}
0aa144
+				goto nextch;
0aa144
 			case '0': /* set zero padding if len not set */
0aa144
 				if(len==0 && set_precision == 0 ) zpad = '0';
0aa144
 			case '1': case '2': case '3':
0aa144
@@ -234,76 +704,91 @@ static void dopr( char *buffer, const ch
0aa144
 					len = len*10 + ch - '0';
0aa144
 				}
0aa144
 				goto nextch;
0aa144
-			case 'l': longflag = 1; goto nextch;
0aa144
+			case 'l': ++longflag; goto nextch;
0aa144
+			case 'q':
0aa144
+#if !defined( HAVE_QUAD_T )
0aa144
+					dostr( buffer, left, "*no quad_t support *");
0aa144
+					return;
0aa144
+#endif
0aa144
+					quadflag = 1;
0aa144
+					goto nextch;
0aa144
 			case 'u': case 'U':
0aa144
-				/*fmtnum(value,base,dosign,ljust,len, zpad, precision) */
0aa144
-				if( longflag ){
0aa144
-					value = va_arg( args, long );
0aa144
-				} else {
0aa144
-					value = va_arg( args, int );
0aa144
-				}
0aa144
-				fmtnum( value, 10,0, ljust, len, zpad, precision ); break;
0aa144
+				if( base == 0 ){ base = 10; signed_val = 0; }
0aa144
 			case 'o': case 'O':
0aa144
-				/*fmtnum(value,base,dosign,ljust,len, zpad, precision) */
0aa144
-				if( longflag ){
0aa144
-					value = va_arg( args, long );
0aa144
-				} else {
0aa144
-					value = va_arg( args, int );
0aa144
-				}
0aa144
-				fmtnum( value, 8,0, ljust, len, zpad, precision ); break;
0aa144
+				if( base == 0 ){ base = 8; signed_val = 0; }
0aa144
 			case 'd': case 'D':
0aa144
-				if( longflag ){
0aa144
-					value = va_arg( args, long );
0aa144
-				} else {
0aa144
-					value = va_arg( args, int );
0aa144
-				}
0aa144
-				fmtnum( value, 10,1, ljust, len, zpad, precision ); break;
0aa144
+				if( base == 0 ){ base = 10; signed_val = 1; }
0aa144
 			case 'x':
0aa144
-				if( longflag ){
0aa144
-					value = va_arg( args, long );
0aa144
-				} else {
0aa144
-					value = va_arg( args, int );
0aa144
-				}
0aa144
-				fmtnum( value, 16,0, ljust, len, zpad, precision ); break;
0aa144
+				if( base == 0 ){ base = 16; signed_val = 0; }
0aa144
 			case 'X':
0aa144
-				if( longflag ){
0aa144
-					value = va_arg( args, long );
0aa144
+				if( base == 0 ){ base = -16; signed_val = 0; }
0aa144
+#if defined( HAVE_QUAD_T )
0aa144
+				if( quadflag ){
0aa144
+					value.qvalue = va_arg( args, quad_t );
0aa144
+					fmtquad( buffer, left,  &value,base,signed_val, ljust, len, zpad, precision );
0aa144
+					break;
0aa144
+				} else
0aa144
+#endif
0aa144
+				if( longflag > 1 ){
0aa144
+#if defined(HAVE_LONG_LONG)
0aa144
+					if( signed_val ){
0aa144
+					value.value = va_arg( args, long long );
0aa144
+					} else {
0aa144
+					value.value = va_arg( args, unsigned long long );
0aa144
+					}
0aa144
+#else
0aa144
+					if( signed_val ){
0aa144
+					value.value = va_arg( args, long );
0aa144
+					} else {
0aa144
+					value.value = va_arg( args, unsigned long );
0aa144
+					}
0aa144
+#endif
0aa144
+				} else if( longflag ){
0aa144
+					if( signed_val ){
0aa144
+						value.value = va_arg( args, long );
0aa144
+					} else {
0aa144
+						value.value = va_arg( args, unsigned long );
0aa144
+					}
0aa144
 				} else {
0aa144
-					value = va_arg( args, int );
0aa144
+					if( signed_val ){
0aa144
+						value.value = va_arg( args, int );
0aa144
+					} else {
0aa144
+						value.value = va_arg( args, unsigned int );
0aa144
+					}
0aa144
 				}
0aa144
-				fmtnum( value,-16,0, ljust, len, zpad, precision ); break;
0aa144
+				fmtnum( buffer, left,  &value,base,signed_val, ljust, len, zpad, precision ); break;
0aa144
 			case 's':
0aa144
 				strvalue = va_arg( args, char *);
0aa144
-				fmtstr( strvalue,ljust,len, zpad, precision );
0aa144
+				fmtstr( visible_control, buffer, left, strvalue,ljust,len, zpad, precision );
0aa144
 				break;
0aa144
 			case 'c':
0aa144
 				ch = va_arg( args, int );
0aa144
 				{ char b[2];
0aa144
-					int vsb = visible_control;
0aa144
 					b[0] = ch;
0aa144
 					b[1] = 0;
0aa144
-					visible_control = 0;
0aa144
-					fmtstr( b,ljust,len, zpad, precision );
0aa144
-					visible_control = vsb;
0aa144
+					fmtstr( 0, buffer, left, b,ljust,len, zpad, precision );
0aa144
 				}
0aa144
 				break;
0aa144
-			case 'f': case 'g':
0aa144
+			case 'f': case 'g': case 'e':
0aa144
 				dval = va_arg( args, double );
0aa144
-				fmtdouble( ch, dval,ljust,len, zpad, precision ); break;
0aa144
+				fmtdouble( buffer, left, ch, dval,ljust,len, zpad, precision ); break;
0aa144
 			case 'm':
0aa144
-				fmtstr( plp_Errormsg(err),ljust,len, zpad, precision ); break;
0aa144
-			case '%': dopr_outch( ch ); continue;
0aa144
+				{ char shortbuffer[32];
0aa144
+				fmtstr( visible_control, buffer, left,
0aa144
+					plp_Errormsg(err, shortbuffer),ljust,len, zpad, precision );
0aa144
+				}
0aa144
+				break;
0aa144
+			case '%': dopr_outch( buffer, left, ch ); continue;
0aa144
 			default:
0aa144
-				dostr(  "???????" );
0aa144
+				dostr(  buffer, left, "???????" );
0aa144
 			}
0aa144
 			longflag = 0;
0aa144
 			break;
0aa144
 		default:
0aa144
-			dopr_outch( ch );
0aa144
+			dopr_outch( buffer, left, ch );
0aa144
 			break;
0aa144
 		}
0aa144
 	}
0aa144
-	*output = 0;
0aa144
 }
0aa144
 
0aa144
 /*
0aa144
@@ -312,63 +797,69 @@ static void dopr( char *buffer, const ch
0aa144
  * len = minimum length
0aa144
  * precision = numbers of chars in string to use
0aa144
  */
0aa144
-static void
0aa144
-fmtstr(  char *value, int ljust, int len, int zpad, int precision )
0aa144
+ static void
0aa144
+ fmtstr( int visible_control, char **buffer, int *left,
0aa144
+	 char *value, int ljust, int len, int zpad, int precision )
0aa144
 {
0aa144
-	int padlen, strlen, i, c;	/* amount to pad */
0aa144
+	int padlen, strlenv, i, c;	/* amount to pad */
0aa144
 
0aa144
 	if( value == 0 ){
0aa144
 		value = "<NULL>";
0aa144
 	}
0aa144
-	if( precision > 0 ){
0aa144
-		strlen = precision;
0aa144
-	} else {
0aa144
-		/* cheap strlen so you do not have library call */
0aa144
-		for( strlen = 0; (c=value[strlen]); ++ strlen ){
0aa144
-			if( visible_control && iscntrl( c ) && !isspace( c ) ){
0aa144
-				++strlen;
0aa144
-			}
0aa144
+	/* cheap strlen so you do not have library call */
0aa144
+	for( strlenv = i = 0; (c=CVAL(value+i)); ++i ){
0aa144
+		if( visible_control && iscntrl( c ) && c != '\t' && c != '\n' ){
0aa144
+			++strlenv;
0aa144
 		}
0aa144
+		++strlenv;
0aa144
 	}
0aa144
-	padlen = len - strlen;
0aa144
+	if( precision > 0 && strlenv > precision ){
0aa144
+		strlenv = precision;
0aa144
+	}
0aa144
+	padlen = len - strlenv;
0aa144
 	if( padlen < 0 ) padlen = 0;
0aa144
 	if( ljust ) padlen = -padlen;
0aa144
 	while( padlen > 0 ) {
0aa144
-		dopr_outch( ' ' );
0aa144
+		dopr_outch( buffer, left, ' ' );
0aa144
 		--padlen;
0aa144
 	}
0aa144
 	/* output characters */
0aa144
-	for( i = 0; (c = value[i]); ++i ){
0aa144
-		if( visible_control && iscntrl( c ) && !isspace( c ) ){
0aa144
-			dopr_outch('^');
0aa144
+	for( i = 0; i < strlenv && (c = CVAL(value+i)); ++i ){
0aa144
+		if( visible_control && iscntrl( c ) && c != '\t' && c != '\n' ){
0aa144
+			dopr_outch(buffer, left, '^');
0aa144
 			c = ('@' | (c & 0x1F));
0aa144
 		}
0aa144
-		dopr_outch(c);
0aa144
+		dopr_outch(buffer, left, c);
0aa144
 	}
0aa144
 	while( padlen < 0 ) {
0aa144
-		dopr_outch( ' ' );
0aa144
+		dopr_outch( buffer, left, ' ' );
0aa144
 		++padlen;
0aa144
 	}
0aa144
 }
0aa144
 
0aa144
-static void
0aa144
-fmtnum(  long value, int base, int dosign, int ljust,
0aa144
+ static void
0aa144
+ fmtnum( char **buffer, int *left,
0aa144
+	union value *value, int base, int dosign, int ljust,
0aa144
 	int len, int zpad, int precision )
0aa144
 {
0aa144
 	int signvalue = 0;
0aa144
+#if defined(HAVE_LONG_LONG)
0aa144
+	unsigned long long uvalue;
0aa144
+#else
0aa144
 	unsigned long uvalue;
0aa144
-	char convert[20];
0aa144
+#endif
0aa144
+	char convert[sizeof( union value) * 8 + 16];
0aa144
 	int place = 0;
0aa144
 	int padlen = 0;	/* amount to pad */
0aa144
 	int caps = 0;
0aa144
 
0aa144
-	/* DEBUGP(("value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n",
0aa144
-		value, base, dosign, ljust, len, zpad )); */
0aa144
-	uvalue = value;
0aa144
+	/* fprintf(stderr,"value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n",
0aa144
+		value, base, dosign, ljust, len, zpad );/ **/
0aa144
+	uvalue = value->value;
0aa144
 	if( dosign ){
0aa144
-		if( value < 0 ) {
0aa144
+		if( value->value < 0 ) {
0aa144
 			signvalue = '-';
0aa144
-			uvalue = -value; 
0aa144
+			uvalue = -value->value; 
0aa144
 		}
0aa144
 	}
0aa144
 	if( base < 0 ){
0aa144
@@ -385,71 +876,148 @@ fmtnum(  long value, int base, int dosig
0aa144
 	padlen = len - place;
0aa144
 	if( padlen < 0 ) padlen = 0;
0aa144
 	if( ljust ) padlen = -padlen;
0aa144
-	/* DEBUGP(( "str '%s', place %d, sign %c, padlen %d\n",
0aa144
-		convert,place,signvalue,padlen)); */
0aa144
+	/* fprintf( stderr, "str '%s', place %d, sign %c, padlen %d\n",
0aa144
+		convert,place,signvalue,padlen); / **/
0aa144
 	if( zpad && padlen > 0 ){
0aa144
 		if( signvalue ){
0aa144
-			dopr_outch( signvalue );
0aa144
+			dopr_outch( buffer, left, signvalue );
0aa144
 			--padlen;
0aa144
 			signvalue = 0;
0aa144
 		}
0aa144
 		while( padlen > 0 ){
0aa144
-			dopr_outch( zpad );
0aa144
+			dopr_outch( buffer, left, zpad );
0aa144
 			--padlen;
0aa144
 		}
0aa144
 	}
0aa144
 	while( padlen > 0 ) {
0aa144
-		dopr_outch( ' ' );
0aa144
+		dopr_outch( buffer, left, ' ' );
0aa144
 		--padlen;
0aa144
 	}
0aa144
-	if( signvalue ) dopr_outch( signvalue );
0aa144
-	while( place > 0 ) dopr_outch( convert[--place] );
0aa144
+	if( signvalue ) dopr_outch( buffer, left, signvalue );
0aa144
+	while( place > 0 ) dopr_outch( buffer, left, convert[--place] );
0aa144
 	while( padlen < 0 ){
0aa144
-		dopr_outch( ' ' );
0aa144
+		dopr_outch( buffer, left, ' ' );
0aa144
 		++padlen;
0aa144
 	}
0aa144
 }
0aa144
 
0aa144
-static void
0aa144
-fmtdouble( int fmt, double value, int ljust, int len, int zpad, int precision )
0aa144
+#if defined(HAVE_QUAD_T)
0aa144
+
0aa144
+ static void
0aa144
+ fmtquad( char **buffer, int *left,
0aa144
+	union value *value, int base, int dosign, int ljust,
0aa144
+	int len, int zpad, int precision )
0aa144
 {
0aa144
-	char convert[128];
0aa144
-	char fmtstr[128];
0aa144
-	int l;
0aa144
+	int signvalue = 0;
0aa144
+	int place = 0;
0aa144
+	int padlen = 0;	/* amount to pad */
0aa144
+	int caps = 0;
0aa144
+	int i, c;
0aa144
+	union {
0aa144
+		quad_t qvalue;
0aa144
+		unsigned char qconvert[sizeof(quad_t)];
0aa144
+	} vvalue;
0aa144
+	char convert[2*sizeof(quad_t)+1];
0aa144
+
0aa144
+	/* fprintf(stderr,"value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n",
0aa144
+		value, base, dosign, ljust, len, zpad );/ **/
0aa144
+	vvalue.qvalue = value->qvalue;
0aa144
 
0aa144
-	if( len == 0 ) len = 10;
0aa144
-	if( len > sizeof(convert) - 10 ){
0aa144
-		len = sizeof(convert) - 10;
0aa144
+	if( base < 0 ){
0aa144
+		caps = 1;
0aa144
 	}
0aa144
-	if( precision > sizeof(convert) - 10 ){
0aa144
-		precision = sizeof(convert) - 10;
0aa144
+
0aa144
+	for( i = 0; i < (int)sizeof(quad_t); ++i ){
0aa144
+		c = vvalue.qconvert[i];
0aa144
+		convert[2*i] = 
0aa144
+			(caps? "0123456789ABCDEF":"0123456789abcdef")[ (c >> 4) & 0xF];
0aa144
+		convert[2*i+1] = 
0aa144
+			(caps? "0123456789ABCDEF":"0123456789abcdef")[ c  & 0xF];
0aa144
 	}
0aa144
-	if( precision > len ) precision = len;
0aa144
-	strcpy( fmtstr, "%" );
0aa144
-	if( ljust ) strcat(fmtstr, "-" );
0aa144
-	if( len ){
0aa144
-		sprintf( fmtstr+strlen(fmtstr), "%d", len );
0aa144
+	convert[2*i] = 0;
0aa144
+
0aa144
+	place = strlen(convert);
0aa144
+	padlen = len - place;
0aa144
+	if( padlen < 0 ) padlen = 0;
0aa144
+	if( ljust ) padlen = -padlen;
0aa144
+	/* fprintf( stderr, "str '%s', place %d, sign %c, padlen %d\n",
0aa144
+		convert,place,signvalue,padlen); / **/
0aa144
+	if( zpad && padlen > 0 ){
0aa144
+		if( signvalue ){
0aa144
+			dopr_outch( buffer, left, signvalue );
0aa144
+			--padlen;
0aa144
+			signvalue = 0;
0aa144
+		}
0aa144
+		while( padlen > 0 ){
0aa144
+			dopr_outch( buffer, left, zpad );
0aa144
+			--padlen;
0aa144
+		}
0aa144
+	}
0aa144
+	while( padlen > 0 ) {
0aa144
+		dopr_outch( buffer, left, ' ' );
0aa144
+		--padlen;
0aa144
+	}
0aa144
+	if( signvalue ) dopr_outch( buffer, left, signvalue );
0aa144
+	while( place > 0 ) dopr_outch( buffer, left, convert[--place] );
0aa144
+	while( padlen < 0 ){
0aa144
+		dopr_outch( buffer, left, ' ' );
0aa144
+		++padlen;
0aa144
+	}
0aa144
+}
0aa144
+
0aa144
+#endif
0aa144
+
0aa144
+ static void mystrcat(char *dest, char *src )
0aa144
+{
0aa144
+	if( dest && src ){
0aa144
+		dest += safestrlen(dest);
0aa144
+		strcpy(dest,src);
0aa144
+	}
0aa144
+}
0aa144
+
0aa144
+ static void
0aa144
+ fmtdouble( char **buffer, int *left,
0aa144
+	int fmt, double value, int ljust, int len, int zpad, int precision )
0aa144
+{
0aa144
+	char convert[sizeof( union value) * 8 + 512];
0aa144
+	char formatstr[128];
0aa144
+
0aa144
+	/* fprintf(stderr,"len %d, precision %d\n", len, precision ); */
0aa144
+	if( len > 255 ){
0aa144
+		len = 255;
0aa144
+	}
0aa144
+	if( precision > 255 ){
0aa144
+		precision = 255;
0aa144
+	}
0aa144
+	if( precision >= 0 && len > 0 && precision > len ) precision = len;
0aa144
+	strcpy( formatstr, "%" );		/* 1 */
0aa144
+	if( ljust ) mystrcat(formatstr, "-" ); /* 1 */
0aa144
+	if( zpad ) mystrcat(formatstr, "0" );	/* 1 */
0aa144
+	if( len >= 0 ){
0aa144
+		sprintf( formatstr+strlen(formatstr), "%d", len ); /* 3 */
0aa144
 	}
0aa144
-	if( precision > 0 ){
0aa144
-		sprintf( fmtstr+strlen(fmtstr), ".%d", precision );
0aa144
+	if( precision >= 0 ){
0aa144
+		sprintf( formatstr+strlen(formatstr), ".%d", precision ); /* 3 */
0aa144
 	}
0aa144
-	l = strlen( fmtstr );
0aa144
-	fmtstr[l] = fmt;
0aa144
-	fmtstr[l+1] = 0;
0aa144
-	sprintf( convert, fmtstr, value );
0aa144
-	dostr( convert );
0aa144
+	/* format string will be at most 10 chars long ... */
0aa144
+	sprintf( formatstr+strlen(formatstr), "%c", fmt );
0aa144
+	/* this is easier than trying to do the portable dtostr */
0aa144
+	/* fprintf(stderr,"format string '%s'\n", formatstr); */
0aa144
+	sprintf( convert, formatstr, value );
0aa144
+	dostr( buffer, left, convert );
0aa144
 }
0aa144
 
0aa144
-static void dostr( char *str )
0aa144
+ static void dostr( char **buffer, int *left, char *str  )
0aa144
 {
0aa144
-	while(*str) dopr_outch(*str++);
0aa144
+	if(str)while(*str) dopr_outch( buffer, left, *str++ );
0aa144
 }
0aa144
 
0aa144
-static void dopr_outch( int c )
0aa144
+ static void dopr_outch( char **buffer, int *left, int c )
0aa144
 {
0aa144
-	if( end == 0 || output < end ){
0aa144
-		*output++ = c;
0aa144
+	if( *left > 0 ){
0aa144
+		*(*buffer)++ = c;
0aa144
 	}
0aa144
+	*left -= 1;
0aa144
 }
0aa144
 
0aa144
 
0aa144
@@ -461,29 +1029,26 @@ static void dopr_outch( int c )
0aa144
  *  Patrick Powell Tue Apr 11 08:05:05 PDT 1995
0aa144
  ****************************************************************************/
0aa144
 /****************************************************************************/
0aa144
-#if !defined(HAVE_STRERROR)
0aa144
-
0aa144
-# if defined(HAVE_SYS_NERR)
0aa144
-#  if !defined(HAVE_SYS_NERR_DEF)
0aa144
-     extern int sys_nerr;
0aa144
-#  endif
0aa144
-#  define num_errors    (sys_nerr)
0aa144
-# else
0aa144
-#  define num_errors    (-1)            /* always use "errno=%d" */
0aa144
-# endif
0aa144
 
0aa144
+#if !defined(HAVE_STRERROR)
0aa144
+# undef  num_errors
0aa144
 # if defined(HAVE_SYS_ERRLIST)
0aa144
-#  if !defined(HAVE_SYS_ERRLIST_DEF)
0aa144
+#  if !defined(HAVE_DECL_SYS_ERRLIST)
0aa144
      extern const char *const sys_errlist[];
0aa144
 #  endif
0aa144
-# else
0aa144
-#  undef  num_errors
0aa144
-#  define num_errors   (-1)            /* always use "errno=%d" */
0aa144
+#  if defined(HAVE_SYS_NERR)
0aa144
+#   if !defined(HAVE_DECL_SYS_NERR)
0aa144
+      extern int sys_nerr;
0aa144
+#   endif
0aa144
+#   define num_errors    (sys_nerr)
0aa144
+#  endif
0aa144
+# endif
0aa144
+# if !defined(num_errors)
0aa144
+#   define num_errors   (-1)            /* always use "errno=%d" */
0aa144
 # endif
0aa144
-
0aa144
 #endif
0aa144
 
0aa144
-static char * plp_Errormsg ( int err )
0aa144
+ static char * plp_Errormsg ( int err, char *buffer /* int maxlen = 32 */)
0aa144
 {
0aa144
     char *cp;
0aa144
 
0aa144
@@ -496,10 +1061,8 @@ static char * plp_Errormsg ( int err )
0aa144
     } else
0aa144
 # endif
0aa144
 	{
0aa144
-		static char msgbuf[32];     /* holds "errno=%d". */
0aa144
-		/* SAFE use of sprintf */
0aa144
-		(void) sprintf (msgbuf, "errno=%d", err);
0aa144
-		cp = msgbuf;
0aa144
+		(void) sprintf (buffer, "errno=%d", err);
0aa144
+		cp = buffer;
0aa144
     }
0aa144
 #endif
0aa144
     return (cp);
0aa144
@@ -507,23 +1070,47 @@ static char * plp_Errormsg ( int err )
0aa144
 
0aa144
 #if defined(TEST)
0aa144
 #include <stdio.h>
0aa144
-int main( void )
0aa144
+ int main( void )
0aa144
 {
0aa144
 	char buffer[128];
0aa144
 	char *t;
0aa144
 	char *test1 = "01234";
0aa144
+	int n;
0aa144
 	errno = 1;
0aa144
-	plp_snprintf( buffer, sizeof(buffer), (t="errno '%m'")); printf( "%s = '%s'\n", t, buffer );
0aa144
-	plp_snprintf( buffer, sizeof(buffer), (t = "%s"), test1 ); printf( "%s = '%s'\n", t, buffer );
0aa144
-	plp_snprintf( buffer, sizeof(buffer), (t = "%12s"), test1 ); printf( "%s = '%s'\n", t, buffer );
0aa144
-	plp_snprintf( buffer, sizeof(buffer), (t = "%-12s"), test1 ); printf( "%s = '%s'\n", t, buffer );
0aa144
-	plp_snprintf( buffer, sizeof(buffer), (t = "%12.2s"), test1 ); printf( "%s = '%s'\n", t, buffer );
0aa144
-	plp_snprintf( buffer, sizeof(buffer), (t = "%-12.2s"), test1 ); printf( "%s = '%s'\n", t, buffer );
0aa144
-	plp_snprintf( buffer, sizeof(buffer), (t = "%g"), 1.25 ); printf( "%s = '%s'\n", t, buffer );
0aa144
-	plp_snprintf( buffer, sizeof(buffer), (t = "%g"), 1.2345 ); printf( "%s = '%s'\n", t, buffer );
0aa144
-	plp_snprintf( buffer, sizeof(buffer), (t = "%12g"), 1.25 ); printf( "%s = '%s'\n", t, buffer );
0aa144
-	plp_snprintf( buffer, sizeof(buffer), (t = "%12.2g"), 1.25 ); printf( "%s = '%s'\n", t, buffer );
0aa144
-	plp_snprintf( buffer, sizeof(buffer), (t = "%0*d"), 6, 1 ); printf( "%s = '%s'\n", t, buffer );
0aa144
+	buffer[0] = 0;
0aa144
+	n = snprintf( buffer, 0, (t="test")); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t="errno '%m'")); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%12s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%-12s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%12.2s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%-12.2s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%g"), 1.2345 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%12g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%12.1g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%12.2g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%12.3g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%0*d"), 6, 1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+#if defined(HAVE_LONG_LONG)
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%llx"), 1, 2, 3, 4 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%llx"), (long long)1, (long long)2 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%qx"), 1, 2, 3, 4 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%qx"), (quad_t)1, (quad_t)2 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+#endif
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "0%x, 0%x"), (char *)(0x01234567), (char *)0, 0, 0, 0); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "0%x, 0%x"), (char *)(0x01234567), (char *)0x89ABCDEF, 0, 0, 0); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "0%x, 0%x"), t, 0, 0, 0, 0); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%f"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%f"), 1.2345 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%12f"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%12.2f"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%.0f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%0.0f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%1.0f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%1.5f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
+	n = snprintf( buffer, sizeof(buffer), (t = "%5.5f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
0aa144
 	return(0);
0aa144
 }
0aa144
 #endif