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