/*
* Copyright 2014 Garrett D'Amore <garrett@damore.org>
* Copyright 2010 Nexenta Systems, Inc. All rights reserved.
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <err.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <alloca.h>
#include <ctype.h>
#include <locale.h>
#include <note.h>
#define _(x) gettext(x)
char *b = NULL; \
if (havewidth) \
if (haveprec) \
else \
else if (haveprec) \
else \
if (b) { \
free(b); \
} \
static int asciicode(void);
static char *doformat(char *, int *);
static int getchr(void);
static int getfloating(long double *, int);
static int getint(int *);
static const char
*getstr(void);
static char *mknum(char *, char);
static void usage(void);
static int myargc;
static char **myargv;
static char **gargv;
static char **maxargv;
int
{
argv++;
argc--;
/*
* POSIX says: Standard utilities that do not accept options,
* but that do accept operands, shall recognize "--" as a
* first argument to be discarded.
*/
argc--;
argv++;
}
if (argc < 1) {
usage();
return (1);
}
/*
* Basic algorithm is to scan the format string for conversion
* specifications -- once one is found, find out if the field
* width or precision is a '*'; if it is, gather up value. Note,
* format strings are reused as necessary to use up the provided
* up the format string.
*/
for (;;) {
/* nop */;
if (fmt[0] == '%') {
stdout);
/* %% prints a % */
(void) putchar('%');
fmt += 2;
} else {
return (1);
end = 0;
}
} else
fmt++;
}
if (end == 1) {
return (1);
}
if (!*gargv)
return (rval);
/* Restart at the beginning of the format string. */
end = 1;
}
/* NOTREACHED */
}
static char *
{
char *start;
char **fargv;
char *dptr;
int l;
*dptr++ = '%';
*dptr = 0;
fmt++;
/* look for "n$" field index specifier */
if ((l > 0) && (fmt[l] == '$')) {
} else {
}
}
fmt += l + 1;
/* save format argument */
} else {
}
/* skip to field width */
*dptr = 0;
}
if (*fmt == '*') {
fmt++;
if ((l > 0) && (fmt[l] == '$')) {
return (NULL);
}
} else {
}
fmt += l + 1;
return (NULL);
}
if (getint(&fieldwidth))
return (NULL);
}
havewidth = 1;
*dptr++ = '*';
*dptr = 0;
} else {
havewidth = 0;
/* skip to possible '.', get following precision */
*dptr = 0;
}
}
if (*fmt == '.') {
/* precision present? */
fmt++;
*dptr++ = '.';
if (*fmt == '*') {
fmt++;
if ((l > 0) && (fmt[l] == '$')) {
warnx1(_("incomplete use of n$"),
return (NULL);
}
} else {
}
fmt += l + 1;
return (NULL);
}
return (NULL);
}
haveprec = 1;
*dptr++ = '*';
*dptr = 0;
} else {
haveprec = 0;
/* skip to conversion char */
*dptr = 0;
}
}
} else
haveprec = 0;
if (!*fmt) {
return (NULL);
}
*dptr = 0;
/*
* Look for a length modifier. POSIX doesn't have these, so
* we only support them for floating-point conversions, which
* are extensions. This is useful because the L modifier can
* be used to gain extra range and precision, while omitting
* it is more likely to produce consistent results on different
* architectures. This is not so important for integers
* because overflow is the only bad thing that can happen to
* them, but consider the command printf %a 1.1
*/
if (*fmt == 'L') {
mod_ldbl = 1;
fmt++;
return (NULL);
}
} else {
mod_ldbl = 0;
}
/* save the current arg offset, and set to the format arg */
}
*fmt = '\0';
switch (convch) {
case 'b': {
char *p;
int getout;
if (p == NULL) {
return (NULL);
}
free(p);
if (getout)
break;
}
case 'c': {
char p;
p = getchr();
break;
}
case 's': {
const char *p;
p = getstr();
break;
}
case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
char *f;
int signedconv;
return (NULL);
*rval = 1;
if (signedconv)
else
break;
}
case 'e': case 'E':
case 'f': case 'F':
case 'g': case 'G':
case 'a': case 'A': {
long double p;
if (getfloating(&p, mod_ldbl))
*rval = 1;
if (mod_ldbl)
else
break;
}
default:
return (NULL);
}
/* return the gargv to the next element */
return (fmt);
}
static char *
{
static char *copy;
char *newcopy;
return (NULL);
}
}
return (copy);
}
static int
{
int value;
if (c != '\\') {
*store = c;
continue;
}
switch (*++fmt) {
case '\0': /* EOS, user error */
*store = '\\';
*++store = '\0';
return (0);
case '\\': /* backslash */
case '\'': /* single quote */
break;
*store = '\a';
break;
case 'b': /* backspace */
*store = '\b';
break;
case 'c':
if (!percent) {
*store = '\0';
return (1);
}
*store = 'c';
break;
case 'f': /* form-feed */
*store = '\f';
break;
case 'n': /* newline */
*store = '\n';
break;
case 'r': /* carriage-return */
*store = '\r';
break;
case 't': /* horizontal tab */
*store = '\t';
break;
case 'v': /* vertical tab */
*store = '\v';
break;
/* octal constant */
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
for (value = 0;
value <<= 3;
}
--fmt;
*store++ = '%';
*store = '%';
} else
break;
default:
break;
}
}
*store = '\0';
return (0);
}
static int
getchr(void)
{
if (!*gargv)
return ('\0');
return ((int)**gargv++);
}
static const char *
getstr(void)
{
if (!*gargv)
return ("");
return (*gargv++);
}
static int
{
int rval;
return (1);
rval = 0;
rval = 1;
}
return (rval);
}
static int
{
char *ep;
int rval;
if (!*gargv) {
return (0);
}
if (signedconv)
else
return (0);
}
rval = 0;
errno = 0;
if (signedconv)
else
rval = 1;
} else if (*ep != '\0') {
rval = 1;
}
rval = 1;
}
++gargv;
return (rval);
}
static int
{
char *ep;
int rval;
if (!*gargv) {
*dp = 0.0;
return (0);
}
return (0);
}
rval = 0;
errno = 0;
if (mod_ldbl)
else
rval = 1;
} else if (*ep != '\0') {
rval = 1;
}
rval = 1;
}
++gargv;
return (rval);
}
static int
asciicode(void)
{
int ch;
++gargv;
return (ch);
}
static void
usage(void)
{
}