598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore * Copyright 2014 Garrett D'Amore <garrett@damore.org>
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * Copyright (c) 1989, 1993
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * The Regents of the University of California. All rights reserved.
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * Redistribution and use in source and binary forms, with or without
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * modification, are permitted provided that the following conditions
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * 1. Redistributions of source code must retain the above copyright
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * notice, this list of conditions and the following disclaimer.
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * 2. Redistributions in binary form must reproduce the above copyright
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * notice, this list of conditions and the following disclaimer in the
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * documentation and/or other materials provided with the distribution.
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * 4. Neither the name of the University nor the names of its contributors
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * may be used to endorse or promote products derived from this software
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * without specific prior written permission.
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * SUCH DAMAGE.
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore#define PTRDIFF(x, y) ((uintptr_t)(x) - (uintptr_t)(y))
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore#define _(x) gettext(x)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore (void) asprintf(&b, f, fieldwidth, precision, func); \
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic int asciicode(void);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic char *doformat(char *, int *);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic int getchr(void);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic int getfloating(long double *, int);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic int getint(int *);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic int getnum(intmax_t *, uintmax_t *, int);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic const char
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic char *mknum(char *, char);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic void usage(void);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * POSIX says: Standard utilities that do not accept options,
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * but that do accept operands, shall recognize "--" as a
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * first argument to be discarded.
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * Basic algorithm is to scan the format string for conversion
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * specifications -- once one is found, find out if the field
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * width or precision is a '*'; if it is, gather up value. Note,
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * format strings are reused as necessary to use up the provided
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * arguments, arguments of zero/null string are provided to use
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * up the format string.
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore (void) escape(fmt, 1, &len); /* backslash interpretation */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore (void) fwrite(start, 1, PTRDIFF(fmt, start),
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /* %% prints a % */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx1(_("missing format character"), NULL, NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore (void) fwrite(start, 1, PTRDIFF(fmt, start), stdout);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /* Restart at the beginning of the format string. */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /* NOTREACHED */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore int fieldwidth, haveprec, havewidth, mod_ldbl, precision;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /* look for "n$" field index specifier */
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore /* save format argument */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /* skip to field width */
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore while (*fmt && strchr(skip1, *fmt) != NULL) {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore warnx1(_("incomplete use of n$"), NULL, NULL);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore warnx1(_("incomplete use of n$"), NULL, NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /* skip to possible '.', get following precision */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /* precision present? */
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore warnx1(_("incomplete use of n$"), NULL, NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /* skip to conversion char */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx1(_("missing format character"), NULL, NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * Look for a length modifier. POSIX doesn't have these, so
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * we only support them for floating-point conversions, which
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * are extensions. This is useful because the L modifier can
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * be used to gain extra range and precision, while omitting
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * it is more likely to produce consistent results on different
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * architectures. This is not so important for integers
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * because overflow is the only bad thing that can happen to
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore * them, but consider the command printf %a 1.1
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx2(_("bad modifier L for %%%c"), *fmt, NULL);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore /* save the current arg offset, and set to the format arg */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore const char *p;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore signedconv = (convch == 'd' || convch == 'i');
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore long double p;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx2(_("illegal format character %c"), convch, NULL);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore /* return the gargv to the next element */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if ((newcopy = realloc(copy, newlen)) == NULL) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore for (save = store = fmt; ((c = *fmt) != 0); ++fmt, ++store) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (c != '\\') {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore switch (*++fmt) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /* octal constant */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return ('\0');
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return ((int)**gargv++);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic const char *
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amoregetnum(intmax_t *ip, uintmax_t *uip, int signedconv)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx2(_("%s: expected numeric value"), *gargv, NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx2(_("%s: not completely converted"), *gargv, NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx2(_("%s: expected numeric value"), *gargv, NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx2(_("%s: not completely converted"), *gargv, NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore (void) fprintf(stderr, _("usage: printf format [arguments ...]\n"));