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
|
/*
* snpf.c -- snprintf() empulation functions for lsof library
*
* V. Abell
* Purdue University Computing Center
*/
/*
* Copyright 2000 Purdue Research Foundation, West Lafayette, Indiana
* 47907. All rights reserved.
*
* Written by Victor A. Abell
*
* This software is not subject to any license of the American Telephone
* and Telegraph Company or the Regents of the University of California.
*
* This software has been adapted from snprintf.c in sendmail 8.9.3. It
* is subject to the sendmail copyright statements listed below, and the
* sendmail licensing terms stated in the sendmail LICENSE file comment
* section of this file.
*
* Permission is granted to anyone to use this software for any purpose on
* any computer system, and to alter it and redistribute it freely, subject
* to the following restrictions:
*
* 1. Neither the authors nor Purdue University are responsible for any
* consequences of the use of this software.
*
* 2. The origin of this software must not be misrepresented, either by
* explicit claim or by omission. Credit to the authors and Purdue
* University must appear in documentation and sources.
*
* 3. Altered versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 4. This notice may not be removed or altered.
*/
#include "../machine.h"
#ifdef USE_LIB_SNPF
/*
* Sendmail copyright statements:
*
* Copyright (c) 1998 Sendmail, Inc. All rights reserved.
* Copyright (c) 1997 Eric P. Allman. All rights reserved.
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*
* The LICENSE file may be found in the following comment section.
*/
/*
* Begin endmail LICENSE file.
SENDMAIL LICENSE
The following license terms and conditions apply, unless a different
license is obtained from Sendmail, Inc., 1401 Park Avenue, Emeryville, CA
94608, or by electronic mail at license@sendmail.com.
License Terms:
Use, Modification and Redistribution (including distribution of any
modified or derived work) in source and binary forms is permitted only if
each of the following conditions is met:
1. Redistributions qualify as "freeware" or "Open Source Software" under
one of the following terms:
(a) Redistributions are made at no charge beyond the reasonable cost of
materials and delivery.
(b) Redistributions are accompanied by a copy of the Source Code or by an
irrevocable offer to provide a copy of the Source Code for up to three
years at the cost of materials and delivery. Such redistributions
must allow further use, modification, and redistribution of the Source
Code under substantially the same terms as this license. For the
purposes of redistribution "Source Code" means the complete source
code of sendmail including all modifications.
Other forms of redistribution are allowed only under a separate royalty-
free agreement permitting such redistribution subject to standard
commercial terms and conditions. A copy of such agreement may be
obtained from Sendmail, Inc. at the above address.
2. Redistributions of source code must retain the copyright notices as they
appear in each source code file, these license terms, and the
disclaimer/limitation of liability set forth as paragraph 6 below.
3. Redistributions in binary form must reproduce the Copyright Notice,
these license terms, and the disclaimer/limitation of liability set
forth as paragraph 6 below, in the documentation and/or other materials
provided with the distribution. For the purposes of binary distribution
the "Copyright Notice" refers to the following language:
"Copyright (c) 1998 Sendmail, Inc. All rights reserved."
4. Neither the name of Sendmail, Inc. nor the University of California nor
the names of their contributors may be used to endorse or promote
products derived from this software without specific prior written
permission. The name "sendmail" is a trademark of Sendmail, Inc.
5. All redistributions must comply with the conditions imposed by the
University of California on certain embedded code, whose copyright
notice and conditions for redistribution are as follows:
(a) Copyright (c) 1988, 1993 The Regents of the University of
California. All rights reserved.
(b) Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
(i) Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
(ii) Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
(iii) All advertising materials mentioning features or use of this
software must display the following acknowledgement: "This
product includes software developed by the University of
California, Berkeley and its contributors."
(iv) Neither the name of the University nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
6. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY
SENDMAIL, INC. AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
NO EVENT SHALL SENDMAIL, INC., THE REGENTS OF THE UNIVERSITY OF
CALIFORNIA OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
(Version 8.6, last updated 6/24/1998)
* End endmail LICENSE file.
*/
/*
* If "ll" format support is not possible -- e.g., the long long type isn't
* supported -- define HAS_NO_LONG_LONG.
*/
# ifndef lint
static char copyright[] =
"@(#) Copyright 2000 Purdue Research Foundation.\nAll rights reserved.\n";
# endif /* !defined(lint) */
#include <varargs.h>
#if defined(__STDC__)
#define _PROTOTYPE(function, params) function params
#else /* !defined(__STDC__) */
#define _PROTOTYPE(function, params) function()
#endif /* defined(__STDC__) */
/*
** SNPRINTF, VSNPRINT -- counted versions of printf
**
** These versions have been grabbed off the net. They have been
** cleaned up to compile properly and support for .precision and
** %lx has been added.
*/
/**************************************************************
* Original:
* Patrick Powell Tue Apr 11 09:48:21 PDT 1995
* A bombproof version of doprnt (dopr) included.
* Sigh. This sort of thing is always nasty do deal with. Note that
* the version here does not include floating point...
*
* snprintf() is used instead of sprintf() as it does limit checks
* for string length. This covers a nasty loophole.
*
* The other functions are there to prevent NULL pointers from
* causing nast effects.
**************************************************************/
/*static char _id[] = "$Id: snpf.c,v 1.5 2008/10/21 16:13:23 abe Exp $";*/
/*
* Local function prototypes
*/
_PROTOTYPE(static void dopr,(char *bp, char *ep, char *fmt, va_list args));
_PROTOTYPE(static void dopr_outch,(char **bp, char *ep, int c));
_PROTOTYPE(static void dostr,(char **bp, char *ep, char *str, int));
# if !defined(HAS_NO_LONG_LONG)
_PROTOTYPE(static void fmtllnum,(char **bp, char *ep, long long value,
int base, int dosign, int ljust, int len,
int zpad));
# endif /* !defined(HAS_NO_LONG_LONG) */
_PROTOTYPE(static void fmtnum,(char **bp, char *ep, long value, int base,
int dosign, int ljust, int len, int zpad));
_PROTOTYPE(static void fmtstr,(char **bp, char *ep, char *value, int ljust,
int len, int zpad,
int maxwidth));
/*
* Local variables
*/
static int Length;
/*
* snpf() -- count-controlled sprintf()
*/
int
snpf(va_alist)
va_dcl /* requires at least three arguments:
* bp = receiving buffer pointer
* ct = length of buffer
* fmt = format string
*/
{
va_list args;
char *bp, *fmt;
int ct, len;
va_start(args);
bp = va_arg(args, char *);
ct = va_arg(args, int);
fmt = va_arg(args, char *);
len = vsnpf(bp, ct, fmt, args);
va_end(args);
return(len);
}
/*
* vsnpf() -- count-controlled vsprintf()
*/
int
vsnpf(str, count, fmt, args)
char *str; /* result buffer */
int count; /* size of buffer */
char *fmt; /* format */
va_list args; /* variable length argument list */
{
char *ep = str + count - 1;
*str = '\0';
(void) dopr(str, ep, fmt, args);
if (count > 0)
*ep = '\0';
return(Length);
}
/*
* dopr() -- poor man's version of doprintf
*/
static void
dopr(bp, ep, fmt, args)
char *bp; /* buffer start */
char *ep; /* buffer end (start + length - 1) */
char *fmt; /* format */
va_list args; /* variable length argument list */
{
int ch;
char ebuf[64];
int ebufl = (int)(sizeof(ebuf) - 1);
long value;
int longflag = 0;
int longlongflag = 0;
int pointflag = 0;
int maxwidth = 0;
char *strvalue;
int ljust;
int len;
int zpad;
int zxflag = 0;
# if !defined(HAS_NO_LONG_LONG)
long long llvalue;
# endif /* !defined(HAS_NO_LONG_LONG) */
Length = 0;
while((ch = *fmt++)) {
switch (ch) {
case '%':
ljust = len = zpad = zxflag = maxwidth = 0;
longflag = longlongflag = pointflag = 0;
nextch:
ch = *fmt++;
switch (ch) {
case '\0':
dostr(&bp, ep, "**end of format**" , 0);
return;
case '-':
ljust = 1;
goto nextch;
case '0': /* set zero padding if len not set */
if ((len == 0) && !pointflag)
zpad = '0';
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (pointflag)
maxwidth = (maxwidth * 10) + (int)(ch - '0');
else
len = (len * 10) + (int)(ch - '0');
goto nextch;
case '*':
if (pointflag)
maxwidth = va_arg(args, int);
else
len = va_arg(args, int);
goto nextch;
case '#':
zxflag = 1;
goto nextch;
case '.':
pointflag = 1;
goto nextch;
case 'l':
if (longflag) {
longflag = 0;
longlongflag = 1;
goto nextch;
}
longflag = 1;
goto nextch;
case 'u':
case 'U':
if (longlongflag) {
# if !defined(HAS_NO_LONG_LONG)
llvalue = va_arg(args, long long);
(void) fmtllnum(&bp,ep,llvalue,10,0,ljust,len,zpad);
# else /* defined(HAS_NO_LONG_LONG) */
(void) strncpy(ebuf, "ll is unsupported", ebufl);
ebuf[(int)ebufl] = '\0';
(void) dostr(&bp, ep, ebuf, 0);
# endif /* !defined(HAS_NO_LONG_LONG) */
break;
}
if (longflag)
value = va_arg(args, long);
else
value = va_arg(args, int);
(void) fmtnum(&bp, ep, value, 10,0, ljust, len, zpad);
break;
case 'o':
case 'O':
if (longlongflag) {
# if !defined(HAS_NO_LONG_LONG)
llvalue = va_arg(args, long long);
(void) fmtllnum(&bp,ep,llvalue,8,0,ljust,len,zpad);
# else /* defined(HAS_NO_LONG_LONG) */
(void) strncpy(ebuf, "ll is unsupported", ebufl);
ebuf[(int)ebufl] = '\0';
(void) dostr(&bp, ep, ebuf, 0);
# endif /* !defined(HAS_NO_LONG_LONG) */
break;
}
if (longflag)
value = va_arg(args, long);
else
value = va_arg(args, int);
(void) fmtnum(&bp, ep, value, 8,0, ljust, len, zpad);
break;
case 'd':
case 'D':
if (longlongflag) {
# if !defined(HAS_NO_LONG_LONG)
llvalue = va_arg(args, long long);
(void) fmtllnum(&bp,ep,llvalue,10,1,ljust,len,zpad);
# else /* defined(HAS_NO_LONG_LONG) */
(void) strncpy(ebuf, "ll is unsupported", ebufl);
ebuf[(int)ebufl] = '\0';
(void) dostr(&bp, ep, ebuf, 0);
# endif /* !defined(HAS_NO_LONG_LONG) */
break;
}
if (longflag)
value = va_arg(args, long);
else
value = va_arg(args, int);
(void) fmtnum(&bp, ep, value, 10,1, ljust, len, zpad);
break;
case 'x':
if (longlongflag) {
# if !defined(HAS_NO_LONG_LONG)
llvalue = va_arg(args, long long);
if (zxflag && llvalue) {
(void) dostr(&bp, ep, "0x", 0);
if (len >= 2)
len -= 2;
}
(void) fmtllnum(&bp,ep,llvalue,16,0,ljust,len,zpad);
# else /* defined(HAS_NO_LONG_LONG) */
(void) strncpy(ebuf, "ll is unsupported", ebufl);
ebuf[(int)ebufl] = '\0';
(void) dostr(&bp, ep, ebuf, 0);
# endif /* !defined(HAS_NO_LONG_LONG) */
break;
}
if (longflag)
value = va_arg(args, long);
else
value = va_arg(args, int);
if (zxflag && value) {
(void) dostr(&bp, ep, "0x", 0);
if (len >= 2)
len -= 2;
}
(void) fmtnum(&bp, ep, value, 16,0, ljust, len, zpad);
break;
case 'X':
if (longlongflag) {
# if !defined(HAS_NO_LONG_LONG)
llvalue = va_arg(args, long long);
if (zxflag && llvalue) {
(void) dostr(&bp, ep, "0x", 0);
if (len >= 2)
len -= 2;
}
(void) fmtllnum(&bp,ep,llvalue,-16,0,ljust,len,zpad);
# else /* defined(HAS_NO_LONG_LONG) */
(void) strncpy(ebuf, "ll is unsupported", ebufl);
ebuf[(int)ebufl] = '\0';
(void) dostr(&bp, ep, ebuf, 0);
# endif /* !defined(HAS_NO_LONG_LONG) */
break;
}
if (longflag)
value = va_arg(args, long);
else
value = va_arg(args, int);
if (zxflag && value) {
(void) dostr(&bp, ep, "0x", 0);
if (len >= 2)
len -= 2;
}
(void) fmtnum(&bp, ep, value,-16,0, ljust, len, zpad);
break;
case 's':
strvalue = va_arg(args, char *);
if (maxwidth > 0 || !pointflag) {
if (pointflag && len > maxwidth)
len = maxwidth; /* Adjust padding */
(void) fmtstr(&bp, ep, strvalue, ljust, len, zpad,
maxwidth);
}
break;
case 'c':
ch = va_arg(args, int);
dopr_outch(&bp, ep, ch);
break;
case '%':
(void) dopr_outch(&bp, ep, ch);
continue;
default:
ebuf[0] = ch;
(void) strncpy(&ebuf[1], " is unsupported", ebufl);
ebuf[(int)ebufl] = '\0';
(void) dostr(&bp, ep, ebuf, 0);
}
break;
default:
(void) dopr_outch(&bp, ep, ch);
break;
}
}
*bp = '\0';
}
# if !defined(HAS_NO_LONG_LONG)
/*
* fmtllnum() -- format long long number for output
*/
static void
fmtllnum(bp, ep, value, base, dosign, ljust, len, zpad)
char **bp; /* current buffer pointer */
char *ep; /* end of buffer (-1) */
long long value; /* number to format */
int base; /* number base */
int dosign; /* sign request */
int ljust; /* left justfication request */
int len; /* length request */
int zpad; /* zero padding request */
{
int signvalue = 0;
unsigned long long uvalue;
char convert[20];
int place = 0;
int padlen = 0; /* amount to pad */
int caps = 0;
uvalue = value;
if (dosign) {
if (value < 0) {
signvalue = '-';
uvalue = -value;
}
}
if (base < 0) {
caps = 1;
base = -base;
}
do {
convert[place++] =
(caps ? "0123456789ABCDEF" : "0123456789abcdef")
[uvalue % (unsigned)base];
uvalue = (uvalue / (unsigned)base);
} while (uvalue && (place < (int)(sizeof(convert) - 1)));
convert[place] = 0;
padlen = len - place;
if (padlen < 0)
padlen = 0;
if(ljust)
padlen = -padlen;
if (zpad && padlen > 0) {
if (signvalue) {
(void) dopr_outch(bp, ep, signvalue);
--padlen;
signvalue = 0;
}
while (padlen > 0) {
(void) dopr_outch(bp, ep, zpad);
--padlen;
}
}
while (padlen > 0) {
(void) dopr_outch(bp, ep, ' ');
--padlen;
}
if (signvalue)
(void) dopr_outch(bp, ep, signvalue);
while (place > 0)
(void) dopr_outch(bp, ep, convert[--place]);
while (padlen < 0) {
(void) dopr_outch(bp, ep, ' ');
++padlen;
}
}
# endif /* !defined(HAS_NO_LONG_LONG) */
/*
* fmtnum() -- format number for output
*/
static void
fmtnum(bp, ep, value, base, dosign, ljust, len, zpad)
char **bp; /* current buffer pointer */
char *ep; /* end of buffer (-1) */
long value; /* number to format */
int base; /* number base */
int dosign; /* sign request */
int ljust; /* left justfication request */
int len; /* length request */
int zpad; /* zero padding request */
{
int signvalue = 0;
unsigned long uvalue;
char convert[20];
int place = 0;
int padlen = 0; /* amount to pad */
int caps = 0;
uvalue = value;
if (dosign) {
if (value < 0) {
signvalue = '-';
uvalue = -value;
}
}
if (base < 0) {
caps = 1;
base = -base;
}
do {
convert[place++] =
(caps ? "0123456789ABCDEF" : "0123456789abcdef")
[uvalue % (unsigned)base];
uvalue = (uvalue / (unsigned)base);
} while (uvalue && (place < (int)(sizeof(convert) - 1)));
convert[place] = 0;
padlen = len - place;
if (padlen < 0)
padlen = 0;
if(ljust)
padlen = -padlen;
if (zpad && padlen > 0) {
if (signvalue) {
(void) dopr_outch(bp, ep, signvalue);
--padlen;
signvalue = 0;
}
while (padlen > 0) {
(void) dopr_outch(bp, ep, zpad);
--padlen;
}
}
while (padlen > 0) {
(void) dopr_outch(bp, ep, ' ');
--padlen;
}
if (signvalue)
(void) dopr_outch(bp, ep, signvalue);
while (place > 0)
(void) dopr_outch(bp, ep, convert[--place]);
while (padlen < 0) {
(void) dopr_outch(bp, ep, ' ');
++padlen;
}
}
/*
* fmtstr() -- format string for output
*/
static void
fmtstr(bp, ep, value, ljust, len, zpad, maxwidth)
char **bp; /* current buffer pointer */
char *ep; /* end of buffer (-1) */
char *value; /* string to format */
int ljust; /* left justification request */
int len; /* length request */
int zpad; /* zero padding request */
int maxwidth; /* maximum width request */
{
int padlen, strlen; /* amount to pad */
if (value == 0)
value = "<NULL>";
for (strlen = 0; value[strlen]; ++ strlen) /* strlen() */
;
if ((strlen > maxwidth) && maxwidth)
strlen = maxwidth;
padlen = len - strlen;
if (padlen < 0)
padlen = 0;
if (ljust)
padlen = -padlen;
while (padlen > 0) {
(void) dopr_outch(bp, ep, ' ');
--padlen;
}
(void) dostr(bp, ep, value, maxwidth);
while (padlen < 0) {
(void) dopr_outch(bp, ep, ' ');
++padlen;
}
}
/*
* dostr() -- do string output
*/
static void
dostr(bp, ep, str, cut)
char **bp; /* current buffer pointer */
char *ep; /* end of buffer (-1) */
char *str; /* string to output */
int cut; /* limit on amount of string to output:
* 0 == no limit */
{
int f;
f = cut ? 1 : 0;
while (*str) {
if (f) {
if (cut-- > 0)
(void) dopr_outch(bp, ep, *str);
} else
(void) dopr_outch(bp, ep, *str);
str++;
}
}
/*
* dopr_outch() -- output a character (or two)
*/
static void
dopr_outch(bp, ep, c)
char **bp; /* current buffer pointer */
char *ep; /* end of buffer (-1) */
int c; /* character to output */
{
register char *cp = *bp;
if (iscntrl(c) && c != '\n' && c != '\t') {
c = '@' + (c & 0x1F);
if (cp < ep)
*cp++ = '^';
Length++;
}
if (cp < ep)
*cp++ = c;
*bp = cp;
Length++;
}
#else /* !defined(USE_LIB_SNPF) */
char snpf_d1[] = "d"; char *snpf_d2 = snpf_d1;
#endif /* defined(USE_LIB_SNPF) */
|