19b50d902SRodney W. Grimes /* 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 * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #if !defined(BUILTIN) && !defined(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> 569b50d902SRodney W. Grimes #include <stdio.h> 579b50d902SRodney W. Grimes #include <stdlib.h> 589b50d902SRodney W. Grimes #include <string.h> 599d19feb5SSteve Price #include <unistd.h> 609b50d902SRodney W. Grimes 619b50d902SRodney W. Grimes #ifdef SHELL 629b50d902SRodney W. Grimes #define main printfcmd 63d9f93710SJoerg Wunsch #include "bltin/bltin.h" 643c6e4a5cSBen Smithurst #include "memalloc.h" 65*9897c45fSJilles Tjoelker #include "error.h" 663b53d380SBruce Evans #else 673b53d380SBruce Evans #define warnx1(a, b, c) warnx(a) 683b53d380SBruce Evans #define warnx2(a, b, c) warnx(a, b) 693b53d380SBruce Evans #define warnx3(a, b, c) warnx(a, b, c) 709b50d902SRodney W. Grimes #endif 719b50d902SRodney W. Grimes 72dc7d8c99SAndrey A. Chernov #ifndef BUILTIN 73dc7d8c99SAndrey A. Chernov #include <locale.h> 74dc7d8c99SAndrey A. Chernov #endif 75dc7d8c99SAndrey A. Chernov 76bacab7d6STim J. Robbins #define PF(f, func) do { \ 77d72f654cSPeter Wemm char *b = NULL; \ 7898dd6386STim J. Robbins if (havewidth) \ 7998dd6386STim J. Robbins if (haveprec) \ 80d72f654cSPeter Wemm (void)asprintf(&b, f, fieldwidth, precision, func); \ 819b50d902SRodney W. Grimes else \ 82d72f654cSPeter Wemm (void)asprintf(&b, f, fieldwidth, func); \ 8398dd6386STim J. Robbins else if (haveprec) \ 84d72f654cSPeter Wemm (void)asprintf(&b, f, precision, func); \ 859b50d902SRodney W. Grimes else \ 86d72f654cSPeter Wemm (void)asprintf(&b, f, func); \ 87d72f654cSPeter Wemm if (b) { \ 88d72f654cSPeter Wemm (void)fputs(b, stdout); \ 89d72f654cSPeter Wemm free(b); \ 90d72f654cSPeter Wemm } \ 91bacab7d6STim J. Robbins } while (0) 929b50d902SRodney W. Grimes 93d3cb5dedSWarner Losh static int asciicode(void); 94*9897c45fSJilles Tjoelker static char *printf_doformat(char *, int *); 953ec96cafSStefan Farfeleder static int escape(char *, int, size_t *); 96d3cb5dedSWarner Losh static int getchr(void); 97fd757c50SDavid Schultz static int getfloating(long double *, int); 98d3cb5dedSWarner Losh static int getint(int *); 99d41d23e1SStefan Farfeleder static int getnum(intmax_t *, uintmax_t *, int); 100bacab7d6STim J. Robbins static const char 101bacab7d6STim J. Robbins *getstr(void); 102d41d23e1SStefan Farfeleder static char *mknum(char *, int); 103d3cb5dedSWarner Losh static void usage(void); 1049b50d902SRodney W. Grimes 1059b50d902SRodney W. Grimes static char **gargv; 1069b50d902SRodney W. Grimes 1079b50d902SRodney W. Grimes int 1089b50d902SRodney W. Grimes #ifdef BUILTIN 109f4ac32deSDavid Malone progprintf(int argc, char *argv[]) 1109b50d902SRodney W. Grimes #else 111f4ac32deSDavid Malone main(int argc, char *argv[]) 1129b50d902SRodney W. Grimes #endif 1139b50d902SRodney W. Grimes { 1143ec96cafSStefan Farfeleder size_t len; 115fbd08684SStefan Farfeleder int ch, chopped, end, rval; 116fbd08684SStefan Farfeleder char *format, *fmt, *start; 1179b50d902SRodney W. Grimes 118*9897c45fSJilles Tjoelker #if !defined(BUILTIN) && !defined(SHELL) 119dc7d8c99SAndrey A. Chernov (void) setlocale(LC_NUMERIC, ""); 120dc7d8c99SAndrey A. Chernov #endif 121*9897c45fSJilles Tjoelker #ifdef SHELL 122*9897c45fSJilles Tjoelker optreset = 1; optind = 1; opterr = 0; /* initialize getopt */ 123*9897c45fSJilles Tjoelker #endif 1241c8af878SWarner Losh while ((ch = getopt(argc, argv, "")) != -1) 1259b50d902SRodney W. Grimes switch (ch) { 1269b50d902SRodney W. Grimes case '?': 1279b50d902SRodney W. Grimes default: 1289b50d902SRodney W. Grimes usage(); 1299b50d902SRodney W. Grimes return (1); 1309b50d902SRodney W. Grimes } 1319b50d902SRodney W. Grimes argc -= optind; 1329b50d902SRodney W. Grimes argv += optind; 1339b50d902SRodney W. Grimes 1349b50d902SRodney W. Grimes if (argc < 1) { 1359b50d902SRodney W. Grimes usage(); 1369b50d902SRodney W. Grimes return (1); 1379b50d902SRodney W. Grimes } 1389b50d902SRodney W. Grimes 139*9897c45fSJilles Tjoelker #ifdef SHELL 140*9897c45fSJilles Tjoelker INTOFF; 141*9897c45fSJilles Tjoelker #endif 1429b50d902SRodney W. Grimes /* 1439b50d902SRodney W. Grimes * Basic algorithm is to scan the format string for conversion 1449b50d902SRodney W. Grimes * specifications -- once one is found, find out if the field 1459b50d902SRodney W. Grimes * width or precision is a '*'; if it is, gather up value. Note, 1469b50d902SRodney W. Grimes * format strings are reused as necessary to use up the provided 1479b50d902SRodney W. Grimes * arguments, arguments of zero/null string are provided to use 1489b50d902SRodney W. Grimes * up the format string. 1499b50d902SRodney W. Grimes */ 1503ec96cafSStefan Farfeleder fmt = format = *argv; 1513ec96cafSStefan Farfeleder chopped = escape(fmt, 1, &len); /* backslash interpretation */ 152fbd08684SStefan Farfeleder rval = end = 0; 1539b50d902SRodney W. Grimes gargv = ++argv; 1549b50d902SRodney W. Grimes for (;;) { 155fbd08684SStefan Farfeleder start = fmt; 1563ec96cafSStefan Farfeleder while (fmt < format + len) { 157fbd08684SStefan Farfeleder if (fmt[0] == '%') { 158fbd08684SStefan Farfeleder fwrite(start, 1, fmt - start, stdout); 159fbd08684SStefan Farfeleder if (fmt[1] == '%') { 1609b50d902SRodney W. Grimes /* %% prints a % */ 161fbd08684SStefan Farfeleder putchar('%'); 162fbd08684SStefan Farfeleder fmt += 2; 163fbd08684SStefan Farfeleder } else { 164*9897c45fSJilles Tjoelker fmt = printf_doformat(fmt, &rval); 165*9897c45fSJilles Tjoelker if (fmt == NULL) { 166*9897c45fSJilles Tjoelker #ifdef SHELL 167*9897c45fSJilles Tjoelker INTON; 168*9897c45fSJilles Tjoelker #endif 169fbd08684SStefan Farfeleder return (1); 170*9897c45fSJilles Tjoelker } 171fbd08684SStefan Farfeleder end = 0; 1729b50d902SRodney W. Grimes } 173fbd08684SStefan Farfeleder start = fmt; 174fbd08684SStefan Farfeleder } else 175fbd08684SStefan Farfeleder fmt++; 1769b50d902SRodney W. Grimes } 1779b50d902SRodney W. Grimes 178fbd08684SStefan Farfeleder if (end == 1) { 179fbd08684SStefan Farfeleder warnx1("missing format character", NULL, NULL); 180*9897c45fSJilles Tjoelker #ifdef SHELL 181*9897c45fSJilles Tjoelker INTON; 182*9897c45fSJilles Tjoelker #endif 183fbd08684SStefan Farfeleder return (1); 184fbd08684SStefan Farfeleder } 185fbd08684SStefan Farfeleder fwrite(start, 1, fmt - start, stdout); 186*9897c45fSJilles Tjoelker if (chopped || !*gargv) { 187*9897c45fSJilles Tjoelker #ifdef SHELL 188*9897c45fSJilles Tjoelker INTON; 189*9897c45fSJilles Tjoelker #endif 190fbd08684SStefan Farfeleder return (rval); 191*9897c45fSJilles Tjoelker } 192fbd08684SStefan Farfeleder /* Restart at the beginning of the format string. */ 193fbd08684SStefan Farfeleder fmt = format; 194fbd08684SStefan Farfeleder end = 1; 195fbd08684SStefan Farfeleder } 196fbd08684SStefan Farfeleder /* NOTREACHED */ 197fbd08684SStefan Farfeleder } 198fbd08684SStefan Farfeleder 199fbd08684SStefan Farfeleder 200fbd08684SStefan Farfeleder static char * 201*9897c45fSJilles Tjoelker printf_doformat(char *start, int *rval) 202fbd08684SStefan Farfeleder { 203fbd08684SStefan Farfeleder static const char skip1[] = "#'-+ 0"; 204fbd08684SStefan Farfeleder static const char skip2[] = "0123456789"; 205fbd08684SStefan Farfeleder char *fmt; 206fbd08684SStefan Farfeleder int fieldwidth, haveprec, havewidth, mod_ldbl, precision; 207fbd08684SStefan Farfeleder char convch, nextch; 208fbd08684SStefan Farfeleder 209fbd08684SStefan Farfeleder fmt = start + 1; 2109b50d902SRodney W. Grimes /* skip to field width */ 2110ba01198SStefan Farfeleder fmt += strspn(fmt, skip1); 2129b50d902SRodney W. Grimes if (*fmt == '*') { 2139b50d902SRodney W. Grimes if (getint(&fieldwidth)) 214fbd08684SStefan Farfeleder return (NULL); 21598dd6386STim J. Robbins havewidth = 1; 216d867cefdSJoerg Wunsch ++fmt; 217d867cefdSJoerg Wunsch } else { 21898dd6386STim J. Robbins havewidth = 0; 2199b50d902SRodney W. Grimes 2209b50d902SRodney W. Grimes /* skip to possible '.', get following precision */ 2210ba01198SStefan Farfeleder fmt += strspn(fmt, skip2); 222d867cefdSJoerg Wunsch } 223d867cefdSJoerg Wunsch if (*fmt == '.') { 224d867cefdSJoerg Wunsch /* precision present? */ 2259b50d902SRodney W. Grimes ++fmt; 2269b50d902SRodney W. Grimes if (*fmt == '*') { 2279b50d902SRodney W. Grimes if (getint(&precision)) 228fbd08684SStefan Farfeleder return (NULL); 22998dd6386STim J. Robbins haveprec = 1; 230d867cefdSJoerg Wunsch ++fmt; 231d867cefdSJoerg Wunsch } else { 23298dd6386STim J. Robbins haveprec = 0; 2339b50d902SRodney W. Grimes 2349b50d902SRodney W. Grimes /* skip to conversion char */ 2350ba01198SStefan Farfeleder fmt += strspn(fmt, skip2); 236d867cefdSJoerg Wunsch } 237d867cefdSJoerg Wunsch } else 23898dd6386STim J. Robbins haveprec = 0; 2399b50d902SRodney W. Grimes if (!*fmt) { 2403b53d380SBruce Evans warnx1("missing format character", NULL, NULL); 241fbd08684SStefan Farfeleder return (NULL); 2429b50d902SRodney W. Grimes } 2439b50d902SRodney W. Grimes 244fd757c50SDavid Schultz /* 245fd757c50SDavid Schultz * Look for a length modifier. POSIX doesn't have these, so 246fd757c50SDavid Schultz * we only support them for floating-point conversions, which 247fd757c50SDavid Schultz * are extensions. This is useful because the L modifier can 248fd757c50SDavid Schultz * be used to gain extra range and precision, while omitting 249fd757c50SDavid Schultz * it is more likely to produce consistent results on different 250fd757c50SDavid Schultz * architectures. This is not so important for integers 251fd757c50SDavid Schultz * because overflow is the only bad thing that can happen to 252fd757c50SDavid Schultz * them, but consider the command printf %a 1.1 253fd757c50SDavid Schultz */ 254fd757c50SDavid Schultz if (*fmt == 'L') { 255fd757c50SDavid Schultz mod_ldbl = 1; 256fd757c50SDavid Schultz fmt++; 257fd757c50SDavid Schultz if (!strchr("aAeEfFgG", *fmt)) { 258fd757c50SDavid Schultz warnx2("bad modifier L for %%%c", *fmt, NULL); 259fbd08684SStefan Farfeleder return (NULL); 260fd757c50SDavid Schultz } 261fd757c50SDavid Schultz } else { 262fd757c50SDavid Schultz mod_ldbl = 0; 263fd757c50SDavid Schultz } 264fd757c50SDavid Schultz 2659b50d902SRodney W. Grimes convch = *fmt; 2669b50d902SRodney W. Grimes nextch = *++fmt; 2679b50d902SRodney W. Grimes *fmt = '\0'; 2689b50d902SRodney W. Grimes switch (convch) { 269ab5a295bSJuli Mallett case 'b': { 2703ec96cafSStefan Farfeleder size_t len; 271ab5a295bSJuli Mallett char *p; 272ab5a295bSJuli Mallett int getout; 273ab5a295bSJuli Mallett 274bacab7d6STim J. Robbins #ifdef SHELL 275bacab7d6STim J. Robbins p = savestr(getstr()); 276bacab7d6STim J. Robbins #else 277bacab7d6STim J. Robbins p = strdup(getstr()); 278bacab7d6STim J. Robbins #endif 279bacab7d6STim J. Robbins if (p == NULL) { 280bacab7d6STim J. Robbins warnx2("%s", strerror(ENOMEM), NULL); 281fbd08684SStefan Farfeleder return (NULL); 282bacab7d6STim J. Robbins } 2833ec96cafSStefan Farfeleder getout = escape(p, 0, &len); 284ab5a295bSJuli Mallett *(fmt - 1) = 's'; 285bacab7d6STim J. Robbins PF(start, p); 286ab5a295bSJuli Mallett *(fmt - 1) = 'b'; 287bacab7d6STim J. Robbins #ifdef SHELL 288bacab7d6STim J. Robbins ckfree(p); 289bacab7d6STim J. Robbins #else 290ab5a295bSJuli Mallett free(p); 291bacab7d6STim J. Robbins #endif 292ab5a295bSJuli Mallett if (getout) 293fbd08684SStefan Farfeleder return (fmt); 294ab5a295bSJuli Mallett break; 295ab5a295bSJuli Mallett } 2969b50d902SRodney W. Grimes case 'c': { 2979b50d902SRodney W. Grimes char p; 2989b50d902SRodney W. Grimes 2999b50d902SRodney W. Grimes p = getchr(); 3009b50d902SRodney W. Grimes PF(start, p); 3019b50d902SRodney W. Grimes break; 3029b50d902SRodney W. Grimes } 3039b50d902SRodney W. Grimes case 's': { 30445af1a4cSDavid Malone const char *p; 3059b50d902SRodney W. Grimes 3069b50d902SRodney W. Grimes p = getstr(); 3079b50d902SRodney W. Grimes PF(start, p); 3089b50d902SRodney W. Grimes break; 3099b50d902SRodney W. Grimes } 3109b50d902SRodney W. Grimes case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': { 3119b50d902SRodney W. Grimes char *f; 312d41d23e1SStefan Farfeleder intmax_t val; 313d41d23e1SStefan Farfeleder uintmax_t uval; 314bacab7d6STim J. Robbins int signedconv; 3159b50d902SRodney W. Grimes 316bacab7d6STim J. Robbins signedconv = (convch == 'd' || convch == 'i'); 317d41d23e1SStefan Farfeleder if ((f = mknum(start, convch)) == NULL) 318fbd08684SStefan Farfeleder return (NULL); 319d41d23e1SStefan Farfeleder if (getnum(&val, &uval, signedconv)) 320fbd08684SStefan Farfeleder *rval = 1; 321bacab7d6STim J. Robbins if (signedconv) 322bacab7d6STim J. Robbins PF(f, val); 323bacab7d6STim J. Robbins else 324bacab7d6STim J. Robbins PF(f, uval); 3259b50d902SRodney W. Grimes break; 3269b50d902SRodney W. Grimes } 32703b2eaacSDavid Schultz case 'e': case 'E': 32803b2eaacSDavid Schultz case 'f': case 'F': 32903b2eaacSDavid Schultz case 'g': case 'G': 33003b2eaacSDavid Schultz case 'a': case 'A': { 331fd757c50SDavid Schultz long double p; 3329b50d902SRodney W. Grimes 333fd757c50SDavid Schultz if (getfloating(&p, mod_ldbl)) 334fbd08684SStefan Farfeleder *rval = 1; 335fd757c50SDavid Schultz if (mod_ldbl) 3369b50d902SRodney W. Grimes PF(start, p); 337fd757c50SDavid Schultz else 338fd757c50SDavid Schultz PF(start, (double)p); 3399b50d902SRodney W. Grimes break; 3409b50d902SRodney W. Grimes } 3419b50d902SRodney W. Grimes default: 3423b53d380SBruce Evans warnx2("illegal format character %c", convch, NULL); 343fbd08684SStefan Farfeleder return (NULL); 3449b50d902SRodney W. Grimes } 3459b50d902SRodney W. Grimes *fmt = nextch; 346fbd08684SStefan Farfeleder return (fmt); 3479b50d902SRodney W. Grimes } 3489b50d902SRodney W. Grimes 3499b50d902SRodney W. Grimes static char * 350d41d23e1SStefan Farfeleder mknum(char *str, int ch) 3519b50d902SRodney W. Grimes { 3523c6e4a5cSBen Smithurst static char *copy; 3533c6e4a5cSBen Smithurst static size_t copy_size; 3543c6e4a5cSBen Smithurst char *newcopy; 355bacab7d6STim J. Robbins size_t len, newlen; 3569b50d902SRodney W. Grimes 3579b50d902SRodney W. Grimes len = strlen(str) + 2; 3583c6e4a5cSBen Smithurst if (len > copy_size) { 3593c6e4a5cSBen Smithurst newlen = ((len + 1023) >> 10) << 10; 3603c6e4a5cSBen Smithurst #ifdef SHELL 3613c6e4a5cSBen Smithurst if ((newcopy = ckrealloc(copy, newlen)) == NULL) 3623c6e4a5cSBen Smithurst #else 3633c6e4a5cSBen Smithurst if ((newcopy = realloc(copy, newlen)) == NULL) 3643c6e4a5cSBen Smithurst #endif 365bacab7d6STim J. Robbins { 366bacab7d6STim J. Robbins warnx2("%s", strerror(ENOMEM), NULL); 3673c6e4a5cSBen Smithurst return (NULL); 368bacab7d6STim J. Robbins } 3693c6e4a5cSBen Smithurst copy = newcopy; 3703c6e4a5cSBen Smithurst copy_size = newlen; 3713c6e4a5cSBen Smithurst } 37262a721e7SStefan Eßer 3739b50d902SRodney W. Grimes memmove(copy, str, len - 3); 374d41d23e1SStefan Farfeleder copy[len - 3] = 'j'; 3759b50d902SRodney W. Grimes copy[len - 2] = ch; 3769b50d902SRodney W. Grimes copy[len - 1] = '\0'; 3779b50d902SRodney W. Grimes return (copy); 3789b50d902SRodney W. Grimes } 3799b50d902SRodney W. Grimes 380ab5a295bSJuli Mallett static int 3813ec96cafSStefan Farfeleder escape(char *fmt, int percent, size_t *len) 3829b50d902SRodney W. Grimes { 3833ec96cafSStefan Farfeleder char *save, *store; 384f4ac32deSDavid Malone int value, c; 3859b50d902SRodney W. Grimes 3863ec96cafSStefan Farfeleder for (save = store = fmt; (c = *fmt); ++fmt, ++store) { 3879b50d902SRodney W. Grimes if (c != '\\') { 3889b50d902SRodney W. Grimes *store = c; 3899b50d902SRodney W. Grimes continue; 3909b50d902SRodney W. Grimes } 3919b50d902SRodney W. Grimes switch (*++fmt) { 3929b50d902SRodney W. Grimes case '\0': /* EOS, user error */ 3939b50d902SRodney W. Grimes *store = '\\'; 3949b50d902SRodney W. Grimes *++store = '\0'; 3953ec96cafSStefan Farfeleder *len = store - save; 396ab5a295bSJuli Mallett return (0); 3979b50d902SRodney W. Grimes case '\\': /* backslash */ 3989b50d902SRodney W. Grimes case '\'': /* single quote */ 3999b50d902SRodney W. Grimes *store = *fmt; 4009b50d902SRodney W. Grimes break; 4019b50d902SRodney W. Grimes case 'a': /* bell/alert */ 402f3f148d2SStefan Farfeleder *store = '\a'; 4039b50d902SRodney W. Grimes break; 4049b50d902SRodney W. Grimes case 'b': /* backspace */ 4059b50d902SRodney W. Grimes *store = '\b'; 4069b50d902SRodney W. Grimes break; 407ab5a295bSJuli Mallett case 'c': 408ab5a295bSJuli Mallett *store = '\0'; 4093ec96cafSStefan Farfeleder *len = store - save; 410ab5a295bSJuli Mallett return (1); 4119b50d902SRodney W. Grimes case 'f': /* form-feed */ 4129b50d902SRodney W. Grimes *store = '\f'; 4139b50d902SRodney W. Grimes break; 4149b50d902SRodney W. Grimes case 'n': /* newline */ 4159b50d902SRodney W. Grimes *store = '\n'; 4169b50d902SRodney W. Grimes break; 4179b50d902SRodney W. Grimes case 'r': /* carriage-return */ 4189b50d902SRodney W. Grimes *store = '\r'; 4199b50d902SRodney W. Grimes break; 4209b50d902SRodney W. Grimes case 't': /* horizontal tab */ 4219b50d902SRodney W. Grimes *store = '\t'; 4229b50d902SRodney W. Grimes break; 4239b50d902SRodney W. Grimes case 'v': /* vertical tab */ 424f3f148d2SStefan Farfeleder *store = '\v'; 4259b50d902SRodney W. Grimes break; 4269b50d902SRodney W. Grimes /* octal constant */ 4279b50d902SRodney W. Grimes case '0': case '1': case '2': case '3': 4289b50d902SRodney W. Grimes case '4': case '5': case '6': case '7': 4299d65050eSDavid Schultz c = (!percent && *fmt == '0') ? 4 : 3; 4309d65050eSDavid Schultz for (value = 0; 4319b50d902SRodney W. Grimes c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) { 4329b50d902SRodney W. Grimes value <<= 3; 4339b50d902SRodney W. Grimes value += *fmt - '0'; 4349b50d902SRodney W. Grimes } 4359b50d902SRodney W. Grimes --fmt; 43612e8db40STim J. Robbins if (percent && value == '%') { 43737fd4590STim J. Robbins *store++ = '%'; 43837fd4590STim J. Robbins *store = '%'; 43937fd4590STim J. Robbins } else 4409b50d902SRodney W. Grimes *store = value; 4419b50d902SRodney W. Grimes break; 4429b50d902SRodney W. Grimes default: 4439b50d902SRodney W. Grimes *store = *fmt; 4449b50d902SRodney W. Grimes break; 4459b50d902SRodney W. Grimes } 4469b50d902SRodney W. Grimes } 4479b50d902SRodney W. Grimes *store = '\0'; 4483ec96cafSStefan Farfeleder *len = store - save; 449ab5a295bSJuli Mallett return (0); 4509b50d902SRodney W. Grimes } 4519b50d902SRodney W. Grimes 4529b50d902SRodney W. Grimes static int 453f4ac32deSDavid Malone getchr(void) 4549b50d902SRodney W. Grimes { 4559b50d902SRodney W. Grimes if (!*gargv) 4569b50d902SRodney W. Grimes return ('\0'); 4579b50d902SRodney W. Grimes return ((int)**gargv++); 4589b50d902SRodney W. Grimes } 4599b50d902SRodney W. Grimes 46045af1a4cSDavid Malone static const char * 461f4ac32deSDavid Malone getstr(void) 4629b50d902SRodney W. Grimes { 4639b50d902SRodney W. Grimes if (!*gargv) 4649b50d902SRodney W. Grimes return (""); 4659b50d902SRodney W. Grimes return (*gargv++); 4669b50d902SRodney W. Grimes } 4679b50d902SRodney W. Grimes 4689b50d902SRodney W. Grimes static int 469f4ac32deSDavid Malone getint(int *ip) 4709b50d902SRodney W. Grimes { 471d41d23e1SStefan Farfeleder intmax_t val; 472d41d23e1SStefan Farfeleder uintmax_t uval; 473bacab7d6STim J. Robbins int rval; 4749b50d902SRodney W. Grimes 475d41d23e1SStefan Farfeleder if (getnum(&val, &uval, 1)) 4769b50d902SRodney W. Grimes return (1); 477bacab7d6STim J. Robbins rval = 0; 478bacab7d6STim J. Robbins if (val < INT_MIN || val > INT_MAX) { 4793b53d380SBruce Evans warnx3("%s: %s", *gargv, strerror(ERANGE)); 480bacab7d6STim J. Robbins rval = 1; 481bacab7d6STim J. Robbins } 48262a721e7SStefan Eßer *ip = (int)val; 483bacab7d6STim J. Robbins return (rval); 4849b50d902SRodney W. Grimes } 4859b50d902SRodney W. Grimes 4869b50d902SRodney W. Grimes static int 487d41d23e1SStefan Farfeleder getnum(intmax_t *ip, uintmax_t *uip, int signedconv) 4889b50d902SRodney W. Grimes { 4899b50d902SRodney W. Grimes char *ep; 490bacab7d6STim J. Robbins int rval; 4919b50d902SRodney W. Grimes 4929b50d902SRodney W. Grimes if (!*gargv) { 493d41d23e1SStefan Farfeleder *ip = 0; 4949b50d902SRodney W. Grimes return (0); 4959b50d902SRodney W. Grimes } 496ab5a295bSJuli Mallett if (**gargv == '"' || **gargv == '\'') { 497bacab7d6STim J. Robbins if (signedconv) 498d41d23e1SStefan Farfeleder *ip = asciicode(); 499bacab7d6STim J. Robbins else 500d41d23e1SStefan Farfeleder *uip = asciicode(); 5019b50d902SRodney W. Grimes return (0); 5029b50d902SRodney W. Grimes } 503bacab7d6STim J. Robbins rval = 0; 504ab5a295bSJuli Mallett errno = 0; 505bacab7d6STim J. Robbins if (signedconv) 506d41d23e1SStefan Farfeleder *ip = strtoimax(*gargv, &ep, 0); 507bacab7d6STim J. Robbins else 508d41d23e1SStefan Farfeleder *uip = strtoumax(*gargv, &ep, 0); 509bacab7d6STim J. Robbins if (ep == *gargv) { 510ab5a295bSJuli Mallett warnx2("%s: expected numeric value", *gargv, NULL); 511bacab7d6STim J. Robbins rval = 1; 512bacab7d6STim J. Robbins } 513bacab7d6STim J. Robbins else if (*ep != '\0') { 514ab5a295bSJuli Mallett warnx2("%s: not completely converted", *gargv, NULL); 515bacab7d6STim J. Robbins rval = 1; 516bacab7d6STim J. Robbins } 517bacab7d6STim J. Robbins if (errno == ERANGE) { 518ab5a295bSJuli Mallett warnx3("%s: %s", *gargv, strerror(ERANGE)); 519bacab7d6STim J. Robbins rval = 1; 520bacab7d6STim J. Robbins } 521ab5a295bSJuli Mallett ++gargv; 522bacab7d6STim J. Robbins return (rval); 5239b50d902SRodney W. Grimes } 5249b50d902SRodney W. Grimes 525bacab7d6STim J. Robbins static int 526fd757c50SDavid Schultz getfloating(long double *dp, int mod_ldbl) 5279b50d902SRodney W. Grimes { 528ab5a295bSJuli Mallett char *ep; 529bacab7d6STim J. Robbins int rval; 530ab5a295bSJuli Mallett 5315ec2b8dcSStefan Farfeleder if (!*gargv) { 5325ec2b8dcSStefan Farfeleder *dp = 0.0; 533bacab7d6STim J. Robbins return (0); 5345ec2b8dcSStefan Farfeleder } 535ab5a295bSJuli Mallett if (**gargv == '"' || **gargv == '\'') { 536bacab7d6STim J. Robbins *dp = asciicode(); 537bacab7d6STim J. Robbins return (0); 538ab5a295bSJuli Mallett } 5398c423a99SColin Percival rval = 0; 540ab5a295bSJuli Mallett errno = 0; 541fd757c50SDavid Schultz if (mod_ldbl) 542fd757c50SDavid Schultz *dp = strtold(*gargv, &ep); 543fd757c50SDavid Schultz else 544bacab7d6STim J. Robbins *dp = strtod(*gargv, &ep); 545bacab7d6STim J. Robbins if (ep == *gargv) { 546ab5a295bSJuli Mallett warnx2("%s: expected numeric value", *gargv, NULL); 547bacab7d6STim J. Robbins rval = 1; 548bacab7d6STim J. Robbins } else if (*ep != '\0') { 549ab5a295bSJuli Mallett warnx2("%s: not completely converted", *gargv, NULL); 550bacab7d6STim J. Robbins rval = 1; 551bacab7d6STim J. Robbins } 552bacab7d6STim J. Robbins if (errno == ERANGE) { 553ab5a295bSJuli Mallett warnx3("%s: %s", *gargv, strerror(ERANGE)); 554bacab7d6STim J. Robbins rval = 1; 555bacab7d6STim J. Robbins } 556ab5a295bSJuli Mallett ++gargv; 557bacab7d6STim J. Robbins return (rval); 5589b50d902SRodney W. Grimes } 5599b50d902SRodney W. Grimes 5609b50d902SRodney W. Grimes static int 561f4ac32deSDavid Malone asciicode(void) 5629b50d902SRodney W. Grimes { 563f4ac32deSDavid Malone int ch; 5649b50d902SRodney W. Grimes 5659b50d902SRodney W. Grimes ch = **gargv; 5669b50d902SRodney W. Grimes if (ch == '\'' || ch == '"') 5679b50d902SRodney W. Grimes ch = (*gargv)[1]; 5689b50d902SRodney W. Grimes ++gargv; 5699b50d902SRodney W. Grimes return (ch); 5709b50d902SRodney W. Grimes } 5719b50d902SRodney W. Grimes 5729b50d902SRodney W. Grimes static void 573f4ac32deSDavid Malone usage(void) 5749b50d902SRodney W. Grimes { 575f682f10cSRuslan Ermilov (void)fprintf(stderr, "usage: printf format [arguments ...]\n"); 5769b50d902SRodney W. Grimes } 577