]> git.saurik.com Git - apple/shell_cmds.git/blobdiff - printf/printf.c
shell_cmds-216.40.4.tar.gz
[apple/shell_cmds.git] / printf / printf.c
index 438b85ece61f18d90bbe68f01446a783386be8e3..6cc09ae731aa2178a8bd9f39ff4f4ea7ed9a793e 100644 (file)
@@ -1,4 +1,7 @@
 /*-
+ * 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
@@ -12,7 +15,7 @@
  * 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.
  *
@@ -46,7 +49,7 @@ static char const copyright[] =
 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>
@@ -70,20 +73,15 @@ static const char rcsid[] =
 #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);
@@ -378,13 +376,25 @@ printf_doformat(char *fmt, int *rval)
                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);
@@ -394,7 +404,8 @@ printf_doformat(char *fmt, int *rval)
                char p;
 
                p = getchr();
-               PF(start, p);
+               if (p != '\0')
+                       PF(start, p);
                break;
        }
        case 's': {