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 */ 2977fae5c1SJilles Tjoelker /* 3077fae5c1SJilles Tjoelker * Important: This file is used both as a standalone program /usr/bin/printf 3177fae5c1SJilles Tjoelker * and as a builtin for /bin/sh (#define SHELL). 3277fae5c1SJilles 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> 6198102dabSJilles Tjoelker #include <wchar.h> 629b50d902SRodney W. Grimes 639b50d902SRodney W. Grimes #ifdef SHELL 649b50d902SRodney W. Grimes #define main printfcmd 65d9f93710SJoerg Wunsch #include "bltin/bltin.h" 669897c45fSJilles Tjoelker #include "error.h" 67*7cbda738SJilles Tjoelker #include "options.h" 689b50d902SRodney W. Grimes #endif 699b50d902SRodney W. Grimes 70bacab7d6STim J. Robbins #define PF(f, func) do { \ 71d72f654cSPeter Wemm char *b = NULL; \ 7298dd6386STim J. Robbins if (havewidth) \ 7398dd6386STim J. Robbins if (haveprec) \ 74d72f654cSPeter Wemm (void)asprintf(&b, f, fieldwidth, precision, func); \ 759b50d902SRodney W. Grimes else \ 76d72f654cSPeter Wemm (void)asprintf(&b, f, fieldwidth, func); \ 7798dd6386STim J. Robbins else if (haveprec) \ 78d72f654cSPeter Wemm (void)asprintf(&b, f, precision, func); \ 799b50d902SRodney W. Grimes else \ 80d72f654cSPeter Wemm (void)asprintf(&b, f, func); \ 81d72f654cSPeter Wemm if (b) { \ 82d72f654cSPeter Wemm (void)fputs(b, stdout); \ 83d72f654cSPeter Wemm free(b); \ 84d72f654cSPeter Wemm } \ 85bacab7d6STim J. Robbins } while (0) 869b50d902SRodney W. Grimes 87d3cb5dedSWarner Losh static int asciicode(void); 889897c45fSJilles Tjoelker static char *printf_doformat(char *, int *); 893ec96cafSStefan Farfeleder static int escape(char *, int, size_t *); 90d3cb5dedSWarner Losh static int getchr(void); 91fd757c50SDavid Schultz static int getfloating(long double *, int); 92d3cb5dedSWarner Losh static int getint(int *); 93d41d23e1SStefan Farfeleder static int getnum(intmax_t *, uintmax_t *, int); 94bacab7d6STim J. Robbins static const char 95bacab7d6STim J. Robbins *getstr(void); 969180853bSXin LI static char *mknum(char *, char); 97d3cb5dedSWarner Losh static void usage(void); 989b50d902SRodney W. Grimes 999b50d902SRodney W. Grimes static char **gargv; 1009b50d902SRodney W. Grimes 1019b50d902SRodney W. Grimes int 102f4ac32deSDavid Malone main(int argc, char *argv[]) 1039b50d902SRodney W. Grimes { 1043ec96cafSStefan Farfeleder size_t len; 105*7cbda738SJilles Tjoelker int chopped, end, rval; 106fbd08684SStefan Farfeleder char *format, *fmt, *start; 1071866e8abSJilles Tjoelker #ifndef SHELL 108*7cbda738SJilles Tjoelker int ch; 109*7cbda738SJilles Tjoelker 11069be0c5eSXin LI (void) setlocale(LC_ALL, ""); 111dc7d8c99SAndrey A. Chernov #endif 112*7cbda738SJilles Tjoelker 1139897c45fSJilles Tjoelker #ifdef SHELL 114*7cbda738SJilles Tjoelker nextopt(""); 115*7cbda738SJilles Tjoelker argc -= argptr - argv; 116*7cbda738SJilles Tjoelker argv = argptr; 117*7cbda738SJilles Tjoelker #else 118de46de4dSXin LI while ((ch = getopt(argc, argv, "")) != -1) 119de46de4dSXin LI switch (ch) { 120de46de4dSXin LI case '?': 121de46de4dSXin LI default: 122de46de4dSXin LI usage(); 123de46de4dSXin LI return (1); 1249b50d902SRodney W. Grimes } 125de46de4dSXin LI argc -= optind; 126de46de4dSXin LI argv += optind; 127*7cbda738SJilles Tjoelker #endif 1289b50d902SRodney W. Grimes 1299b50d902SRodney W. Grimes if (argc < 1) { 1309b50d902SRodney W. Grimes usage(); 1315eccc000SXin LI return (1); 1329b50d902SRodney W. Grimes } 1339b50d902SRodney W. Grimes 1349897c45fSJilles Tjoelker #ifdef SHELL 1359897c45fSJilles Tjoelker INTOFF; 1369897c45fSJilles Tjoelker #endif 1379b50d902SRodney W. Grimes /* 1389b50d902SRodney W. Grimes * Basic algorithm is to scan the format string for conversion 1399b50d902SRodney W. Grimes * specifications -- once one is found, find out if the field 1409b50d902SRodney W. Grimes * width or precision is a '*'; if it is, gather up value. Note, 1419b50d902SRodney W. Grimes * format strings are reused as necessary to use up the provided 1429b50d902SRodney W. Grimes * arguments, arguments of zero/null string are provided to use 1439b50d902SRodney W. Grimes * up the format string. 1449b50d902SRodney W. Grimes */ 1453ec96cafSStefan Farfeleder fmt = format = *argv; 1463ec96cafSStefan Farfeleder chopped = escape(fmt, 1, &len); /* backslash interpretation */ 147fbd08684SStefan Farfeleder rval = end = 0; 1489b50d902SRodney W. Grimes gargv = ++argv; 1499b50d902SRodney W. Grimes for (;;) { 150fbd08684SStefan Farfeleder start = fmt; 1513ec96cafSStefan Farfeleder while (fmt < format + len) { 152fbd08684SStefan Farfeleder if (fmt[0] == '%') { 153fbd08684SStefan Farfeleder fwrite(start, 1, fmt - start, stdout); 154fbd08684SStefan Farfeleder if (fmt[1] == '%') { 1559b50d902SRodney W. Grimes /* %% prints a % */ 156fbd08684SStefan Farfeleder putchar('%'); 157fbd08684SStefan Farfeleder fmt += 2; 158fbd08684SStefan Farfeleder } else { 1599897c45fSJilles Tjoelker fmt = printf_doformat(fmt, &rval); 1609897c45fSJilles Tjoelker if (fmt == NULL) { 1619897c45fSJilles Tjoelker #ifdef SHELL 1629897c45fSJilles Tjoelker INTON; 1639897c45fSJilles Tjoelker #endif 164fbd08684SStefan Farfeleder return (1); 1659897c45fSJilles Tjoelker } 166fbd08684SStefan Farfeleder end = 0; 1679b50d902SRodney W. Grimes } 168fbd08684SStefan Farfeleder start = fmt; 169fbd08684SStefan Farfeleder } else 170fbd08684SStefan Farfeleder fmt++; 1719b50d902SRodney W. Grimes } 1729b50d902SRodney W. Grimes 173fbd08684SStefan Farfeleder if (end == 1) { 1746a6760dbSJilles Tjoelker warnx("missing format character"); 1759897c45fSJilles Tjoelker #ifdef SHELL 1769897c45fSJilles Tjoelker INTON; 1779897c45fSJilles Tjoelker #endif 178fbd08684SStefan Farfeleder return (1); 179fbd08684SStefan Farfeleder } 180fbd08684SStefan Farfeleder fwrite(start, 1, fmt - start, stdout); 1819897c45fSJilles Tjoelker if (chopped || !*gargv) { 1829897c45fSJilles Tjoelker #ifdef SHELL 1839897c45fSJilles Tjoelker INTON; 1849897c45fSJilles Tjoelker #endif 185fbd08684SStefan Farfeleder return (rval); 1869897c45fSJilles Tjoelker } 187fbd08684SStefan Farfeleder /* Restart at the beginning of the format string. */ 188fbd08684SStefan Farfeleder fmt = format; 189fbd08684SStefan Farfeleder end = 1; 190fbd08684SStefan Farfeleder } 191fbd08684SStefan Farfeleder /* NOTREACHED */ 192fbd08684SStefan Farfeleder } 193fbd08684SStefan Farfeleder 194fbd08684SStefan Farfeleder 195fbd08684SStefan Farfeleder static char * 1969897c45fSJilles Tjoelker printf_doformat(char *start, int *rval) 197fbd08684SStefan Farfeleder { 198fbd08684SStefan Farfeleder static const char skip1[] = "#'-+ 0"; 199fbd08684SStefan Farfeleder static const char skip2[] = "0123456789"; 200fbd08684SStefan Farfeleder char *fmt; 201fbd08684SStefan Farfeleder int fieldwidth, haveprec, havewidth, mod_ldbl, precision; 202fbd08684SStefan Farfeleder char convch, nextch; 203fbd08684SStefan Farfeleder 204fbd08684SStefan Farfeleder fmt = start + 1; 2059b50d902SRodney W. Grimes /* skip to field width */ 2060ba01198SStefan Farfeleder fmt += strspn(fmt, skip1); 2079b50d902SRodney W. Grimes if (*fmt == '*') { 2089b50d902SRodney W. Grimes if (getint(&fieldwidth)) 209fbd08684SStefan Farfeleder return (NULL); 21098dd6386STim J. Robbins havewidth = 1; 211d867cefdSJoerg Wunsch ++fmt; 212d867cefdSJoerg Wunsch } else { 21398dd6386STim J. Robbins havewidth = 0; 2149b50d902SRodney W. Grimes 2159b50d902SRodney W. Grimes /* skip to possible '.', get following precision */ 2160ba01198SStefan Farfeleder fmt += strspn(fmt, skip2); 217d867cefdSJoerg Wunsch } 218d867cefdSJoerg Wunsch if (*fmt == '.') { 219d867cefdSJoerg Wunsch /* precision present? */ 2209b50d902SRodney W. Grimes ++fmt; 2219b50d902SRodney W. Grimes if (*fmt == '*') { 2229b50d902SRodney W. Grimes if (getint(&precision)) 223fbd08684SStefan Farfeleder return (NULL); 22498dd6386STim J. Robbins haveprec = 1; 225d867cefdSJoerg Wunsch ++fmt; 226d867cefdSJoerg Wunsch } else { 22798dd6386STim J. Robbins haveprec = 0; 2289b50d902SRodney W. Grimes 2299b50d902SRodney W. Grimes /* skip to conversion char */ 2300ba01198SStefan Farfeleder fmt += strspn(fmt, skip2); 231d867cefdSJoerg Wunsch } 232d867cefdSJoerg Wunsch } else 23398dd6386STim J. Robbins haveprec = 0; 2349b50d902SRodney W. Grimes if (!*fmt) { 2356a6760dbSJilles Tjoelker warnx("missing format character"); 236fbd08684SStefan Farfeleder return (NULL); 2379b50d902SRodney W. Grimes } 2389b50d902SRodney W. Grimes 239fd757c50SDavid Schultz /* 240fd757c50SDavid Schultz * Look for a length modifier. POSIX doesn't have these, so 241fd757c50SDavid Schultz * we only support them for floating-point conversions, which 242fd757c50SDavid Schultz * are extensions. This is useful because the L modifier can 243fd757c50SDavid Schultz * be used to gain extra range and precision, while omitting 244fd757c50SDavid Schultz * it is more likely to produce consistent results on different 245fd757c50SDavid Schultz * architectures. This is not so important for integers 246fd757c50SDavid Schultz * because overflow is the only bad thing that can happen to 247fd757c50SDavid Schultz * them, but consider the command printf %a 1.1 248fd757c50SDavid Schultz */ 249fd757c50SDavid Schultz if (*fmt == 'L') { 250fd757c50SDavid Schultz mod_ldbl = 1; 251fd757c50SDavid Schultz fmt++; 252fd757c50SDavid Schultz if (!strchr("aAeEfFgG", *fmt)) { 2536a6760dbSJilles Tjoelker warnx("bad modifier L for %%%c", *fmt); 254fbd08684SStefan Farfeleder return (NULL); 255fd757c50SDavid Schultz } 256fd757c50SDavid Schultz } else { 257fd757c50SDavid Schultz mod_ldbl = 0; 258fd757c50SDavid Schultz } 259fd757c50SDavid Schultz 2609b50d902SRodney W. Grimes convch = *fmt; 2619b50d902SRodney W. Grimes nextch = *++fmt; 2629b50d902SRodney W. Grimes *fmt = '\0'; 2639b50d902SRodney W. Grimes switch (convch) { 264ab5a295bSJuli Mallett case 'b': { 2653ec96cafSStefan Farfeleder size_t len; 266ab5a295bSJuli Mallett char *p; 267ab5a295bSJuli Mallett int getout; 268ab5a295bSJuli Mallett 269bacab7d6STim J. Robbins p = strdup(getstr()); 270bacab7d6STim J. Robbins if (p == NULL) { 2716a6760dbSJilles Tjoelker warnx("%s", strerror(ENOMEM)); 272fbd08684SStefan Farfeleder return (NULL); 273bacab7d6STim J. Robbins } 2743ec96cafSStefan Farfeleder getout = escape(p, 0, &len); 275ab5a295bSJuli Mallett *(fmt - 1) = 's'; 276bacab7d6STim J. Robbins PF(start, p); 277ab5a295bSJuli Mallett *(fmt - 1) = 'b'; 278ab5a295bSJuli Mallett free(p); 279ab5a295bSJuli Mallett if (getout) 280fbd08684SStefan Farfeleder return (fmt); 281ab5a295bSJuli Mallett break; 282ab5a295bSJuli Mallett } 2839b50d902SRodney W. Grimes case 'c': { 2849b50d902SRodney W. Grimes char p; 2859b50d902SRodney W. Grimes 2869b50d902SRodney W. Grimes p = getchr(); 2879b50d902SRodney W. Grimes PF(start, p); 2889b50d902SRodney W. Grimes break; 2899b50d902SRodney W. Grimes } 2909b50d902SRodney W. Grimes case 's': { 29145af1a4cSDavid Malone const char *p; 2929b50d902SRodney W. Grimes 2939b50d902SRodney W. Grimes p = getstr(); 2949b50d902SRodney W. Grimes PF(start, p); 2959b50d902SRodney W. Grimes break; 2969b50d902SRodney W. Grimes } 2979b50d902SRodney W. Grimes case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': { 2989b50d902SRodney W. Grimes char *f; 299d41d23e1SStefan Farfeleder intmax_t val; 300d41d23e1SStefan Farfeleder uintmax_t uval; 301bacab7d6STim J. Robbins int signedconv; 3029b50d902SRodney W. Grimes 303bacab7d6STim J. Robbins signedconv = (convch == 'd' || convch == 'i'); 304d41d23e1SStefan Farfeleder if ((f = mknum(start, convch)) == NULL) 305fbd08684SStefan Farfeleder return (NULL); 306d41d23e1SStefan Farfeleder if (getnum(&val, &uval, signedconv)) 307fbd08684SStefan Farfeleder *rval = 1; 308bacab7d6STim J. Robbins if (signedconv) 309bacab7d6STim J. Robbins PF(f, val); 310bacab7d6STim J. Robbins else 311bacab7d6STim J. Robbins PF(f, uval); 3129b50d902SRodney W. Grimes break; 3139b50d902SRodney W. Grimes } 31403b2eaacSDavid Schultz case 'e': case 'E': 31503b2eaacSDavid Schultz case 'f': case 'F': 31603b2eaacSDavid Schultz case 'g': case 'G': 31703b2eaacSDavid Schultz case 'a': case 'A': { 318fd757c50SDavid Schultz long double p; 3199b50d902SRodney W. Grimes 320fd757c50SDavid Schultz if (getfloating(&p, mod_ldbl)) 321fbd08684SStefan Farfeleder *rval = 1; 322fd757c50SDavid Schultz if (mod_ldbl) 3239b50d902SRodney W. Grimes PF(start, p); 324fd757c50SDavid Schultz else 325fd757c50SDavid Schultz PF(start, (double)p); 3269b50d902SRodney W. Grimes break; 3279b50d902SRodney W. Grimes } 3289b50d902SRodney W. Grimes default: 3296a6760dbSJilles Tjoelker warnx("illegal format character %c", convch); 330fbd08684SStefan Farfeleder return (NULL); 3319b50d902SRodney W. Grimes } 3329b50d902SRodney W. Grimes *fmt = nextch; 333fbd08684SStefan Farfeleder return (fmt); 3349b50d902SRodney W. Grimes } 3359b50d902SRodney W. Grimes 3369b50d902SRodney W. Grimes static char * 3379180853bSXin LI mknum(char *str, char ch) 3389b50d902SRodney W. Grimes { 3393c6e4a5cSBen Smithurst static char *copy; 3403c6e4a5cSBen Smithurst static size_t copy_size; 3413c6e4a5cSBen Smithurst char *newcopy; 342bacab7d6STim J. Robbins size_t len, newlen; 3439b50d902SRodney W. Grimes 3449b50d902SRodney W. Grimes len = strlen(str) + 2; 3453c6e4a5cSBen Smithurst if (len > copy_size) { 3463c6e4a5cSBen Smithurst newlen = ((len + 1023) >> 10) << 10; 3473c6e4a5cSBen Smithurst if ((newcopy = realloc(copy, newlen)) == NULL) 348bacab7d6STim J. Robbins { 3496a6760dbSJilles Tjoelker warnx("%s", strerror(ENOMEM)); 3503c6e4a5cSBen Smithurst return (NULL); 351bacab7d6STim J. Robbins } 3523c6e4a5cSBen Smithurst copy = newcopy; 3533c6e4a5cSBen Smithurst copy_size = newlen; 3543c6e4a5cSBen Smithurst } 35562a721e7SStefan Eßer 3569b50d902SRodney W. Grimes memmove(copy, str, len - 3); 357d41d23e1SStefan Farfeleder copy[len - 3] = 'j'; 3589b50d902SRodney W. Grimes copy[len - 2] = ch; 3599b50d902SRodney W. Grimes copy[len - 1] = '\0'; 3609b50d902SRodney W. Grimes return (copy); 3619b50d902SRodney W. Grimes } 3629b50d902SRodney W. Grimes 363ab5a295bSJuli Mallett static int 3643ec96cafSStefan Farfeleder escape(char *fmt, int percent, size_t *len) 3659b50d902SRodney W. Grimes { 366ab71f271SPedro F. Giffuni char *save, *store, c; 367ab71f271SPedro F. Giffuni int value; 3689b50d902SRodney W. Grimes 369ab71f271SPedro F. Giffuni for (save = store = fmt; ((c = *fmt) != 0); ++fmt, ++store) { 3709b50d902SRodney W. Grimes if (c != '\\') { 3719b50d902SRodney W. Grimes *store = c; 3729b50d902SRodney W. Grimes continue; 3739b50d902SRodney W. Grimes } 3749b50d902SRodney W. Grimes switch (*++fmt) { 3759b50d902SRodney W. Grimes case '\0': /* EOS, user error */ 3769b50d902SRodney W. Grimes *store = '\\'; 3779b50d902SRodney W. Grimes *++store = '\0'; 3783ec96cafSStefan Farfeleder *len = store - save; 379ab5a295bSJuli Mallett return (0); 3809b50d902SRodney W. Grimes case '\\': /* backslash */ 3819b50d902SRodney W. Grimes case '\'': /* single quote */ 3829b50d902SRodney W. Grimes *store = *fmt; 3839b50d902SRodney W. Grimes break; 3849b50d902SRodney W. Grimes case 'a': /* bell/alert */ 385f3f148d2SStefan Farfeleder *store = '\a'; 3869b50d902SRodney W. Grimes break; 3879b50d902SRodney W. Grimes case 'b': /* backspace */ 3889b50d902SRodney W. Grimes *store = '\b'; 3899b50d902SRodney W. Grimes break; 390ab5a295bSJuli Mallett case 'c': 391ab5a295bSJuli Mallett *store = '\0'; 3923ec96cafSStefan Farfeleder *len = store - save; 393ab5a295bSJuli Mallett return (1); 3949b50d902SRodney W. Grimes case 'f': /* form-feed */ 3959b50d902SRodney W. Grimes *store = '\f'; 3969b50d902SRodney W. Grimes break; 3979b50d902SRodney W. Grimes case 'n': /* newline */ 3989b50d902SRodney W. Grimes *store = '\n'; 3999b50d902SRodney W. Grimes break; 4009b50d902SRodney W. Grimes case 'r': /* carriage-return */ 4019b50d902SRodney W. Grimes *store = '\r'; 4029b50d902SRodney W. Grimes break; 4039b50d902SRodney W. Grimes case 't': /* horizontal tab */ 4049b50d902SRodney W. Grimes *store = '\t'; 4059b50d902SRodney W. Grimes break; 4069b50d902SRodney W. Grimes case 'v': /* vertical tab */ 407f3f148d2SStefan Farfeleder *store = '\v'; 4089b50d902SRodney W. Grimes break; 4099b50d902SRodney W. Grimes /* octal constant */ 4109b50d902SRodney W. Grimes case '0': case '1': case '2': case '3': 4119b50d902SRodney W. Grimes case '4': case '5': case '6': case '7': 4129d65050eSDavid Schultz c = (!percent && *fmt == '0') ? 4 : 3; 4139d65050eSDavid Schultz for (value = 0; 4149b50d902SRodney W. Grimes c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) { 4159b50d902SRodney W. Grimes value <<= 3; 4169b50d902SRodney W. Grimes value += *fmt - '0'; 4179b50d902SRodney W. Grimes } 4189b50d902SRodney W. Grimes --fmt; 41912e8db40STim J. Robbins if (percent && value == '%') { 42037fd4590STim J. Robbins *store++ = '%'; 42137fd4590STim J. Robbins *store = '%'; 42237fd4590STim J. Robbins } else 423ab71f271SPedro F. Giffuni *store = (char)value; 4249b50d902SRodney W. Grimes break; 4259b50d902SRodney W. Grimes default: 4269b50d902SRodney W. Grimes *store = *fmt; 4279b50d902SRodney W. Grimes break; 4289b50d902SRodney W. Grimes } 4299b50d902SRodney W. Grimes } 4309b50d902SRodney W. Grimes *store = '\0'; 4313ec96cafSStefan Farfeleder *len = store - save; 432ab5a295bSJuli Mallett return (0); 4339b50d902SRodney W. Grimes } 4349b50d902SRodney W. Grimes 4359b50d902SRodney W. Grimes static int 436f4ac32deSDavid Malone getchr(void) 4379b50d902SRodney W. Grimes { 4389b50d902SRodney W. Grimes if (!*gargv) 4399b50d902SRodney W. Grimes return ('\0'); 4409b50d902SRodney W. Grimes return ((int)**gargv++); 4419b50d902SRodney W. Grimes } 4429b50d902SRodney W. Grimes 44345af1a4cSDavid Malone static const char * 444f4ac32deSDavid Malone getstr(void) 4459b50d902SRodney W. Grimes { 4469b50d902SRodney W. Grimes if (!*gargv) 4479b50d902SRodney W. Grimes return (""); 4489b50d902SRodney W. Grimes return (*gargv++); 4499b50d902SRodney W. Grimes } 4509b50d902SRodney W. Grimes 4519b50d902SRodney W. Grimes static int 452f4ac32deSDavid Malone getint(int *ip) 4539b50d902SRodney W. Grimes { 454d41d23e1SStefan Farfeleder intmax_t val; 455d41d23e1SStefan Farfeleder uintmax_t uval; 456bacab7d6STim J. Robbins int rval; 4579b50d902SRodney W. Grimes 458d41d23e1SStefan Farfeleder if (getnum(&val, &uval, 1)) 4599b50d902SRodney W. Grimes return (1); 460bacab7d6STim J. Robbins rval = 0; 461bacab7d6STim J. Robbins if (val < INT_MIN || val > INT_MAX) { 4626a6760dbSJilles Tjoelker warnx("%s: %s", *gargv, strerror(ERANGE)); 463bacab7d6STim J. Robbins rval = 1; 464bacab7d6STim J. Robbins } 46562a721e7SStefan Eßer *ip = (int)val; 466bacab7d6STim J. Robbins return (rval); 4679b50d902SRodney W. Grimes } 4689b50d902SRodney W. Grimes 4699b50d902SRodney W. Grimes static int 470d41d23e1SStefan Farfeleder getnum(intmax_t *ip, uintmax_t *uip, int signedconv) 4719b50d902SRodney W. Grimes { 4729b50d902SRodney W. Grimes char *ep; 473bacab7d6STim J. Robbins int rval; 4749b50d902SRodney W. Grimes 4759b50d902SRodney W. Grimes if (!*gargv) { 476d41d23e1SStefan Farfeleder *ip = 0; 4779b50d902SRodney W. Grimes return (0); 4789b50d902SRodney W. Grimes } 479ab5a295bSJuli Mallett if (**gargv == '"' || **gargv == '\'') { 480bacab7d6STim J. Robbins if (signedconv) 481d41d23e1SStefan Farfeleder *ip = asciicode(); 482bacab7d6STim J. Robbins else 483d41d23e1SStefan Farfeleder *uip = asciicode(); 4849b50d902SRodney W. Grimes return (0); 4859b50d902SRodney W. Grimes } 486bacab7d6STim J. Robbins rval = 0; 487ab5a295bSJuli Mallett errno = 0; 488bacab7d6STim J. Robbins if (signedconv) 489d41d23e1SStefan Farfeleder *ip = strtoimax(*gargv, &ep, 0); 490bacab7d6STim J. Robbins else 491d41d23e1SStefan Farfeleder *uip = strtoumax(*gargv, &ep, 0); 492bacab7d6STim J. Robbins if (ep == *gargv) { 4936a6760dbSJilles Tjoelker warnx("%s: expected numeric value", *gargv); 494bacab7d6STim J. Robbins rval = 1; 495bacab7d6STim J. Robbins } 496bacab7d6STim J. Robbins else if (*ep != '\0') { 4976a6760dbSJilles Tjoelker warnx("%s: not completely converted", *gargv); 498bacab7d6STim J. Robbins rval = 1; 499bacab7d6STim J. Robbins } 500bacab7d6STim J. Robbins if (errno == ERANGE) { 5016a6760dbSJilles Tjoelker warnx("%s: %s", *gargv, strerror(ERANGE)); 502bacab7d6STim J. Robbins rval = 1; 503bacab7d6STim J. Robbins } 504ab5a295bSJuli Mallett ++gargv; 505bacab7d6STim J. Robbins return (rval); 5069b50d902SRodney W. Grimes } 5079b50d902SRodney W. Grimes 508bacab7d6STim J. Robbins static int 509fd757c50SDavid Schultz getfloating(long double *dp, int mod_ldbl) 5109b50d902SRodney W. Grimes { 511ab5a295bSJuli Mallett char *ep; 512bacab7d6STim J. Robbins int rval; 513ab5a295bSJuli Mallett 5145ec2b8dcSStefan Farfeleder if (!*gargv) { 5155ec2b8dcSStefan Farfeleder *dp = 0.0; 516bacab7d6STim J. Robbins return (0); 5175ec2b8dcSStefan Farfeleder } 518ab5a295bSJuli Mallett if (**gargv == '"' || **gargv == '\'') { 519bacab7d6STim J. Robbins *dp = asciicode(); 520bacab7d6STim J. Robbins return (0); 521ab5a295bSJuli Mallett } 5228c423a99SColin Percival rval = 0; 523ab5a295bSJuli Mallett errno = 0; 524fd757c50SDavid Schultz if (mod_ldbl) 525fd757c50SDavid Schultz *dp = strtold(*gargv, &ep); 526fd757c50SDavid Schultz else 527bacab7d6STim J. Robbins *dp = strtod(*gargv, &ep); 528bacab7d6STim J. Robbins if (ep == *gargv) { 5296a6760dbSJilles Tjoelker warnx("%s: expected numeric value", *gargv); 530bacab7d6STim J. Robbins rval = 1; 531bacab7d6STim J. Robbins } else if (*ep != '\0') { 5326a6760dbSJilles Tjoelker warnx("%s: not completely converted", *gargv); 533bacab7d6STim J. Robbins rval = 1; 534bacab7d6STim J. Robbins } 535bacab7d6STim J. Robbins if (errno == ERANGE) { 5366a6760dbSJilles Tjoelker warnx("%s: %s", *gargv, strerror(ERANGE)); 537bacab7d6STim J. Robbins rval = 1; 538bacab7d6STim J. Robbins } 539ab5a295bSJuli Mallett ++gargv; 540bacab7d6STim J. Robbins return (rval); 5419b50d902SRodney W. Grimes } 5429b50d902SRodney W. Grimes 5439b50d902SRodney W. Grimes static int 544f4ac32deSDavid Malone asciicode(void) 5459b50d902SRodney W. Grimes { 546f4ac32deSDavid Malone int ch; 54798102dabSJilles Tjoelker wchar_t wch; 54898102dabSJilles Tjoelker mbstate_t mbs; 5499b50d902SRodney W. Grimes 55098102dabSJilles Tjoelker ch = (unsigned char)**gargv; 55198102dabSJilles Tjoelker if (ch == '\'' || ch == '"') { 55298102dabSJilles Tjoelker memset(&mbs, 0, sizeof(mbs)); 55398102dabSJilles Tjoelker switch (mbrtowc(&wch, *gargv + 1, MB_LEN_MAX, &mbs)) { 55498102dabSJilles Tjoelker case (size_t)-2: 55598102dabSJilles Tjoelker case (size_t)-1: 55698102dabSJilles Tjoelker wch = (unsigned char)gargv[0][1]; 55798102dabSJilles Tjoelker break; 55898102dabSJilles Tjoelker case 0: 55998102dabSJilles Tjoelker wch = 0; 56098102dabSJilles Tjoelker break; 56198102dabSJilles Tjoelker } 56298102dabSJilles Tjoelker ch = wch; 56398102dabSJilles Tjoelker } 5649b50d902SRodney W. Grimes ++gargv; 5659b50d902SRodney W. Grimes return (ch); 5669b50d902SRodney W. Grimes } 5679b50d902SRodney W. Grimes 5689b50d902SRodney W. Grimes static void 569f4ac32deSDavid Malone usage(void) 5709b50d902SRodney W. Grimes { 571f682f10cSRuslan Ermilov (void)fprintf(stderr, "usage: printf format [arguments ...]\n"); 5729b50d902SRodney W. Grimes } 573