f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore/*
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 *
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 * are met:
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 *
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 */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore#include <sys/types.h>
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore#include <err.h>
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore#include <errno.h>
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore#include <inttypes.h>
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore#include <limits.h>
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore#include <stdio.h>
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore#include <stdlib.h>
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore#include <string.h>
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore#include <unistd.h>
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore#include <alloca.h>
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore#include <ctype.h>
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore#include <locale.h>
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore#include <note.h>
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore#define warnx1(a, b, c) warnx(a)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore#define warnx2(a, b, c) warnx(a, b)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore#define warnx3(a, b, c) warnx(a, b, c)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore#define PTRDIFF(x, y) ((uintptr_t)(x) - (uintptr_t)(y))
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore#define _(x) gettext(x)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore#define PF(f, func) do { \
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore char *b = NULL; \
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (havewidth) \
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (haveprec) \
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore (void) asprintf(&b, f, fieldwidth, precision, func); \
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore else \
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore (void) asprintf(&b, f, fieldwidth, func); \
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore else if (haveprec) \
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore (void) asprintf(&b, f, precision, func); \
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore else \
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore (void) asprintf(&b, f, func); \
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (b) { \
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore (void) fputs(b, stdout); \
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore free(b); \
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore } \
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore_NOTE(CONSTCOND) } while (0)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic int asciicode(void);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic char *doformat(char *, int *);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic int escape(char *, int, size_t *);
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'Amore *getstr(void);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic char *mknum(char *, char);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic void usage(void);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amorestatic const char digits[] = "0123456789";
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic int myargc;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic char **myargv;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic char **gargv;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amorestatic char **maxargv;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amoreint
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amoremain(int argc, char *argv[])
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore{
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore size_t len;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore int end, rval;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore char *format, *fmt, *start;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore (void) setlocale(LC_ALL, "");
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore argv++;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore argc--;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /*
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 */
12d82915cd18b6426e4b3d41a9712bf0ea3c5f00Garrett D'Amore if (argc && strcmp(argv[0], "--") == 0) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore argc--;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore argv++;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (argc < 1) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore usage();
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (1);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /*
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.
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore fmt = format = *argv;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore (void) escape(fmt, 1, &len); /* backslash interpretation */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore rval = end = 0;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore gargv = ++argv;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore for (;;) {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore maxargv = gargv;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore myargv = gargv;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore for (myargc = 0; gargv[myargc]; myargc++)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /* nop */;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore start = fmt;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore while (fmt < format + len) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (fmt[0] == '%') {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore (void) fwrite(start, 1, PTRDIFF(fmt, start),
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore stdout);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (fmt[1] == '%') {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /* %% prints a % */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore (void) putchar('%');
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore fmt += 2;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore } else {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore fmt = doformat(fmt, &rval);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (fmt == NULL)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (1);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore end = 0;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore start = fmt;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore } else
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore fmt++;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (gargv > maxargv)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore maxargv = gargv;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore gargv = maxargv;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (end == 1) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx1(_("missing format character"), NULL, NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (1);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore (void) fwrite(start, 1, PTRDIFF(fmt, start), stdout);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore if (!*gargv)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (rval);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /* Restart at the beginning of the format string. */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore fmt = format;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore end = 1;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /* NOTREACHED */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore}
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic char *
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amoredoformat(char *fmt, int *rval)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore{
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore static const char skip1[] = "#'-+ 0";
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore int fieldwidth, haveprec, havewidth, mod_ldbl, precision;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore char convch, nextch;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore char *start;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore char **fargv;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore char *dptr;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore int l;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore start = alloca(strlen(fmt) + 1);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore dptr = start;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore *dptr++ = '%';
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore *dptr = 0;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore fmt++;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /* look for "n$" field index specifier */
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore l = strspn(fmt, digits);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore if ((l > 0) && (fmt[l] == '$')) {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore int idx = atoi(fmt);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (idx <= myargc) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore gargv = &myargv[idx - 1];
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore } else {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore gargv = &myargv[myargc];
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore if (gargv > maxargv) {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore maxargv = gargv;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore }
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore fmt += l + 1;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore /* save format argument */
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore fargv = gargv;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore } else {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore fargv = NULL;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /* skip to field width */
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore while (*fmt && strchr(skip1, *fmt) != NULL) {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore *dptr++ = *fmt++;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore *dptr = 0;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore }
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (*fmt == '*') {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore fmt++;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore l = strspn(fmt, digits);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore if ((l > 0) && (fmt[l] == '$')) {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore int idx = atoi(fmt);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore if (fargv == NULL) {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore warnx1(_("incomplete use of n$"), NULL, NULL);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore return (NULL);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore }
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore if (idx <= myargc) {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore gargv = &myargv[idx - 1];
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore } else {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore gargv = &myargv[myargc];
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore }
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore fmt += l + 1;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore } else if (fargv != NULL) {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore warnx1(_("incomplete use of n$"), NULL, NULL);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore return (NULL);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore }
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (getint(&fieldwidth))
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (NULL);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore if (gargv > maxargv) {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore maxargv = gargv;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore havewidth = 1;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore *dptr++ = '*';
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore *dptr = 0;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore } else {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore havewidth = 0;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /* skip to possible '.', get following precision */
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore while (isdigit(*fmt)) {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore *dptr++ = *fmt++;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore *dptr = 0;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (*fmt == '.') {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /* precision present? */
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore fmt++;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore *dptr++ = '.';
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (*fmt == '*') {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore fmt++;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore l = strspn(fmt, digits);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore if ((l > 0) && (fmt[l] == '$')) {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore int idx = atoi(fmt);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore if (fargv == NULL) {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore warnx1(_("incomplete use of n$"),
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore NULL, NULL);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore return (NULL);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore }
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore if (idx <= myargc) {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore gargv = &myargv[idx - 1];
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore } else {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore gargv = &myargv[myargc];
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore }
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore fmt += l + 1;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore } else if (fargv != NULL) {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore warnx1(_("incomplete use of n$"), NULL, NULL);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore return (NULL);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore }
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (getint(&precision))
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (NULL);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore if (gargv > maxargv) {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore maxargv = gargv;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore haveprec = 1;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore *dptr++ = '*';
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore *dptr = 0;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore } else {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore haveprec = 0;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /* skip to conversion char */
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore while (isdigit(*fmt)) {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore *dptr++ = *fmt++;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore *dptr = 0;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore } else
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore haveprec = 0;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (!*fmt) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx1(_("missing format character"), NULL, NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore *dptr++ = *fmt;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore *dptr = 0;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /*
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 */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (*fmt == 'L') {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore mod_ldbl = 1;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore fmt++;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (!strchr("aAeEfFgG", *fmt)) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx2(_("bad modifier L for %%%c"), *fmt, NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore } else {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore mod_ldbl = 0;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore /* save the current arg offset, and set to the format arg */
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore if (fargv != NULL) {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore gargv = fargv;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore }
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore convch = *fmt;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore nextch = *++fmt;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *fmt = '\0';
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore switch (convch) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case 'b': {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore size_t len;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore char *p;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore int getout;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore p = strdup(getstr());
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (p == NULL) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx2("%s", strerror(ENOMEM), NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore getout = escape(p, 0, &len);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore (void) fputs(p, stdout);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore free(p);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (getout)
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore exit(*rval);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore break;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case 'c': {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore char p;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore p = getchr();
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore PF(start, p);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore break;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case 's': {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore const char *p;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore p = getstr();
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore PF(start, p);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore break;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore char *f;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore intmax_t val;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore uintmax_t uval;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore int signedconv;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore signedconv = (convch == 'd' || convch == 'i');
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if ((f = mknum(start, convch)) == NULL)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (getnum(&val, &uval, signedconv))
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *rval = 1;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (signedconv)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore PF(f, val);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore else
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore PF(f, uval);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore break;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case 'e': case 'E':
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case 'f': case 'F':
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case 'g': case 'G':
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case 'a': case 'A': {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore long double p;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (getfloating(&p, mod_ldbl))
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *rval = 1;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (mod_ldbl)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore PF(start, p);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore else
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore PF(start, (double)p);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore break;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore default:
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx2(_("illegal format character %c"), convch, NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *fmt = nextch;
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore /* return the gargv to the next element */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (fmt);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore}
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic char *
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amoremknum(char *str, char ch)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore{
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore static char *copy;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore static size_t copy_size;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore char *newcopy;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore size_t len, newlen;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore len = strlen(str) + 2;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (len > copy_size) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore newlen = ((len + 1023) >> 10) << 10;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if ((newcopy = realloc(copy, newlen)) == NULL) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx2("%s", strerror(ENOMEM), NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore copy = newcopy;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore copy_size = newlen;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore (void) memmove(copy, str, len - 3);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore copy[len - 3] = 'j';
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore copy[len - 2] = ch;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore copy[len - 1] = '\0';
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (copy);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore}
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic int
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amoreescape(char *fmt, int percent, size_t *len)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore{
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore char *save, *store, c;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore int value;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore for (save = store = fmt; ((c = *fmt) != 0); ++fmt, ++store) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (c != '\\') {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *store = c;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore continue;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore switch (*++fmt) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case '\0': /* EOS, user error */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *store = '\\';
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *++store = '\0';
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *len = PTRDIFF(store, save);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (0);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case '\\': /* backslash */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case '\'': /* single quote */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *store = *fmt;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore break;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case 'a': /* bell/alert */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *store = '\a';
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore break;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case 'b': /* backspace */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *store = '\b';
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore break;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case 'c':
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore if (!percent) {
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore *store = '\0';
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore *len = PTRDIFF(store, save);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore return (1);
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore }
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore *store = 'c';
598f4ceed9327d2d6c2325dd67cae3aa06f7fea6Garrett D'Amore break;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case 'f': /* form-feed */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *store = '\f';
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore break;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case 'n': /* newline */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *store = '\n';
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore break;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case 'r': /* carriage-return */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *store = '\r';
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore break;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case 't': /* horizontal tab */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *store = '\t';
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore break;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case 'v': /* vertical tab */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *store = '\v';
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore break;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore /* octal constant */
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case '0': case '1': case '2': case '3':
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore case '4': case '5': case '6': case '7':
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore c = (!percent && *fmt == '0') ? 4 : 3;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore for (value = 0;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore value <<= 3;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore value += *fmt - '0';
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore --fmt;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (percent && value == '%') {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *store++ = '%';
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *store = '%';
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore } else
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *store = (char)value;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore break;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore default:
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *store = *fmt;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore break;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *store = '\0';
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *len = PTRDIFF(store, save);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (0);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore}
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic int
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amoregetchr(void)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore{
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (!*gargv)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return ('\0');
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return ((int)**gargv++);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore}
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic const char *
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amoregetstr(void)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore{
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (!*gargv)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return ("");
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (*gargv++);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore}
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic int
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amoregetint(int *ip)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore{
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore intmax_t val;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore uintmax_t uval;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore int rval;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (getnum(&val, &uval, 1))
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (1);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore rval = 0;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (val < INT_MIN || val > INT_MAX) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx3("%s: %s", *gargv, strerror(ERANGE));
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore rval = 1;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *ip = (int)val;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (rval);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore}
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic int
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amoregetnum(intmax_t *ip, uintmax_t *uip, int signedconv)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore{
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore char *ep;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore int rval;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (!*gargv) {
e56bd28502ece54c39ad86fd96643790902bb93eGarrett D'Amore *ip = *uip = 0;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (0);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (**gargv == '"' || **gargv == '\'') {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (signedconv)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *ip = asciicode();
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore else
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *uip = asciicode();
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (0);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore rval = 0;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore errno = 0;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (signedconv)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *ip = strtoimax(*gargv, &ep, 0);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore else
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *uip = strtoumax(*gargv, &ep, 0);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (ep == *gargv) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx2(_("%s: expected numeric value"), *gargv, NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore rval = 1;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore } else if (*ep != '\0') {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx2(_("%s: not completely converted"), *gargv, NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore rval = 1;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (errno == ERANGE) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx3("%s: %s", *gargv, strerror(ERANGE));
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore rval = 1;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore ++gargv;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (rval);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore}
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic int
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amoregetfloating(long double *dp, int mod_ldbl)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore{
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore char *ep;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore int rval;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (!*gargv) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *dp = 0.0;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (0);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (**gargv == '"' || **gargv == '\'') {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *dp = asciicode();
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (0);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore rval = 0;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore errno = 0;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (mod_ldbl)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *dp = strtold(*gargv, &ep);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore else
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore *dp = strtod(*gargv, &ep);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (ep == *gargv) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx2(_("%s: expected numeric value"), *gargv, NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore rval = 1;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore } else if (*ep != '\0') {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx2(_("%s: not completely converted"), *gargv, NULL);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore rval = 1;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (errno == ERANGE) {
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore warnx3("%s: %s", *gargv, strerror(ERANGE));
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore rval = 1;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore }
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore ++gargv;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (rval);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore}
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic int
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amoreasciicode(void)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore{
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore int ch;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore ch = **gargv;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore if (ch == '\'' || ch == '"')
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore ch = (*gargv)[1];
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore ++gargv;
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore return (ch);
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore}
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amorestatic void
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amoreusage(void)
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore{
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore (void) fprintf(stderr, _("usage: printf format [arguments ...]\n"));
f967d548b23673f68e7356c6c741329b7698269cGarrett D'Amore}