Deleted Added
full compact
printf.c (185037) printf.c (209837)
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.

--- 21 unchanged lines hidden (view full) ---

30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
35 */
36
37#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.

--- 21 unchanged lines hidden (view full) ---

30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/lib/libstand/printf.c 185037 2008-11-18 00:01:16Z delphij $");
38__FBSDID("$FreeBSD: head/lib/libstand/printf.c 209837 2010-07-08 22:21:18Z jkim $");
39
40/*
41 * Standaloneified version of the FreeBSD kernel printf family.
42 */
43
44#include <sys/types.h>
45#include <sys/stddef.h>
46#include <sys/stdint.h>

--- 107 unchanged lines hidden (view full) ---

154#define PCHAR(c) {int cc=(c); if (func) (*func)(cc); else *d++ = cc; retval++; }
155 char nbuf[MAXNBUF];
156 char *d;
157 const char *p, *percent, *q;
158 u_char *up;
159 int ch, n;
160 uintmax_t num;
161 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
39
40/*
41 * Standaloneified version of the FreeBSD kernel printf family.
42 */
43
44#include <sys/types.h>
45#include <sys/stddef.h>
46#include <sys/stdint.h>

--- 107 unchanged lines hidden (view full) ---

154#define PCHAR(c) {int cc=(c); if (func) (*func)(cc); else *d++ = cc; retval++; }
155 char nbuf[MAXNBUF];
156 char *d;
157 const char *p, *percent, *q;
158 u_char *up;
159 int ch, n;
160 uintmax_t num;
161 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
162 int jflag, tflag, zflag;
162 int cflag, hflag, jflag, tflag, zflag;
163 int dwidth, upper;
164 char padc;
163 int dwidth, upper;
164 char padc;
165 int retval = 0;
165 int stop = 0, retval = 0;
166
167 num = 0;
168 if (!func)
169 d = (char *) arg;
170 else
171 d = NULL;
172
173 if (fmt == NULL)
174 fmt = "(fmt null)\n";
175
176 if (radix < 2 || radix > 36)
177 radix = 10;
178
179 for (;;) {
180 padc = ' ';
181 width = 0;
166
167 num = 0;
168 if (!func)
169 d = (char *) arg;
170 else
171 d = NULL;
172
173 if (fmt == NULL)
174 fmt = "(fmt null)\n";
175
176 if (radix < 2 || radix > 36)
177 radix = 10;
178
179 for (;;) {
180 padc = ' ';
181 width = 0;
182 while ((ch = (u_char)*fmt++) != '%') {
182 while ((ch = (u_char)*fmt++) != '%' || stop) {
183 if (ch == '\0')
184 return (retval);
185 PCHAR(ch);
186 }
187 percent = fmt - 1;
188 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
189 sign = 0; dot = 0; dwidth = 0; upper = 0;
183 if (ch == '\0')
184 return (retval);
185 PCHAR(ch);
186 }
187 percent = fmt - 1;
188 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
189 sign = 0; dot = 0; dwidth = 0; upper = 0;
190 jflag = 0; tflag = 0; zflag = 0;
190 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
191reswitch: switch (ch = (u_char)*fmt++) {
192 case '.':
193 dot = 1;
194 goto reswitch;
195 case '#':
196 sharpflag = 1;
197 goto reswitch;
198 case '+':

--- 30 unchanged lines hidden (view full) ---

229 break;
230 }
231 if (dot)
232 dwidth = n;
233 else
234 width = n;
235 goto reswitch;
236 case 'b':
191reswitch: switch (ch = (u_char)*fmt++) {
192 case '.':
193 dot = 1;
194 goto reswitch;
195 case '#':
196 sharpflag = 1;
197 goto reswitch;
198 case '+':

--- 30 unchanged lines hidden (view full) ---

229 break;
230 }
231 if (dot)
232 dwidth = n;
233 else
234 width = n;
235 goto reswitch;
236 case 'b':
237 num = va_arg(ap, int);
237 num = (u_int)va_arg(ap, int);
238 p = va_arg(ap, char *);
239 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
240 PCHAR(*q--);
241
242 if (num == 0)
243 break;
244
245 for (tmp = 0; *p;) {

--- 27 unchanged lines hidden (view full) ---

273 PCHAR(*q);
274 }
275 break;
276 case 'd':
277 case 'i':
278 base = 10;
279 sign = 1;
280 goto handle_sign;
238 p = va_arg(ap, char *);
239 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
240 PCHAR(*q--);
241
242 if (num == 0)
243 break;
244
245 for (tmp = 0; *p;) {

--- 27 unchanged lines hidden (view full) ---

273 PCHAR(*q);
274 }
275 break;
276 case 'd':
277 case 'i':
278 base = 10;
279 sign = 1;
280 goto handle_sign;
281 case 'h':
282 if (hflag) {
283 hflag = 0;
284 cflag = 1;
285 } else
286 hflag = 1;
287 goto reswitch;
281 case 'j':
282 jflag = 1;
283 goto reswitch;
284 case 'l':
285 if (lflag) {
286 lflag = 0;
287 qflag = 1;
288 } else
289 lflag = 1;
290 goto reswitch;
291 case 'n':
292 if (jflag)
293 *(va_arg(ap, intmax_t *)) = retval;
294 else if (qflag)
295 *(va_arg(ap, quad_t *)) = retval;
296 else if (lflag)
297 *(va_arg(ap, long *)) = retval;
298 else if (zflag)
299 *(va_arg(ap, size_t *)) = retval;
288 case 'j':
289 jflag = 1;
290 goto reswitch;
291 case 'l':
292 if (lflag) {
293 lflag = 0;
294 qflag = 1;
295 } else
296 lflag = 1;
297 goto reswitch;
298 case 'n':
299 if (jflag)
300 *(va_arg(ap, intmax_t *)) = retval;
301 else if (qflag)
302 *(va_arg(ap, quad_t *)) = retval;
303 else if (lflag)
304 *(va_arg(ap, long *)) = retval;
305 else if (zflag)
306 *(va_arg(ap, size_t *)) = retval;
307 else if (hflag)
308 *(va_arg(ap, short *)) = retval;
309 else if (cflag)
310 *(va_arg(ap, char *)) = retval;
300 else
301 *(va_arg(ap, int *)) = retval;
302 break;
303 case 'o':
304 base = 8;
305 goto handle_nosign;
306 case 'p':
307 base = 16;

--- 55 unchanged lines hidden (view full) ---

363 else if (qflag)
364 num = va_arg(ap, u_quad_t);
365 else if (tflag)
366 num = va_arg(ap, ptrdiff_t);
367 else if (lflag)
368 num = va_arg(ap, u_long);
369 else if (zflag)
370 num = va_arg(ap, size_t);
311 else
312 *(va_arg(ap, int *)) = retval;
313 break;
314 case 'o':
315 base = 8;
316 goto handle_nosign;
317 case 'p':
318 base = 16;

--- 55 unchanged lines hidden (view full) ---

374 else if (qflag)
375 num = va_arg(ap, u_quad_t);
376 else if (tflag)
377 num = va_arg(ap, ptrdiff_t);
378 else if (lflag)
379 num = va_arg(ap, u_long);
380 else if (zflag)
381 num = va_arg(ap, size_t);
382 else if (hflag)
383 num = (u_short)va_arg(ap, int);
384 else if (cflag)
385 num = (u_char)va_arg(ap, int);
371 else
372 num = va_arg(ap, u_int);
373 goto number;
374handle_sign:
375 if (jflag)
376 num = va_arg(ap, intmax_t);
377 else if (qflag)
378 num = va_arg(ap, quad_t);
379 else if (tflag)
380 num = va_arg(ap, ptrdiff_t);
381 else if (lflag)
382 num = va_arg(ap, long);
383 else if (zflag)
384 num = va_arg(ap, ssize_t);
386 else
387 num = va_arg(ap, u_int);
388 goto number;
389handle_sign:
390 if (jflag)
391 num = va_arg(ap, intmax_t);
392 else if (qflag)
393 num = va_arg(ap, quad_t);
394 else if (tflag)
395 num = va_arg(ap, ptrdiff_t);
396 else if (lflag)
397 num = va_arg(ap, long);
398 else if (zflag)
399 num = va_arg(ap, ssize_t);
400 else if (hflag)
401 num = (short)va_arg(ap, int);
402 else if (cflag)
403 num = (char)va_arg(ap, int);
385 else
386 num = va_arg(ap, int);
387number:
388 if (sign && (intmax_t)num < 0) {
389 neg = 1;
390 num = -(intmax_t)num;
391 }
404 else
405 num = va_arg(ap, int);
406number:
407 if (sign && (intmax_t)num < 0) {
408 neg = 1;
409 num = -(intmax_t)num;
410 }
392 p = ksprintn(nbuf, num, base, &tmp, upper);
411 p = ksprintn(nbuf, num, base, &n, upper);
412 tmp = 0;
393 if (sharpflag && num != 0) {
394 if (base == 8)
395 tmp++;
396 else if (base == 16)
397 tmp += 2;
398 }
399 if (neg)
400 tmp++;
401
413 if (sharpflag && num != 0) {
414 if (base == 8)
415 tmp++;
416 else if (base == 16)
417 tmp += 2;
418 }
419 if (neg)
420 tmp++;
421
402 if (!ladjust && width && (width -= tmp) > 0)
403 while (width--)
404 PCHAR(padc);
422 if (!ladjust && padc == '0')
423 dwidth = width - tmp;
424 width -= tmp + MAX(dwidth, n);
425 dwidth -= n;
426 if (!ladjust)
427 while (width-- > 0)
428 PCHAR(' ');
405 if (neg)
406 PCHAR('-');
407 if (sharpflag && num != 0) {
408 if (base == 8) {
409 PCHAR('0');
410 } else if (base == 16) {
411 PCHAR('0');
412 PCHAR('x');
413 }
414 }
429 if (neg)
430 PCHAR('-');
431 if (sharpflag && num != 0) {
432 if (base == 8) {
433 PCHAR('0');
434 } else if (base == 16) {
435 PCHAR('0');
436 PCHAR('x');
437 }
438 }
439 while (dwidth-- > 0)
440 PCHAR('0');
415
416 while (*p)
417 PCHAR(*p--);
418
441
442 while (*p)
443 PCHAR(*p--);
444
419 if (ladjust && width && (width -= tmp) > 0)
420 while (width--)
421 PCHAR(padc);
445 if (ladjust)
446 while (width-- > 0)
447 PCHAR(' ');
422
423 break;
424 default:
425 while (percent < fmt)
426 PCHAR(*percent++);
448
449 break;
450 default:
451 while (percent < fmt)
452 PCHAR(*percent++);
453 /*
454 * Since we ignore an formatting argument it is no
455 * longer safe to obey the remaining formatting
456 * arguments as the arguments will no longer match
457 * the format specs.
458 */
459 stop = 1;
427 break;
428 }
429 }
430#undef PCHAR
431}
460 break;
461 }
462 }
463#undef PCHAR
464}