1c9aca921SXin LI /*- 29b50d902SRodney W. Grimes * Copyright (c) 1989, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 149b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 159b50d902SRodney W. Grimes * without specific prior written permission. 169b50d902SRodney W. Grimes * 179b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 189b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 199b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 209b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 219b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 229b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 239b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 249b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 259b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 269b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 279b50d902SRodney W. Grimes * SUCH DAMAGE. 289b50d902SRodney W. Grimes */ 29*77fae5c1SJilles Tjoelker /* 30*77fae5c1SJilles Tjoelker * Important: This file is used both as a standalone program /usr/bin/printf 31*77fae5c1SJilles Tjoelker * and as a builtin for /bin/sh (#define SHELL). 32*77fae5c1SJilles Tjoelker */ 339b50d902SRodney W. Grimes 341866e8abSJilles Tjoelker #ifndef SHELL 359b50d902SRodney W. Grimes #ifndef lint 363ec30b79SSteve Price static char const copyright[] = 379b50d902SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\ 389b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 399b50d902SRodney W. Grimes #endif /* not lint */ 409b50d902SRodney W. Grimes #endif 419b50d902SRodney W. Grimes 429b50d902SRodney W. Grimes #ifndef lint 43c9e05349SWarner Losh #if 0 443ec30b79SSteve Price static char const sccsid[] = "@(#)printf.c 8.1 (Berkeley) 7/20/93"; 45c9e05349SWarner Losh #endif 461ea7321bSMartin Cracauer static const char rcsid[] = 471ea7321bSMartin Cracauer "$FreeBSD$"; 489b50d902SRodney W. Grimes #endif /* not lint */ 499b50d902SRodney W. Grimes 509b50d902SRodney W. Grimes #include <sys/types.h> 519b50d902SRodney W. Grimes 529b50d902SRodney W. Grimes #include <err.h> 539b50d902SRodney W. Grimes #include <errno.h> 54d41d23e1SStefan Farfeleder #include <inttypes.h> 559b50d902SRodney W. Grimes #include <limits.h> 56c9aca921SXin LI #include <locale.h> 579b50d902SRodney W. Grimes #include <stdio.h> 589b50d902SRodney W. Grimes #include <stdlib.h> 599b50d902SRodney W. Grimes #include <string.h> 609d19feb5SSteve Price #include <unistd.h> 619b50d902SRodney W. Grimes 629b50d902SRodney W. Grimes #ifdef SHELL 639b50d902SRodney W. Grimes #define main printfcmd 64d9f93710SJoerg Wunsch #include "bltin/bltin.h" 659897c45fSJilles Tjoelker #include "error.h" 669b50d902SRodney W. Grimes #endif 679b50d902SRodney W. Grimes 68bacab7d6STim J. Robbins #define PF(f, func) do { \ 69d72f654cSPeter Wemm char *b = NULL; \ 7098dd6386STim J. Robbins if (havewidth) \ 7198dd6386STim J. Robbins if (haveprec) \ 72d72f654cSPeter Wemm (void)asprintf(&b, f, fieldwidth, precision, func); \ 739b50d902SRodney W. Grimes else \ 74d72f654cSPeter Wemm (void)asprintf(&b, f, fieldwidth, func); \ 7598dd6386STim J. Robbins else if (haveprec) \ 76d72f654cSPeter Wemm (void)asprintf(&b, f, precision, func); \ 779b50d902SRodney W. Grimes else \ 78d72f654cSPeter Wemm (void)asprintf(&b, f, func); \ 79d72f654cSPeter Wemm if (b) { \ 80d72f654cSPeter Wemm (void)fputs(b, stdout); \ 81d72f654cSPeter Wemm free(b); \ 82d72f654cSPeter Wemm } \ 83bacab7d6STim J. Robbins } while (0) 849b50d902SRodney W. Grimes 85d3cb5dedSWarner Losh static int asciicode(void); 869897c45fSJilles Tjoelker static char *printf_doformat(char *, int *); 873ec96cafSStefan Farfeleder static int escape(char *, int, size_t *); 88d3cb5dedSWarner Losh static int getchr(void); 89fd757c50SDavid Schultz static int getfloating(long double *, int); 90d3cb5dedSWarner Losh static int getint(int *); 91d41d23e1SStefan Farfeleder static int getnum(intmax_t *, uintmax_t *, int); 92bacab7d6STim J. Robbins static const char 93bacab7d6STim J. Robbins *getstr(void); 949180853bSXin LI static char *mknum(char *, char); 95d3cb5dedSWarner Losh static void usage(void); 969b50d902SRodney W. Grimes 979b50d902SRodney W. Grimes static char **gargv; 989b50d902SRodney W. Grimes 999b50d902SRodney W. Grimes int 100f4ac32deSDavid Malone main(int argc, char *argv[]) 1019b50d902SRodney W. Grimes { 1023ec96cafSStefan Farfeleder size_t len; 103de46de4dSXin LI int ch, chopped, end, rval; 104fbd08684SStefan Farfeleder char *format, *fmt, *start; 1059b50d902SRodney W. Grimes 1061866e8abSJilles Tjoelker #ifndef SHELL 10769be0c5eSXin LI (void) setlocale(LC_ALL, ""); 108dc7d8c99SAndrey A. Chernov #endif 1099897c45fSJilles Tjoelker #ifdef SHELL 1109897c45fSJilles Tjoelker optreset = 1; optind = 1; opterr = 0; /* initialize getopt */ 1119897c45fSJilles Tjoelker #endif 112de46de4dSXin LI while ((ch = getopt(argc, argv, "")) != -1) 113de46de4dSXin LI switch (ch) { 114de46de4dSXin LI case '?': 115de46de4dSXin LI default: 116de46de4dSXin LI usage(); 117de46de4dSXin LI return (1); 1189b50d902SRodney W. Grimes } 119de46de4dSXin LI argc -= optind; 120de46de4dSXin LI argv += optind; 1219b50d902SRodney W. Grimes 1229b50d902SRodney W. Grimes if (argc < 1) { 1239b50d902SRodney W. Grimes usage(); 1245eccc000SXin LI return (1); 1259b50d902SRodney W. Grimes } 1269b50d902SRodney W. Grimes 1279897c45fSJilles Tjoelker #ifdef SHELL 1289897c45fSJilles Tjoelker INTOFF; 1299897c45fSJilles Tjoelker #endif 1309b50d902SRodney W. Grimes /* 1319b50d902SRodney W. Grimes * Basic algorithm is to scan the format string for conversion 1329b50d902SRodney W. Grimes * specifications -- once one is found, find out if the field 1339b50d902SRodney W. Grimes * width or precision is a '*'; if it is, gather up value. Note, 1349b50d902SRodney W. Grimes * format strings are reused as necessary to use up the provided 1359b50d902SRodney W. Grimes * arguments, arguments of zero/null string are provided to use 1369b50d902SRodney W. Grimes * up the format string. 1379b50d902SRodney W. Grimes */ 1383ec96cafSStefan Farfeleder fmt = format = *argv; 1393ec96cafSStefan Farfeleder chopped = escape(fmt, 1, &len); /* backslash interpretation */ 140fbd08684SStefan Farfeleder rval = end = 0; 1419b50d902SRodney W. Grimes gargv = ++argv; 1429b50d902SRodney W. Grimes for (;;) { 143fbd08684SStefan Farfeleder start = fmt; 1443ec96cafSStefan Farfeleder while (fmt < format + len) { 145fbd08684SStefan Farfeleder if (fmt[0] == '%') { 146fbd08684SStefan Farfeleder fwrite(start, 1, fmt - start, stdout); 147fbd08684SStefan Farfeleder if (fmt[1] == '%') { 1489b50d902SRodney W. Grimes /* %% prints a % */ 149fbd08684SStefan Farfeleder putchar('%'); 150fbd08684SStefan Farfeleder fmt += 2; 151fbd08684SStefan Farfeleder } else { 1529897c45fSJilles Tjoelker fmt = printf_doformat(fmt, &rval); 1539897c45fSJilles Tjoelker if (fmt == NULL) { 1549897c45fSJilles Tjoelker #ifdef SHELL 1559897c45fSJilles Tjoelker INTON; 1569897c45fSJilles Tjoelker #endif 157fbd08684SStefan Farfeleder return (1); 1589897c45fSJilles Tjoelker } 159fbd08684SStefan Farfeleder end = 0; 1609b50d902SRodney W. Grimes } 161fbd08684SStefan Farfeleder start = fmt; 162fbd08684SStefan Farfeleder } else 163fbd08684SStefan Farfeleder fmt++; 1649b50d902SRodney W. Grimes } 1659b50d902SRodney W. Grimes 166fbd08684SStefan Farfeleder if (end == 1) { 1676a6760dbSJilles Tjoelker warnx("missing format character"); 1689897c45fSJilles Tjoelker #ifdef SHELL 1699897c45fSJilles Tjoelker INTON; 1709897c45fSJilles Tjoelker #endif 171fbd08684SStefan Farfeleder return (1); 172fbd08684SStefan Farfeleder } 173fbd08684SStefan Farfeleder fwrite(start, 1, fmt - start, stdout); 1749897c45fSJilles Tjoelker if (chopped || !*gargv) { 1759897c45fSJilles Tjoelker #ifdef SHELL 1769897c45fSJilles Tjoelker INTON; 1779897c45fSJilles Tjoelker #endif 178fbd08684SStefan Farfeleder return (rval); 1799897c45fSJilles Tjoelker } 180fbd08684SStefan Farfeleder /* Restart at the beginning of the format string. */ 181fbd08684SStefan Farfeleder fmt = format; 182fbd08684SStefan Farfeleder end = 1; 183fbd08684SStefan Farfeleder } 184fbd08684SStefan Farfeleder /* NOTREACHED */ 185fbd08684SStefan Farfeleder } 186fbd08684SStefan Farfeleder 187fbd08684SStefan Farfeleder 188fbd08684SStefan Farfeleder static char * 1899897c45fSJilles Tjoelker printf_doformat(char *start, int *rval) 190fbd08684SStefan Farfeleder { 191fbd08684SStefan Farfeleder static const char skip1[] = "#'-+ 0"; 192fbd08684SStefan Farfeleder static const char skip2[] = "0123456789"; 193fbd08684SStefan Farfeleder char *fmt; 194fbd08684SStefan Farfeleder int fieldwidth, haveprec, havewidth, mod_ldbl, precision; 195fbd08684SStefan Farfeleder char convch, nextch; 196fbd08684SStefan Farfeleder 197fbd08684SStefan Farfeleder fmt = start + 1; 1989b50d902SRodney W. Grimes /* skip to field width */ 1990ba01198SStefan Farfeleder fmt += strspn(fmt, skip1); 2009b50d902SRodney W. Grimes if (*fmt == '*') { 2019b50d902SRodney W. Grimes if (getint(&fieldwidth)) 202fbd08684SStefan Farfeleder return (NULL); 20398dd6386STim J. Robbins havewidth = 1; 204d867cefdSJoerg Wunsch ++fmt; 205d867cefdSJoerg Wunsch } else { 20698dd6386STim J. Robbins havewidth = 0; 2079b50d902SRodney W. Grimes 2089b50d902SRodney W. Grimes /* skip to possible '.', get following precision */ 2090ba01198SStefan Farfeleder fmt += strspn(fmt, skip2); 210d867cefdSJoerg Wunsch } 211d867cefdSJoerg Wunsch if (*fmt == '.') { 212d867cefdSJoerg Wunsch /* precision present? */ 2139b50d902SRodney W. Grimes ++fmt; 2149b50d902SRodney W. Grimes if (*fmt == '*') { 2159b50d902SRodney W. Grimes if (getint(&precision)) 216fbd08684SStefan Farfeleder return (NULL); 21798dd6386STim J. Robbins haveprec = 1; 218d867cefdSJoerg Wunsch ++fmt; 219d867cefdSJoerg Wunsch } else { 22098dd6386STim J. Robbins haveprec = 0; 2219b50d902SRodney W. Grimes 2229b50d902SRodney W. Grimes /* skip to conversion char */ 2230ba01198SStefan Farfeleder fmt += strspn(fmt, skip2); 224d867cefdSJoerg Wunsch } 225d867cefdSJoerg Wunsch } else 22698dd6386STim J. Robbins haveprec = 0; 2279b50d902SRodney W. Grimes if (!*fmt) { 2286a6760dbSJilles Tjoelker warnx("missing format character"); 229fbd08684SStefan Farfeleder return (NULL); 2309b50d902SRodney W. Grimes } 2319b50d902SRodney W. Grimes 232fd757c50SDavid Schultz /* 233fd757c50SDavid Schultz * Look for a length modifier. POSIX doesn't have these, so 234fd757c50SDavid Schultz * we only support them for floating-point conversions, which 235fd757c50SDavid Schultz * are extensions. This is useful because the L modifier can 236fd757c50SDavid Schultz * be used to gain extra range and precision, while omitting 237fd757c50SDavid Schultz * it is more likely to produce consistent results on different 238fd757c50SDavid Schultz * architectures. This is not so important for integers 239fd757c50SDavid Schultz * because overflow is the only bad thing that can happen to 240fd757c50SDavid Schultz * them, but consider the command printf %a 1.1 241fd757c50SDavid Schultz */ 242fd757c50SDavid Schultz if (*fmt == 'L') { 243fd757c50SDavid Schultz mod_ldbl = 1; 244fd757c50SDavid Schultz fmt++; 245fd757c50SDavid Schultz if (!strchr("aAeEfFgG", *fmt)) { 2466a6760dbSJilles Tjoelker warnx("bad modifier L for %%%c", *fmt); 247fbd08684SStefan Farfeleder return (NULL); 248fd757c50SDavid Schultz } 249fd757c50SDavid Schultz } else { 250fd757c50SDavid Schultz mod_ldbl = 0; 251fd757c50SDavid Schultz } 252fd757c50SDavid Schultz 2539b50d902SRodney W. Grimes convch = *fmt; 2549b50d902SRodney W. Grimes nextch = *++fmt; 2559b50d902SRodney W. Grimes *fmt = '\0'; 2569b50d902SRodney W. Grimes switch (convch) { 257ab5a295bSJuli Mallett case 'b': { 2583ec96cafSStefan Farfeleder size_t len; 259ab5a295bSJuli Mallett char *p; 260ab5a295bSJuli Mallett int getout; 261ab5a295bSJuli Mallett 262bacab7d6STim J. Robbins p = strdup(getstr()); 263bacab7d6STim J. Robbins if (p == NULL) { 2646a6760dbSJilles Tjoelker warnx("%s", strerror(ENOMEM)); 265fbd08684SStefan Farfeleder return (NULL); 266bacab7d6STim J. Robbins } 2673ec96cafSStefan Farfeleder getout = escape(p, 0, &len); 268ab5a295bSJuli Mallett *(fmt - 1) = 's'; 269bacab7d6STim J. Robbins PF(start, p); 270ab5a295bSJuli Mallett *(fmt - 1) = 'b'; 271ab5a295bSJuli Mallett free(p); 272ab5a295bSJuli Mallett if (getout) 273fbd08684SStefan Farfeleder return (fmt); 274ab5a295bSJuli Mallett break; 275ab5a295bSJuli Mallett } 2769b50d902SRodney W. Grimes case 'c': { 2779b50d902SRodney W. Grimes char p; 2789b50d902SRodney W. Grimes 2799b50d902SRodney W. Grimes p = getchr(); 2809b50d902SRodney W. Grimes PF(start, p); 2819b50d902SRodney W. Grimes break; 2829b50d902SRodney W. Grimes } 2839b50d902SRodney W. Grimes case 's': { 28445af1a4cSDavid Malone const char *p; 2859b50d902SRodney W. Grimes 2869b50d902SRodney W. Grimes p = getstr(); 2879b50d902SRodney W. Grimes PF(start, p); 2889b50d902SRodney W. Grimes break; 2899b50d902SRodney W. Grimes } 2909b50d902SRodney W. Grimes case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': { 2919b50d902SRodney W. Grimes char *f; 292d41d23e1SStefan Farfeleder intmax_t val; 293d41d23e1SStefan Farfeleder uintmax_t uval; 294bacab7d6STim J. Robbins int signedconv; 2959b50d902SRodney W. Grimes 296bacab7d6STim J. Robbins signedconv = (convch == 'd' || convch == 'i'); 297d41d23e1SStefan Farfeleder if ((f = mknum(start, convch)) == NULL) 298fbd08684SStefan Farfeleder return (NULL); 299d41d23e1SStefan Farfeleder if (getnum(&val, &uval, signedconv)) 300fbd08684SStefan Farfeleder *rval = 1; 301bacab7d6STim J. Robbins if (signedconv) 302bacab7d6STim J. Robbins PF(f, val); 303bacab7d6STim J. Robbins else 304bacab7d6STim J. Robbins PF(f, uval); 3059b50d902SRodney W. Grimes break; 3069b50d902SRodney W. Grimes } 30703b2eaacSDavid Schultz case 'e': case 'E': 30803b2eaacSDavid Schultz case 'f': case 'F': 30903b2eaacSDavid Schultz case 'g': case 'G': 31003b2eaacSDavid Schultz case 'a': case 'A': { 311fd757c50SDavid Schultz long double p; 3129b50d902SRodney W. Grimes 313fd757c50SDavid Schultz if (getfloating(&p, mod_ldbl)) 314fbd08684SStefan Farfeleder *rval = 1; 315fd757c50SDavid Schultz if (mod_ldbl) 3169b50d902SRodney W. Grimes PF(start, p); 317fd757c50SDavid Schultz else 318fd757c50SDavid Schultz PF(start, (double)p); 3199b50d902SRodney W. Grimes break; 3209b50d902SRodney W. Grimes } 3219b50d902SRodney W. Grimes default: 3226a6760dbSJilles Tjoelker warnx("illegal format character %c", convch); 323fbd08684SStefan Farfeleder return (NULL); 3249b50d902SRodney W. Grimes } 3259b50d902SRodney W. Grimes *fmt = nextch; 326fbd08684SStefan Farfeleder return (fmt); 3279b50d902SRodney W. Grimes } 3289b50d902SRodney W. Grimes 3299b50d902SRodney W. Grimes static char * 3309180853bSXin LI mknum(char *str, char ch) 3319b50d902SRodney W. Grimes { 3323c6e4a5cSBen Smithurst static char *copy; 3333c6e4a5cSBen Smithurst static size_t copy_size; 3343c6e4a5cSBen Smithurst char *newcopy; 335bacab7d6STim J. Robbins size_t len, newlen; 3369b50d902SRodney W. Grimes 3379b50d902SRodney W. Grimes len = strlen(str) + 2; 3383c6e4a5cSBen Smithurst if (len > copy_size) { 3393c6e4a5cSBen Smithurst newlen = ((len + 1023) >> 10) << 10; 3403c6e4a5cSBen Smithurst if ((newcopy = realloc(copy, newlen)) == NULL) 341bacab7d6STim J. Robbins { 3426a6760dbSJilles Tjoelker warnx("%s", strerror(ENOMEM)); 3433c6e4a5cSBen Smithurst return (NULL); 344bacab7d6STim J. Robbins } 3453c6e4a5cSBen Smithurst copy = newcopy; 3463c6e4a5cSBen Smithurst copy_size = newlen; 3473c6e4a5cSBen Smithurst } 34862a721e7SStefan Eßer 3499b50d902SRodney W. Grimes memmove(copy, str, len - 3); 350d41d23e1SStefan Farfeleder copy[len - 3] = 'j'; 3519b50d902SRodney W. Grimes copy[len - 2] = ch; 3529b50d902SRodney W. Grimes copy[len - 1] = '\0'; 3539b50d902SRodney W. Grimes return (copy); 3549b50d902SRodney W. Grimes } 3559b50d902SRodney W. Grimes 356ab5a295bSJuli Mallett static int 3573ec96cafSStefan Farfeleder escape(char *fmt, int percent, size_t *len) 3589b50d902SRodney W. Grimes { 3593ec96cafSStefan Farfeleder char *save, *store; 360f4ac32deSDavid Malone int value, c; 3619b50d902SRodney W. Grimes 3623ec96cafSStefan Farfeleder for (save = store = fmt; (c = *fmt); ++fmt, ++store) { 3639b50d902SRodney W. Grimes if (c != '\\') { 3649b50d902SRodney W. Grimes *store = c; 3659b50d902SRodney W. Grimes continue; 3669b50d902SRodney W. Grimes } 3679b50d902SRodney W. Grimes switch (*++fmt) { 3689b50d902SRodney W. Grimes case '\0': /* EOS, user error */ 3699b50d902SRodney W. Grimes *store = '\\'; 3709b50d902SRodney W. Grimes *++store = '\0'; 3713ec96cafSStefan Farfeleder *len = store - save; 372ab5a295bSJuli Mallett return (0); 3739b50d902SRodney W. Grimes case '\\': /* backslash */ 3749b50d902SRodney W. Grimes case '\'': /* single quote */ 3759b50d902SRodney W. Grimes *store = *fmt; 3769b50d902SRodney W. Grimes break; 3779b50d902SRodney W. Grimes case 'a': /* bell/alert */ 378f3f148d2SStefan Farfeleder *store = '\a'; 3799b50d902SRodney W. Grimes break; 3809b50d902SRodney W. Grimes case 'b': /* backspace */ 3819b50d902SRodney W. Grimes *store = '\b'; 3829b50d902SRodney W. Grimes break; 383ab5a295bSJuli Mallett case 'c': 384ab5a295bSJuli Mallett *store = '\0'; 3853ec96cafSStefan Farfeleder *len = store - save; 386ab5a295bSJuli Mallett return (1); 3879b50d902SRodney W. Grimes case 'f': /* form-feed */ 3889b50d902SRodney W. Grimes *store = '\f'; 3899b50d902SRodney W. Grimes break; 3909b50d902SRodney W. Grimes case 'n': /* newline */ 3919b50d902SRodney W. Grimes *store = '\n'; 3929b50d902SRodney W. Grimes break; 3939b50d902SRodney W. Grimes case 'r': /* carriage-return */ 3949b50d902SRodney W. Grimes *store = '\r'; 3959b50d902SRodney W. Grimes break; 3969b50d902SRodney W. Grimes case 't': /* horizontal tab */ 3979b50d902SRodney W. Grimes *store = '\t'; 3989b50d902SRodney W. Grimes break; 3999b50d902SRodney W. Grimes case 'v': /* vertical tab */ 400f3f148d2SStefan Farfeleder *store = '\v'; 4019b50d902SRodney W. Grimes break; 4029b50d902SRodney W. Grimes /* octal constant */ 4039b50d902SRodney W. Grimes case '0': case '1': case '2': case '3': 4049b50d902SRodney W. Grimes case '4': case '5': case '6': case '7': 4059d65050eSDavid Schultz c = (!percent && *fmt == '0') ? 4 : 3; 4069d65050eSDavid Schultz for (value = 0; 4079b50d902SRodney W. Grimes c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) { 4089b50d902SRodney W. Grimes value <<= 3; 4099b50d902SRodney W. Grimes value += *fmt - '0'; 4109b50d902SRodney W. Grimes } 4119b50d902SRodney W. Grimes --fmt; 41212e8db40STim J. Robbins if (percent && value == '%') { 41337fd4590STim J. Robbins *store++ = '%'; 41437fd4590STim J. Robbins *store = '%'; 41537fd4590STim J. Robbins } else 4169b50d902SRodney W. Grimes *store = value; 4179b50d902SRodney W. Grimes break; 4189b50d902SRodney W. Grimes default: 4199b50d902SRodney W. Grimes *store = *fmt; 4209b50d902SRodney W. Grimes break; 4219b50d902SRodney W. Grimes } 4229b50d902SRodney W. Grimes } 4239b50d902SRodney W. Grimes *store = '\0'; 4243ec96cafSStefan Farfeleder *len = store - save; 425ab5a295bSJuli Mallett return (0); 4269b50d902SRodney W. Grimes } 4279b50d902SRodney W. Grimes 4289b50d902SRodney W. Grimes static int 429f4ac32deSDavid Malone getchr(void) 4309b50d902SRodney W. Grimes { 4319b50d902SRodney W. Grimes if (!*gargv) 4329b50d902SRodney W. Grimes return ('\0'); 4339b50d902SRodney W. Grimes return ((int)**gargv++); 4349b50d902SRodney W. Grimes } 4359b50d902SRodney W. Grimes 43645af1a4cSDavid Malone static const char * 437f4ac32deSDavid Malone getstr(void) 4389b50d902SRodney W. Grimes { 4399b50d902SRodney W. Grimes if (!*gargv) 4409b50d902SRodney W. Grimes return (""); 4419b50d902SRodney W. Grimes return (*gargv++); 4429b50d902SRodney W. Grimes } 4439b50d902SRodney W. Grimes 4449b50d902SRodney W. Grimes static int 445f4ac32deSDavid Malone getint(int *ip) 4469b50d902SRodney W. Grimes { 447d41d23e1SStefan Farfeleder intmax_t val; 448d41d23e1SStefan Farfeleder uintmax_t uval; 449bacab7d6STim J. Robbins int rval; 4509b50d902SRodney W. Grimes 451d41d23e1SStefan Farfeleder if (getnum(&val, &uval, 1)) 4529b50d902SRodney W. Grimes return (1); 453bacab7d6STim J. Robbins rval = 0; 454bacab7d6STim J. Robbins if (val < INT_MIN || val > INT_MAX) { 4556a6760dbSJilles Tjoelker warnx("%s: %s", *gargv, strerror(ERANGE)); 456bacab7d6STim J. Robbins rval = 1; 457bacab7d6STim J. Robbins } 45862a721e7SStefan Eßer *ip = (int)val; 459bacab7d6STim J. Robbins return (rval); 4609b50d902SRodney W. Grimes } 4619b50d902SRodney W. Grimes 4629b50d902SRodney W. Grimes static int 463d41d23e1SStefan Farfeleder getnum(intmax_t *ip, uintmax_t *uip, int signedconv) 4649b50d902SRodney W. Grimes { 4659b50d902SRodney W. Grimes char *ep; 466bacab7d6STim J. Robbins int rval; 4679b50d902SRodney W. Grimes 4689b50d902SRodney W. Grimes if (!*gargv) { 469d41d23e1SStefan Farfeleder *ip = 0; 4709b50d902SRodney W. Grimes return (0); 4719b50d902SRodney W. Grimes } 472ab5a295bSJuli Mallett if (**gargv == '"' || **gargv == '\'') { 473bacab7d6STim J. Robbins if (signedconv) 474d41d23e1SStefan Farfeleder *ip = asciicode(); 475bacab7d6STim J. Robbins else 476d41d23e1SStefan Farfeleder *uip = asciicode(); 4779b50d902SRodney W. Grimes return (0); 4789b50d902SRodney W. Grimes } 479bacab7d6STim J. Robbins rval = 0; 480ab5a295bSJuli Mallett errno = 0; 481bacab7d6STim J. Robbins if (signedconv) 482d41d23e1SStefan Farfeleder *ip = strtoimax(*gargv, &ep, 0); 483bacab7d6STim J. Robbins else 484d41d23e1SStefan Farfeleder *uip = strtoumax(*gargv, &ep, 0); 485bacab7d6STim J. Robbins if (ep == *gargv) { 4866a6760dbSJilles Tjoelker warnx("%s: expected numeric value", *gargv); 487bacab7d6STim J. Robbins rval = 1; 488bacab7d6STim J. Robbins } 489bacab7d6STim J. Robbins else if (*ep != '\0') { 4906a6760dbSJilles Tjoelker warnx("%s: not completely converted", *gargv); 491bacab7d6STim J. Robbins rval = 1; 492bacab7d6STim J. Robbins } 493bacab7d6STim J. Robbins if (errno == ERANGE) { 4946a6760dbSJilles Tjoelker warnx("%s: %s", *gargv, strerror(ERANGE)); 495bacab7d6STim J. Robbins rval = 1; 496bacab7d6STim J. Robbins } 497ab5a295bSJuli Mallett ++gargv; 498bacab7d6STim J. Robbins return (rval); 4999b50d902SRodney W. Grimes } 5009b50d902SRodney W. Grimes 501bacab7d6STim J. Robbins static int 502fd757c50SDavid Schultz getfloating(long double *dp, int mod_ldbl) 5039b50d902SRodney W. Grimes { 504ab5a295bSJuli Mallett char *ep; 505bacab7d6STim J. Robbins int rval; 506ab5a295bSJuli Mallett 5075ec2b8dcSStefan Farfeleder if (!*gargv) { 5085ec2b8dcSStefan Farfeleder *dp = 0.0; 509bacab7d6STim J. Robbins return (0); 5105ec2b8dcSStefan Farfeleder } 511ab5a295bSJuli Mallett if (**gargv == '"' || **gargv == '\'') { 512bacab7d6STim J. Robbins *dp = asciicode(); 513bacab7d6STim J. Robbins return (0); 514ab5a295bSJuli Mallett } 5158c423a99SColin Percival rval = 0; 516ab5a295bSJuli Mallett errno = 0; 517fd757c50SDavid Schultz if (mod_ldbl) 518fd757c50SDavid Schultz *dp = strtold(*gargv, &ep); 519fd757c50SDavid Schultz else 520bacab7d6STim J. Robbins *dp = strtod(*gargv, &ep); 521bacab7d6STim J. Robbins if (ep == *gargv) { 5226a6760dbSJilles Tjoelker warnx("%s: expected numeric value", *gargv); 523bacab7d6STim J. Robbins rval = 1; 524bacab7d6STim J. Robbins } else if (*ep != '\0') { 5256a6760dbSJilles Tjoelker warnx("%s: not completely converted", *gargv); 526bacab7d6STim J. Robbins rval = 1; 527bacab7d6STim J. Robbins } 528bacab7d6STim J. Robbins if (errno == ERANGE) { 5296a6760dbSJilles Tjoelker warnx("%s: %s", *gargv, strerror(ERANGE)); 530bacab7d6STim J. Robbins rval = 1; 531bacab7d6STim J. Robbins } 532ab5a295bSJuli Mallett ++gargv; 533bacab7d6STim J. Robbins return (rval); 5349b50d902SRodney W. Grimes } 5359b50d902SRodney W. Grimes 5369b50d902SRodney W. Grimes static int 537f4ac32deSDavid Malone asciicode(void) 5389b50d902SRodney W. Grimes { 539f4ac32deSDavid Malone int ch; 5409b50d902SRodney W. Grimes 5419b50d902SRodney W. Grimes ch = **gargv; 5429b50d902SRodney W. Grimes if (ch == '\'' || ch == '"') 5439b50d902SRodney W. Grimes ch = (*gargv)[1]; 5449b50d902SRodney W. Grimes ++gargv; 5459b50d902SRodney W. Grimes return (ch); 5469b50d902SRodney W. Grimes } 5479b50d902SRodney W. Grimes 5489b50d902SRodney W. Grimes static void 549f4ac32deSDavid Malone usage(void) 5509b50d902SRodney W. Grimes { 551f682f10cSRuslan Ermilov (void)fprintf(stderr, "usage: printf format [arguments ...]\n"); 5529b50d902SRodney W. Grimes } 553