/*-
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
* Copyright 2014 Garrett D'Amore <garrett@damore.org>
* Copyright 2010 Nexenta Systems, Inc. All rights reserved.
* Copyright (c) 1989, 1993
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. 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.
*
static char const sccsid[] = "@(#)printf.c 8.1 (Berkeley) 7/20/93";
#endif
static const char rcsid[] =
- "$FreeBSD: head/usr.bin/printf/printf.c 279503 2015-03-01 21:46:55Z jilles $";
+ "$FreeBSD$";
#endif /* not lint */
#include <sys/types.h>
#endif
#define PF(f, func) do { \
- char *b = NULL; \
if (havewidth) \
if (haveprec) \
- (void)asprintf(&b, f, fieldwidth, precision, func); \
+ (void)printf(f, fieldwidth, precision, func); \
else \
- (void)asprintf(&b, f, fieldwidth, func); \
+ (void)printf(f, fieldwidth, func); \
else if (haveprec) \
- (void)asprintf(&b, f, precision, func); \
+ (void)printf(f, precision, func); \
else \
- (void)asprintf(&b, f, func); \
- if (b) { \
- (void)fputs(b, stdout); \
- free(b); \
- } \
+ (void)printf(f, func); \
} while (0)
static int asciicode(void);
char *p;
int getout;
- p = strdup(getstr());
- if (p == NULL) {
+ /* Convert "b" to "s" for output. */
+ start[strlen(start) - 1] = 's';
+ if ((p = strdup(getstr())) == NULL) {
warnx("%s", strerror(ENOMEM));
return (NULL);
}
getout = escape(p, 0, &len);
- fputs(p, stdout);
+ /*
+ * Special-case conversion of zero; print it instead of
+ * feeding an empty string to printf("%s") which would not
+ * print anything.
+ */
+ if (len == 1 && p[0] == '\0') {
+ putchar(p[0]);
+ } else {
+ PF(start, p);
+ }
+ /* Restore format for next loop. */
+
free(p);
if (getout)
return (end_fmt);
char p;
p = getchr();
- PF(start, p);
+ if (p != '\0')
+ PF(start, p);
break;
}
case 's': {