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 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved. 34 * @(#)printf.c 8.1 (Berkeley) 7/20/93 35 * $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/usr.bin/printf/printf.c,v 1.27 2004/03/07 22:22:13 cperciva Exp $ 36 * $DragonFly: src/usr.bin/printf/printf.c,v 1.5 2004/03/19 16:37:24 cpressey Exp $ 37 */ 38 39 #include <sys/types.h> 40 41 #include <err.h> 42 #include <errno.h> 43 #include <limits.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 49 #ifdef SHELL 50 #define main printfcmd 51 #include "bltin/bltin.h" 52 #include "memalloc.h" 53 #else 54 #define warnx1(a, b, c) warnx(a) 55 #define warnx2(a, b, c) warnx(a, b) 56 #define warnx3(a, b, c) warnx(a, b, c) 57 #endif 58 59 #ifndef BUILTIN 60 #include <locale.h> 61 #endif 62 63 #define PF(f, func) do { \ 64 char *b = NULL; \ 65 if (fieldwidth) \ 66 if (precision) \ 67 (void)asprintf(&b, f, fieldwidth, precision, func); \ 68 else \ 69 (void)asprintf(&b, f, fieldwidth, func); \ 70 else if (precision) \ 71 (void)asprintf(&b, f, precision, func); \ 72 else \ 73 (void)asprintf(&b, f, func); \ 74 if (b) { \ 75 (void)fputs(b, stdout); \ 76 free(b); \ 77 } \ 78 } while (0) 79 80 static int asciicode(void); 81 static int escape(char *); 82 static int getchr(void); 83 static int getdouble(double *); 84 static int getint(int *); 85 static int getlonglongs(long long *, unsigned long long *, int); 86 static const char 87 *getstr(void); 88 static char *mkquad(char *, int); 89 static void usage(void); 90 91 static char **gargv; 92 93 int 94 #ifdef BUILTIN 95 progprintf(int argc, char **argv) 96 #else 97 main(int argc, char **argv) 98 #endif 99 { 100 static const char *skip1, *skip2; 101 int ch, chopped, end, fieldwidth, precision, rval; 102 char convch, nextch, *format, *fmt, *start; 103 104 #ifndef BUILTIN 105 (void) setlocale(LC_NUMERIC, ""); 106 #endif 107 while ((ch = getopt(argc, argv, "")) != -1) 108 switch (ch) { 109 case '?': 110 default: 111 usage(); 112 return (1); 113 } 114 argc -= optind; 115 argv += optind; 116 117 if (argc < 1) { 118 usage(); 119 return (1); 120 } 121 122 /* 123 * Basic algorithm is to scan the format string for conversion 124 * specifications -- once one is found, find out if the field 125 * width or precision is a '*'; if it is, gather up value. Note, 126 * format strings are reused as necessary to use up the provided 127 * arguments, arguments of zero/null string are provided to use 128 * up the format string. 129 */ 130 skip1 = "#-+ 0"; 131 skip2 = "0123456789"; 132 133 chopped = escape(fmt = format = *argv); /* backslash interpretation */ 134 rval = 0; 135 gargv = ++argv; 136 for (;;) { 137 end = 0; 138 /* find next format specification */ 139 next: for (start = fmt;; ++fmt) { 140 if (!*fmt) { 141 /* avoid infinite loop */ 142 if (chopped) { 143 (void)printf("%s", start); 144 return (rval); 145 } 146 if (end == 1) { 147 warnx1("missing format character", 148 NULL, NULL); 149 return (1); 150 } 151 end = 1; 152 if (fmt > start) 153 (void)printf("%s", start); 154 if (!*gargv) 155 return (rval); 156 fmt = format; 157 goto next; 158 } 159 /* %% prints a % */ 160 if (*fmt == '%') { 161 if (*++fmt != '%') 162 break; 163 *fmt++ = '\0'; 164 (void)printf("%s", start); 165 goto next; 166 } 167 } 168 169 /* skip to field width */ 170 for (; strchr(skip1, *fmt); ++fmt); 171 if (*fmt == '*') { 172 if (getint(&fieldwidth)) 173 return (1); 174 ++fmt; 175 } else { 176 fieldwidth = 0; 177 178 /* skip to possible '.', get following precision */ 179 for (; strchr(skip2, *fmt); ++fmt); 180 } 181 if (*fmt == '.') { 182 /* precision present? */ 183 ++fmt; 184 if (*fmt == '*') { 185 if (getint(&precision)) 186 return (1); 187 ++fmt; 188 } else { 189 precision = 0; 190 191 /* skip to conversion char */ 192 for (; strchr(skip2, *fmt); ++fmt); 193 } 194 } else 195 precision = 0; 196 if (!*fmt) { 197 warnx1("missing format character", NULL, NULL); 198 return (1); 199 } 200 201 convch = *fmt; 202 nextch = *++fmt; 203 *fmt = '\0'; 204 switch(convch) { 205 case 'b': { 206 char *p; 207 int getout; 208 209 #ifdef SHELL 210 p = savestr(getstr()); 211 #else 212 p = strdup(getstr()); 213 #endif 214 if (p == NULL) { 215 warnx2("%s", strerror(ENOMEM), NULL); 216 return (1); 217 } 218 getout = escape(p); 219 *(fmt - 1) = 's'; 220 PF(start, p); 221 *(fmt - 1) = 'b'; 222 #ifdef SHELL 223 ckfree(p); 224 #else 225 free(p); 226 #endif 227 if (getout) 228 return (rval); 229 break; 230 } 231 case 'c': { 232 char p; 233 234 p = getchr(); 235 PF(start, p); 236 break; 237 } 238 case 's': { 239 const char *p; 240 241 p = getstr(); 242 PF(start, p); 243 break; 244 } 245 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': { 246 char *f; 247 long long val; 248 unsigned long long uval; 249 int signedconv; 250 251 signedconv = (convch == 'd' || convch == 'i'); 252 if ((f = mkquad(start, convch)) == NULL) 253 return (1); 254 if (getlonglongs(&val, &uval, signedconv)) 255 rval = 1; 256 if (signedconv) 257 PF(f, val); 258 else 259 PF(f, uval); 260 break; 261 } 262 case 'e': case 'E': case 'f': case 'g': case 'G': { 263 double p; 264 265 if (getdouble(&p)) 266 rval = 1; 267 PF(start, p); 268 break; 269 } 270 default: 271 warnx2("illegal format character %c", convch, NULL); 272 return (1); 273 } 274 *fmt = nextch; 275 } 276 /* NOTREACHED */ 277 } 278 279 static char * 280 mkquad(char *str, int ch) 281 { 282 static char *copy; 283 static size_t copy_size; 284 char *newcopy; 285 size_t len, newlen; 286 287 len = strlen(str) + 2; 288 if (len > copy_size) { 289 newlen = ((len + 1023) >> 10) << 10; 290 #ifdef SHELL 291 if ((newcopy = ckrealloc(copy, newlen)) == NULL) 292 #else 293 if ((newcopy = realloc(copy, newlen)) == NULL) 294 #endif 295 { 296 warnx2("%s", strerror(ENOMEM), NULL); 297 return (NULL); 298 } 299 copy = newcopy; 300 copy_size = newlen; 301 } 302 303 memmove(copy, str, len - 3); 304 copy[len - 3] = 'q'; 305 copy[len - 2] = ch; 306 copy[len - 1] = '\0'; 307 return (copy); 308 } 309 310 static int 311 escape(register char *fmt) 312 { 313 register char *store; 314 register int value, c; 315 316 for (store = fmt; (c = *fmt); ++fmt, ++store) { 317 if (c != '\\') { 318 *store = c; 319 continue; 320 } 321 switch (*++fmt) { 322 case '\0': /* EOS, user error */ 323 *store = '\\'; 324 *++store = '\0'; 325 return (0); 326 case '\\': /* backslash */ 327 case '\'': /* single quote */ 328 *store = *fmt; 329 break; 330 case 'a': /* bell/alert */ 331 *store = '\7'; 332 break; 333 case 'b': /* backspace */ 334 *store = '\b'; 335 break; 336 case 'c': 337 *store = '\0'; 338 return (1); 339 case 'f': /* form-feed */ 340 *store = '\f'; 341 break; 342 case 'n': /* newline */ 343 *store = '\n'; 344 break; 345 case 'r': /* carriage-return */ 346 *store = '\r'; 347 break; 348 case 't': /* horizontal tab */ 349 *store = '\t'; 350 break; 351 case 'v': /* vertical tab */ 352 *store = '\13'; 353 break; 354 /* octal constant */ 355 case '0': case '1': case '2': case '3': 356 case '4': case '5': case '6': case '7': 357 for (c = *fmt == '0' ? 4 : 3, value = 0; 358 c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) { 359 value <<= 3; 360 value += *fmt - '0'; 361 } 362 --fmt; 363 *store = value; 364 break; 365 default: 366 *store = *fmt; 367 break; 368 } 369 } 370 *store = '\0'; 371 return (0); 372 } 373 374 static int 375 getchr(void) 376 { 377 if (!*gargv) 378 return ('\0'); 379 return ((int)**gargv++); 380 } 381 382 static const char * 383 getstr(void) 384 { 385 if (!*gargv) 386 return (""); 387 return (*gargv++); 388 } 389 390 static int 391 getint(int *ip) 392 { 393 long long val; 394 unsigned long long uval; 395 int rval; 396 397 if (getlonglongs(&val, &uval, 1)) 398 return (1); 399 rval = 0; 400 if (val < INT_MIN || val > INT_MAX) { 401 warnx3("%s: %s", *gargv, strerror(ERANGE)); 402 rval = 1; 403 } 404 *ip = (int)val; 405 return (rval); 406 } 407 408 static int 409 getlonglongs(long long *qp, unsigned long long *uqp, int signedconv) 410 { 411 char *ep; 412 int rval; 413 414 if (!*gargv) { 415 *qp = 0; 416 return (0); 417 } 418 if (**gargv == '"' || **gargv == '\'') { 419 if (signedconv) 420 *qp = asciicode(); 421 else 422 *uqp = asciicode(); 423 return (0); 424 } 425 rval = 0; 426 errno = 0; 427 if (signedconv) 428 *qp = strtoll(*gargv, &ep, 0); 429 else 430 *uqp = strtoull(*gargv, &ep, 0); 431 if (ep == *gargv) { 432 warnx2("%s: expected numeric value", *gargv, NULL); 433 rval = 1; 434 } 435 else if (*ep != '\0') { 436 warnx2("%s: not completely converted", *gargv, NULL); 437 rval = 1; 438 } 439 if (errno == ERANGE) { 440 warnx3("%s: %s", *gargv, strerror(ERANGE)); 441 rval = 1; 442 } 443 ++gargv; 444 return (rval); 445 } 446 447 static int 448 getdouble(double *dp) 449 { 450 char *ep; 451 int rval; 452 453 if (!*gargv) 454 return (0); 455 if (**gargv == '"' || **gargv == '\'') { 456 *dp = asciicode(); 457 return (0); 458 } 459 rval = 0; 460 errno = 0; 461 *dp = strtod(*gargv, &ep); 462 if (ep == *gargv) { 463 warnx2("%s: expected numeric value", *gargv, NULL); 464 rval = 1; 465 } else if (*ep != '\0') { 466 warnx2("%s: not completely converted", *gargv, NULL); 467 rval = 1; 468 } 469 if (errno == ERANGE) { 470 warnx3("%s: %s", *gargv, strerror(ERANGE)); 471 rval = 1; 472 } 473 ++gargv; 474 return (rval); 475 } 476 477 static int 478 asciicode(void) 479 { 480 register int ch; 481 482 ch = **gargv; 483 if (ch == '\'' || ch == '"') 484 ch = (*gargv)[1]; 485 ++gargv; 486 return (ch); 487 } 488 489 static void 490 usage(void) 491 { 492 (void)fprintf(stderr, "usage: printf format [arg ...]\n"); 493 } 494