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