Loading...
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 | /*- * Copyright (c) 2005 Poul-Henning Kamp * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Chris Torek. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. 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. * 3. 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. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 THE REGENTS 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 DAMAGE. * * $FreeBSD: src/lib/libc/stdio/xprintf_int.c,v 1.2 2005/12/22 14:23:54 cognet Exp $ */ #include <namespace.h> #include <err.h> #include <sys/types.h> #include <stddef.h> #include <stdlib.h> #include <stdio.h> #include <limits.h> #include <locale.h> #include <stdint.h> #include <assert.h> #include <namespace.h> #include <string.h> #include <wchar.h> #include <un-namespace.h> #include "printf.h" #include "xprintf_private.h" /* private stuff -----------------------------------------------------*/ union arg { int intarg; u_int uintarg; long longarg; u_long ulongarg; intmax_t intmaxarg; uintmax_t uintmaxarg; }; /* * Macros for converting digits to letters and vice versa */ #define to_char(n) ((n) + '0') /* various globals ---------------------------------------------------*/ /* * The size of the buffer we use for integer conversions. * Technically, we would need the most space for base 10 * conversions with thousands' grouping characters between * each pair of digits: 39 digits for 128 bit intmax_t plus * 20 grouping characters (which may be multibyte). * Use a bit more for better alignment of stuff. */ #define BUF 128 /* misc --------------------------------------------------------------*/ extern const char *__fix_nogrouping(const char *str); /* * Convert an unsigned long to ASCII for printf purposes, returning * a pointer to the first character of the string representation. * Octal numbers can be forced to have a leading zero; hex numbers * use the given digits. */ static char * __ultoa(u_long val, char *endp, int base, const char *xdigs, int needgrp, const char *thousep, int thousep_len, const char *grp) { char *cp = endp; long sval; int ndig; /* * Handle the three cases separately, in the hope of getting * better/faster code. */ switch (base) { case 10: if (val < 10) { /* many numbers are 1 digit */ *--cp = to_char(val); return (cp); } ndig = 0; /* * On many machines, unsigned arithmetic is harder than * signed arithmetic, so we do at most one unsigned mod and * divide; this is sufficient to reduce the range of * the incoming value to where signed arithmetic works. */ if (val > LONG_MAX) { *--cp = to_char(val % 10); ndig++; sval = val / 10; } else sval = val; do { *--cp = to_char(sval % 10); ndig++; /* * If (*grp == CHAR_MAX) then no more grouping * should be performed. */ if (needgrp && ndig == *grp && *grp != CHAR_MAX && sval > 9) { cp -= thousep_len; memcpy(cp, thousep, thousep_len); ndig = 0; /* * If (*(grp+1) == '\0') then we have to * use *grp character (last grouping rule) * for all next cases */ if (*(grp+1) != '\0') grp++; } sval /= 10; } while (sval != 0); break; case 8: do { *--cp = to_char(val & 7); val >>= 3; } while (val); break; case 16: do { *--cp = xdigs[val & 15]; val >>= 4; } while (val); break; default: /* oops */ assert(base == 16); } return (cp); } /* Identical to __ultoa, but for intmax_t. */ static char * __ujtoa(uintmax_t val, char *endp, int base, const char *xdigs, int needgrp, const char *thousep, int thousep_len, const char *grp) { char *cp = endp; intmax_t sval; int ndig; switch (base) { case 10: if (val < 10) { *--cp = to_char(val % 10); return (cp); } ndig = 0; if (val > INTMAX_MAX) { *--cp = to_char(val % 10); ndig++; sval = val / 10; } else sval = val; do { *--cp = to_char(sval % 10); ndig++; /* * If (*grp == CHAR_MAX) then no more grouping * should be performed. */ if (needgrp && *grp != CHAR_MAX && ndig == *grp && sval > 9) { cp -= thousep_len; memcpy(cp, thousep, thousep_len); ndig = 0; /* * If (*(grp+1) == '\0') then we have to * use *grp character (last grouping rule) * for all next cases */ if (*(grp+1) != '\0') grp++; } sval /= 10; } while (sval != 0); break; case 8: do { *--cp = to_char(val & 7); val >>= 3; } while (val); break; case 16: do { *--cp = xdigs[val & 15]; val >>= 4; } while (val); break; default: abort(); } return (cp); } /* 'd' ---------------------------------------------------------------*/ __private_extern__ int __printf_arginfo_int(const struct printf_info *pi, size_t n, int *argt) { assert (n > 0); argt[0] = PA_INT; #ifdef VECTORS if (pi->is_vec) argt[0] = PA_VECTOR; else #endif /* VECTORS */ if (pi->is_ptrdiff) argt[0] |= PA_FLAG_PTRDIFF; else if (pi->is_size) argt[0] |= PA_FLAG_SIZE; else if (pi->is_long) argt[0] |= PA_FLAG_LONG; else if (pi->is_intmax) argt[0] |= PA_FLAG_INTMAX; else if (pi->is_quad) argt[0] |= PA_FLAG_QUAD; else if (pi->is_long_double) argt[0] |= PA_FLAG_LONG_LONG; else if (pi->is_short) argt[0] |= PA_FLAG_SHORT; else if (pi->is_char) argt[0] = PA_CHAR; return (1); } __private_extern__ int __printf_render_int(struct __printf_io *io, const struct printf_info *pi, const void *const *arg) { const union arg *argp; char buf[BUF]; char *p, *pe; char ns; int rdx, sign, zext, ngrp, l; const char *nalt, *digit; const char *thousands_sep; /* locale specific thousands separator */ int thousands_sep_len; /* locale specific thousands separator length */ const char *grouping; /* locale specific numeric grouping rules */ uintmax_t uu; int ret; #ifdef VECTORS if (pi->is_vec) return __xprintf_vector(io, pi, arg); #endif /* VECTORS */ ret = 0; nalt = NULL; digit = __lowercase_hex; ns = '\0'; pe = buf + sizeof buf - 1; if (pi->group) { thousands_sep = localeconv_l(pi->loc)->thousands_sep; thousands_sep_len = strlen(thousands_sep); grouping = __fix_nogrouping(localeconv_l(pi->loc)->grouping); ngrp = 1; } else { thousands_sep = NULL; thousands_sep_len = 0; grouping = NULL; ngrp = 0; } switch(pi->spec) { case 'd': case 'i': rdx = 10; sign = 1; break; case 'X': digit = __uppercase_hex; /*FALLTHOUGH*/ case 'x': rdx = 16; sign = 0; break; case 'u': case 'U': rdx = 10; sign = 0; break; case 'o': case 'O': rdx = 8; sign = 0; break; default: fprintf(stderr, "pi->spec = '%c'\n", pi->spec); assert(1 == 0); } argp = arg[0]; if (sign) ns = pi->signchar; if (pi->is_long_double || pi->is_quad || pi->is_intmax || pi->is_size || pi->is_ptrdiff) { if (sign && argp->intmaxarg < 0) { uu = -argp->intmaxarg; ns = '-'; } else uu = argp->uintmaxarg; } else if (pi->is_long) { if (sign && argp->longarg < 0) { uu = (u_long)-argp->longarg; ns = '-'; } else uu = argp->ulongarg; } else if (pi->is_short) { if (sign && (short)argp->intarg < 0) { uu = -(short)argp->intarg; ns = '-'; } else uu = (unsigned short)argp->uintarg; } else if (pi->is_char) { if (sign && (signed char)argp->intarg < 0) { uu = -(signed char)argp->intarg; ns = '-'; } else uu = (unsigned char)argp->uintarg; } else { if (sign && argp->intarg < 0) { uu = (unsigned)-argp->intarg; ns = '-'; } else uu = argp->uintarg; } if (uu <= ULONG_MAX) p = __ultoa(uu, pe, rdx, digit, ngrp, thousands_sep, thousands_sep_len, grouping); else p = __ujtoa(uu, pe, rdx, digit, ngrp, thousands_sep, thousands_sep_len, grouping); l = 0; if (uu == 0) { /*- * ``The result of converting a zero value with an * explicit precision of zero is no characters.'' * -- ANSI X3J11 * * ``The C Standard is clear enough as is. The call * printf("%#.0o", 0) should print 0.'' * -- Defect Report #151 */ ; if (pi->prec == 0 && !(pi->alt && rdx == 8)) p = pe; } else if (pi->alt) { if (rdx == 8) *--p = '0'; if (rdx == 16) { if (pi->spec == 'x') nalt = "0x"; else nalt = "0X"; l += 2; } } l += pe - p; if (ns) l++; /*- * ``... diouXx conversions ... if a precision is * specified, the 0 flag will be ignored.'' * -- ANSI X3J11 */ if (pi->prec > (pe - p)) zext = pi->prec - (pe - p); else if (pi->prec != -1) zext = 0; else if (pi->pad == '0' && pi->width > l && !pi->left) zext = pi->width - l; else zext = 0; l += zext; while (zext > 0 && p > buf) { *--p = '0'; zext--; } if (l < BUF) { if (ns) { *--p = ns; } else if (nalt != NULL) { *--p = nalt[1]; *--p = nalt[0]; } if (pi->width > (pe - p) && !pi->left) { l = pi->width - (pe - p); while (l > 0 && p > buf) { *--p = ' '; l--; } if (l) ret += __printf_pad(io, l, 0); } } else { if (!pi->left && pi->width > l) ret += __printf_pad(io, pi->width - l, 0); if (ns != '\0') ret += __printf_puts(io, &ns, 1); else if (nalt != NULL) ret += __printf_puts(io, nalt, 2); if (zext > 0) ret += __printf_pad(io, zext, 1); } ret += __printf_puts(io, p, pe - p); if (pi->width > ret && pi->left) ret += __printf_pad(io, pi->width - ret, 0); __printf_flush(io); return (ret); } /* 'p' ---------------------------------------------------------------*/ __private_extern__ int __printf_arginfo_ptr(const struct printf_info *pi __unused, size_t n, int *argt) { assert (n > 0); #ifdef VECTORS if (pi->is_vec) argt[0] = PA_VECTOR; else #endif /* VECTORS */ argt[0] = PA_POINTER; return (1); } __private_extern__ int __printf_render_ptr(struct __printf_io *io, const struct printf_info *pi, const void *const *arg) { struct printf_info p2; uintmax_t u; const void *p; #ifdef VECTORS if (pi->is_vec) return __xprintf_vector(io, pi, arg); #endif /* VECTORS */ /*- * ``The argument shall be a pointer to void. The * value of the pointer is converted to a sequence * of printable characters, in an implementation- * defined manner.'' * -- ANSI X3J11 */ u = (uintmax_t)(uintptr_t) *((void **)arg[0]); p2 = *pi; p2.spec = 'x'; p2.alt = 1; p2.is_long_double = 1; p = &u; return (__printf_render_int(io, &p2, &p)); } |