Print this page
4818 printf(1) should support n$ width and precision specifiers
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/printf/printf.c
+++ new/usr/src/cmd/printf/printf.c
1 1 /*
2 + * Copyright 2014 Garrett D'Amore <garrett@damore.org>
2 3 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
3 4 * Copyright (c) 1989, 1993
4 5 * The Regents of the University of California. All rights reserved.
5 6 *
6 7 * Redistribution and use in source and binary forms, with or without
7 8 * modification, are permitted provided that the following conditions
8 9 * are met:
9 10 * 1. Redistributions of source code must retain the above copyright
10 11 * notice, this list of conditions and the following disclaimer.
11 12 * 2. Redistributions in binary form must reproduce the above copyright
12 13 * notice, this list of conditions and the following disclaimer in the
13 14 * documentation and/or other materials provided with the distribution.
14 15 * 4. Neither the name of the University nor the names of its contributors
15 16 * may be used to endorse or promote products derived from this software
16 17 * without specific prior written permission.
17 18 *
18 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 29 * SUCH DAMAGE.
29 30 */
30 31
↓ open down ↓ |
19 lines elided |
↑ open up ↑ |
31 32 #include <sys/types.h>
32 33
33 34 #include <err.h>
34 35 #include <errno.h>
35 36 #include <inttypes.h>
36 37 #include <limits.h>
37 38 #include <stdio.h>
38 39 #include <stdlib.h>
39 40 #include <string.h>
40 41 #include <unistd.h>
42 +#include <alloca.h>
43 +#include <ctype.h>
41 44 #include <locale.h>
42 45 #include <note.h>
43 46
44 47 #define warnx1(a, b, c) warnx(a)
45 48 #define warnx2(a, b, c) warnx(a, b)
46 49 #define warnx3(a, b, c) warnx(a, b, c)
47 50
48 51 #define PTRDIFF(x, y) ((uintptr_t)(x) - (uintptr_t)(y))
49 52
50 53 #define _(x) gettext(x)
51 54
52 55 #define PF(f, func) do { \
53 56 char *b = NULL; \
54 - int dollar = 0; \
55 - if (*f == '$') { \
56 - dollar++; \
57 - *f = '%'; \
58 - } \
59 57 if (havewidth) \
60 58 if (haveprec) \
61 59 (void) asprintf(&b, f, fieldwidth, precision, func); \
62 60 else \
63 61 (void) asprintf(&b, f, fieldwidth, func); \
64 62 else if (haveprec) \
65 63 (void) asprintf(&b, f, precision, func); \
66 64 else \
67 65 (void) asprintf(&b, f, func); \
68 66 if (b) { \
69 67 (void) fputs(b, stdout); \
70 68 free(b); \
71 69 } \
72 - if (dollar) \
73 - *f = '$'; \
74 70 _NOTE(CONSTCOND) } while (0)
75 71
76 72 static int asciicode(void);
77 73 static char *doformat(char *, int *);
78 74 static int escape(char *, int, size_t *);
79 75 static int getchr(void);
80 76 static int getfloating(long double *, int);
81 77 static int getint(int *);
82 78 static int getnum(intmax_t *, uintmax_t *, int);
83 79 static const char
84 80 *getstr(void);
85 81 static char *mknum(char *, char);
86 82 static void usage(void);
87 83
84 +static const char digits[] = "0123456789";
85 +
88 86 static int myargc;
89 87 static char **myargv;
90 88 static char **gargv;
89 +static char **maxargv;
91 90
92 91 int
93 92 main(int argc, char *argv[])
94 93 {
95 94 size_t len;
96 95 int chopped, end, rval;
97 96 char *format, *fmt, *start;
98 97
99 98 (void) setlocale(LC_ALL, "");
100 99
101 100 argv++;
102 101 argc--;
103 102
104 103 /*
105 104 * POSIX says: Standard utilities that do not accept options,
106 105 * but that do accept operands, shall recognize "--" as a
107 106 * first argument to be discarded.
108 107 */
109 108 if (argc && strcmp(argv[0], "--") == 0) {
110 109 argc--;
111 110 argv++;
112 111 }
113 112
114 113 if (argc < 1) {
115 114 usage();
116 115 return (1);
117 116 }
118 117
119 118 /*
120 119 * Basic algorithm is to scan the format string for conversion
121 120 * specifications -- once one is found, find out if the field
122 121 * width or precision is a '*'; if it is, gather up value. Note,
↓ open down ↓ |
22 lines elided |
↑ open up ↑ |
123 122 * format strings are reused as necessary to use up the provided
124 123 * arguments, arguments of zero/null string are provided to use
125 124 * up the format string.
126 125 */
127 126 fmt = format = *argv;
128 127 chopped = escape(fmt, 1, &len); /* backslash interpretation */
129 128 rval = end = 0;
130 129 gargv = ++argv;
131 130
132 131 for (;;) {
133 - char **maxargv = gargv;
132 + maxargv = gargv;
134 133
135 134 myargv = gargv;
136 135 for (myargc = 0; gargv[myargc]; myargc++)
137 136 /* nop */;
138 137 start = fmt;
139 138 while (fmt < format + len) {
140 139 if (fmt[0] == '%') {
141 140 (void) fwrite(start, 1, PTRDIFF(fmt, start),
142 141 stdout);
143 142 if (fmt[1] == '%') {
144 143 /* %% prints a % */
145 144 (void) putchar('%');
146 145 fmt += 2;
147 146 } else {
148 147 fmt = doformat(fmt, &rval);
149 148 if (fmt == NULL)
150 149 return (1);
151 150 end = 0;
152 151 }
153 152 start = fmt;
154 153 } else
155 154 fmt++;
156 155 if (gargv > maxargv)
157 156 maxargv = gargv;
158 157 }
159 158 gargv = maxargv;
160 159
161 160 if (end == 1) {
162 161 warnx1(_("missing format character"), NULL, NULL);
163 162 return (1);
164 163 }
165 164 (void) fwrite(start, 1, PTRDIFF(fmt, start), stdout);
166 165 if (chopped || !*gargv)
↓ open down ↓ |
23 lines elided |
↑ open up ↑ |
167 166 return (rval);
168 167 /* Restart at the beginning of the format string. */
169 168 fmt = format;
170 169 end = 1;
171 170 }
172 171 /* NOTREACHED */
173 172 }
174 173
175 174
176 175 static char *
177 -doformat(char *start, int *rval)
176 +doformat(char *fmt, int *rval)
178 177 {
179 178 static const char skip1[] = "#'-+ 0";
180 - static const char skip2[] = "0123456789";
181 - char *fmt;
179 + char **save;
182 180 int fieldwidth, haveprec, havewidth, mod_ldbl, precision;
183 181 char convch, nextch;
182 + char *start;
183 + char *dptr;
184 + int l;
184 185
185 - fmt = start + 1;
186 + start = alloca(strlen(fmt) + 1);
186 187
188 + dptr = start;
189 + *dptr++ = '%';
190 + *dptr = 0;
191 +
192 + fmt++;
193 +
187 194 /* look for "n$" field index specifier */
188 - fmt += strspn(fmt, skip2);
189 - if ((*fmt == '$') && (fmt != (start + 1))) {
190 - int idx = atoi(start + 1);
195 + l = strspn(fmt, digits);
196 + if ((l > 0) && (fmt[l] == '$')) {
197 + int idx = atoi(fmt);
191 198 if (idx <= myargc) {
192 199 gargv = &myargv[idx - 1];
193 200 } else {
194 201 gargv = &myargv[myargc];
195 202 }
196 - start = fmt;
197 - fmt++;
198 - } else {
199 - fmt = start + 1;
203 + if (gargv > maxargv) {
204 + maxargv = gargv;
205 + }
206 + fmt += l + 1;
200 207 }
201 208
209 + save = gargv;
210 +
202 211 /* skip to field width */
203 - fmt += strspn(fmt, skip1);
212 + while (strchr(skip1, *fmt) != NULL) {
213 + *dptr++ = *fmt++;
214 + *dptr = 0;
215 + }
216 +
204 217 if (*fmt == '*') {
218 +
219 + fmt++;
220 + l = strspn(fmt, digits);
221 + if ((l > 0) && (fmt[l] == '$')) {
222 + int idx = atoi(fmt);
223 + if (idx <= myargc) {
224 + gargv = &myargv[idx - 1];
225 + } else {
226 + gargv = &myargv[myargc];
227 + }
228 + fmt += l + 1;
229 + }
230 +
205 231 if (getint(&fieldwidth))
206 232 return (NULL);
233 + if (gargv > maxargv) {
234 + maxargv = gargv;
235 + }
207 236 havewidth = 1;
208 - ++fmt;
237 +
238 + *dptr++ = '*';
239 + *dptr = 0;
209 240 } else {
210 241 havewidth = 0;
211 242
212 243 /* skip to possible '.', get following precision */
213 - fmt += strspn(fmt, skip2);
244 + while (isdigit(*fmt)) {
245 + *dptr++ = *fmt++;
246 + *dptr = 0;
247 + }
214 248 }
249 +
215 250 if (*fmt == '.') {
216 251 /* precision present? */
217 - ++fmt;
252 + fmt++;
253 + *dptr++ = '.';
254 +
218 255 if (*fmt == '*') {
256 +
257 + fmt++;
258 + l = strspn(fmt, digits);
259 + if ((l > 0) && (fmt[l] == '$')) {
260 + int idx = atoi(fmt);
261 + if (idx <= myargc) {
262 + gargv = &myargv[idx - 1];
263 + } else {
264 + gargv = &myargv[myargc];
265 + }
266 + fmt += l + 1;
267 + }
268 +
219 269 if (getint(&precision))
220 270 return (NULL);
271 + if (gargv > maxargv) {
272 + maxargv = gargv;
273 + }
221 274 haveprec = 1;
222 - ++fmt;
275 + *dptr++ = '*';
276 + *dptr = 0;
223 277 } else {
224 278 haveprec = 0;
225 279
226 280 /* skip to conversion char */
227 - fmt += strspn(fmt, skip2);
281 + while (isdigit(*fmt)) {
282 + *dptr++ = *fmt++;
283 + *dptr = 0;
284 + }
228 285 }
229 286 } else
230 287 haveprec = 0;
231 288 if (!*fmt) {
232 289 warnx1(_("missing format character"), NULL, NULL);
233 290 return (NULL);
234 291 }
292 + *dptr++ = *fmt;
293 + *dptr = 0;
235 294
236 295 /*
237 296 * Look for a length modifier. POSIX doesn't have these, so
238 297 * we only support them for floating-point conversions, which
239 298 * are extensions. This is useful because the L modifier can
240 299 * be used to gain extra range and precision, while omitting
241 300 * it is more likely to produce consistent results on different
242 301 * architectures. This is not so important for integers
243 302 * because overflow is the only bad thing that can happen to
244 303 * them, but consider the command printf %a 1.1
245 304 */
246 305 if (*fmt == 'L') {
↓ open down ↓ |
2 lines elided |
↑ open up ↑ |
247 306 mod_ldbl = 1;
248 307 fmt++;
249 308 if (!strchr("aAeEfFgG", *fmt)) {
250 309 warnx2(_("bad modifier L for %%%c"), *fmt, NULL);
251 310 return (NULL);
252 311 }
253 312 } else {
254 313 mod_ldbl = 0;
255 314 }
256 315
316 + gargv = save;
257 317 convch = *fmt;
258 318 nextch = *++fmt;
319 +
259 320 *fmt = '\0';
260 321 switch (convch) {
261 322 case 'b': {
262 323 size_t len;
263 324 char *p;
264 325 int getout;
265 326
266 327 p = strdup(getstr());
267 328 if (p == NULL) {
268 329 warnx2("%s", strerror(ENOMEM), NULL);
269 330 return (NULL);
270 331 }
271 332 getout = escape(p, 0, &len);
272 333 *(fmt - 1) = 's';
273 334 PF(start, p);
274 335 *(fmt - 1) = 'b';
275 336 free(p);
276 337
277 338 if (getout)
278 339 return (fmt);
279 340 break;
280 341 }
281 342 case 'c': {
282 343 char p;
283 344
284 345 p = getchr();
285 346 PF(start, p);
286 347 break;
287 348 }
288 349 case 's': {
289 350 const char *p;
290 351
291 352 p = getstr();
292 353 PF(start, p);
293 354 break;
294 355 }
295 356 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
296 357 char *f;
297 358 intmax_t val;
298 359 uintmax_t uval;
299 360 int signedconv;
300 361
301 362 signedconv = (convch == 'd' || convch == 'i');
302 363 if ((f = mknum(start, convch)) == NULL)
303 364 return (NULL);
304 365 if (getnum(&val, &uval, signedconv))
305 366 *rval = 1;
306 367 if (signedconv)
307 368 PF(f, val);
308 369 else
309 370 PF(f, uval);
310 371 break;
311 372 }
312 373 case 'e': case 'E':
313 374 case 'f': case 'F':
314 375 case 'g': case 'G':
315 376 case 'a': case 'A': {
316 377 long double p;
317 378
318 379 if (getfloating(&p, mod_ldbl))
319 380 *rval = 1;
320 381 if (mod_ldbl)
321 382 PF(start, p);
322 383 else
323 384 PF(start, (double)p);
324 385 break;
325 386 }
326 387 default:
327 388 warnx2(_("illegal format character %c"), convch, NULL);
328 389 return (NULL);
329 390 }
330 391 *fmt = nextch;
331 392 return (fmt);
332 393 }
333 394
334 395 static char *
335 396 mknum(char *str, char ch)
336 397 {
337 398 static char *copy;
338 399 static size_t copy_size;
339 400 char *newcopy;
340 401 size_t len, newlen;
341 402
342 403 len = strlen(str) + 2;
343 404 if (len > copy_size) {
344 405 newlen = ((len + 1023) >> 10) << 10;
345 406 if ((newcopy = realloc(copy, newlen)) == NULL) {
346 407 warnx2("%s", strerror(ENOMEM), NULL);
347 408 return (NULL);
348 409 }
349 410 copy = newcopy;
350 411 copy_size = newlen;
351 412 }
352 413
353 414 (void) memmove(copy, str, len - 3);
354 415 copy[len - 3] = 'j';
355 416 copy[len - 2] = ch;
356 417 copy[len - 1] = '\0';
357 418 return (copy);
358 419 }
359 420
360 421 static int
361 422 escape(char *fmt, int percent, size_t *len)
362 423 {
363 424 char *save, *store, c;
364 425 int value;
365 426
366 427 for (save = store = fmt; ((c = *fmt) != 0); ++fmt, ++store) {
367 428 if (c != '\\') {
368 429 *store = c;
369 430 continue;
370 431 }
371 432 switch (*++fmt) {
372 433 case '\0': /* EOS, user error */
373 434 *store = '\\';
374 435 *++store = '\0';
375 436 *len = PTRDIFF(store, save);
376 437 return (0);
377 438 case '\\': /* backslash */
378 439 case '\'': /* single quote */
379 440 *store = *fmt;
380 441 break;
381 442 case 'a': /* bell/alert */
382 443 *store = '\a';
383 444 break;
384 445 case 'b': /* backspace */
385 446 *store = '\b';
386 447 break;
387 448 case 'c':
388 449 *store = '\0';
389 450 *len = PTRDIFF(store, save);
390 451 return (1);
391 452 case 'f': /* form-feed */
392 453 *store = '\f';
393 454 break;
394 455 case 'n': /* newline */
395 456 *store = '\n';
396 457 break;
397 458 case 'r': /* carriage-return */
398 459 *store = '\r';
399 460 break;
400 461 case 't': /* horizontal tab */
401 462 *store = '\t';
402 463 break;
403 464 case 'v': /* vertical tab */
404 465 *store = '\v';
405 466 break;
406 467 /* octal constant */
407 468 case '0': case '1': case '2': case '3':
408 469 case '4': case '5': case '6': case '7':
409 470 c = (!percent && *fmt == '0') ? 4 : 3;
410 471 for (value = 0;
411 472 c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
412 473 value <<= 3;
413 474 value += *fmt - '0';
414 475 }
415 476 --fmt;
416 477 if (percent && value == '%') {
417 478 *store++ = '%';
418 479 *store = '%';
419 480 } else
420 481 *store = (char)value;
421 482 break;
422 483 default:
423 484 *store = *fmt;
424 485 break;
425 486 }
426 487 }
427 488 *store = '\0';
428 489 *len = PTRDIFF(store, save);
429 490 return (0);
430 491 }
431 492
432 493 static int
433 494 getchr(void)
434 495 {
435 496 if (!*gargv)
436 497 return ('\0');
437 498 return ((int)**gargv++);
438 499 }
439 500
440 501 static const char *
441 502 getstr(void)
442 503 {
443 504 if (!*gargv)
444 505 return ("");
445 506 return (*gargv++);
446 507 }
447 508
448 509 static int
449 510 getint(int *ip)
450 511 {
451 512 intmax_t val;
452 513 uintmax_t uval;
453 514 int rval;
454 515
455 516 if (getnum(&val, &uval, 1))
456 517 return (1);
457 518 rval = 0;
458 519 if (val < INT_MIN || val > INT_MAX) {
459 520 warnx3("%s: %s", *gargv, strerror(ERANGE));
460 521 rval = 1;
461 522 }
462 523 *ip = (int)val;
463 524 return (rval);
464 525 }
465 526
466 527 static int
467 528 getnum(intmax_t *ip, uintmax_t *uip, int signedconv)
468 529 {
469 530 char *ep;
470 531 int rval;
471 532
472 533 if (!*gargv) {
473 534 *ip = 0;
474 535 return (0);
475 536 }
476 537 if (**gargv == '"' || **gargv == '\'') {
477 538 if (signedconv)
478 539 *ip = asciicode();
479 540 else
480 541 *uip = asciicode();
481 542 return (0);
482 543 }
483 544 rval = 0;
484 545 errno = 0;
485 546 if (signedconv)
486 547 *ip = strtoimax(*gargv, &ep, 0);
487 548 else
488 549 *uip = strtoumax(*gargv, &ep, 0);
489 550 if (ep == *gargv) {
490 551 warnx2(_("%s: expected numeric value"), *gargv, NULL);
491 552 rval = 1;
492 553 } else if (*ep != '\0') {
493 554 warnx2(_("%s: not completely converted"), *gargv, NULL);
494 555 rval = 1;
495 556 }
496 557 if (errno == ERANGE) {
497 558 warnx3("%s: %s", *gargv, strerror(ERANGE));
498 559 rval = 1;
499 560 }
500 561 ++gargv;
501 562 return (rval);
502 563 }
503 564
504 565 static int
505 566 getfloating(long double *dp, int mod_ldbl)
506 567 {
507 568 char *ep;
508 569 int rval;
509 570
510 571 if (!*gargv) {
511 572 *dp = 0.0;
512 573 return (0);
513 574 }
514 575 if (**gargv == '"' || **gargv == '\'') {
515 576 *dp = asciicode();
516 577 return (0);
517 578 }
518 579 rval = 0;
519 580 errno = 0;
520 581 if (mod_ldbl)
521 582 *dp = strtold(*gargv, &ep);
522 583 else
523 584 *dp = strtod(*gargv, &ep);
524 585 if (ep == *gargv) {
525 586 warnx2(_("%s: expected numeric value"), *gargv, NULL);
526 587 rval = 1;
527 588 } else if (*ep != '\0') {
528 589 warnx2(_("%s: not completely converted"), *gargv, NULL);
529 590 rval = 1;
530 591 }
531 592 if (errno == ERANGE) {
532 593 warnx3("%s: %s", *gargv, strerror(ERANGE));
533 594 rval = 1;
534 595 }
535 596 ++gargv;
536 597 return (rval);
537 598 }
538 599
539 600 static int
540 601 asciicode(void)
541 602 {
542 603 int ch;
543 604
544 605 ch = **gargv;
545 606 if (ch == '\'' || ch == '"')
546 607 ch = (*gargv)[1];
547 608 ++gargv;
548 609 return (ch);
549 610 }
550 611
551 612 static void
552 613 usage(void)
553 614 {
554 615 (void) fprintf(stderr, _("usage: printf format [arguments ...]\n"));
555 616 }
↓ open down ↓ |
287 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX