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 */ 299b50d902SRodney W. Grimes 301866e8abSJilles Tjoelker #ifndef SHELL 319b50d902SRodney W. Grimes #ifndef lint 323ec30b79SSteve Price static char const copyright[] = 339b50d902SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\ 349b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 359b50d902SRodney W. Grimes #endif /* not lint */ 369b50d902SRodney W. Grimes #endif 379b50d902SRodney W. Grimes 389b50d902SRodney W. Grimes #ifndef lint 39c9e05349SWarner Losh #if 0 403ec30b79SSteve Price static char const sccsid[] = "@(#)printf.c 8.1 (Berkeley) 7/20/93"; 41c9e05349SWarner Losh #endif 421ea7321bSMartin Cracauer static const char rcsid[] = 431ea7321bSMartin Cracauer "$FreeBSD$"; 449b50d902SRodney W. Grimes #endif /* not lint */ 459b50d902SRodney W. Grimes 469b50d902SRodney W. Grimes #include <sys/types.h> 479b50d902SRodney W. Grimes 489b50d902SRodney W. Grimes #include <err.h> 499b50d902SRodney W. Grimes #include <errno.h> 50d41d23e1SStefan Farfeleder #include <inttypes.h> 519b50d902SRodney W. Grimes #include <limits.h> 52c9aca921SXin LI #include <locale.h> 539b50d902SRodney W. Grimes #include <stdio.h> 549b50d902SRodney W. Grimes #include <stdlib.h> 559b50d902SRodney W. Grimes #include <string.h> 569d19feb5SSteve Price #include <unistd.h> 579b50d902SRodney W. Grimes 589b50d902SRodney W. Grimes #ifdef SHELL 599b50d902SRodney W. Grimes #define main printfcmd 60d9f93710SJoerg Wunsch #include "bltin/bltin.h" 613c6e4a5cSBen Smithurst #include "memalloc.h" 629897c45fSJilles Tjoelker #include "error.h" 639b50d902SRodney W. Grimes #endif 649b50d902SRodney W. Grimes 65bacab7d6STim J. Robbins #define PF(f, func) do { \ 66d72f654cSPeter Wemm char *b = NULL; \ 6798dd6386STim J. Robbins if (havewidth) \ 6898dd6386STim J. Robbins if (haveprec) \ 69d72f654cSPeter Wemm (void)asprintf(&b, f, fieldwidth, precision, func); \ 709b50d902SRodney W. Grimes else \ 71d72f654cSPeter Wemm (void)asprintf(&b, f, fieldwidth, func); \ 7298dd6386STim J. Robbins else if (haveprec) \ 73d72f654cSPeter Wemm (void)asprintf(&b, f, precision, func); \ 749b50d902SRodney W. Grimes else \ 75d72f654cSPeter Wemm (void)asprintf(&b, f, func); \ 76d72f654cSPeter Wemm if (b) { \ 77d72f654cSPeter Wemm (void)fputs(b, stdout); \ 78d72f654cSPeter Wemm free(b); \ 79d72f654cSPeter Wemm } \ 80bacab7d6STim J. Robbins } while (0) 819b50d902SRodney W. Grimes 82d3cb5dedSWarner Losh static int asciicode(void); 839897c45fSJilles Tjoelker static char *printf_doformat(char *, int *); 843ec96cafSStefan Farfeleder static int escape(char *, int, size_t *); 85d3cb5dedSWarner Losh static int getchr(void); 86fd757c50SDavid Schultz static int getfloating(long double *, int); 87d3cb5dedSWarner Losh static int getint(int *); 88d41d23e1SStefan Farfeleder static int getnum(intmax_t *, uintmax_t *, int); 89bacab7d6STim J. Robbins static const char 90bacab7d6STim J. Robbins *getstr(void); 919180853bSXin LI static char *mknum(char *, char); 92d3cb5dedSWarner Losh static void usage(void); 939b50d902SRodney W. Grimes 949b50d902SRodney W. Grimes static char **gargv; 959b50d902SRodney W. Grimes 969b50d902SRodney W. Grimes int 97f4ac32deSDavid Malone main(int argc, char *argv[]) 989b50d902SRodney W. Grimes { 993ec96cafSStefan Farfeleder size_t len; 100de46de4dSXin LI int ch, chopped, end, rval; 101fbd08684SStefan Farfeleder char *format, *fmt, *start; 1029b50d902SRodney W. Grimes 1031866e8abSJilles Tjoelker #ifndef SHELL 10469be0c5eSXin LI (void) setlocale(LC_ALL, ""); 105dc7d8c99SAndrey A. Chernov #endif 1069897c45fSJilles Tjoelker #ifdef SHELL 1079897c45fSJilles Tjoelker optreset = 1; optind = 1; opterr = 0; /* initialize getopt */ 1089897c45fSJilles Tjoelker #endif 109de46de4dSXin LI while ((ch = getopt(argc, argv, "")) != -1) 110de46de4dSXin LI switch (ch) { 111de46de4dSXin LI case '?': 112de46de4dSXin LI default: 113de46de4dSXin LI usage(); 114de46de4dSXin LI return (1); 1159b50d902SRodney W. Grimes } 116de46de4dSXin LI argc -= optind; 117de46de4dSXin LI argv += optind; 1189b50d902SRodney W. Grimes 1199b50d902SRodney W. Grimes if (argc < 1) { 1209b50d902SRodney W. Grimes usage(); 1215eccc000SXin LI return (1); 1229b50d902SRodney W. Grimes } 1239b50d902SRodney W. Grimes 1249897c45fSJilles Tjoelker #ifdef SHELL 1259897c45fSJilles Tjoelker INTOFF; 1269897c45fSJilles Tjoelker #endif 1279b50d902SRodney W. Grimes /* 1289b50d902SRodney W. Grimes * Basic algorithm is to scan the format string for conversion 1299b50d902SRodney W. Grimes * specifications -- once one is found, find out if the field 1309b50d902SRodney W. Grimes * width or precision is a '*'; if it is, gather up value. Note, 1319b50d902SRodney W. Grimes * format strings are reused as necessary to use up the provided 1329b50d902SRodney W. Grimes * arguments, arguments of zero/null string are provided to use 1339b50d902SRodney W. Grimes * up the format string. 1349b50d902SRodney W. Grimes */ 1353ec96cafSStefan Farfeleder fmt = format = *argv; 1363ec96cafSStefan Farfeleder chopped = escape(fmt, 1, &len); /* backslash interpretation */ 137fbd08684SStefan Farfeleder rval = end = 0; 1389b50d902SRodney W. Grimes gargv = ++argv; 1399b50d902SRodney W. Grimes for (;;) { 140fbd08684SStefan Farfeleder start = fmt; 1413ec96cafSStefan Farfeleder while (fmt < format + len) { 142fbd08684SStefan Farfeleder if (fmt[0] == '%') { 143fbd08684SStefan Farfeleder fwrite(start, 1, fmt - start, stdout); 144fbd08684SStefan Farfeleder if (fmt[1] == '%') { 1459b50d902SRodney W. Grimes /* %% prints a % */ 146fbd08684SStefan Farfeleder putchar('%'); 147fbd08684SStefan Farfeleder fmt += 2; 148fbd08684SStefan Farfeleder } else { 1499897c45fSJilles Tjoelker fmt = printf_doformat(fmt, &rval); 1509897c45fSJilles Tjoelker if (fmt == NULL) { 1519897c45fSJilles Tjoelker #ifdef SHELL 1529897c45fSJilles Tjoelker INTON; 1539897c45fSJilles Tjoelker #endif 154fbd08684SStefan Farfeleder return (1); 1559897c45fSJilles Tjoelker } 156fbd08684SStefan Farfeleder end = 0; 1579b50d902SRodney W. Grimes } 158fbd08684SStefan Farfeleder start = fmt; 159fbd08684SStefan Farfeleder } else 160fbd08684SStefan Farfeleder fmt++; 1619b50d902SRodney W. Grimes } 1629b50d902SRodney W. Grimes 163fbd08684SStefan Farfeleder if (end == 1) { 164*6a6760dbSJilles Tjoelker warnx("missing format character"); 1659897c45fSJilles Tjoelker #ifdef SHELL 1669897c45fSJilles Tjoelker INTON; 1679897c45fSJilles Tjoelker #endif 168fbd08684SStefan Farfeleder return (1); 169fbd08684SStefan Farfeleder } 170fbd08684SStefan Farfeleder fwrite(start, 1, fmt - start, stdout); 1719897c45fSJilles Tjoelker if (chopped || !*gargv) { 1729897c45fSJilles Tjoelker #ifdef SHELL 1739897c45fSJilles Tjoelker INTON; 1749897c45fSJilles Tjoelker #endif 175fbd08684SStefan Farfeleder return (rval); 1769897c45fSJilles Tjoelker } 177fbd08684SStefan Farfeleder /* Restart at the beginning of the format string. */ 178fbd08684SStefan Farfeleder fmt = format; 179fbd08684SStefan Farfeleder end = 1; 180fbd08684SStefan Farfeleder } 181fbd08684SStefan Farfeleder /* NOTREACHED */ 182fbd08684SStefan Farfeleder } 183fbd08684SStefan Farfeleder 184fbd08684SStefan Farfeleder 185fbd08684SStefan Farfeleder static char * 1869897c45fSJilles Tjoelker printf_doformat(char *start, int *rval) 187fbd08684SStefan Farfeleder { 188fbd08684SStefan Farfeleder static const char skip1[] = "#'-+ 0"; 189fbd08684SStefan Farfeleder static const char skip2[] = "0123456789"; 190fbd08684SStefan Farfeleder char *fmt; 191fbd08684SStefan Farfeleder int fieldwidth, haveprec, havewidth, mod_ldbl, precision; 192fbd08684SStefan Farfeleder char convch, nextch; 193fbd08684SStefan Farfeleder 194fbd08684SStefan Farfeleder fmt = start + 1; 1959b50d902SRodney W. Grimes /* skip to field width */ 1960ba01198SStefan Farfeleder fmt += strspn(fmt, skip1); 1979b50d902SRodney W. Grimes if (*fmt == '*') { 1989b50d902SRodney W. Grimes if (getint(&fieldwidth)) 199fbd08684SStefan Farfeleder return (NULL); 20098dd6386STim J. Robbins havewidth = 1; 201d867cefdSJoerg Wunsch ++fmt; 202d867cefdSJoerg Wunsch } else { 20398dd6386STim J. Robbins havewidth = 0; 2049b50d902SRodney W. Grimes 2059b50d902SRodney W. Grimes /* skip to possible '.', get following precision */ 2060ba01198SStefan Farfeleder fmt += strspn(fmt, skip2); 207d867cefdSJoerg Wunsch } 208d867cefdSJoerg Wunsch if (*fmt == '.') { 209d867cefdSJoerg Wunsch /* precision present? */ 2109b50d902SRodney W. Grimes ++fmt; 2119b50d902SRodney W. Grimes if (*fmt == '*') { 2129b50d902SRodney W. Grimes if (getint(&precision)) 213fbd08684SStefan Farfeleder return (NULL); 21498dd6386STim J. Robbins haveprec = 1; 215d867cefdSJoerg Wunsch ++fmt; 216d867cefdSJoerg Wunsch } else { 21798dd6386STim J. Robbins haveprec = 0; 2189b50d902SRodney W. Grimes 2199b50d902SRodney W. Grimes /* skip to conversion char */ 2200ba01198SStefan Farfeleder fmt += strspn(fmt, skip2); 221d867cefdSJoerg Wunsch } 222d867cefdSJoerg Wunsch } else 22398dd6386STim J. Robbins haveprec = 0; 2249b50d902SRodney W. Grimes if (!*fmt) { 225*6a6760dbSJilles Tjoelker warnx("missing format character"); 226fbd08684SStefan Farfeleder return (NULL); 2279b50d902SRodney W. Grimes } 2289b50d902SRodney W. Grimes 229fd757c50SDavid Schultz /* 230fd757c50SDavid Schultz * Look for a length modifier. POSIX doesn't have these, so 231fd757c50SDavid Schultz * we only support them for floating-point conversions, which 232fd757c50SDavid Schultz * are extensions. This is useful because the L modifier can 233fd757c50SDavid Schultz * be used to gain extra range and precision, while omitting 234fd757c50SDavid Schultz * it is more likely to produce consistent results on different 235fd757c50SDavid Schultz * architectures. This is not so important for integers 236fd757c50SDavid Schultz * because overflow is the only bad thing that can happen to 237fd757c50SDavid Schultz * them, but consider the command printf %a 1.1 238fd757c50SDavid Schultz */ 239fd757c50SDavid Schultz if (*fmt == 'L') { 240fd757c50SDavid Schultz mod_ldbl = 1; 241fd757c50SDavid Schultz fmt++; 242fd757c50SDavid Schultz if (!strchr("aAeEfFgG", *fmt)) { 243*6a6760dbSJilles Tjoelker warnx("bad modifier L for %%%c", *fmt); 244fbd08684SStefan Farfeleder return (NULL); 245fd757c50SDavid Schultz } 246fd757c50SDavid Schultz } else { 247fd757c50SDavid Schultz mod_ldbl = 0; 248fd757c50SDavid Schultz } 249fd757c50SDavid Schultz 2509b50d902SRodney W. Grimes convch = *fmt; 2519b50d902SRodney W. Grimes nextch = *++fmt; 2529b50d902SRodney W. Grimes *fmt = '\0'; 2539b50d902SRodney W. Grimes switch (convch) { 254ab5a295bSJuli Mallett case 'b': { 2553ec96cafSStefan Farfeleder size_t len; 256ab5a295bSJuli Mallett char *p; 257ab5a295bSJuli Mallett int getout; 258ab5a295bSJuli Mallett 259bacab7d6STim J. Robbins #ifdef SHELL 260bacab7d6STim J. Robbins p = savestr(getstr()); 261bacab7d6STim J. Robbins #else 262bacab7d6STim J. Robbins p = strdup(getstr()); 263bacab7d6STim J. Robbins #endif 264bacab7d6STim J. Robbins if (p == NULL) { 265*6a6760dbSJilles Tjoelker warnx("%s", strerror(ENOMEM)); 266fbd08684SStefan Farfeleder return (NULL); 267bacab7d6STim J. Robbins } 2683ec96cafSStefan Farfeleder getout = escape(p, 0, &len); 269ab5a295bSJuli Mallett *(fmt - 1) = 's'; 270bacab7d6STim J. Robbins PF(start, p); 271ab5a295bSJuli Mallett *(fmt - 1) = 'b'; 272bacab7d6STim J. Robbins #ifdef SHELL 273bacab7d6STim J. Robbins ckfree(p); 274bacab7d6STim J. Robbins #else 275ab5a295bSJuli Mallett free(p); 276bacab7d6STim J. Robbins #endif 277ab5a295bSJuli Mallett if (getout) 278fbd08684SStefan Farfeleder return (fmt); 279ab5a295bSJuli Mallett break; 280ab5a295bSJuli Mallett } 2819b50d902SRodney W. Grimes case 'c': { 2829b50d902SRodney W. Grimes char p; 2839b50d902SRodney W. Grimes 2849b50d902SRodney W. Grimes p = getchr(); 2859b50d902SRodney W. Grimes PF(start, p); 2869b50d902SRodney W. Grimes break; 2879b50d902SRodney W. Grimes } 2889b50d902SRodney W. Grimes case 's': { 28945af1a4cSDavid Malone const char *p; 2909b50d902SRodney W. Grimes 2919b50d902SRodney W. Grimes p = getstr(); 2929b50d902SRodney W. Grimes PF(start, p); 2939b50d902SRodney W. Grimes break; 2949b50d902SRodney W. Grimes } 2959b50d902SRodney W. Grimes case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': { 2969b50d902SRodney W. Grimes char *f; 297d41d23e1SStefan Farfeleder intmax_t val; 298d41d23e1SStefan Farfeleder uintmax_t uval; 299bacab7d6STim J. Robbins int signedconv; 3009b50d902SRodney W. Grimes 301bacab7d6STim J. Robbins signedconv = (convch == 'd' || convch == 'i'); 302d41d23e1SStefan Farfeleder if ((f = mknum(start, convch)) == NULL) 303fbd08684SStefan Farfeleder return (NULL); 304d41d23e1SStefan Farfeleder if (getnum(&val, &uval, signedconv)) 305fbd08684SStefan Farfeleder *rval = 1; 306bacab7d6STim J. Robbins if (signedconv) 307bacab7d6STim J. Robbins PF(f, val); 308bacab7d6STim J. Robbins else 309bacab7d6STim J. Robbins PF(f, uval); 3109b50d902SRodney W. Grimes break; 3119b50d902SRodney W. Grimes } 31203b2eaacSDavid Schultz case 'e': case 'E': 31303b2eaacSDavid Schultz case 'f': case 'F': 31403b2eaacSDavid Schultz case 'g': case 'G': 31503b2eaacSDavid Schultz case 'a': case 'A': { 316fd757c50SDavid Schultz long double p; 3179b50d902SRodney W. Grimes 318fd757c50SDavid Schultz if (getfloating(&p, mod_ldbl)) 319fbd08684SStefan Farfeleder *rval = 1; 320fd757c50SDavid Schultz if (mod_ldbl) 3219b50d902SRodney W. Grimes PF(start, p); 322fd757c50SDavid Schultz else 323fd757c50SDavid Schultz PF(start, (double)p); 3249b50d902SRodney W. Grimes break; 3259b50d902SRodney W. Grimes } 3269b50d902SRodney W. Grimes default: 327*6a6760dbSJilles Tjoelker warnx("illegal format character %c", convch); 328fbd08684SStefan Farfeleder return (NULL); 3299b50d902SRodney W. Grimes } 3309b50d902SRodney W. Grimes *fmt = nextch; 331fbd08684SStefan Farfeleder return (fmt); 3329b50d902SRodney W. Grimes } 3339b50d902SRodney W. Grimes 3349b50d902SRodney W. Grimes static char * 3359180853bSXin LI mknum(char *str, char ch) 3369b50d902SRodney W. Grimes { 3373c6e4a5cSBen Smithurst static char *copy; 3383c6e4a5cSBen Smithurst static size_t copy_size; 3393c6e4a5cSBen Smithurst char *newcopy; 340bacab7d6STim J. Robbins size_t len, newlen; 3419b50d902SRodney W. Grimes 3429b50d902SRodney W. Grimes len = strlen(str) + 2; 3433c6e4a5cSBen Smithurst if (len > copy_size) { 3443c6e4a5cSBen Smithurst newlen = ((len + 1023) >> 10) << 10; 3453c6e4a5cSBen Smithurst #ifdef SHELL 3463c6e4a5cSBen Smithurst if ((newcopy = ckrealloc(copy, newlen)) == NULL) 3473c6e4a5cSBen Smithurst #else 3483c6e4a5cSBen Smithurst if ((newcopy = realloc(copy, newlen)) == NULL) 3493c6e4a5cSBen Smithurst #endif 350bacab7d6STim J. Robbins { 351*6a6760dbSJilles Tjoelker warnx("%s", strerror(ENOMEM)); 3523c6e4a5cSBen Smithurst return (NULL); 353bacab7d6STim J. Robbins } 3543c6e4a5cSBen Smithurst copy = newcopy; 3553c6e4a5cSBen Smithurst copy_size = newlen; 3563c6e4a5cSBen Smithurst } 35762a721e7SStefan Eßer 3589b50d902SRodney W. Grimes memmove(copy, str, len - 3); 359d41d23e1SStefan Farfeleder copy[len - 3] = 'j'; 3609b50d902SRodney W. Grimes copy[len - 2] = ch; 3619b50d902SRodney W. Grimes copy[len - 1] = '\0'; 3629b50d902SRodney W. Grimes return (copy); 3639b50d902SRodney W. Grimes } 3649b50d902SRodney W. Grimes 365ab5a295bSJuli Mallett static int 3663ec96cafSStefan Farfeleder escape(char *fmt, int percent, size_t *len) 3679b50d902SRodney W. Grimes { 3683ec96cafSStefan Farfeleder char *save, *store; 369f4ac32deSDavid Malone int value, c; 3709b50d902SRodney W. Grimes 3713ec96cafSStefan Farfeleder for (save = store = fmt; (c = *fmt); ++fmt, ++store) { 3729b50d902SRodney W. Grimes if (c != '\\') { 3739b50d902SRodney W. Grimes *store = c; 3749b50d902SRodney W. Grimes continue; 3759b50d902SRodney W. Grimes } 3769b50d902SRodney W. Grimes switch (*++fmt) { 3779b50d902SRodney W. Grimes case '\0': /* EOS, user error */ 3789b50d902SRodney W. Grimes *store = '\\'; 3799b50d902SRodney W. Grimes *++store = '\0'; 3803ec96cafSStefan Farfeleder *len = store - save; 381ab5a295bSJuli Mallett return (0); 3829b50d902SRodney W. Grimes case '\\': /* backslash */ 3839b50d902SRodney W. Grimes case '\'': /* single quote */ 3849b50d902SRodney W. Grimes *store = *fmt; 3859b50d902SRodney W. Grimes break; 3869b50d902SRodney W. Grimes case 'a': /* bell/alert */ 387f3f148d2SStefan Farfeleder *store = '\a'; 3889b50d902SRodney W. Grimes break; 3899b50d902SRodney W. Grimes case 'b': /* backspace */ 3909b50d902SRodney W. Grimes *store = '\b'; 3919b50d902SRodney W. Grimes break; 392ab5a295bSJuli Mallett case 'c': 393ab5a295bSJuli Mallett *store = '\0'; 3943ec96cafSStefan Farfeleder *len = store - save; 395ab5a295bSJuli Mallett return (1); 3969b50d902SRodney W. Grimes case 'f': /* form-feed */ 3979b50d902SRodney W. Grimes *store = '\f'; 3989b50d902SRodney W. Grimes break; 3999b50d902SRodney W. Grimes case 'n': /* newline */ 4009b50d902SRodney W. Grimes *store = '\n'; 4019b50d902SRodney W. Grimes break; 4029b50d902SRodney W. Grimes case 'r': /* carriage-return */ 4039b50d902SRodney W. Grimes *store = '\r'; 4049b50d902SRodney W. Grimes break; 4059b50d902SRodney W. Grimes case 't': /* horizontal tab */ 4069b50d902SRodney W. Grimes *store = '\t'; 4079b50d902SRodney W. Grimes break; 4089b50d902SRodney W. Grimes case 'v': /* vertical tab */ 409f3f148d2SStefan Farfeleder *store = '\v'; 4109b50d902SRodney W. Grimes break; 4119b50d902SRodney W. Grimes /* octal constant */ 4129b50d902SRodney W. Grimes case '0': case '1': case '2': case '3': 4139b50d902SRodney W. Grimes case '4': case '5': case '6': case '7': 4149d65050eSDavid Schultz c = (!percent && *fmt == '0') ? 4 : 3; 4159d65050eSDavid Schultz for (value = 0; 4169b50d902SRodney W. Grimes c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) { 4179b50d902SRodney W. Grimes value <<= 3; 4189b50d902SRodney W. Grimes value += *fmt - '0'; 4199b50d902SRodney W. Grimes } 4209b50d902SRodney W. Grimes --fmt; 42112e8db40STim J. Robbins if (percent && value == '%') { 42237fd4590STim J. Robbins *store++ = '%'; 42337fd4590STim J. Robbins *store = '%'; 42437fd4590STim J. Robbins } else 4259b50d902SRodney W. Grimes *store = value; 4269b50d902SRodney W. Grimes break; 4279b50d902SRodney W. Grimes default: 4289b50d902SRodney W. Grimes *store = *fmt; 4299b50d902SRodney W. Grimes break; 4309b50d902SRodney W. Grimes } 4319b50d902SRodney W. Grimes } 4329b50d902SRodney W. Grimes *store = '\0'; 4333ec96cafSStefan Farfeleder *len = store - save; 434ab5a295bSJuli Mallett return (0); 4359b50d902SRodney W. Grimes } 4369b50d902SRodney W. Grimes 4379b50d902SRodney W. Grimes static int 438f4ac32deSDavid Malone getchr(void) 4399b50d902SRodney W. Grimes { 4409b50d902SRodney W. Grimes if (!*gargv) 4419b50d902SRodney W. Grimes return ('\0'); 4429b50d902SRodney W. Grimes return ((int)**gargv++); 4439b50d902SRodney W. Grimes } 4449b50d902SRodney W. Grimes 44545af1a4cSDavid Malone static const char * 446f4ac32deSDavid Malone getstr(void) 4479b50d902SRodney W. Grimes { 4489b50d902SRodney W. Grimes if (!*gargv) 4499b50d902SRodney W. Grimes return (""); 4509b50d902SRodney W. Grimes return (*gargv++); 4519b50d902SRodney W. Grimes } 4529b50d902SRodney W. Grimes 4539b50d902SRodney W. Grimes static int 454f4ac32deSDavid Malone getint(int *ip) 4559b50d902SRodney W. Grimes { 456d41d23e1SStefan Farfeleder intmax_t val; 457d41d23e1SStefan Farfeleder uintmax_t uval; 458bacab7d6STim J. Robbins int rval; 4599b50d902SRodney W. Grimes 460d41d23e1SStefan Farfeleder if (getnum(&val, &uval, 1)) 4619b50d902SRodney W. Grimes return (1); 462bacab7d6STim J. Robbins rval = 0; 463bacab7d6STim J. Robbins if (val < INT_MIN || val > INT_MAX) { 464*6a6760dbSJilles Tjoelker warnx("%s: %s", *gargv, strerror(ERANGE)); 465bacab7d6STim J. Robbins rval = 1; 466bacab7d6STim J. Robbins } 46762a721e7SStefan Eßer *ip = (int)val; 468bacab7d6STim J. Robbins return (rval); 4699b50d902SRodney W. Grimes } 4709b50d902SRodney W. Grimes 4719b50d902SRodney W. Grimes static int 472d41d23e1SStefan Farfeleder getnum(intmax_t *ip, uintmax_t *uip, int signedconv) 4739b50d902SRodney W. Grimes { 4749b50d902SRodney W. Grimes char *ep; 475bacab7d6STim J. Robbins int rval; 4769b50d902SRodney W. Grimes 4779b50d902SRodney W. Grimes if (!*gargv) { 478d41d23e1SStefan Farfeleder *ip = 0; 4799b50d902SRodney W. Grimes return (0); 4809b50d902SRodney W. Grimes } 481ab5a295bSJuli Mallett if (**gargv == '"' || **gargv == '\'') { 482bacab7d6STim J. Robbins if (signedconv) 483d41d23e1SStefan Farfeleder *ip = asciicode(); 484bacab7d6STim J. Robbins else 485d41d23e1SStefan Farfeleder *uip = asciicode(); 4869b50d902SRodney W. Grimes return (0); 4879b50d902SRodney W. Grimes } 488bacab7d6STim J. Robbins rval = 0; 489ab5a295bSJuli Mallett errno = 0; 490bacab7d6STim J. Robbins if (signedconv) 491d41d23e1SStefan Farfeleder *ip = strtoimax(*gargv, &ep, 0); 492bacab7d6STim J. Robbins else 493d41d23e1SStefan Farfeleder *uip = strtoumax(*gargv, &ep, 0); 494bacab7d6STim J. Robbins if (ep == *gargv) { 495*6a6760dbSJilles Tjoelker warnx("%s: expected numeric value", *gargv); 496bacab7d6STim J. Robbins rval = 1; 497bacab7d6STim J. Robbins } 498bacab7d6STim J. Robbins else if (*ep != '\0') { 499*6a6760dbSJilles Tjoelker warnx("%s: not completely converted", *gargv); 500bacab7d6STim J. Robbins rval = 1; 501bacab7d6STim J. Robbins } 502bacab7d6STim J. Robbins if (errno == ERANGE) { 503*6a6760dbSJilles Tjoelker warnx("%s: %s", *gargv, strerror(ERANGE)); 504bacab7d6STim J. Robbins rval = 1; 505bacab7d6STim J. Robbins } 506ab5a295bSJuli Mallett ++gargv; 507bacab7d6STim J. Robbins return (rval); 5089b50d902SRodney W. Grimes } 5099b50d902SRodney W. Grimes 510bacab7d6STim J. Robbins static int 511fd757c50SDavid Schultz getfloating(long double *dp, int mod_ldbl) 5129b50d902SRodney W. Grimes { 513ab5a295bSJuli Mallett char *ep; 514bacab7d6STim J. Robbins int rval; 515ab5a295bSJuli Mallett 5165ec2b8dcSStefan Farfeleder if (!*gargv) { 5175ec2b8dcSStefan Farfeleder *dp = 0.0; 518bacab7d6STim J. Robbins return (0); 5195ec2b8dcSStefan Farfeleder } 520ab5a295bSJuli Mallett if (**gargv == '"' || **gargv == '\'') { 521bacab7d6STim J. Robbins *dp = asciicode(); 522bacab7d6STim J. Robbins return (0); 523ab5a295bSJuli Mallett } 5248c423a99SColin Percival rval = 0; 525ab5a295bSJuli Mallett errno = 0; 526fd757c50SDavid Schultz if (mod_ldbl) 527fd757c50SDavid Schultz *dp = strtold(*gargv, &ep); 528fd757c50SDavid Schultz else 529bacab7d6STim J. Robbins *dp = strtod(*gargv, &ep); 530bacab7d6STim J. Robbins if (ep == *gargv) { 531*6a6760dbSJilles Tjoelker warnx("%s: expected numeric value", *gargv); 532bacab7d6STim J. Robbins rval = 1; 533bacab7d6STim J. Robbins } else if (*ep != '\0') { 534*6a6760dbSJilles Tjoelker warnx("%s: not completely converted", *gargv); 535bacab7d6STim J. Robbins rval = 1; 536bacab7d6STim J. Robbins } 537bacab7d6STim J. Robbins if (errno == ERANGE) { 538*6a6760dbSJilles Tjoelker warnx("%s: %s", *gargv, strerror(ERANGE)); 539bacab7d6STim J. Robbins rval = 1; 540bacab7d6STim J. Robbins } 541ab5a295bSJuli Mallett ++gargv; 542bacab7d6STim J. Robbins return (rval); 5439b50d902SRodney W. Grimes } 5449b50d902SRodney W. Grimes 5459b50d902SRodney W. Grimes static int 546f4ac32deSDavid Malone asciicode(void) 5479b50d902SRodney W. Grimes { 548f4ac32deSDavid Malone int ch; 5499b50d902SRodney W. Grimes 5509b50d902SRodney W. Grimes ch = **gargv; 5519b50d902SRodney W. Grimes if (ch == '\'' || ch == '"') 5529b50d902SRodney W. Grimes ch = (*gargv)[1]; 5539b50d902SRodney W. Grimes ++gargv; 5549b50d902SRodney W. Grimes return (ch); 5559b50d902SRodney W. Grimes } 5569b50d902SRodney W. Grimes 5579b50d902SRodney W. Grimes static void 558f4ac32deSDavid Malone usage(void) 5599b50d902SRodney W. Grimes { 560f682f10cSRuslan Ermilov (void)fprintf(stderr, "usage: printf format [arguments ...]\n"); 5619b50d902SRodney W. Grimes } 562