1 diff -up xsane-0.999/lib/snprintf.c.OLD xsane-0.999/lib/snprintf.c
2 --- xsane-0.999/lib/snprintf.c.OLD 2014-05-29 10:42:26.795419830 -0400
3 +++ xsane-0.999/lib/snprintf.c 2014-05-29 10:48:58.544431002 -0400
8 -/***************************************************************************
9 - * LPRng - An Extended Print Spooler System
11 - * Copyright 1988-1997, Patrick Powell, San Diego, CA
13 - * See below for conditions of use.
15 - ***************************************************************************
16 - * MODULE: snprintf.c
17 - * PURPOSE: LPRng version of printf - absolutely bombproof (hopefully!)
18 +/**************************************************************************
19 + * Copyright 1994-2003 Patrick Powell, San Diego, CA <papowell@lprng.com>
20 **************************************************************************/
23 - The "Artistic License"
27 -The intent of this document is to state the conditions under which a
28 -Package may be copied, such that the Copyright Holder maintains some
29 -semblance of artistic control over the development of the package,
30 -while giving the users of the package the right to use and distribute
31 -the Package in a more-or-less customary fashion, plus the right to make
32 -reasonable modifications.
36 - "Package" refers to the collection of files distributed by the
37 - Copyright Holder, and derivatives of that collection of files
38 - created through textual modification.
40 - "Standard Version" refers to such a Package if it has not been
41 - modified, or has been modified in accordance with the wishes
42 - of the Copyright Holder as specified below.
44 - "Copyright Holder" is whoever is named in the copyright or
45 - copyrights for the package.
47 - "You" is you, if you are thinking about copying or distributing
50 - "Reasonable copying fee" is whatever you can justify on the
51 - basis of media cost, duplication charges, time of people involved,
52 - and so on. (You will not be required to justify it to the
53 - Copyright Holder, but only to the computing community at large
54 - as a market that must bear the fee.)
56 - "Freely Available" means that no fee is charged for the item
57 - itself, though there may be fees involved in handling the item.
58 - It also means that recipients of the item may redistribute it
59 - under the same conditions they received it.
61 -1. You may make and give away verbatim copies of the source form of the
62 -Standard Version of this Package without restriction, provided that you
63 -duplicate all of the original copyright notices and associated disclaimers.
65 -2. You may apply bug fixes, portability fixes and other modifications
66 -derived from the Public Domain or from the Copyright Holder. A Package
67 -modified in such a way shall still be considered the Standard Version.
69 -3. You may otherwise modify your copy of this Package in any way, provided
70 -that you insert a prominent notice in each changed file stating how and
71 -when you changed that file, and provided that you do at least ONE of the
74 - a) place your modifications in the Public Domain or otherwise make them
75 - Freely Available, such as by posting said modifications to Usenet or
76 - an equivalent medium, or placing the modifications on a major archive
77 - site such as uunet.uu.net, or by allowing the Copyright Holder to include
78 - your modifications in the Standard Version of the Package.
80 - b) use the modified Package only within your corporation or organization.
82 - c) rename any non-standard executables so the names do not conflict
83 - with standard executables, which must also be provided, and provide
84 - a separate manual page for each non-standard executable that clearly
85 - documents how it differs from the Standard Version.
87 - d) make other distribution arrangements with the Copyright Holder.
89 -4. You may distribute the programs of this Package in object code or
90 -executable form, provided that you do at least ONE of the following:
94 - a) distribute a Standard Version of the executables and library files,
95 - together with instructions (in the manual page or equivalent) on where
96 - to get the Standard Version.
97 + snprintf( char *buffer, int len, const char *format,...)
98 + plp_unsafe_snprintf( char *buffer, int len, const char *format,...)
99 + its horribly unsafe companion that does NOT protect you from
100 + the printing of evil control characters, but may be necessary
101 + See the man page documentation below
103 + This version of snprintf was developed originally for printing
104 + on a motley collection of specialized hardware that had NO IO
105 + library. Due to contractual restrictions, a clean room implementation
106 + of the printf() code had to be developed.
108 + The method chosen for printf was to be as paranoid as possible,
109 + as these platforms had NO memory protection, and very small
110 + address spaces. This made it possible to try to print
111 + very long strings, i.e. - all of memory, very easily. To guard
112 + against this, all printing was done via a buffer, generous enough
113 + to hold strings, but small enough to protect against overruns,
116 + Strangely enough, this proved to be of immense importance when
117 + SPRINTFing to a buffer on a stack... The rest, of course, is
118 + well known, as buffer overruns in the stack are a common way to
119 + do horrible things to operating systems, security, etc etc.
121 + This version of snprintf is VERY limited by modern standards.
124 + First Released Version - 1994. This version had NO comments.
125 + First Released Version - 1994. This version had NO comments.
126 + Second Major Released Version - Tue May 23 10:43:44 PDT 2000
127 + Configuration and other items changed. Read this doc.
128 + Treat this as a new version.
129 + Minor Revision - Mon Apr 1 09:41:28 PST 2002
130 + - fixed up some constants and casts
132 + COPYRIGHT AND TERMS OF USE:
134 + You may use, copy, distribute, or otherwise incorporate this software
135 + and documentation into any product or other item, provided that
136 + the copyright in the documentation and source code as well as the
137 + source code generated constant strings in the object, executable
138 + or other code remain in place and are present in executable modules
141 + You may modify this code as appropriate to your usage; however the
142 + modified version must be identified by changing the various source
143 + and object code identification strings as is appropriately noted
144 + in the source code.
146 + You can use this with the GNU CONFIGURE utility.
147 + This should define the following macros appropriately:
149 + HAVE_STDARG_H - if the <stdargs.h> include file is available
150 + HAVE_VARARG_H - if the <varargs.h> include file is available
152 + HAVE_STRERROR - if the strerror() routine is available.
153 + If it is not available, then examine the lines containing
156 + HAVE_SYS_ERRLIST - have sys_errlist available
157 + HAVE_DECL_SYS_ERRLIST - sys_errlist declaration in include files
158 + HAVE_SYS_NERR - have sys_nerr available
159 + HAVE_DECL_SYS_NERR - sys_nerr declaration in include files
161 + HAVE_QUAD_T - if the quad_t type is defined
162 + HAVE_LONG_LONG - if the long long type is defined
163 + HAVE_LONG_DOUBLE - if the long double type is defined
165 + If you are using the GNU configure (autoconf) facility, add the
166 + following line to the configure.in file, to force checking for the
167 + quad_t and long long data types:
170 + AC_CHECK_HEADERS(stdlib.h,stdio.h,unistd.h,errno.h)
171 + AC_CHECK_FUNCS(strerror)
172 + AC_CACHE_CHECK(for errno,
175 + AC_TRY_LINK(,[extern int errno; return (errno);],
176 + ac_cv_errno=yes, ac_cv_errno=no)
178 + if test "$ac_cv_errno" = yes; then
179 + AC_DEFINE(HAVE_ERRNO)
180 + AC_CACHE_CHECK(for errno declaration,
185 + #ifdef HAVE_STDLIB_H
186 + #include <stdlib.h>
188 + #ifdef HAVE_UNISTD_H
189 + #include <unistd.h>
191 + #ifdef HAVE_ERRNO_H
193 + ],[return(sys_nerr);],
194 + ac_cv_decl_errno=yes, ac_cv_decl_errno=no)
196 + if test "$ac_cv_decl_errno" = yes; then
197 + AC_DEFINE(HAVE_DECL_ERRNO)
201 + AC_CACHE_CHECK(for sys_nerr,
204 + AC_TRY_LINK(,[extern int sys_nerr; return (sys_nerr);],
205 + ac_cv_sys_nerr=yes, ac_cv_sys_nerr=no)
207 + if test "$ac_cv_sys_nerr" = yes; then
208 + AC_DEFINE(HAVE_SYS_NERR)
209 + AC_CACHE_CHECK(for sys_nerr declaration,
210 + ac_cv_decl_sys_nerr,
214 + #ifdef HAVE_STDLIB_H
215 + #include <stdlib.h>
217 + #ifdef HAVE_UNISTD_H
218 + #include <unistd.h>
219 + #endif],[return(sys_nerr);],
220 + ac_cv_decl_sys_nerr_def=yes, ac_cv_decl_sys_nerr_def=no)
222 + if test "$ac_cv_decl_sys_nerr" = yes; then
223 + AC_DEFINE(HAVE_DECL_SYS_NERR)
228 + AC_CACHE_CHECK(for sys_errlist array,
230 + [AC_TRY_LINK(,[extern char *sys_errlist[];
232 + ac_cv_sys_errlist=yes, ac_cv_sys_errlist=no)
234 + if test "$ac_cv_sys_errlist" = yes; then
235 + AC_DEFINE(HAVE_SYS_ERRLIST)
236 + AC_CACHE_CHECK(for sys_errlist declaration,
237 + ac_cv_sys_errlist_def,
241 + #ifdef HAVE_STDLIB_H
242 + #include <stdlib.h>
244 + #ifdef HAVE_UNISTD_H
245 + #include <unistd.h>
246 + #endif],[char *s = sys_errlist[0]; return(*s);],
247 + ac_cv_decl_sys_errlist=yes, ac_cv_decl_sys_errlist=no)
249 + if test "$ac_cv_decl_sys_errlist" = yes; then
250 + AC_DEFINE(HAVE_DECL_SYS_ERRLIST)
256 + AC_CACHE_CHECK(checking for long long,
261 + #include <sys/types.h>
262 + ], [printf("%d",sizeof(long long));],
263 + ac_cv_long_long=yes, ac_cv_long_long=no)
265 + if test $ac_cv_long_long = yes; then
266 + AC_DEFINE(HAVE_LONG_LONG)
269 + AC_CACHE_CHECK(checking for long double,
274 + #include <sys/types.h>
275 + ], [printf("%d",sizeof(long double));],
276 + ac_cv_long_double=yes, ac_cv_long_double=no)
278 + if test $ac_cv_long_double = yes; then
279 + AC_DEFINE(HAVE_LONG_DOUBLE)
282 + AC_CACHE_CHECK(checking for quad_t,
287 + #include <sys/types.h>
288 + ], [printf("%d",sizeof(quad_t));],
289 + ac_cv_quad_t=yes, ac_cv_quad_t=no)
291 + if test $ac_cv_quad_t = yes; then
292 + AC_DEFINE(HAVE_QUAD_T)
298 + snprintf, plp_vsnprintf - formatted output conversion
302 + #include <stdarg.h>
305 + snprintf(const char *format, size_t size, va_list ap);
307 + plp_unsafe_snprintf(const char *format, size_t size, va_list ap);
309 + AKA snprintf and unsafe_snprintf in the documentation below
312 + vsnprintf(char *str, size_t size, const char *format, va_list ap);
314 + unsafe_vsnprintf(char *str, size_t size, const char *format, va_list ap);
316 + AKA vsnprintf and unsafe_vsnprintf in the documentation below
318 + (Multithreaded Safe)
321 + The printf() family of functions produces output according to
322 + a format as described below. Snprintf(), and vsnprintf()
323 + write to the character string str. These functions write the
324 + output under the control of a format string that specifies
325 + how subsequent arguments (or arguments accessed via the
326 + variable-length argument facilities of stdarg(3)) are converted
327 + for output. These functions return the number of characters
328 + printed (not including the trailing `\0' used to end output
329 + to strings). Snprintf() and vsnprintf() will write at most
330 + size-1 of the characters printed into the output string (the
331 + size'th character then gets the terminating `\0'); if the
332 + return value is greater than or equal to the size argument,
333 + the string was too short and some of the printed characters
334 + were discarded. The size or str may be given as zero to find
335 + out how many characters are needed; in this case, the str
336 + argument is ignored.
338 + By default, the snprintf function will not format control
339 + characters (except new line and tab) in strings. This is a
340 + safety feature that has proven to be extremely critical when
341 + using snprintf for secure applications and when debugging.
342 + If you MUST have control characters formatted or printed,
343 + then use the unsafe_snprintf() and unsafe_vsnprintf() and on
344 + your own head be the consequences. You have been warned.
346 + There is one exception to the comments above, and that is
347 + the "%c" (character) format. It brutally assumes that the
348 + user will have performed the necessary 'isprint()' or other
349 + checks and uses the integer value as a character.
351 + The format string is composed of zero or more directives:
352 + ordinary characters (not %), which are copied unchanged to
353 + the output stream; and conversion specifications, each
354 + of which results in fetching zero or more subsequent arguments.
355 + Each conversion specification is introduced by the character
356 + %. The arguments must correspond properly (after type promotion)
357 + with the conversion specifier. After the %, the following
358 + appear in sequence:
360 + o Zero or more of the following flags:
362 + - A zero `0' character specifying zero padding. For
363 + all conversions except n, the converted value is padded
364 + on the left with zeros rather than blanks. If a
365 + precision is given with a numeric conversion (d, i,
366 + o, u, i, x, and X), the `0' flag is ignored.
368 + - A negative field width flag `-' indicates the converted
369 + value is to be left adjusted on the field boundary. Except
370 + for n conversions, the converted value is padded on
371 + the right with blanks, rather than on the left with
372 + blanks or zeros. A `-' overrides a `0' if both are
375 + - A space, specifying that a blank should be left before
376 + a positive number produced by a signed conversion (d, e, E, f,
379 + - A `+' character specifying that a sign always be placed
380 + before a number produced by a signed conversion. A `+' overrides
381 + a space if both are used.
383 + o An optional decimal digit string specifying a minimum
384 + field width. If the converted value has fewer
385 + characters than the field width, it will be padded
386 + with spaces on the left (or right, if the
387 + left-adjustment flag has been given) to fill out
390 + o An optional precision, in the form of a period `.' followed
391 + by an optional digit string. If the digit string
392 + is omitted, the precision is taken as zero. This
393 + gives the minimum number of digits to appear for
394 + d, i, o, u, x, and X conversions, the number of
395 + digits to appear after the decimal-point for e,
396 + E, and f conversions, the maximum number of
397 + significant digits for g and G conversions, or
398 + the maximum number of characters to be printed
399 + from a string for s conversions.
401 + o The optional character h, specifying that a following d,
402 + i, o, u, x, or X conversion corresponds to a short
403 + int or unsigned short int argument, or that a
404 + following n conversion corresponds to a pointer
405 + to a short int argument.
407 + o The optional character l (ell) specifying that a following
408 + d, i, o, u, x, or X conversion applies to a pointer
409 + to a long int or unsigned long int argument, or
410 + that a following n conversion corresponds to a
411 + pointer to a long int argument.
413 + o The optional character q, specifying that a following d,
414 + i, o, u, x, or X conversion corresponds to a quad_t
415 + or u_quad_t argument, or that a following n
416 + conversion corresponds to a quad_t argument.
417 + This value is always printed in HEX notation. Tough.
418 + quad_t's are an OS system implementation, and should
421 + o The character L specifying that a following e, E, f, g,
422 + or G conversion corresponds to a long double
425 + o A character that specifies the type of conversion to be applied.
428 + A field width or precision, or both, may be indicated by an asterisk `*'
429 + instead of a digit string. In this case, an int argument supplies the
430 + field width or precision. A negative field width is treated as a left
431 + adjustment flag followed by a positive field width; a negative precision
432 + is treated as though it were missing.
434 + The conversion specifiers and their meanings are:
436 + diouxX The int (or appropriate variant) argument is converted to signed
437 + decimal (d and i), unsigned octal (o), unsigned decimal
438 + (u), or unsigned hexadecimal (x and X) notation. The
439 + letters abcdef are used for x conversions; the letters
440 + ABCDEF are used for X conversions. The precision, if
441 + any, gives the minimum number of digits that must
442 + appear; if the converted value requires fewer digits,
443 + it is padded on the left with zeros.
445 + eE The double argument is rounded and converted in the style
446 + [-]d.ddde+-dd where there is one digit before the decimal-point
447 + character and the number of digits after it is equal
448 + to the precision; if the precision is missing, it is
449 + taken as 6; if the precision is zero, no decimal-point
450 + character appears. An E conversion uses the letter
451 + E (rather than e) to introduce the exponent.
452 + The exponent always contains at least two digits; if
453 + the value is zero, the exponent is 00.
455 + f The double argument is rounded and converted to decimal notation
456 + in the style [-]ddd.ddd, where the number of digits after the
457 + decimal-point character is equal to the precision specification.
458 + If the precision is missing, it is taken as 6; if the precision
459 + is explicitly zero, no decimal-point character appears. If a
460 + decimal point appears, at least one digit appears before it.
462 + g The double argument is converted in style f or e (or
463 + E for G conversions). The precision specifies the
464 + number of significant digits. If the precision is
465 + missing, 6 digits are given; if the precision is zero,
466 + it is treated as 1. Style e is used if the exponent
467 + from its conversion is less than -4 or greater than
468 + or equal to the precision. Trailing zeros are removed
469 + from the fractional part of the result; a decimal
470 + point appears only if it is followed by at least one
473 + c The int argument is converted to an unsigned char,
474 + and the resulting character is written.
476 + s The ``char *'' argument is expected to be a pointer to an array
477 + of character type (pointer to a string). Characters
478 + from the array are written up to (but not including)
479 + a terminating NUL character; if a precision is
480 + specified, no more than the number specified are
481 + written. If a precision is given, no null character
482 + need be present; if the precision is not specified,
483 + or is greater than the size of the array, the array
484 + must contain a terminating NUL character.
486 + % A `%' is written. No argument is converted. The complete
487 + conversion specification is `%%'.
489 + In no case does a non-existent or small field width cause truncation of a
490 + field; if the result of a conversion is wider than the field width, the
491 + field is expanded to contain the conversion result.
494 + To print a date and time in the form `Sunday, July 3, 10:02', where
495 + weekday and month are pointers to strings:
498 + fprintf(stdout, "%s, %s %d, %.2d:%.2d\n",
499 + weekday, month, day, hour, min);
501 + To print pi to five decimal places:
505 + fprintf(stdout, "pi = %.5f\n", 4 * atan(1.0));
507 + To allocate a 128 byte string and print into it:
510 + #include <stdlib.h>
511 + #include <stdarg.h>
512 + char *newfmt(const char *fmt, ...)
516 + if ((p = malloc(128)) == NULL)
519 + (void) vsnprintf(p, 128, fmt, ap);
525 + printf(1), scanf(3)
528 + Turkey C Standardization and wimpy POSIX folks did not define
529 + snprintf or vsnprintf().
532 + The conversion formats %D, %O, and %U are not standard and are provided
533 + only for backward compatibility. The effect of padding the %p format
534 + with zeros (either by the `0' flag or by specifying a precision), and the
535 + benign effect (i.e., none) of the `#' flag on %n and %p conversions, as
536 + well as other nonsensical combinations such as %Ld, are not standard;
537 + such combinations should be avoided.
539 - b) accompany the distribution with the machine-readable source of
540 - the Package with your modifications.
541 + The typedef names quad_t and u_quad_t are infelicitous.
543 - c) give non-standard executables non-standard names, and clearly
544 - document the differences in manual pages (or equivalent), together
545 - with instructions on where to get the Standard Version.
548 - d) make other distribution arrangements with the Copyright Holder.
550 -5. You may charge a reasonable copying fee for any distribution of this
551 -Package. You may charge any fee you choose for support of this
552 -Package. You may not charge a fee for this Package itself. However,
553 -you may distribute this Package in aggregate with other (possibly
554 -commercial) programs as part of a larger (possibly commercial) software
555 -distribution provided that you do not advertise this Package as a
556 -product of your own.
557 +#include <sys/types.h>
561 +#if defined(HAVE_STRING_H)
562 +# include <string.h>
564 +#if defined(HAVE_STRINGS_H)
565 +# include <strings.h>
567 +#if defined(HAVE_ERRNO_H)
571 -6. The name of the Copyright Holder may not be used to endorse or promote
572 -products derived from this software without specific prior written permission.
574 + * For testing, define these values
577 +#define HAVE_STDARG_H 1
579 +#define HAVE_QUAD_T 1
582 -7. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
583 -IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
584 -WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
585 +/**** ENDINCLUDE ****/
589 +/*************************************************
590 + * KEEP THIS STRING - MODIFY AT THE END WITH YOUR REVISIONS
591 + * i.e. - the LOCAL REVISIONS part is for your use
592 + *************************************************/
595 + static char *const _id = "plp_snprintf V2000.08.18 Copyright Patrick Powell 1988-2000 "
596 + "$Id: plp_snprintf.c,v 1.4 2005/04/14 20:05:19 papowell Exp $"
597 + " LOCAL REVISIONS: renamed plp_snprintf to snprintf, conditionalized everything on HAVE_SNPRINTF";
599 +/* varargs declarations: */
601 +# undef HAVE_STDARGS /* let's hope that works everywhere (mj) */
602 +# undef VA_LOCAL_DECL
607 +#if defined(HAVE_STDARG_H)
608 +# include <stdarg.h>
609 +# define HAVE_STDARGS /* let's hope that works everywhere (mj) */
610 +# define VA_LOCAL_DECL va_list ap;
611 +# define VA_START(f) va_start(ap, f)
612 +# define VA_SHIFT(v,t) ; /* no-op for ANSI */
613 +# define VA_END va_end(ap)
615 +# if defined(HAVE_VARARGS_H)
616 +# include <varargs.h>
617 +# undef HAVE_STDARGS
618 +# define VA_LOCAL_DECL va_list ap;
619 +# define VA_START(f) va_start(ap) /* f is ignored! */
620 +# define VA_SHIFT(v,t) v = va_arg(ap,t)
621 +# define VA_END va_end(ap)
623 + XX ** NO VARARGS ** XX
629 -#include <sys/types.h>
630 -#define HAVE_STDARGS /* let's hope that works everywhere (mj) */
631 -#define VA_LOCAL_DECL va_list ap;
632 -#define VA_START(f) va_start(ap, f)
633 -#define VA_SHIFT(v,t) ; /* no-op for ANSI */
634 -#define VA_END va_end(ap)
636 -/**** ENDINCLUDE ****/
638 +#if defined(HAVE_QUAD_T)
641 +#if defined(HAVE_LONG_LONG)
649 -static char *const _id = "$Id: snprintf.c,v 3.2 1997/01/19 14:34:56 papowell Exp $";
651 +#define CVAL(s) (*((unsigned char *)s))
652 +#define safestrlen(s) ((s)?strlen(s):0)
655 - * dopr(): poor man's version of doprintf
658 -static char * plp_Errormsg ( int err );
659 -static void dopr( char *buffer, const char *format, va_list args );
660 -static void fmtstr( char *value, int ljust, int len, int zpad, int precision );
661 -static void fmtnum( long value, int base, int dosign,
662 + static char * plp_Errormsg ( int err, char *buffer );
663 + static void dopr( int visible_control, char **buffer, int *left,
664 + const char *format, va_list args );
665 + static void fmtstr( int visible_control, char **buffer, int *left,
666 + char *value, int ljust, int len, int zpad, int precision );
667 + static void fmtnum( char **buffer, int *left,
668 + union value *value, int base, int dosign,
669 + int ljust, int len, int zpad, int precision );
670 +#if defined(HAVE_QUAD_T)
671 + static void fmtquad( char **buffer, int *left,
672 + union value *value, int base, int dosign,
673 int ljust, int len, int zpad, int precision );
674 -static void fmtdouble( int fmt, double value,
676 + static void fmtdouble( char **bufer, int *left,
677 + int fmt, double value,
678 int ljust, int len, int zpad, int precision );
679 -static void dostr( char * );
680 -static char *output;
681 -static void dopr_outch( int c );
683 -int visible_control = 1;
685 -/**************************************************************
687 - * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
688 - * A bombproof version of doprnt (dopr) included.
689 - * Sigh. This sort of thing is always nasty do deal with. Note that
690 - * the version here does not include floating point...
692 - * plp_snprintf() is used instead of sprintf() as it does limit checks
693 - * for string length. This covers a nasty loophole.
695 - * The other functions are there to prevent NULL pointers from
696 - * causing nast effects.
697 - **************************************************************/
699 -int vsnprintf(char *str, size_t count, const char *fmt, va_list args)
703 - dopr( str, fmt, args );
706 + static void dostr( char **buffer, int *left, char *str );
707 + static void dopr_outch( char **buffer, int *left, int c );
710 + int plp_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
712 + int plp_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
718 + if( (int)count < 0 ) count = 0;
720 + if( count == 0 ) str = 0;
722 + dopr( 1, &buffer, &left, fmt, args );
723 + /* fprintf(stderr,"str 0x%x, buffer 0x%x, count %d, left %d\n",
724 + (int)str, (int)buffer, count, left ); */
725 + if( str && count > 0 ){
727 + str[count-left] = 0;
732 + return(count - left);
737 + int plp_unsafe_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
739 + int plp_unsafe_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
744 + if( (int)count < 0 ) count = 0;
746 + if( count == 0 ) str = 0;
748 + dopr( 0, &buffer, &left, fmt, args );
749 + /* fprintf(stderr,"str 0x%x, buffer 0x%x, count %d, left %d\n",
750 + (int)str, (int)buffer, count, left ); */
751 + if( str && count > 0 ){
753 + str[count-left] = 0;
758 - return(strlen(str));
759 + return(count - left);
764 -int snprintf (char *str,size_t count,const char *fmt,...)
765 + int snprintf (char *str,size_t count,const char *fmt,...)
767 -int snprintf (va_alist) va_dcl
768 + int snprintf (va_alist) va_dcl
772 @@ -183,22 +619,48 @@ int snprintf (va_alist) va_dcl
780 VA_SHIFT (str, char *);
781 VA_SHIFT (count, size_t );
782 VA_SHIFT (fmt, char *);
783 - (void) vsnprintf ( str, count, fmt, ap);
784 + n = plp_vsnprintf ( str, count, fmt, ap);
786 - return( strlen( str ) );
790 -static void dopr( char *buffer, const char *format, va_list args )
794 + int plp_unsafe_snprintf (char *str,size_t count,const char *fmt,...)
796 + int plp_unsafe_snprintf (va_alist) va_dcl
799 +#ifndef HAVE_STDARGS
808 + VA_SHIFT (str, char *);
809 + VA_SHIFT (count, size_t );
810 + VA_SHIFT (fmt, char *);
811 + n = plp_unsafe_vsnprintf ( str, count, fmt, ap);
815 + static void dopr( int visible_control, char **buffer, int *left, const char *format, va_list args )
825 @@ -207,22 +669,30 @@ static void dopr( char *buffer, const ch
830 + int signed_val = 0;
833 while( (ch = *format++) ){
836 - ljust = len = zpad = 0;
837 + longflag = quadflag =
838 + ljust = len = zpad = base = signed_val = 0;
839 precision = -1; set_precision = 0;
844 - dostr( "**end of format**" );
845 + dostr( buffer, left, "**end of format**" );
847 case '-': ljust = 1; goto nextch;
848 case '.': set_precision = 1; precision = 0; goto nextch;
849 - case '*': len = va_arg( args, int ); goto nextch;
851 + if( set_precision ){
852 + precision = va_arg( args, int );
854 + len = va_arg( args, int );
857 case '0': /* set zero padding if len not set */
858 if(len==0 && set_precision == 0 ) zpad = '0';
859 case '1': case '2': case '3':
860 @@ -234,76 +704,91 @@ static void dopr( char *buffer, const ch
861 len = len*10 + ch - '0';
864 - case 'l': longflag = 1; goto nextch;
865 + case 'l': ++longflag; goto nextch;
867 +#if !defined( HAVE_QUAD_T )
868 + dostr( buffer, left, "*no quad_t support *");
874 - /*fmtnum(value,base,dosign,ljust,len, zpad, precision) */
876 - value = va_arg( args, long );
878 - value = va_arg( args, int );
880 - fmtnum( value, 10,0, ljust, len, zpad, precision ); break;
881 + if( base == 0 ){ base = 10; signed_val = 0; }
883 - /*fmtnum(value,base,dosign,ljust,len, zpad, precision) */
885 - value = va_arg( args, long );
887 - value = va_arg( args, int );
889 - fmtnum( value, 8,0, ljust, len, zpad, precision ); break;
890 + if( base == 0 ){ base = 8; signed_val = 0; }
893 - value = va_arg( args, long );
895 - value = va_arg( args, int );
897 - fmtnum( value, 10,1, ljust, len, zpad, precision ); break;
898 + if( base == 0 ){ base = 10; signed_val = 1; }
901 - value = va_arg( args, long );
903 - value = va_arg( args, int );
905 - fmtnum( value, 16,0, ljust, len, zpad, precision ); break;
906 + if( base == 0 ){ base = 16; signed_val = 0; }
909 - value = va_arg( args, long );
910 + if( base == 0 ){ base = -16; signed_val = 0; }
911 +#if defined( HAVE_QUAD_T )
913 + value.qvalue = va_arg( args, quad_t );
914 + fmtquad( buffer, left, &value,base,signed_val, ljust, len, zpad, precision );
918 + if( longflag > 1 ){
919 +#if defined(HAVE_LONG_LONG)
921 + value.value = va_arg( args, long long );
923 + value.value = va_arg( args, unsigned long long );
927 + value.value = va_arg( args, long );
929 + value.value = va_arg( args, unsigned long );
932 + } else if( longflag ){
934 + value.value = va_arg( args, long );
936 + value.value = va_arg( args, unsigned long );
939 - value = va_arg( args, int );
941 + value.value = va_arg( args, int );
943 + value.value = va_arg( args, unsigned int );
946 - fmtnum( value,-16,0, ljust, len, zpad, precision ); break;
947 + fmtnum( buffer, left, &value,base,signed_val, ljust, len, zpad, precision ); break;
949 strvalue = va_arg( args, char *);
950 - fmtstr( strvalue,ljust,len, zpad, precision );
951 + fmtstr( visible_control, buffer, left, strvalue,ljust,len, zpad, precision );
954 ch = va_arg( args, int );
956 - int vsb = visible_control;
959 - visible_control = 0;
960 - fmtstr( b,ljust,len, zpad, precision );
961 - visible_control = vsb;
962 + fmtstr( 0, buffer, left, b,ljust,len, zpad, precision );
965 - case 'f': case 'g':
966 + case 'f': case 'g': case 'e':
967 dval = va_arg( args, double );
968 - fmtdouble( ch, dval,ljust,len, zpad, precision ); break;
969 + fmtdouble( buffer, left, ch, dval,ljust,len, zpad, precision ); break;
971 - fmtstr( plp_Errormsg(err),ljust,len, zpad, precision ); break;
972 - case '%': dopr_outch( ch ); continue;
973 + { char shortbuffer[32];
974 + fmtstr( visible_control, buffer, left,
975 + plp_Errormsg(err, shortbuffer),ljust,len, zpad, precision );
978 + case '%': dopr_outch( buffer, left, ch ); continue;
980 - dostr( "???????" );
981 + dostr( buffer, left, "???????" );
987 + dopr_outch( buffer, left, ch );
995 @@ -312,63 +797,69 @@ static void dopr( char *buffer, const ch
996 * len = minimum length
997 * precision = numbers of chars in string to use
1000 -fmtstr( char *value, int ljust, int len, int zpad, int precision )
1002 + fmtstr( int visible_control, char **buffer, int *left,
1003 + char *value, int ljust, int len, int zpad, int precision )
1005 - int padlen, strlen, i, c; /* amount to pad */
1006 + int padlen, strlenv, i, c; /* amount to pad */
1011 - if( precision > 0 ){
1012 - strlen = precision;
1014 - /* cheap strlen so you do not have library call */
1015 - for( strlen = 0; (c=value[strlen]); ++ strlen ){
1016 - if( visible_control && iscntrl( c ) && !isspace( c ) ){
1019 + /* cheap strlen so you do not have library call */
1020 + for( strlenv = i = 0; (c=CVAL(value+i)); ++i ){
1021 + if( visible_control && iscntrl( c ) && c != '\t' && c != '\n' ){
1026 - padlen = len - strlen;
1027 + if( precision > 0 && strlenv > precision ){
1028 + strlenv = precision;
1030 + padlen = len - strlenv;
1031 if( padlen < 0 ) padlen = 0;
1032 if( ljust ) padlen = -padlen;
1033 while( padlen > 0 ) {
1034 - dopr_outch( ' ' );
1035 + dopr_outch( buffer, left, ' ' );
1038 /* output characters */
1039 - for( i = 0; (c = value[i]); ++i ){
1040 - if( visible_control && iscntrl( c ) && !isspace( c ) ){
1042 + for( i = 0; i < strlenv && (c = CVAL(value+i)); ++i ){
1043 + if( visible_control && iscntrl( c ) && c != '\t' && c != '\n' ){
1044 + dopr_outch(buffer, left, '^');
1045 c = ('@' | (c & 0x1F));
1048 + dopr_outch(buffer, left, c);
1050 while( padlen < 0 ) {
1051 - dopr_outch( ' ' );
1052 + dopr_outch( buffer, left, ' ' );
1058 -fmtnum( long value, int base, int dosign, int ljust,
1060 + fmtnum( char **buffer, int *left,
1061 + union value *value, int base, int dosign, int ljust,
1062 int len, int zpad, int precision )
1065 +#if defined(HAVE_LONG_LONG)
1066 + unsigned long long uvalue;
1068 unsigned long uvalue;
1071 + char convert[sizeof( union value) * 8 + 16];
1073 int padlen = 0; /* amount to pad */
1076 - /* DEBUGP(("value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n",
1077 - value, base, dosign, ljust, len, zpad )); */
1079 + /* fprintf(stderr,"value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n",
1080 + value, base, dosign, ljust, len, zpad );/ **/
1081 + uvalue = value->value;
1084 + if( value->value < 0 ) {
1087 + uvalue = -value->value;
1091 @@ -385,71 +876,148 @@ fmtnum( long value, int base, int dosig
1092 padlen = len - place;
1093 if( padlen < 0 ) padlen = 0;
1094 if( ljust ) padlen = -padlen;
1095 - /* DEBUGP(( "str '%s', place %d, sign %c, padlen %d\n",
1096 - convert,place,signvalue,padlen)); */
1097 + /* fprintf( stderr, "str '%s', place %d, sign %c, padlen %d\n",
1098 + convert,place,signvalue,padlen); / **/
1099 if( zpad && padlen > 0 ){
1101 - dopr_outch( signvalue );
1102 + dopr_outch( buffer, left, signvalue );
1106 while( padlen > 0 ){
1107 - dopr_outch( zpad );
1108 + dopr_outch( buffer, left, zpad );
1112 while( padlen > 0 ) {
1113 - dopr_outch( ' ' );
1114 + dopr_outch( buffer, left, ' ' );
1117 - if( signvalue ) dopr_outch( signvalue );
1118 - while( place > 0 ) dopr_outch( convert[--place] );
1119 + if( signvalue ) dopr_outch( buffer, left, signvalue );
1120 + while( place > 0 ) dopr_outch( buffer, left, convert[--place] );
1121 while( padlen < 0 ){
1122 - dopr_outch( ' ' );
1123 + dopr_outch( buffer, left, ' ' );
1129 -fmtdouble( int fmt, double value, int ljust, int len, int zpad, int precision )
1130 +#if defined(HAVE_QUAD_T)
1133 + fmtquad( char **buffer, int *left,
1134 + union value *value, int base, int dosign, int ljust,
1135 + int len, int zpad, int precision )
1137 - char convert[128];
1140 + int signvalue = 0;
1142 + int padlen = 0; /* amount to pad */
1147 + unsigned char qconvert[sizeof(quad_t)];
1149 + char convert[2*sizeof(quad_t)+1];
1151 + /* fprintf(stderr,"value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n",
1152 + value, base, dosign, ljust, len, zpad );/ **/
1153 + vvalue.qvalue = value->qvalue;
1155 - if( len == 0 ) len = 10;
1156 - if( len > sizeof(convert) - 10 ){
1157 - len = sizeof(convert) - 10;
1161 - if( precision > sizeof(convert) - 10 ){
1162 - precision = sizeof(convert) - 10;
1164 + for( i = 0; i < (int)sizeof(quad_t); ++i ){
1165 + c = vvalue.qconvert[i];
1167 + (caps? "0123456789ABCDEF":"0123456789abcdef")[ (c >> 4) & 0xF];
1169 + (caps? "0123456789ABCDEF":"0123456789abcdef")[ c & 0xF];
1171 - if( precision > len ) precision = len;
1172 - strcpy( fmtstr, "%" );
1173 - if( ljust ) strcat(fmtstr, "-" );
1175 - sprintf( fmtstr+strlen(fmtstr), "%d", len );
1178 + place = strlen(convert);
1179 + padlen = len - place;
1180 + if( padlen < 0 ) padlen = 0;
1181 + if( ljust ) padlen = -padlen;
1182 + /* fprintf( stderr, "str '%s', place %d, sign %c, padlen %d\n",
1183 + convert,place,signvalue,padlen); / **/
1184 + if( zpad && padlen > 0 ){
1186 + dopr_outch( buffer, left, signvalue );
1190 + while( padlen > 0 ){
1191 + dopr_outch( buffer, left, zpad );
1195 + while( padlen > 0 ) {
1196 + dopr_outch( buffer, left, ' ' );
1199 + if( signvalue ) dopr_outch( buffer, left, signvalue );
1200 + while( place > 0 ) dopr_outch( buffer, left, convert[--place] );
1201 + while( padlen < 0 ){
1202 + dopr_outch( buffer, left, ' ' );
1209 + static void mystrcat(char *dest, char *src )
1211 + if( dest && src ){
1212 + dest += safestrlen(dest);
1218 + fmtdouble( char **buffer, int *left,
1219 + int fmt, double value, int ljust, int len, int zpad, int precision )
1221 + char convert[sizeof( union value) * 8 + 512];
1222 + char formatstr[128];
1224 + /* fprintf(stderr,"len %d, precision %d\n", len, precision ); */
1228 + if( precision > 255 ){
1231 + if( precision >= 0 && len > 0 && precision > len ) precision = len;
1232 + strcpy( formatstr, "%" ); /* 1 */
1233 + if( ljust ) mystrcat(formatstr, "-" ); /* 1 */
1234 + if( zpad ) mystrcat(formatstr, "0" ); /* 1 */
1236 + sprintf( formatstr+strlen(formatstr), "%d", len ); /* 3 */
1238 - if( precision > 0 ){
1239 - sprintf( fmtstr+strlen(fmtstr), ".%d", precision );
1240 + if( precision >= 0 ){
1241 + sprintf( formatstr+strlen(formatstr), ".%d", precision ); /* 3 */
1243 - l = strlen( fmtstr );
1246 - sprintf( convert, fmtstr, value );
1248 + /* format string will be at most 10 chars long ... */
1249 + sprintf( formatstr+strlen(formatstr), "%c", fmt );
1250 + /* this is easier than trying to do the portable dtostr */
1251 + /* fprintf(stderr,"format string '%s'\n", formatstr); */
1252 + sprintf( convert, formatstr, value );
1253 + dostr( buffer, left, convert );
1256 -static void dostr( char *str )
1257 + static void dostr( char **buffer, int *left, char *str )
1259 - while(*str) dopr_outch(*str++);
1260 + if(str)while(*str) dopr_outch( buffer, left, *str++ );
1263 -static void dopr_outch( int c )
1264 + static void dopr_outch( char **buffer, int *left, int c )
1266 - if( end == 0 || output < end ){
1275 @@ -461,29 +1029,26 @@ static void dopr_outch( int c )
1276 * Patrick Powell Tue Apr 11 08:05:05 PDT 1995
1277 ****************************************************************************/
1278 /****************************************************************************/
1279 -#if !defined(HAVE_STRERROR)
1281 -# if defined(HAVE_SYS_NERR)
1282 -# if !defined(HAVE_SYS_NERR_DEF)
1283 - extern int sys_nerr;
1285 -# define num_errors (sys_nerr)
1287 -# define num_errors (-1) /* always use "errno=%d" */
1290 +#if !defined(HAVE_STRERROR)
1292 # if defined(HAVE_SYS_ERRLIST)
1293 -# if !defined(HAVE_SYS_ERRLIST_DEF)
1294 +# if !defined(HAVE_DECL_SYS_ERRLIST)
1295 extern const char *const sys_errlist[];
1299 -# define num_errors (-1) /* always use "errno=%d" */
1300 +# if defined(HAVE_SYS_NERR)
1301 +# if !defined(HAVE_DECL_SYS_NERR)
1302 + extern int sys_nerr;
1304 +# define num_errors (sys_nerr)
1307 +# if !defined(num_errors)
1308 +# define num_errors (-1) /* always use "errno=%d" */
1313 -static char * plp_Errormsg ( int err )
1314 + static char * plp_Errormsg ( int err, char *buffer /* int maxlen = 32 */)
1318 @@ -496,10 +1061,8 @@ static char * plp_Errormsg ( int err )
1322 - static char msgbuf[32]; /* holds "errno=%d". */
1323 - /* SAFE use of sprintf */
1324 - (void) sprintf (msgbuf, "errno=%d", err);
1326 + (void) sprintf (buffer, "errno=%d", err);
1331 @@ -507,23 +1070,47 @@ static char * plp_Errormsg ( int err )
1340 char *test1 = "01234";
1343 - plp_snprintf( buffer, sizeof(buffer), (t="errno '%m'")); printf( "%s = '%s'\n", t, buffer );
1344 - plp_snprintf( buffer, sizeof(buffer), (t = "%s"), test1 ); printf( "%s = '%s'\n", t, buffer );
1345 - plp_snprintf( buffer, sizeof(buffer), (t = "%12s"), test1 ); printf( "%s = '%s'\n", t, buffer );
1346 - plp_snprintf( buffer, sizeof(buffer), (t = "%-12s"), test1 ); printf( "%s = '%s'\n", t, buffer );
1347 - plp_snprintf( buffer, sizeof(buffer), (t = "%12.2s"), test1 ); printf( "%s = '%s'\n", t, buffer );
1348 - plp_snprintf( buffer, sizeof(buffer), (t = "%-12.2s"), test1 ); printf( "%s = '%s'\n", t, buffer );
1349 - plp_snprintf( buffer, sizeof(buffer), (t = "%g"), 1.25 ); printf( "%s = '%s'\n", t, buffer );
1350 - plp_snprintf( buffer, sizeof(buffer), (t = "%g"), 1.2345 ); printf( "%s = '%s'\n", t, buffer );
1351 - plp_snprintf( buffer, sizeof(buffer), (t = "%12g"), 1.25 ); printf( "%s = '%s'\n", t, buffer );
1352 - plp_snprintf( buffer, sizeof(buffer), (t = "%12.2g"), 1.25 ); printf( "%s = '%s'\n", t, buffer );
1353 - plp_snprintf( buffer, sizeof(buffer), (t = "%0*d"), 6, 1 ); printf( "%s = '%s'\n", t, buffer );
1355 + n = snprintf( buffer, 0, (t="test")); printf( "[%d] %s = '%s'\n", n, t, buffer );
1356 + n = snprintf( buffer, sizeof(buffer), (t="errno '%m'")); printf( "[%d] %s = '%s'\n", n, t, buffer );
1357 + n = snprintf( buffer, sizeof(buffer), (t = "%s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1358 + n = snprintf( buffer, sizeof(buffer), (t = "%12s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1359 + n = snprintf( buffer, sizeof(buffer), (t = "%-12s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1360 + n = snprintf( buffer, sizeof(buffer), (t = "%12.2s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1361 + n = snprintf( buffer, sizeof(buffer), (t = "%-12.2s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1362 + n = snprintf( buffer, sizeof(buffer), (t = "%g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1363 + n = snprintf( buffer, sizeof(buffer), (t = "%g"), 1.2345 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1364 + n = snprintf( buffer, sizeof(buffer), (t = "%12g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1365 + n = snprintf( buffer, sizeof(buffer), (t = "%12.1g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1366 + n = snprintf( buffer, sizeof(buffer), (t = "%12.2g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1367 + n = snprintf( buffer, sizeof(buffer), (t = "%12.3g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1368 + n = snprintf( buffer, sizeof(buffer), (t = "%0*d"), 6, 1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1369 +#if defined(HAVE_LONG_LONG)
1370 + n = snprintf( buffer, sizeof(buffer), (t = "%llx"), 1, 2, 3, 4 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1371 + n = snprintf( buffer, sizeof(buffer), (t = "%llx"), (long long)1, (long long)2 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1372 + n = snprintf( buffer, sizeof(buffer), (t = "%qx"), 1, 2, 3, 4 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1373 + n = snprintf( buffer, sizeof(buffer), (t = "%qx"), (quad_t)1, (quad_t)2 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1375 + 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 );
1376 + 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 );
1377 + n = snprintf( buffer, sizeof(buffer), (t = "0%x, 0%x"), t, 0, 0, 0, 0); printf( "[%d] %s = '%s'\n", n, t, buffer );
1378 + n = snprintf( buffer, sizeof(buffer), (t = "%f"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1379 + n = snprintf( buffer, sizeof(buffer), (t = "%f"), 1.2345 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1380 + n = snprintf( buffer, sizeof(buffer), (t = "%12f"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1381 + n = snprintf( buffer, sizeof(buffer), (t = "%12.2f"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1382 + n = snprintf( buffer, sizeof(buffer), (t = "%f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1383 + n = snprintf( buffer, sizeof(buffer), (t = "%.0f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1384 + n = snprintf( buffer, sizeof(buffer), (t = "%0.0f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1385 + n = snprintf( buffer, sizeof(buffer), (t = "%1.0f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1386 + n = snprintf( buffer, sizeof(buffer), (t = "%1.5f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
1387 + n = snprintf( buffer, sizeof(buffer), (t = "%5.5f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );