]> git.pld-linux.org Git - packages/xsane.git/blob - xsane-0.999-snprintf-update.patch
20cf52c1ed27770f1792ba07590216b9713cb7db
[packages/xsane.git] / xsane-0.999-snprintf-update.patch
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
4 @@ -2,180 +2,616 @@
5  
6  #ifndef HAVE_SNPRINTF
7  
8 -/***************************************************************************
9 - * LPRng - An Extended Print Spooler System
10 - *
11 - * Copyright 1988-1997, Patrick Powell, San Diego, CA
12 - *     papowell@sdsu.edu
13 - * See below for conditions of use.
14 - *
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   **************************************************************************/
21 -#if 0
22 -
23 -                        The "Artistic License"
24 -
25 -                               Preamble
26 -
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.
33 -
34 -Definitions:
35 -
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.
39 -
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.
43 -
44 -       "Copyright Holder" is whoever is named in the copyright or
45 -       copyrights for the package.
46 -
47 -       "You" is you, if you are thinking about copying or distributing
48 -       this Package.
49 -
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.)
55  
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.
60 -
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.
64 -
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.
68 -
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
72 -following:
73 -
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.
79 -
80 -    b) use the modified Package only within your corporation or organization.
81 -
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.
86 -
87 -    d) make other distribution arrangements with the Copyright Holder.
88 -
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:
91 +/*
92 +   Overview:
93  
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
102 +   
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.
107 +   
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,
114 +   etc.
115 +   
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.
120 +   
121 +   This version of snprintf is VERY limited by modern standards.
122 +
123 +   Revision History:
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 
131 +   
132 +   COPYRIGHT AND TERMS OF USE:
133 +   
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
139 +   or objects.
140 +   
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.
145 +   
146 +   You can use this with the GNU CONFIGURE utility.
147 +   This should define the following macros appropriately:
148 +   
149 +   HAVE_STDARG_H - if the <stdargs.h> include file is available
150 +   HAVE_VARARG_H - if the <varargs.h> include file is available
151 +   
152 +   HAVE_STRERROR - if the strerror() routine is available.
153 +     If it is not available, then examine the lines containing
154 +     the tests below.
155 +
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
160 +   
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
164 +   
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:
168 +
169 +
170 +    AC_CHECK_HEADERS(stdlib.h,stdio.h,unistd.h,errno.h)
171 +       AC_CHECK_FUNCS(strerror)
172 +       AC_CACHE_CHECK(for errno,
173 +       ac_cv_errno,
174 +       [
175 +       AC_TRY_LINK(,[extern int errno; return (errno);],
176 +               ac_cv_errno=yes, ac_cv_errno=no)
177 +       ])
178 +       if test "$ac_cv_errno" = yes; then
179 +               AC_DEFINE(HAVE_ERRNO)
180 +               AC_CACHE_CHECK(for errno declaration,
181 +               ac_cv_decl_errno,
182 +               [
183 +               AC_TRY_COMPILE([
184 +               #include <stdio.h>
185 +               #ifdef HAVE_STDLIB_H
186 +               #include <stdlib.h>
187 +               #endif
188 +               #ifdef HAVE_UNISTD_H
189 +               #include <unistd.h>
190 +               #endif
191 +               #ifdef HAVE_ERRNO_H
192 +               #include <errno.h>
193 +               ],[return(sys_nerr);],
194 +                       ac_cv_decl_errno=yes, ac_cv_decl_errno=no)
195 +               ])
196 +               if test "$ac_cv_decl_errno" = yes; then
197 +                       AC_DEFINE(HAVE_DECL_ERRNO)
198 +               fi;
199 +       fi
200 +
201 +       AC_CACHE_CHECK(for sys_nerr,
202 +       ac_cv_sys_nerr,
203 +       [
204 +       AC_TRY_LINK(,[extern int sys_nerr; return (sys_nerr);],
205 +               ac_cv_sys_nerr=yes, ac_cv_sys_nerr=no)
206 +       ])
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,
211 +               [
212 +               AC_TRY_COMPILE([
213 +               #include <stdio.h>
214 +               #ifdef HAVE_STDLIB_H
215 +               #include <stdlib.h>
216 +               #endif
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)
221 +               ])
222 +               if test "$ac_cv_decl_sys_nerr" = yes; then
223 +                       AC_DEFINE(HAVE_DECL_SYS_NERR)
224 +               fi
225 +       fi
226 +
227 +
228 +       AC_CACHE_CHECK(for sys_errlist array,
229 +       ac_cv_sys_errlist,
230 +       [AC_TRY_LINK(,[extern char *sys_errlist[];
231 +               sys_errlist[0];],
232 +               ac_cv_sys_errlist=yes, ac_cv_sys_errlist=no)
233 +       ])
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,
238 +               [AC_TRY_COMPILE([
239 +               #include <stdio.h>
240 +               #include <errno.h>
241 +               #ifdef HAVE_STDLIB_H
242 +               #include <stdlib.h>
243 +               #endif
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)
248 +               ])
249 +               if test "$ac_cv_decl_sys_errlist" = yes; then
250 +                       AC_DEFINE(HAVE_DECL_SYS_ERRLIST)
251 +               fi
252 +       fi
253 +
254 +
255 +
256 +       AC_CACHE_CHECK(checking for long long,
257 +       ac_cv_long_long,
258 +       [
259 +       AC_TRY_COMPILE([
260 +       #include <stdio.h>
261 +       #include <sys/types.h>
262 +       ], [printf("%d",sizeof(long long));],
263 +       ac_cv_long_long=yes, ac_cv_long_long=no)
264 +       ])
265 +       if test $ac_cv_long_long = yes; then
266 +         AC_DEFINE(HAVE_LONG_LONG)
267 +       fi
268 +
269 +       AC_CACHE_CHECK(checking for long double,
270 +       ac_cv_long_double,
271 +       [
272 +       AC_TRY_COMPILE([
273 +       #include <stdio.h>
274 +       #include <sys/types.h>
275 +       ], [printf("%d",sizeof(long double));],
276 +       ac_cv_long_double=yes, ac_cv_long_double=no)
277 +       ])
278 +       if test $ac_cv_long_double = yes; then
279 +         AC_DEFINE(HAVE_LONG_DOUBLE)
280 +       fi
281 +
282 +       AC_CACHE_CHECK(checking for quad_t,
283 +       ac_cv_quad_t,
284 +       [
285 +       AC_TRY_COMPILE([
286 +       #include <stdio.h>
287 +       #include <sys/types.h>
288 +       ], [printf("%d",sizeof(quad_t));],
289 +       ac_cv_quad_t=yes, ac_cv_quad_t=no)
290 +       ])
291 +       if test $ac_cv_quad_t = yes; then
292 +         AC_DEFINE(HAVE_QUAD_T)
293 +       fi
294 +
295 +
296 +
297 + NAME
298 +     snprintf, plp_vsnprintf - formatted output conversion
299 +
300 + SYNOPSIS
301 +     #include <stdio.h>
302 +     #include <stdarg.h>
303 +
304 +     int
305 +     snprintf(const char *format, size_t size, va_list ap);
306 +     int
307 +     plp_unsafe_snprintf(const char *format, size_t size, va_list ap);
308 +
309 +     AKA snprintf and unsafe_snprintf in the documentation below
310 +
311 +     int
312 +     vsnprintf(char *str, size_t size, const char *format, va_list ap);
313 +     int
314 +     unsafe_vsnprintf(char *str, size_t size, const char *format, va_list ap);
315 +
316 +     AKA vsnprintf and unsafe_vsnprintf in the documentation below
317 +
318 +     (Multithreaded Safe)
319 +
320 + DESCRIPTION
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.
337 +
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.
345 +
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.
350 +
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:
359 +
360 +     o   Zero or more of the following flags:
361 +
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.
367 +
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
373 +            given.
374 +
375 +          -   A space, specifying that a blank should be left before
376 +            a positive number produced by a signed conversion (d, e, E, f,
377 +            g, G, or i).
378 +
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.
382 +
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
388 +                the field width.
389 +
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.
400 +
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.
406 +
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.
412 +
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
419 +         not be allowed.
420 +
421 +     o   The character L specifying that a following e, E, f, g,
422 +                or G conversion corresponds to a long double
423 +                argument.
424 +
425 +     o   A character that specifies the type of conversion to be applied.
426 +
427 +
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.
433 +
434 +     The conversion specifiers and their meanings are:
435 +
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.
444 +
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.
454 +
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.
461 +
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
471 +                        digit.
472 +
473 +     c       The int argument is converted to an unsigned char,
474 +             and the resulting character is written.
475 +
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.
485 +
486 +     %       A `%' is written. No argument is converted. The complete
487 +             conversion specification is `%%'.
488 +
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.
492 +
493 + EXAMPLES
494 +     To print a date and time in the form `Sunday, July 3, 10:02', where
495 +     weekday and month are pointers to strings:
496 +
497 +           #include <stdio.h>
498 +           fprintf(stdout, "%s, %s %d, %.2d:%.2d\n",
499 +                   weekday, month, day, hour, min);
500 +
501 +     To print pi to five decimal places:
502 +
503 +           #include <math.h>
504 +           #include <stdio.h>
505 +           fprintf(stdout, "pi = %.5f\n", 4 * atan(1.0));
506 +
507 +     To allocate a 128 byte string and print into it:
508 +
509 +           #include <stdio.h>
510 +           #include <stdlib.h>
511 +           #include <stdarg.h>
512 +           char *newfmt(const char *fmt, ...)
513 +           {
514 +               char *p;
515 +               va_list ap;
516 +               if ((p = malloc(128)) == NULL)
517 +                       return (NULL);
518 +               va_start(ap, fmt);
519 +               (void) vsnprintf(p, 128, fmt, ap);
520 +               va_end(ap);
521 +               return (p);
522 +           }
523 +
524 + SEE ALSO
525 +     printf(1),  scanf(3)
526 +
527 + STANDARDS
528 +     Turkey C Standardization and wimpy POSIX folks did not define
529 +     snprintf or vsnprintf().
530 +
531 + BUGS
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.
538  
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.
542  
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.
546 +*/
547  
548 -    d) make other distribution arrangements with the Copyright Holder.
549  
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>
558 +#include <ctype.h>
559 +#include <stdlib.h>
560 +#include <stdio.h>
561 +#if defined(HAVE_STRING_H)
562 +# include <string.h>
563 +#endif
564 +#if defined(HAVE_STRINGS_H)
565 +# include <strings.h>
566 +#endif
567 +#if defined(HAVE_ERRNO_H)
568 +#include <errno.h>
569 +#endif
570  
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.
573 +/*
574 + * For testing, define these values
575 + */
576 +#if 0
577 +#define HAVE_STDARG_H 1
578 +#define TEST 1
579 +#define HAVE_QUAD_T 1
580 +#endif
581  
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 ****/
586  
587 -                               The End
588 -#include "lp.h"
589 +/*************************************************
590 + * KEEP THIS STRING - MODIFY AT THE END WITH YOUR REVISIONS
591 + * i.e. - the LOCAL REVISIONS part is for your use
592 + *************************************************/
593
594
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";
598 +
599 +/* varargs declarations: */
600 +
601 +# undef HAVE_STDARGS    /* let's hope that works everywhere (mj) */
602 +# undef VA_LOCAL_DECL
603 +# undef VA_START
604 +# undef VA_SHIFT
605 +# undef VA_END
606 +
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)
614 +#else
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)
622 +# else
623 + XX ** NO VARARGS ** XX
624 +# endif
625  #endif
626  
627 -#include <stdarg.h>
628 -#include <errno.h>
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)
635 -
636 -/**** ENDINCLUDE ****/
637 + union value {
638 +#if defined(HAVE_QUAD_T)
639 +       quad_t qvalue;
640 +#endif
641 +#if defined(HAVE_LONG_LONG)
642 +       long long value;
643 +#else
644 +       long value;
645 +#endif
646 +       double dvalue;
647 +};
648  
649 -static char *const _id = "$Id: snprintf.c,v 3.2 1997/01/19 14:34:56 papowell Exp $";
650 +#undef CVAL 
651 +#define CVAL(s) (*((unsigned char *)s))
652 +#define safestrlen(s) ((s)?strlen(s):0)
653  
654 -/*
655 - * dopr(): poor man's version of doprintf
656 - */
657  
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,
675 +#endif
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 );
682 -static char *end;
683 -int visible_control = 1;
684 -
685 -/**************************************************************
686 - * Original:
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...
691 - *
692 - * plp_snprintf() is used instead of sprintf() as it does limit checks
693 - * for string length.  This covers a nasty loophole.
694 - *
695 - * The other functions are there to prevent NULL pointers from
696 - * causing nast effects.
697 - **************************************************************/
698 -
699 -int vsnprintf(char *str, size_t count, const char *fmt, va_list args)
700 -{
701 -       str[0] = 0;
702 -       end = str+count-1;
703 -       dopr( str, fmt, args );
704 -       if( count>0 ){
705 -               end[0] = 0;
706 + static void dostr(  char **buffer, int *left, char *str );
707 + static void dopr_outch(  char **buffer, int *left, int c );
708 +/* VARARGS3 */
709 +#ifdef HAVE_STDARGS
710 + int plp_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
711 +#else
712 + int plp_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
713 +#endif
714 +
715 +{
716 +       int left;
717 +       char *buffer;
718 +       if( (int)count < 0 ) count = 0;
719 +       left = count;
720 +       if( count == 0 ) str = 0;
721 +       buffer = str;
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 ){
726 +               if( left > 0 ){
727 +                       str[count-left] = 0;
728 +               } else {
729 +                       str[count-1] = 0;
730 +               }
731 +       }
732 +       return(count - left);
733 +}
734 +
735 +/* VARARGS3 */
736 +#ifdef HAVE_STDARGS
737 + int plp_unsafe_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
738 +#else
739 + int plp_unsafe_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
740 +#endif
741 +{
742 +       int left;
743 +       char *buffer;
744 +       if( (int)count < 0 ) count = 0;
745 +       left = count;
746 +       if( count == 0 ) str = 0;
747 +       buffer = str;
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 ){
752 +               if( left > 0 ){
753 +                       str[count-left] = 0;
754 +               } else {
755 +                       str[count-1] = 0;
756 +               }
757         }
758 -       return(strlen(str));
759 +       return(count - left);
760  }
761  
762  /* VARARGS3 */
763  #ifdef HAVE_STDARGS
764 -int snprintf (char *str,size_t count,const char *fmt,...)
765 + int snprintf (char *str,size_t count,const char *fmt,...)
766  #else
767 -int snprintf (va_alist) va_dcl
768 + int snprintf (va_alist) va_dcl
769  #endif
770  {
771  #ifndef HAVE_STDARGS
772 @@ -183,22 +619,48 @@ int snprintf (va_alist) va_dcl
773         size_t count;
774      char *fmt;
775  #endif
776 +       int n = 0;
777      VA_LOCAL_DECL
778  
779      VA_START (fmt);
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);
785      VA_END;
786 -       return( strlen( str ) );
787 +       return( n );
788  }
789  
790 -static void dopr( char *buffer, const char *format, va_list args )
791 +
792 +/* VARARGS3 */
793 +#ifdef HAVE_STDARGS
794 + int plp_unsafe_snprintf (char *str,size_t count,const char *fmt,...)
795 +#else
796 + int plp_unsafe_snprintf (va_alist) va_dcl
797 +#endif
798 +{
799 +#ifndef HAVE_STDARGS
800 +    char *str;
801 +       size_t count;
802 +    char *fmt;
803 +#endif
804 +       int n = 0;
805 +    VA_LOCAL_DECL
806 +
807 +    VA_START (fmt);
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);
812 +    VA_END;
813 +       return( n );
814 +}
815 + static void dopr( int visible_control, char **buffer, int *left, const char *format, va_list args )
816  {
817         int ch;
818 -       long value;
819 +       union value value;
820         int longflag = 0;
821 +       int quadflag = 0;
822         char *strvalue;
823         int ljust;
824         int len;
825 @@ -207,22 +669,30 @@ static void dopr( char *buffer, const ch
826         int set_precision;
827         double dval;
828         int err = errno;
829 +       int base = 0;
830 +       int signed_val = 0;
831  
832 -       output = buffer;
833         while( (ch = *format++) ){
834                 switch( ch ){
835                 case '%':
836 -                       ljust = len = zpad = 0;
837 +                       longflag = quadflag =
838 +                       ljust = len = zpad = base = signed_val = 0;
839                         precision = -1; set_precision = 0;
840                 nextch: 
841                         ch = *format++;
842                         switch( ch ){
843                         case 0:
844 -                               dostr( "**end of format**" );
845 +                               dostr( buffer, left, "**end of format**" );
846                                 return;
847                         case '-': ljust = 1; goto nextch;
848                         case '.': set_precision = 1; precision = 0; goto nextch;
849 -                       case '*': len = va_arg( args, int ); goto nextch;
850 +                       case '*':
851 +                               if( set_precision ){
852 +                                       precision = va_arg( args, int );
853 +                               } else {
854 +                                       len = va_arg( args, int );
855 +                               }
856 +                               goto nextch;
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';
862                                 }
863                                 goto nextch;
864 -                       case 'l': longflag = 1; goto nextch;
865 +                       case 'l': ++longflag; goto nextch;
866 +                       case 'q':
867 +#if !defined( HAVE_QUAD_T )
868 +                                       dostr( buffer, left, "*no quad_t support *");
869 +                                       return;
870 +#endif
871 +                                       quadflag = 1;
872 +                                       goto nextch;
873                         case 'u': case 'U':
874 -                               /*fmtnum(value,base,dosign,ljust,len, zpad, precision) */
875 -                               if( longflag ){
876 -                                       value = va_arg( args, long );
877 -                               } else {
878 -                                       value = va_arg( args, int );
879 -                               }
880 -                               fmtnum( value, 10,0, ljust, len, zpad, precision ); break;
881 +                               if( base == 0 ){ base = 10; signed_val = 0; }
882                         case 'o': case 'O':
883 -                               /*fmtnum(value,base,dosign,ljust,len, zpad, precision) */
884 -                               if( longflag ){
885 -                                       value = va_arg( args, long );
886 -                               } else {
887 -                                       value = va_arg( args, int );
888 -                               }
889 -                               fmtnum( value, 8,0, ljust, len, zpad, precision ); break;
890 +                               if( base == 0 ){ base = 8; signed_val = 0; }
891                         case 'd': case 'D':
892 -                               if( longflag ){
893 -                                       value = va_arg( args, long );
894 -                               } else {
895 -                                       value = va_arg( args, int );
896 -                               }
897 -                               fmtnum( value, 10,1, ljust, len, zpad, precision ); break;
898 +                               if( base == 0 ){ base = 10; signed_val = 1; }
899                         case 'x':
900 -                               if( longflag ){
901 -                                       value = va_arg( args, long );
902 -                               } else {
903 -                                       value = va_arg( args, int );
904 -                               }
905 -                               fmtnum( value, 16,0, ljust, len, zpad, precision ); break;
906 +                               if( base == 0 ){ base = 16; signed_val = 0; }
907                         case 'X':
908 -                               if( longflag ){
909 -                                       value = va_arg( args, long );
910 +                               if( base == 0 ){ base = -16; signed_val = 0; }
911 +#if defined( HAVE_QUAD_T )
912 +                               if( quadflag ){
913 +                                       value.qvalue = va_arg( args, quad_t );
914 +                                       fmtquad( buffer, left,  &value,base,signed_val, ljust, len, zpad, precision );
915 +                                       break;
916 +                               } else
917 +#endif
918 +                               if( longflag > 1 ){
919 +#if defined(HAVE_LONG_LONG)
920 +                                       if( signed_val ){
921 +                                       value.value = va_arg( args, long long );
922 +                                       } else {
923 +                                       value.value = va_arg( args, unsigned long long );
924 +                                       }
925 +#else
926 +                                       if( signed_val ){
927 +                                       value.value = va_arg( args, long );
928 +                                       } else {
929 +                                       value.value = va_arg( args, unsigned long );
930 +                                       }
931 +#endif
932 +                               } else if( longflag ){
933 +                                       if( signed_val ){
934 +                                               value.value = va_arg( args, long );
935 +                                       } else {
936 +                                               value.value = va_arg( args, unsigned long );
937 +                                       }
938                                 } else {
939 -                                       value = va_arg( args, int );
940 +                                       if( signed_val ){
941 +                                               value.value = va_arg( args, int );
942 +                                       } else {
943 +                                               value.value = va_arg( args, unsigned int );
944 +                                       }
945                                 }
946 -                               fmtnum( value,-16,0, ljust, len, zpad, precision ); break;
947 +                               fmtnum( buffer, left,  &value,base,signed_val, ljust, len, zpad, precision ); break;
948                         case 's':
949                                 strvalue = va_arg( args, char *);
950 -                               fmtstr( strvalue,ljust,len, zpad, precision );
951 +                               fmtstr( visible_control, buffer, left, strvalue,ljust,len, zpad, precision );
952                                 break;
953                         case 'c':
954                                 ch = va_arg( args, int );
955                                 { char b[2];
956 -                                       int vsb = visible_control;
957                                         b[0] = ch;
958                                         b[1] = 0;
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 );
963                                 }
964                                 break;
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;
970                         case 'm':
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 );
976 +                               }
977 +                               break;
978 +                       case '%': dopr_outch( buffer, left, ch ); continue;
979                         default:
980 -                               dostr(  "???????" );
981 +                               dostr(  buffer, left, "???????" );
982                         }
983                         longflag = 0;
984                         break;
985                 default:
986 -                       dopr_outch( ch );
987 +                       dopr_outch( buffer, left, ch );
988                         break;
989                 }
990         }
991 -       *output = 0;
992  }
993  
994  /*
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
998   */
999 -static void
1000 -fmtstr(  char *value, int ljust, int len, int zpad, int precision )
1001 + static void
1002 + fmtstr( int visible_control, char **buffer, int *left,
1003 +        char *value, int ljust, int len, int zpad, int precision )
1004  {
1005 -       int padlen, strlen, i, c;       /* amount to pad */
1006 +       int padlen, strlenv, i, c;      /* amount to pad */
1007  
1008         if( value == 0 ){
1009                 value = "<NULL>";
1010         }
1011 -       if( precision > 0 ){
1012 -               strlen = precision;
1013 -       } else {
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 ) ){
1017 -                               ++strlen;
1018 -                       }
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' ){
1022 +                       ++strlenv;
1023                 }
1024 +               ++strlenv;
1025         }
1026 -       padlen = len - strlen;
1027 +       if( precision > 0 && strlenv > precision ){
1028 +               strlenv = precision;
1029 +       }
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, ' ' );
1036                 --padlen;
1037         }
1038         /* output characters */
1039 -       for( i = 0; (c = value[i]); ++i ){
1040 -               if( visible_control && iscntrl( c ) && !isspace( c ) ){
1041 -                       dopr_outch('^');
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));
1046                 }
1047 -               dopr_outch(c);
1048 +               dopr_outch(buffer, left, c);
1049         }
1050         while( padlen < 0 ) {
1051 -               dopr_outch( ' ' );
1052 +               dopr_outch( buffer, left, ' ' );
1053                 ++padlen;
1054         }
1055  }
1056  
1057 -static void
1058 -fmtnum(  long value, int base, int dosign, int ljust,
1059 + static void
1060 + fmtnum( char **buffer, int *left,
1061 +       union value *value, int base, int dosign, int ljust,
1062         int len, int zpad, int precision )
1063  {
1064         int signvalue = 0;
1065 +#if defined(HAVE_LONG_LONG)
1066 +       unsigned long long uvalue;
1067 +#else
1068         unsigned long uvalue;
1069 -       char convert[20];
1070 +#endif
1071 +       char convert[sizeof( union value) * 8 + 16];
1072         int place = 0;
1073         int padlen = 0; /* amount to pad */
1074         int caps = 0;
1075  
1076 -       /* DEBUGP(("value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n",
1077 -               value, base, dosign, ljust, len, zpad )); */
1078 -       uvalue = value;
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;
1082         if( dosign ){
1083 -               if( value < 0 ) {
1084 +               if( value->value < 0 ) {
1085                         signvalue = '-';
1086 -                       uvalue = -value; 
1087 +                       uvalue = -value->value; 
1088                 }
1089         }
1090         if( base < 0 ){
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 ){
1100                 if( signvalue ){
1101 -                       dopr_outch( signvalue );
1102 +                       dopr_outch( buffer, left, signvalue );
1103                         --padlen;
1104                         signvalue = 0;
1105                 }
1106                 while( padlen > 0 ){
1107 -                       dopr_outch( zpad );
1108 +                       dopr_outch( buffer, left, zpad );
1109                         --padlen;
1110                 }
1111         }
1112         while( padlen > 0 ) {
1113 -               dopr_outch( ' ' );
1114 +               dopr_outch( buffer, left, ' ' );
1115                 --padlen;
1116         }
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, ' ' );
1124                 ++padlen;
1125         }
1126  }
1127  
1128 -static void
1129 -fmtdouble( int fmt, double value, int ljust, int len, int zpad, int precision )
1130 +#if defined(HAVE_QUAD_T)
1131 +
1132 + static void
1133 + fmtquad( char **buffer, int *left,
1134 +       union value *value, int base, int dosign, int ljust,
1135 +       int len, int zpad, int precision )
1136  {
1137 -       char convert[128];
1138 -       char fmtstr[128];
1139 -       int l;
1140 +       int signvalue = 0;
1141 +       int place = 0;
1142 +       int padlen = 0; /* amount to pad */
1143 +       int caps = 0;
1144 +       int i, c;
1145 +       union {
1146 +               quad_t qvalue;
1147 +               unsigned char qconvert[sizeof(quad_t)];
1148 +       } vvalue;
1149 +       char convert[2*sizeof(quad_t)+1];
1150 +
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;
1154  
1155 -       if( len == 0 ) len = 10;
1156 -       if( len > sizeof(convert) - 10 ){
1157 -               len = sizeof(convert) - 10;
1158 +       if( base < 0 ){
1159 +               caps = 1;
1160         }
1161 -       if( precision > sizeof(convert) - 10 ){
1162 -               precision = sizeof(convert) - 10;
1163 +
1164 +       for( i = 0; i < (int)sizeof(quad_t); ++i ){
1165 +               c = vvalue.qconvert[i];
1166 +               convert[2*i] = 
1167 +                       (caps? "0123456789ABCDEF":"0123456789abcdef")[ (c >> 4) & 0xF];
1168 +               convert[2*i+1] = 
1169 +                       (caps? "0123456789ABCDEF":"0123456789abcdef")[ c  & 0xF];
1170         }
1171 -       if( precision > len ) precision = len;
1172 -       strcpy( fmtstr, "%" );
1173 -       if( ljust ) strcat(fmtstr, "-" );
1174 -       if( len ){
1175 -               sprintf( fmtstr+strlen(fmtstr), "%d", len );
1176 +       convert[2*i] = 0;
1177 +
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 ){
1185 +               if( signvalue ){
1186 +                       dopr_outch( buffer, left, signvalue );
1187 +                       --padlen;
1188 +                       signvalue = 0;
1189 +               }
1190 +               while( padlen > 0 ){
1191 +                       dopr_outch( buffer, left, zpad );
1192 +                       --padlen;
1193 +               }
1194 +       }
1195 +       while( padlen > 0 ) {
1196 +               dopr_outch( buffer, left, ' ' );
1197 +               --padlen;
1198 +       }
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, ' ' );
1203 +               ++padlen;
1204 +       }
1205 +}
1206 +
1207 +#endif
1208 +
1209 + static void mystrcat(char *dest, char *src )
1210 +{
1211 +       if( dest && src ){
1212 +               dest += safestrlen(dest);
1213 +               strcpy(dest,src);
1214 +       }
1215 +}
1216 +
1217 + static void
1218 + fmtdouble( char **buffer, int *left,
1219 +       int fmt, double value, int ljust, int len, int zpad, int precision )
1220 +{
1221 +       char convert[sizeof( union value) * 8 + 512];
1222 +       char formatstr[128];
1223 +
1224 +       /* fprintf(stderr,"len %d, precision %d\n", len, precision ); */
1225 +       if( len > 255 ){
1226 +               len = 255;
1227 +       }
1228 +       if( precision > 255 ){
1229 +               precision = 255;
1230 +       }
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 */
1235 +       if( len >= 0 ){
1236 +               sprintf( formatstr+strlen(formatstr), "%d", len ); /* 3 */
1237         }
1238 -       if( precision > 0 ){
1239 -               sprintf( fmtstr+strlen(fmtstr), ".%d", precision );
1240 +       if( precision >= 0 ){
1241 +               sprintf( formatstr+strlen(formatstr), ".%d", precision ); /* 3 */
1242         }
1243 -       l = strlen( fmtstr );
1244 -       fmtstr[l] = fmt;
1245 -       fmtstr[l+1] = 0;
1246 -       sprintf( convert, fmtstr, value );
1247 -       dostr( convert );
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 );
1254  }
1255  
1256 -static void dostr( char *str )
1257 + static void dostr( char **buffer, int *left, char *str  )
1258  {
1259 -       while(*str) dopr_outch(*str++);
1260 +       if(str)while(*str) dopr_outch( buffer, left, *str++ );
1261  }
1262  
1263 -static void dopr_outch( int c )
1264 + static void dopr_outch( char **buffer, int *left, int c )
1265  {
1266 -       if( end == 0 || output < end ){
1267 -               *output++ = c;
1268 +       if( *left > 0 ){
1269 +               *(*buffer)++ = c;
1270         }
1271 +       *left -= 1;
1272  }
1273  
1274  
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)
1280 -
1281 -# if defined(HAVE_SYS_NERR)
1282 -#  if !defined(HAVE_SYS_NERR_DEF)
1283 -     extern int sys_nerr;
1284 -#  endif
1285 -#  define num_errors    (sys_nerr)
1286 -# else
1287 -#  define num_errors    (-1)            /* always use "errno=%d" */
1288 -# endif
1289  
1290 +#if !defined(HAVE_STRERROR)
1291 +# undef  num_errors
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[];
1296  #  endif
1297 -# else
1298 -#  undef  num_errors
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;
1303 +#   endif
1304 +#   define num_errors    (sys_nerr)
1305 +#  endif
1306 +# endif
1307 +# if !defined(num_errors)
1308 +#   define num_errors   (-1)            /* always use "errno=%d" */
1309  # endif
1310 -
1311  #endif
1312  
1313 -static char * plp_Errormsg ( int err )
1314 + static char * plp_Errormsg ( int err, char *buffer /* int maxlen = 32 */)
1315  {
1316      char *cp;
1317  
1318 @@ -496,10 +1061,8 @@ static char * plp_Errormsg ( int err )
1319      } else
1320  # endif
1321         {
1322 -               static char msgbuf[32];     /* holds "errno=%d". */
1323 -               /* SAFE use of sprintf */
1324 -               (void) sprintf (msgbuf, "errno=%d", err);
1325 -               cp = msgbuf;
1326 +               (void) sprintf (buffer, "errno=%d", err);
1327 +               cp = buffer;
1328      }
1329  #endif
1330      return (cp);
1331 @@ -507,23 +1070,47 @@ static char * plp_Errormsg ( int err )
1332  
1333  #if defined(TEST)
1334  #include <stdio.h>
1335 -int main( void )
1336 + int main( void )
1337  {
1338         char buffer[128];
1339         char *t;
1340         char *test1 = "01234";
1341 +       int n;
1342         errno = 1;
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 );
1354 +       buffer[0] = 0;
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 );
1374 +#endif
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 );
1388         return(0);
1389  }
1390  #endif
This page took 0.131651 seconds and 2 git commands to generate.