printf.c (113159) | printf.c (156518) |
---|---|
1/*- 2 * Copyright (c) 1986, 1988, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. --- 25 unchanged lines hidden (view full) --- 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94 39 */ 40 41#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 1986, 1988, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. --- 25 unchanged lines hidden (view full) --- 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94 39 */ 40 41#include <sys/cdefs.h> |
42__FBSDID("$FreeBSD: head/lib/libstand/printf.c 113159 2003-04-06 05:25:48Z peter $"); | 42__FBSDID("$FreeBSD: head/lib/libstand/printf.c 156518 2006-03-09 22:37:34Z jkim $"); |
43 44/* 45 * Standaloneified version of the FreeBSD kernel printf family. 46 */ 47 48#include <sys/types.h> 49#include <sys/stddef.h> 50#include <sys/stdint.h> --- 4 unchanged lines hidden (view full) --- 55/* 56 * Note that stdarg.h and the ANSI style va_start macro is used for both 57 * ANSI and traditional C compilers. 58 */ 59#include <machine/stdarg.h> 60 61#define MAXNBUF (sizeof(intmax_t) * CHAR_BIT + 1) 62 | 43 44/* 45 * Standaloneified version of the FreeBSD kernel printf family. 46 */ 47 48#include <sys/types.h> 49#include <sys/stddef.h> 50#include <sys/stdint.h> --- 4 unchanged lines hidden (view full) --- 55/* 56 * Note that stdarg.h and the ANSI style va_start macro is used for both 57 * ANSI and traditional C compilers. 58 */ 59#include <machine/stdarg.h> 60 61#define MAXNBUF (sizeof(intmax_t) * CHAR_BIT + 1) 62 |
63static char *ksprintn (char *buf, uintmax_t num, int base, int *len); | 63static char *ksprintn (char *buf, uintmax_t num, int base, int *len, int upper); |
64static int kvprintf(char const *fmt, void (*func)(int), void *arg, int radix, va_list ap); 65 66int 67printf(const char *fmt, ...) 68{ 69 va_list ap; 70 int retval; 71 --- 34 unchanged lines hidden (view full) --- 106 107/* 108 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse 109 * order; return an optional length and a pointer to the last character 110 * written in the buffer (i.e., the first character of the string). 111 * The buffer pointed to by `nbuf' must have length >= MAXNBUF. 112 */ 113static char * | 64static int kvprintf(char const *fmt, void (*func)(int), void *arg, int radix, va_list ap); 65 66int 67printf(const char *fmt, ...) 68{ 69 va_list ap; 70 int retval; 71 --- 34 unchanged lines hidden (view full) --- 106 107/* 108 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse 109 * order; return an optional length and a pointer to the last character 110 * written in the buffer (i.e., the first character of the string). 111 * The buffer pointed to by `nbuf' must have length >= MAXNBUF. 112 */ 113static char * |
114ksprintn(char *nbuf, uintmax_t num, int base, int *lenp) | 114ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper) |
115{ | 115{ |
116 char *p; | 116 char *p, c; |
117 118 p = nbuf; 119 *p = '\0'; 120 do { | 117 118 p = nbuf; 119 *p = '\0'; 120 do { |
121 *++p = hex2ascii(num % base); | 121 c = hex2ascii(num % base); 122 *++p = upper ? toupper(c) : c; |
122 } while (num /= base); 123 if (lenp) 124 *lenp = p - nbuf; 125 return (p); 126} 127 128/* 129 * Scaled down version of printf(3). --- 28 unchanged lines hidden (view full) --- 158 char nbuf[MAXNBUF]; 159 char *d; 160 const char *p, *percent, *q; 161 u_char *up; 162 int ch, n; 163 uintmax_t num; 164 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot; 165 int jflag, tflag, zflag; | 123 } while (num /= base); 124 if (lenp) 125 *lenp = p - nbuf; 126 return (p); 127} 128 129/* 130 * Scaled down version of printf(3). --- 28 unchanged lines hidden (view full) --- 159 char nbuf[MAXNBUF]; 160 char *d; 161 const char *p, *percent, *q; 162 u_char *up; 163 int ch, n; 164 uintmax_t num; 165 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot; 166 int jflag, tflag, zflag; |
166 int dwidth; | 167 int dwidth, upper; |
167 char padc; 168 int retval = 0; 169 170 num = 0; 171 if (!func) 172 d = (char *) arg; 173 else 174 d = NULL; --- 9 unchanged lines hidden (view full) --- 184 width = 0; 185 while ((ch = (u_char)*fmt++) != '%') { 186 if (ch == '\0') 187 return (retval); 188 PCHAR(ch); 189 } 190 percent = fmt - 1; 191 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0; | 168 char padc; 169 int retval = 0; 170 171 num = 0; 172 if (!func) 173 d = (char *) arg; 174 else 175 d = NULL; --- 9 unchanged lines hidden (view full) --- 185 width = 0; 186 while ((ch = (u_char)*fmt++) != '%') { 187 if (ch == '\0') 188 return (retval); 189 PCHAR(ch); 190 } 191 percent = fmt - 1; 192 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0; |
192 sign = 0; dot = 0; dwidth = 0; | 193 sign = 0; dot = 0; dwidth = 0; upper = 0; |
193 jflag = 0; tflag = 0; zflag = 0; 194reswitch: switch (ch = (u_char)*fmt++) { 195 case '.': 196 dot = 1; 197 goto reswitch; 198 case '#': 199 sharpflag = 1; 200 goto reswitch; --- 33 unchanged lines hidden (view full) --- 234 if (dot) 235 dwidth = n; 236 else 237 width = n; 238 goto reswitch; 239 case 'b': 240 num = va_arg(ap, int); 241 p = va_arg(ap, char *); | 194 jflag = 0; tflag = 0; zflag = 0; 195reswitch: switch (ch = (u_char)*fmt++) { 196 case '.': 197 dot = 1; 198 goto reswitch; 199 case '#': 200 sharpflag = 1; 201 goto reswitch; --- 33 unchanged lines hidden (view full) --- 235 if (dot) 236 dwidth = n; 237 else 238 width = n; 239 goto reswitch; 240 case 'b': 241 num = va_arg(ap, int); 242 p = va_arg(ap, char *); |
242 for (q = ksprintn(nbuf, num, *p++, NULL); *q;) | 243 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;) |
243 PCHAR(*q--); 244 245 if (num == 0) 246 break; 247 248 for (tmp = 0; *p;) { 249 n = *p++; 250 if (num & (1 << (n - 1))) { --- 91 unchanged lines hidden (view full) --- 342 PCHAR(padc); 343 break; 344 case 't': 345 tflag = 1; 346 goto reswitch; 347 case 'u': 348 base = 10; 349 goto handle_nosign; | 244 PCHAR(*q--); 245 246 if (num == 0) 247 break; 248 249 for (tmp = 0; *p;) { 250 n = *p++; 251 if (num & (1 << (n - 1))) { --- 91 unchanged lines hidden (view full) --- 343 PCHAR(padc); 344 break; 345 case 't': 346 tflag = 1; 347 goto reswitch; 348 case 'u': 349 base = 10; 350 goto handle_nosign; |
350 case 'x': | |
351 case 'X': | 351 case 'X': |
352 upper = 1; 353 case 'x': |
|
352 base = 16; 353 goto handle_nosign; 354 case 'y': 355 base = 16; 356 sign = 1; 357 goto handle_sign; 358 case 'z': 359 zflag = 1; --- 26 unchanged lines hidden (view full) --- 386 num = va_arg(ap, size_t); 387 else 388 num = va_arg(ap, int); 389number: 390 if (sign && (intmax_t)num < 0) { 391 neg = 1; 392 num = -(intmax_t)num; 393 } | 354 base = 16; 355 goto handle_nosign; 356 case 'y': 357 base = 16; 358 sign = 1; 359 goto handle_sign; 360 case 'z': 361 zflag = 1; --- 26 unchanged lines hidden (view full) --- 388 num = va_arg(ap, size_t); 389 else 390 num = va_arg(ap, int); 391number: 392 if (sign && (intmax_t)num < 0) { 393 neg = 1; 394 num = -(intmax_t)num; 395 } |
394 p = ksprintn(nbuf, num, base, &tmp); | 396 p = ksprintn(nbuf, num, base, &tmp, upper); |
395 if (sharpflag && num != 0) { 396 if (base == 8) 397 tmp++; 398 else if (base == 16) 399 tmp += 2; 400 } 401 if (neg) 402 tmp++; --- 31 unchanged lines hidden --- | 397 if (sharpflag && num != 0) { 398 if (base == 8) 399 tmp++; 400 else if (base == 16) 401 tmp += 2; 402 } 403 if (neg) 404 tmp++; --- 31 unchanged lines hidden --- |