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