1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
|
/**************************************************************************
* Copyright 1994-2000 Patrick Powell, San Diego, CA <papowell@astart.com>
*
* Modified for Netatalk 2002/02/12 Burkhard Schmidt <bs@cpfs.mpg.de>
**************************************************************************/
/*
Overview:
plp_snprintf( char *buffer, int len, const char *format,...)
plp_unsafe_snprintf( char *buffer, int len, const char *format,...)
its horribly unsafe companion that does NOT protect you from
the printing of evil control characters, but may be necessary
See the man page documentation below
This version of snprintf was developed originally for printing
on a motley collection of specialized hardware that had NO IO
library. Due to contractual restrictions, a clean room implementation
of the printf() code had to be developed.
The method chosen for printf was to be as paranoid as possible,
as these platforms had NO memory protection, and very small
address spaces. This made it possible to try to print
very long strings, i.e. - all of memory, very easily. To guard
against this, all printing was done via a buffer, generous enough
to hold strings, but small enough to protect against overruns,
etc.
Strangely enough, this proved to be of immense importance when
SPRINTFing to a buffer on a stack... The rest, of course, is
well known, as buffer overruns in the stack are a common way to
do horrible things to operating systems, security, etc etc.
This version of snprintf is VERY limited by modern standards.
Revision History:
First Released Version - 1994. This version had NO comments.
First Released Version - 1994. This version had NO comments.
Second Major Released Version - Tue May 23 10:43:44 PDT 2000
Configuration and other items changed. Read this doc.
Treat this as a new version.
COPYRIGHT AND TERMS OF USE:
You may use, copy, distribute, or otherwise incorporate this software
and documentation into any product or other item, provided that
the copyright in the documentation and source code as well as the
source code generated constant strings in the object, executable
or other code remain in place and are present in executable modules
or objects.
You may modify this code as appropriate to your usage; however the
modified version must be identified by changing the various source
and object code identification strings as is appropriately noted
in the source code.
The next include line is expected to work in conjunction with the
GNU CONFIGURE utility. You should define the following macros
appropriately:
HAVE_STDARG_H - if the <stdargs.h> include file is available
HAVE_VARARG_H - if the <varargs.h> include file is available
HAVE_STRERROR - if the strerror() routine is available.
If it is not available, then examine the lines containing
the tests below. You may need to fiddle with HAVE_SYS_NERR
HAVE_SYS_ERRLIST
HAVE_DECL_SYS_ERRLIST
HAVE_SYS_NERR
HAVE_DECL_SYS_NERR
HAVE_QUAD_T - if the quad_t type is defined
HAVE_LONG_LONG - if the long long type is defined
HAVE_LONG_DOUBLE - if the long double type is defined
If you are using the GNU configure (autoconf) facility, add the
following line to the configure.in file, to force checking for the
quad_t and long long data types:
AC_CHECK_FUNCS(strerror);
AC_CACHE_CHECK(for errno,
ac_cv_errno,
[
AC_TRY_LINK(,[extern int errno; return (errno);],
ac_cv_errno=yes, ac_cv_errno=no)
])
if test "$ac_cv_errno" = yes; then
AC_DEFINE(HAVE_ERRNO)
AC_CACHE_CHECK(for errno declaration,
ac_cv_decl_errno,
[
AC_TRY_COMPILE([
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
],[return(sys_nerr);],
ac_cv_decl_errno=yes, ac_cv_decl_errno=no)
])
if test "$ac_cv_decl_errno" = yes; then
AC_DEFINE(HAVE_DECL_ERRNO)
fi;
fi
AC_CACHE_CHECK(for sys_nerr,
ac_cv_sys_nerr,
[
AC_TRY_LINK(,[extern int sys_nerr; return (sys_nerr);],
ac_cv_sys_nerr=yes, ac_cv_sys_nerr=no)
])
if test "$ac_cv_sys_nerr" = yes; then
AC_DEFINE(HAVE_SYS_NERR)
AC_CACHE_CHECK(for sys_nerr declaration,
ac_cv_decl_sys_nerr,
[
AC_TRY_COMPILE([
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif],[return(sys_nerr);],
ac_cv_decl_sys_nerr_def=yes, ac_cv_decl_sys_nerr_def=no)
])
if test "$ac_cv_decl_sys_nerr" = yes; then
AC_DEFINE(HAVE_DECL_SYS_NERR)
fi
fi
AC_CACHE_CHECK(for sys_errlist array,
ac_cv_sys_errlist,
[AC_TRY_LINK(,[extern char *sys_errlist[];
sys_errlist[0];],
ac_cv_sys_errlist=yes, ac_cv_sys_errlist=no)
])
if test "$ac_cv_sys_errlist" = yes; then
AC_DEFINE(HAVE_SYS_ERRLIST)
AC_CACHE_CHECK(for sys_errlist declaration,
ac_cv_sys_errlist_def,
[AC_TRY_COMPILE([
#include <stdio.h>
#include <errno.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif],[char *s = sys_errlist[0]; return(*s);],
ac_cv_decl_sys_errlist=yes, ac_cv_decl_sys_errlist=no)
])
if test "$ac_cv_decl_sys_errlist" = yes; then
AC_DEFINE(HAVE_DECL_SYS_ERRLIST)
fi
fi
AC_CACHE_CHECK(checking for long long,
ac_cv_long_long,
[
AC_TRY_COMPILE([
#include <stdio.h>
#include <sys/types.h>
], [printf("%d",sizeof(long long));],
ac_cv_long_long=yes, ac_cv_long_long=no)
])
if test $ac_cv_long_long = yes; then
AC_DEFINE(HAVE_LONG_LONG)
fi
AC_CACHE_CHECK(checking for long double,
ac_cv_long_double,
[
AC_TRY_COMPILE([
#include <stdio.h>
#include <sys/types.h>
], [printf("%d",sizeof(long double));],
ac_cv_long_double=yes, ac_cv_long_double=no)
])
if test $ac_cv_long_double = yes; then
AC_DEFINE(HAVE_LONG_DOUBLE)
fi
AC_CACHE_CHECK(checking for quad_t,
ac_cv_quad_t,
[
AC_TRY_COMPILE([
#include <stdio.h>
#include <sys/types.h>
], [printf("%d",sizeof(quad_t));],
ac_cv_quad_t=yes, ac_cv_quad_t=no)
])
if test $ac_cv_quad_t = yes; then
AC_DEFINE(HAVE_QUAD_T)
fi
NAME
plp_snprintf, plp_vsnprintf - formatted output conversion
SYNOPSIS
#include <stdio.h>
#include <stdarg.h>
int
plp_snprintf(const char *format, size_t size, va_list ap);
int
plp_unsafe_snprintf(const char *format, size_t size, va_list ap);
AKA snprintf and unsafe_snprintf in the documentation below
int
vsnprintf(char *str, size_t size, const char *format, va_list ap);
int
unsafe_vsnprintf(char *str, size_t size, const char *format, va_list ap);
AKA vsnprintf and unsafe_vsnprintf in the documentation below
(Multithreaded Safe)
DESCRIPTION
The printf() family of functions produces output according to
a format as described below. Snprintf(), and vsnprintf()
write to the character string str. These functions write the
output under the control of a format string that specifies
how subsequent arguments (or arguments accessed via the
variable-length argument facilities of stdarg(3)) are converted
for output. These functions return the number of characters
printed (not including the trailing `\0' used to end output
to strings). Snprintf() and vsnprintf() will write at most
size-1 of the characters printed into the output string (the
size'th character then gets the terminating `\0'); if the
return value is greater than or equal to the size argument,
the string was too short and some of the printed characters
were discarded. The size or str may be given as zero to find
out how many characters are needed; in this case, the str
argument is ignored.
By default, the snprintf function will not format control
characters (except new line and tab) in strings. This is a
safety feature that has proven to be extremely critical when
using snprintf for secure applications and when debugging.
If you MUST have control characters formatted or printed,
then use the unsafe_snprintf() and unsafe_vsnprintf() and on
your own head be the consequences. You have been warned.
There is one exception to the comments above, and that is
the "%c" (character) format. It brutally assumes that the
user will have performed the necessary 'isprint()' or other
checks and uses the integer value as a character.
The format string is composed of zero or more directives:
ordinary characters (not %), which are copied unchanged to
the output stream; and conversion specifications, each
of which results in fetching zero or more subsequent arguments.
Each conversion specification is introduced by the character
%. The arguments must correspond properly (after type promotion)
with the conversion specifier. After the %, the following
appear in sequence:
o Zero or more of the following flags:
- A zero `0' character specifying zero padding. For
all conversions except n, the converted value is padded
on the left with zeros rather than blanks. If a
precision is given with a numeric conversion (d, i,
o, u, i, x, and X), the `0' flag is ignored.
- A negative field width flag `-' indicates the converted
value is to be left adjusted on the field boundary. Except
for n conversions, the converted value is padded on
the right with blanks, rather than on the left with
blanks or zeros. A `-' overrides a `0' if both are
given.
- A space, specifying that a blank should be left before
a positive number produced by a signed conversion (d, e, E, f,
g, G, or i).
- A `+' character specifying that a sign always be placed
before a number produced by a signed conversion. A `+' overrides
a space if both are used.
o An optional decimal digit string specifying a minimum
field width. If the converted value has fewer
characters than the field width, it will be padded
with spaces on the left (or right, if the
left-adjustment flag has been given) to fill out
the field width.
o An optional precision, in the form of a period `.' followed
by an optional digit string. If the digit string
is omitted, the precision is taken as zero. This
gives the minimum number of digits to appear for
d, i, o, u, x, and X conversions, the number of
digits to appear after the decimal-point for e,
E, and f conversions, the maximum number of
significant digits for g and G conversions, or
the maximum number of characters to be printed
from a string for s conversions.
o The optional character h, specifying that a following d,
i, o, u, x, or X conversion corresponds to a short
int or unsigned short int argument, or that a
following n conversion corresponds to a pointer
to a short int argument.
o The optional character l (ell) specifying that a following
d, i, o, u, x, or X conversion applies to a pointer
to a long int or unsigned long int argument, or
that a following n conversion corresponds to a
pointer to a long int argument.
o The optional character q, specifying that a following d,
i, o, u, x, or X conversion corresponds to a quad_t
or u_quad_t argument, or that a following n
conversion corresponds to a quad_t argument.
This value is always printed in HEX notation. Tough.
quad_t's are an OS system implementation, and should
not be allowed.
o The character L specifying that a following e, E, f, g,
or G conversion corresponds to a long double
argument.
o A character that specifies the type of conversion to be applied.
A field width or precision, or both, may be indicated by an asterisk `*'
instead of a digit string. In this case, an int argument supplies the
field width or precision. A negative field width is treated as a left
adjustment flag followed by a positive field width; a negative precision
is treated as though it were missing.
The conversion specifiers and their meanings are:
diouxX The int (or appropriate variant) argument is converted to signed
decimal (d and i), unsigned octal (o), unsigned decimal
(u), or unsigned hexadecimal (x and X) notation. The
letters abcdef are used for x conversions; the letters
ABCDEF are used for X conversions. The precision, if
any, gives the minimum number of digits that must
appear; if the converted value requires fewer digits,
it is padded on the left with zeros.
eE The double argument is rounded and converted in the style
[-]d.ddde+-dd where there is one digit before the decimal-point
character and the number of digits after it is equal
to the precision; if the precision is missing, it is
taken as 6; if the precision is zero, no decimal-point
character appears. An E conversion uses the letter
E (rather than e) to introduce the exponent.
The exponent always contains at least two digits; if
the value is zero, the exponent is 00.
f The double argument is rounded and converted to decimal notation
in the style [-]ddd.ddd, where the number of digits after the
decimal-point character is equal to the precision specification.
If the precision is missing, it is taken as 6; if the precision
is explicitly zero, no decimal-point character appears. If a
decimal point appears, at least one digit appears before it.
g The double argument is converted in style f or e (or
E for G conversions). The precision specifies the
number of significant digits. If the precision is
missing, 6 digits are given; if the precision is zero,
it is treated as 1. Style e is used if the exponent
from its conversion is less than -4 or greater than
or equal to the precision. Trailing zeros are removed
from the fractional part of the result; a decimal
point appears only if it is followed by at least one
digit.
c The int argument is converted to an unsigned char,
and the resulting character is written.
s The ``char *'' argument is expected to be a pointer to an array
of character type (pointer to a string). Characters
from the array are written up to (but not including)
a terminating NUL character; if a precision is
specified, no more than the number specified are
written. If a precision is given, no null character
need be present; if the precision is not specified,
or is greater than the size of the array, the array
must contain a terminating NUL character.
% A `%' is written. No argument is converted. The complete
conversion specification is `%%'.
In no case does a non-existent or small field width cause truncation of a
field; if the result of a conversion is wider than the field width, the
field is expanded to contain the conversion result.
EXAMPLES
To print a date and time in the form `Sunday, July 3, 10:02', where
weekday and month are pointers to strings:
#include <stdio.h>
fprintf(stdout, "%s, %s %d, %.2d:%.2d\n",
weekday, month, day, hour, min);
To print pi to five decimal places:
#include <math.h>
#include <stdio.h>
fprintf(stdout, "pi = %.5f\n", 4 * atan(1.0));
To allocate a 128 byte string and print into it:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
char *newfmt(const char *fmt, ...)
{
char *p;
va_list ap;
if ((p = malloc(128)) == NULL)
return (NULL);
va_start(ap, fmt);
(void) vsnprintf(p, 128, fmt, ap);
va_end(ap);
return (p);
}
SEE ALSO
printf(1), scanf(3)
STANDARDS
Turkey C Standardization and wimpy POSIX folks did not define
snprintf or vsnprintf().
BUGS
The conversion formats %D, %O, and %U are not standard and are provided
only for backward compatibility. The effect of padding the %p format
with zeros (either by the `0' flag or by specifying a precision), and the
benign effect (i.e., none) of the `#' flag on %n and %p conversions, as
well as other nonsensical combinations such as %Ld, are not standard;
such combinations should be avoided.
The typedef names quad_t and u_quad_t are infelicitous.
*/
#include "config.h"
#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
#include <sys/types.h>
#include <ctype.h>
#include <stdlib.h>
#if defined(HAVE_STRING_H)
# include <string.h>
#endif
#if defined(HAVE_STRINGS_H)
# include <strings.h>
#endif
#include <stdio.h>
/*
* For testing, define these values
*/
#if 0
#define HAVE_STDARG_H 1
#define TEST 1
#define HAVE_QUAD_T 1
#endif
/**** ENDINCLUDE ****/
/*************************************************
* KEEP THIS STRING - MODIFY AT THE END WITH YOUR REVISIONS
* i.e. - the LOCAL REVISIONS part is for your use
*************************************************/
static char *const _id = "plp_snprintf V98.12.21 Copyright Patrick Powell 1988-2000 "
"$Id: snprintf.c,v 1.2 2008-11-14 10:29:08 didg Exp $"
" LOCAL REVISIONS: Modified for Netatalk 2002/02/12 Burkhard Schmidt";
/* varargs declarations: */
# undef HAVE_STDARGS /* let's hope that works everywhere (mj) */
# undef VA_LOCAL_DECL
# undef VA_START
# undef VA_SHIFT
# undef VA_END
#if defined(HAVE_STDARG_H)
# include <stdarg.h>
# define HAVE_STDARGS /* let's hope that works everywhere (mj) */
# define VA_LOCAL_DECL va_list ap;
# define VA_START(f) va_start(ap, f)
# define VA_SHIFT(v,t) ; /* no-op for ANSI */
# define VA_END va_end(ap)
#else
# if defined(HAVE_VARARGS_H)
# include <varargs.h>
# undef HAVE_STDARGS
# define VA_LOCAL_DECL va_list ap;
# define VA_START(f) va_start(ap) /* f is ignored! */
# define VA_SHIFT(v,t) v = va_arg(ap,t)
# define VA_END va_end(ap)
# else
XX ** NO VARARGS ** XX
# endif
#endif
union value {
#if defined(HAVE_QUAD_T)
quad_t qvalue;
#endif
#if defined(HAVE_LONG_LONG)
long long value;
#else
long value;
#endif
double dvalue;
};
#undef CVAL
#define CVAL(s) (*((unsigned char *)s))
extern int errno;
static char * plp_Errormsg ( int err, char *buffer );
static void dopr( int visible_control, char **buffer, int *left,
const char *format, va_list args );
static void fmtstr( int visible_control, char **buffer, int *left,
char *value, int ljust, int len, int zpad, int precision );
static void fmtnum( char **buffer, int *left,
union value *value, int base, int dosign,
int ljust, int len, int zpad, int precision );
#if defined(HAVE_QUAD_T)
static void fmtquad( char **buffer, int *left,
union value *value, int base, int dosign,
int ljust, int len, int zpad, int precision );
#endif
static void fmtdouble( char **bufer, int *left,
int fmt, double value,
int ljust, int len, int zpad, int precision );
static void dostr( char **buffer, int *left, char *str );
static void dopr_outch( char **buffer, int *left, int c );
int plp_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
{
int left;
char *buffer;
if( count < 0 ) count = 0;
left = count;
if( count == 0 ) str = 0;
buffer = str;
dopr( 1, &buffer, &left, fmt, args );
/* fprintf(stderr,"str 0x%x, buffer 0x%x, count %d, left %d\n",
(int)str, (int)buffer, count, left ); */
if( str && count > 0 ){
if( left > 0 ){
str[count-left] = 0;
} else {
str[count-1] = 0;
}
}
return(count - left);
}
int plp_unsafe_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
{
int left;
char *buffer;
if( count < 0 ) count = 0;
left = count;
if( count == 0 ) str = 0;
buffer = str;
dopr( 0, &buffer, &left, fmt, args );
/* fprintf(stderr,"str 0x%x, buffer 0x%x, count %d, left %d\n",
(int)str, (int)buffer, count, left ); */
if( str && count > 0 ){
if( left > 0 ){
str[count-left] = 0;
} else {
str[count-1] = 0;
}
}
return(count - left);
}
/* VARARGS3 */
#ifdef HAVE_STDARGS
int plp_snprintf (char *str,size_t count,const char *fmt,...)
#else
int plp_snprintf (va_alist) va_dcl
#endif
{
#ifndef HAVE_STDARGS
char *str;
size_t count;
char *fmt;
#endif
int n = 0;
VA_LOCAL_DECL
VA_START (fmt);
VA_SHIFT (str, char *);
VA_SHIFT (count, size_t );
VA_SHIFT (fmt, char *);
n = plp_vsnprintf ( str, count, fmt, ap);
VA_END;
return( n );
}
/* VARARGS3 */
#ifdef HAVE_STDARGS
int plp_unsafe_snprintf (char *str,size_t count,const char *fmt,...)
#else
int plp_unsafe_snprintf (va_alist) va_dcl
#endif
{
#ifndef HAVE_STDARGS
char *str;
size_t count;
char *fmt;
#endif
int n = 0;
VA_LOCAL_DECL
VA_START (fmt);
VA_SHIFT (str, char *);
VA_SHIFT (count, size_t );
VA_SHIFT (fmt, char *);
n = plp_unsafe_vsnprintf ( str, count, fmt, ap);
VA_END;
return( n );
}
static void dopr( int visible_control, char **buffer, int *left, const char *format, va_list args )
{
int ch;
union value value;
int longflag = 0;
int quadflag = 0;
char *strvalue;
int ljust;
int len;
int zpad;
int precision;
int set_precision;
double dval;
int err = errno;
int base = 0;
int signed_val = 0;
while( (ch = *format++) ){
switch( ch ){
case '%':
longflag = quadflag =
ljust = len = zpad = base = signed_val = 0;
precision = -1; set_precision = 0;
nextch:
ch = *format++;
switch( ch ){
case 0:
dostr( buffer, left, "**end of format**" );
return;
case '-': ljust = 1; goto nextch;
case '.': set_precision = 1; precision = 0; goto nextch;
case '*': len = va_arg( args, int ); goto nextch;
case '0': /* set zero padding if len not set */
if(len==0 && set_precision == 0 ) zpad = '0';
case '1': case '2': case '3':
case '4': case '5': case '6':
case '7': case '8': case '9':
if( set_precision ){
precision = precision*10 + ch - '0';
} else {
len = len*10 + ch - '0';
}
goto nextch;
case 'l': ++longflag; goto nextch;
case 'q':
#if !defined( HAVE_QUAD_T )
dostr( buffer, left, "*no quad_t support *");
return;
#endif
quadflag = 1;
goto nextch;
case 'u': case 'U':
if( base == 0 ){ base = 10; signed_val = 0; }
case 'o': case 'O':
if( base == 0 ){ base = 8; signed_val = 0; }
case 'd': case 'D':
if( base == 0 ){ base = 10; signed_val = 1; }
case 'x':
if( base == 0 ){ base = 16; signed_val = 0; }
case 'X':
if( base == 0 ){ base = -16; signed_val = 0; }
#if defined( HAVE_QUAD_T )
if( quadflag ){
value.qvalue = va_arg( args, quad_t );
fmtquad( buffer, left, &value,base,signed_val, ljust, len, zpad, precision );
break;
} else
#endif
if( longflag > 1 ){
#if defined(HAVE_LONG_LONG)
if( signed_val ){
value.value = va_arg( args, long long );
} else {
value.value = va_arg( args, unsigned long long );
}
#else
if( signed_val ){
value.value = va_arg( args, long );
} else {
value.value = va_arg( args, unsigned long );
}
#endif
} else if( longflag ){
if( signed_val ){
value.value = va_arg( args, long );
} else {
value.value = va_arg( args, unsigned long );
}
} else {
if( signed_val ){
value.value = va_arg( args, int );
} else {
value.value = va_arg( args, unsigned int );
}
}
fmtnum( buffer, left, &value,base,signed_val, ljust, len, zpad, precision ); break;
case 's':
strvalue = va_arg( args, char *);
fmtstr( visible_control, buffer, left, strvalue,ljust,len, zpad, precision );
break;
case 'c':
ch = va_arg( args, int );
{ char b[2];
b[0] = ch;
b[1] = 0;
fmtstr( 0, buffer, left, b,ljust,len, zpad, precision );
}
break;
case 'f': case 'g': case 'e':
dval = va_arg( args, double );
fmtdouble( buffer, left, ch, dval,ljust,len, zpad, precision ); break;
case 'm':
{ char shortbuffer[32];
fmtstr( visible_control, buffer, left,
plp_Errormsg(err, shortbuffer),ljust,len, zpad, precision );
}
break;
case '%': dopr_outch( buffer, left, ch ); continue;
default:
dostr( buffer, left, "???????" );
}
longflag = 0;
break;
default:
dopr_outch( buffer, left, ch );
break;
}
}
}
/*
* Format '%[-]len[.precision]s'
* - = left justify (ljust)
* len = minimum length
* precision = numbers of chars in string to use
*/
static void
fmtstr( int visible_control, char **buffer, int *left,
char *value, int ljust, int len, int zpad, int precision )
{
int padlen, strlenv, i, c; /* amount to pad */
if( value == 0 ){
value = "<NULL>";
}
/* cheap strlen so you do not have library call */
for( strlenv = i = 0; (c=CVAL(value+i)); ++i ){
if( visible_control && iscntrl( c ) && c != '\t' && c != '\n' ){
++strlenv;
}
++strlenv;
}
if( precision > 0 && strlenv > precision ){
strlenv = precision;
}
padlen = len - strlenv;
if( padlen < 0 ) padlen = 0;
if( ljust ) padlen = -padlen;
while( padlen > 0 ) {
dopr_outch( buffer, left, ' ' );
--padlen;
}
/* output characters */
for( i = 0; i < strlenv && (c = CVAL(value+i)); ++i ){
if( visible_control && iscntrl( c ) && c != '\t' && c != '\n' ){
dopr_outch(buffer, left, '^');
c = ('@' | (c & 0x1F));
}
dopr_outch(buffer, left, c);
}
while( padlen < 0 ) {
dopr_outch( buffer, left, ' ' );
++padlen;
}
}
static void
fmtnum( char **buffer, int *left,
union value *value, int base, int dosign, int ljust,
int len, int zpad, int precision )
{
int signvalue = 0;
#if defined(HAVE_LONG_LONG)
unsigned long long uvalue;
#else
unsigned long uvalue;
#endif
char convert[sizeof( union value) * 8 + 16];
int place = 0;
int padlen = 0; /* amount to pad */
int caps = 0;
/* fprintf(stderr,"value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n",
value, base, dosign, ljust, len, zpad );/ **/
uvalue = value->value;
if( dosign ){
if( value->value < 0 ) {
signvalue = '-';
uvalue = -value->value;
}
}
if( base < 0 ){
caps = 1;
base = -base;
}
do{
convert[place++] =
(caps? "0123456789ABCDEF":"0123456789abcdef")
[uvalue % (unsigned)base ];
uvalue = (uvalue / (unsigned)base );
}while(uvalue);
convert[place] = 0;
padlen = len - place;
if( padlen < 0 ) padlen = 0;
if( ljust ) padlen = -padlen;
/* fprintf( stderr, "str '%s', place %d, sign %c, padlen %d\n",
convert,place,signvalue,padlen); / **/
if( zpad && padlen > 0 ){
if( signvalue ){
dopr_outch( buffer, left, signvalue );
--padlen;
signvalue = 0;
}
while( padlen > 0 ){
dopr_outch( buffer, left, zpad );
--padlen;
}
}
while( padlen > 0 ) {
dopr_outch( buffer, left, ' ' );
--padlen;
}
if( signvalue ) dopr_outch( buffer, left, signvalue );
while( place > 0 ) dopr_outch( buffer, left, convert[--place] );
while( padlen < 0 ){
dopr_outch( buffer, left, ' ' );
++padlen;
}
}
#if defined(HAVE_QUAD_T)
static void
fmtquad( char **buffer, int *left,
union value *value, int base, int dosign, int ljust,
int len, int zpad, int precision )
{
int signvalue = 0;
int place = 0;
int padlen = 0; /* amount to pad */
int caps = 0;
int i, c;
union {
quad_t qvalue;
unsigned char qconvert[sizeof(quad_t)];
} vvalue;
char convert[2*sizeof(quad_t)+1];
/* fprintf(stderr,"value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n",
value, base, dosign, ljust, len, zpad );/ **/
vvalue.qvalue = value->qvalue;
if( base < 0 ){
caps = 1;
}
for( i = 0; i < sizeof(quad_t); ++i ){
c = vvalue.qconvert[i];
convert[2*i] =
(caps? "0123456789ABCDEF":"0123456789abcdef")[ (c >> 4) & 0xF];
convert[2*i+1] =
(caps? "0123456789ABCDEF":"0123456789abcdef")[ c & 0xF];
}
convert[2*i] = 0;
place = strlen(convert);
padlen = len - place;
if( padlen < 0 ) padlen = 0;
if( ljust ) padlen = -padlen;
/* fprintf( stderr, "str '%s', place %d, sign %c, padlen %d\n",
convert,place,signvalue,padlen); / **/
if( zpad && padlen > 0 ){
if( signvalue ){
dopr_outch( buffer, left, signvalue );
--padlen;
signvalue = 0;
}
while( padlen > 0 ){
dopr_outch( buffer, left, zpad );
--padlen;
}
}
while( padlen > 0 ) {
dopr_outch( buffer, left, ' ' );
--padlen;
}
if( signvalue ) dopr_outch( buffer, left, signvalue );
while( place > 0 ) dopr_outch( buffer, left, convert[--place] );
while( padlen < 0 ){
dopr_outch( buffer, left, ' ' );
++padlen;
}
}
#endif
static void mystrcat(char *dest, char *src )
{
if( dest && src ){
dest += strlen(dest);
strcpy(dest,src);
}
}
static void
fmtdouble( char **buffer, int *left,
int fmt, double value, int ljust, int len, int zpad, int precision )
{
char convert[sizeof( union value) * 8 + 16];
char formatstr[128];
/* fprintf(stderr,"len %d, precision %d\n", len, precision ); */
if( len > (sizeof(convert) - 20) ){
len = sizeof(convert) - 20;
}
if( precision >= 0 && precision > sizeof(convert) - 20 ){
precision = sizeof(convert) - 20;
}
if( precision >= 0 && precision > len ) precision = len;
strcpy( formatstr, "%" );
if( ljust ) mystrcat(formatstr, "-" );
if( zpad ) mystrcat(formatstr, "0" );
if( len ){
sprintf( formatstr+strlen(formatstr), "%d", len );
}
if( precision >= 0 ){
sprintf( formatstr+strlen(formatstr), ".%d", precision );
}
sprintf( formatstr+strlen(formatstr), "%c", fmt );
/* this is easier than trying to do the portable dtostr */
/* fprintf(stderr,"format string '%s'\n", formatstr); */
sprintf( convert, formatstr, value );
dostr( buffer, left, convert );
}
static void dostr( char **buffer, int *left, char *str )
{
if(str)while(*str) dopr_outch( buffer, left, *str++ );
}
static void dopr_outch( char **buffer, int *left, int c )
{
if( *left > 0 ){
*(*buffer)++ = c;
}
*left -= 1;
}
/****************************************************************************
* static char *plp_errormsg( int err )
* returns a printable form of the
* errormessage corresponding to the valie of err.
* This is the poor man's version of sperror(), not available on all systems
* Patrick Powell Tue Apr 11 08:05:05 PDT 1995
****************************************************************************/
/****************************************************************************/
#if !defined(HAVE_STRERROR)
# undef num_errors
# if defined(HAVE_SYS_ERRLIST)
# if !defined(HAVE_DECL_SYS_ERRLIST)
extern const char *const sys_errlist[];
# endif
# if defined(HAVE_SYS_NERR)
# if !defined(HAVE_DECL_SYS_NERR)
extern int sys_nerr;
# endif
# define num_errors (sys_nerr)
# endif
# endif
# if !defined(num_errors)
# define num_errors (-1) /* always use "errno=%d" */
# endif
#endif
static char * plp_Errormsg ( int err, char *buffer /* int maxlen = 32 */)
{
char *cp;
#if defined(HAVE_STRERROR)
cp = (void *)strerror(err);
#else
# if defined(HAVE_SYS_ERRLIST)
if (err >= 0 && err < num_errors) {
cp = (void *)sys_errlist[err];
} else
# endif
{
(void) sprintf (buffer, "errno=%d", err);
cp = buffer;
}
#endif
return (cp);
}
#if defined(TEST)
#include <stdio.h>
int main( void )
{
char buffer[128];
char *t;
char *test1 = "01234";
int n;
errno = 1;
buffer[0] = 0;
n = plp_snprintf( buffer, 0, (t="test")); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t="errno '%s'")); printf( "[%d] %s = '%s'\n", n, t, buffer, strerror(errno) );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%12s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%-12s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%12.2s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%-12.2s"), test1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%g"), 1.2345 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%12g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%12.1g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%12.2g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%12.3g"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%0*d"), 6, 1 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
#if defined(HAVE_LONG_LONG)
n = plp_snprintf( buffer, sizeof(buffer), (t = "%llx"), 1, 2, 3, 4 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%llx"), (long long)1, (long long)2 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%qx"), 1, 2, 3, 4 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%qx"), (quad_t)1, (quad_t)2 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
#endif
n = plp_snprintf( buffer, sizeof(buffer), (t = "0%x, 0%x"), (char *)(0x01234567), (char *)0, 0, 0, 0); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "0%x, 0%x"), (char *)(0x01234567), (char *)0x89ABCDEF, 0, 0, 0); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "0%x, 0%x"), t, 0, 0, 0, 0); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%f"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%f"), 1.2345 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%12f"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%12.2f"), 1.25 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%.0f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%0.0f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%1.0f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%1.5f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
n = plp_snprintf( buffer, sizeof(buffer), (t = "%5.5f"), 1.0 ); printf( "[%d] %s = '%s'\n", n, t, buffer );
return(0);
}
#endif
#endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
#ifndef HAVE_VSNPRINTF
int vsnprintf(char *str, size_t count, const char *fmt, va_list args)
{
int n;
n = plp_vsnprintf(str, count, fmt, args);
return(n);
}
#endif /* ! HAVE_VSNPRINTF */
#ifndef HAVE_SNPRINTF
#ifdef HAVE_STDARGS
int snprintf (char *str,size_t count,const char *fmt,...)
#else
int snprintf (va_alist) va_dcl
#endif
{
#ifndef HAVE_STDARGS
char *str;
size_t count;
char *fmt;
#endif
int n = 0;
VA_LOCAL_DECL
VA_START (fmt);
VA_SHIFT (str, char *);
VA_SHIFT (count, size_t );
VA_SHIFT (fmt, char *);
n = plp_vsnprintf ( str, count, fmt, ap);
VA_END;
return( n );
}
#endif /* ! HAVE_VNSPRINTF */
|