Print this page
4854 printf(1) doesn't support %b and 
   1 /*

   2  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
   3  * Copyright (c) 1989, 1993
   4  *      The Regents of the University of California.  All rights reserved.
   5  *
   6  * Redistribution and use in source and binary forms, with or without
   7  * modification, are permitted provided that the following conditions
   8  * are met:
   9  * 1. Redistributions of source code must retain the above copyright
  10  *    notice, this list of conditions and the following disclaimer.
  11  * 2. Redistributions in binary form must reproduce the above copyright
  12  *    notice, this list of conditions and the following disclaimer in the
  13  *    documentation and/or other materials provided with the distribution.
  14  * 4. Neither the name of the University nor the names of its contributors
  15  *    may be used to endorse or promote products derived from this software
  16  *    without specific prior written permission.
  17  *
  18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28  * SUCH DAMAGE.
  29  */
  30 
  31 #include <sys/types.h>
  32 
  33 #include <err.h>
  34 #include <errno.h>
  35 #include <inttypes.h>
  36 #include <limits.h>
  37 #include <stdio.h>
  38 #include <stdlib.h>
  39 #include <string.h>
  40 #include <unistd.h>


  41 #include <locale.h>
  42 #include <note.h>
  43 
  44 #define warnx1(a, b, c)         warnx(a)
  45 #define warnx2(a, b, c)         warnx(a, b)
  46 #define warnx3(a, b, c)         warnx(a, b, c)
  47 
  48 #define PTRDIFF(x, y)   ((uintptr_t)(x) - (uintptr_t)(y))
  49 
  50 #define _(x)    gettext(x)
  51 
  52 #define PF(f, func) do {                                                \
  53         char *b = NULL;                                                 \
  54         int dollar = 0;                                                 \
  55         if (*f == '$')  {                                               \
  56                 dollar++;                                               \
  57                 *f = '%';                                               \
  58         }                                                               \
  59         if (havewidth)                                                  \
  60                 if (haveprec)                                           \
  61                         (void) asprintf(&b, f, fieldwidth, precision, func); \
  62                 else                                                    \
  63                         (void) asprintf(&b, f, fieldwidth, func);   \
  64         else if (haveprec)                                              \
  65                 (void) asprintf(&b, f, precision, func);            \
  66         else                                                            \
  67                 (void) asprintf(&b, f, func);                               \
  68         if (b) {                                                        \
  69                 (void) fputs(b, stdout);                                \
  70                 free(b);                                                \
  71         }                                                               \
  72         if (dollar)                                                     \
  73                 *f = '$';                                               \
  74 _NOTE(CONSTCOND) } while (0)
  75 
  76 static int       asciicode(void);
  77 static char     *doformat(char *, int *);
  78 static int       escape(char *, int, size_t *);
  79 static int       getchr(void);
  80 static int       getfloating(long double *, int);
  81 static int       getint(int *);
  82 static int       getnum(intmax_t *, uintmax_t *, int);
  83 static const char
  84                 *getstr(void);
  85 static char     *mknum(char *, char);
  86 static void      usage(void);
  87 


  88 static int  myargc;
  89 static char **myargv;
  90 static char **gargv;

  91 
  92 int
  93 main(int argc, char *argv[])
  94 {
  95         size_t len;
  96         int chopped, end, rval;
  97         char *format, *fmt, *start;
  98 
  99         (void) setlocale(LC_ALL, "");
 100 
 101         argv++;
 102         argc--;
 103 
 104         /*
 105          * POSIX says: Standard utilities that do not accept options,
 106          * but that do accept operands, shall recognize "--" as a
 107          * first argument to be discarded.
 108          */
 109         if (argc && strcmp(argv[0], "--") == 0) {
 110                 argc--;
 111                 argv++;
 112         }
 113 
 114         if (argc < 1) {
 115                 usage();
 116                 return (1);
 117         }
 118 
 119         /*
 120          * Basic algorithm is to scan the format string for conversion
 121          * specifications -- once one is found, find out if the field
 122          * width or precision is a '*'; if it is, gather up value.  Note,
 123          * format strings are reused as necessary to use up the provided
 124          * arguments, arguments of zero/null string are provided to use
 125          * up the format string.
 126          */
 127         fmt = format = *argv;
 128         chopped = escape(fmt, 1, &len);             /* backslash interpretation */
 129         rval = end = 0;
 130         gargv = ++argv;
 131 
 132         for (;;) {
 133                 char **maxargv = gargv;
 134 
 135                 myargv = gargv;
 136                 for (myargc = 0; gargv[myargc]; myargc++)
 137                         /* nop */;
 138                 start = fmt;
 139                 while (fmt < format + len) {
 140                         if (fmt[0] == '%') {
 141                                 (void) fwrite(start, 1, PTRDIFF(fmt, start),
 142                                     stdout);
 143                                 if (fmt[1] == '%') {
 144                                         /* %% prints a % */
 145                                         (void) putchar('%');
 146                                         fmt += 2;
 147                                 } else {
 148                                         fmt = doformat(fmt, &rval);
 149                                         if (fmt == NULL)
 150                                                 return (1);
 151                                         end = 0;
 152                                 }
 153                                 start = fmt;
 154                         } else
 155                                 fmt++;
 156                         if (gargv > maxargv)
 157                                 maxargv = gargv;
 158                 }
 159                 gargv = maxargv;
 160 
 161                 if (end == 1) {
 162                         warnx1(_("missing format character"), NULL, NULL);
 163                         return (1);
 164                 }
 165                 (void) fwrite(start, 1, PTRDIFF(fmt, start), stdout);
 166                 if (chopped || !*gargv)
 167                         return (rval);
 168                 /* Restart at the beginning of the format string. */
 169                 fmt = format;
 170                 end = 1;
 171         }
 172         /* NOTREACHED */
 173 }
 174 
 175 
 176 static char *
 177 doformat(char *start, int *rval)
 178 {
 179         static const char skip1[] = "#'-+ 0";
 180         static const char skip2[] = "0123456789";
 181         char *fmt;
 182         int fieldwidth, haveprec, havewidth, mod_ldbl, precision;
 183         char convch, nextch;




 184 
 185         fmt = start + 1;
 186 






 187         /* look for "n$" field index specifier */
 188         fmt += strspn(fmt, skip2);
 189         if ((*fmt == '$') && (fmt != (start + 1))) {
 190                 int idx = atoi(start + 1);
 191                 if (idx <= myargc) {
 192                         gargv = &myargv[idx - 1];
 193                 } else {
 194                         gargv = &myargv[myargc];
 195                 }
 196                 start = fmt;
 197                 fmt++;





 198         } else {
 199                 fmt = start + 1;
 200         }
 201 
 202         /* skip to field width */
 203         fmt += strspn(fmt, skip1);





 204         if (*fmt == '*') {













 205                 if (getint(&fieldwidth))
 206                         return (NULL);



 207                 havewidth = 1;
 208                 ++fmt;


 209         } else {
 210                 havewidth = 0;
 211 
 212                 /* skip to possible '.', get following precision */
 213                 fmt += strspn(fmt, skip2);


 214         }


 215         if (*fmt == '.') {
 216                 /* precision present? */
 217                 ++fmt;


 218                 if (*fmt == '*') {













 219                         if (getint(&precision))
 220                                 return (NULL);



 221                         haveprec = 1;
 222                         ++fmt;

 223                 } else {
 224                         haveprec = 0;
 225 
 226                         /* skip to conversion char */
 227                         fmt += strspn(fmt, skip2);


 228                 }

 229         } else
 230                 haveprec = 0;
 231         if (!*fmt) {
 232                 warnx1(_("missing format character"), NULL, NULL);
 233                 return (NULL);
 234         }


 235 
 236         /*
 237          * Look for a length modifier.  POSIX doesn't have these, so
 238          * we only support them for floating-point conversions, which
 239          * are extensions.  This is useful because the L modifier can
 240          * be used to gain extra range and precision, while omitting
 241          * it is more likely to produce consistent results on different
 242          * architectures.  This is not so important for integers
 243          * because overflow is the only bad thing that can happen to
 244          * them, but consider the command  printf %a 1.1
 245          */
 246         if (*fmt == 'L') {
 247                 mod_ldbl = 1;
 248                 fmt++;
 249                 if (!strchr("aAeEfFgG", *fmt)) {
 250                         warnx2(_("bad modifier L for %%%c"), *fmt, NULL);
 251                         return (NULL);
 252                 }
 253         } else {
 254                 mod_ldbl = 0;
 255         }
 256 





 257         convch = *fmt;
 258         nextch = *++fmt;

 259         *fmt = '\0';
 260         switch (convch) {
 261         case 'b': {
 262                 size_t len;
 263                 char *p;
 264                 int getout;
 265 
 266                 p = strdup(getstr());
 267                 if (p == NULL) {
 268                         warnx2("%s", strerror(ENOMEM), NULL);
 269                         return (NULL);
 270                 }
 271                 getout = escape(p, 0, &len);
 272                 *(fmt - 1) = 's';
 273                 PF(start, p);
 274                 *(fmt - 1) = 'b';
 275                 free(p);
 276 
 277                 if (getout)
 278                         return (fmt);
 279                 break;
 280         }
 281         case 'c': {
 282                 char p;
 283 
 284                 p = getchr();
 285                 PF(start, p);
 286                 break;
 287         }
 288         case 's': {
 289                 const char *p;
 290 
 291                 p = getstr();
 292                 PF(start, p);
 293                 break;
 294         }
 295         case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
 296                 char *f;
 297                 intmax_t val;
 298                 uintmax_t uval;


 311         }
 312         case 'e': case 'E':
 313         case 'f': case 'F':
 314         case 'g': case 'G':
 315         case 'a': case 'A': {
 316                 long double p;
 317 
 318                 if (getfloating(&p, mod_ldbl))
 319                         *rval = 1;
 320                 if (mod_ldbl)
 321                         PF(start, p);
 322                 else
 323                         PF(start, (double)p);
 324                 break;
 325         }
 326         default:
 327                 warnx2(_("illegal format character %c"), convch, NULL);
 328                 return (NULL);
 329         }
 330         *fmt = nextch;


 331         return (fmt);
 332 }
 333 
 334 static char *
 335 mknum(char *str, char ch)
 336 {
 337         static char *copy;
 338         static size_t copy_size;
 339         char *newcopy;
 340         size_t len, newlen;
 341 
 342         len = strlen(str) + 2;
 343         if (len > copy_size) {
 344                 newlen = ((len + 1023) >> 10) << 10;
 345                 if ((newcopy = realloc(copy, newlen)) == NULL) {
 346                         warnx2("%s", strerror(ENOMEM), NULL);
 347                         return (NULL);
 348                 }
 349                 copy = newcopy;
 350                 copy_size = newlen;


 368                         *store = c;
 369                         continue;
 370                 }
 371                 switch (*++fmt) {
 372                 case '\0':              /* EOS, user error */
 373                         *store = '\\';
 374                         *++store = '\0';
 375                         *len = PTRDIFF(store, save);
 376                         return (0);
 377                 case '\\':              /* backslash */
 378                 case '\'':              /* single quote */
 379                         *store = *fmt;
 380                         break;
 381                 case 'a':               /* bell/alert */
 382                         *store = '\a';
 383                         break;
 384                 case 'b':               /* backspace */
 385                         *store = '\b';
 386                         break;
 387                 case 'c':

 388                         *store = '\0';
 389                         *len = PTRDIFF(store, save);
 390                         return (1);



 391                 case 'f':               /* form-feed */
 392                         *store = '\f';
 393                         break;
 394                 case 'n':               /* newline */
 395                         *store = '\n';
 396                         break;
 397                 case 'r':               /* carriage-return */
 398                         *store = '\r';
 399                         break;
 400                 case 't':               /* horizontal tab */
 401                         *store = '\t';
 402                         break;
 403                 case 'v':               /* vertical tab */
 404                         *store = '\v';
 405                         break;
 406                                         /* octal constant */
 407                 case '0': case '1': case '2': case '3':
 408                 case '4': case '5': case '6': case '7':
 409                         c = (!percent && *fmt == '0') ? 4 : 3;
 410                         for (value = 0;


   1 /*
   2  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
   3  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
   4  * Copyright (c) 1989, 1993
   5  *      The Regents of the University of California.  All rights reserved.
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  * 1. Redistributions of source code must retain the above copyright
  11  *    notice, this list of conditions and the following disclaimer.
  12  * 2. Redistributions in binary form must reproduce the above copyright
  13  *    notice, this list of conditions and the following disclaimer in the
  14  *    documentation and/or other materials provided with the distribution.
  15  * 4. Neither the name of the University nor the names of its contributors
  16  *    may be used to endorse or promote products derived from this software
  17  *    without specific prior written permission.
  18  *
  19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29  * SUCH DAMAGE.
  30  */
  31 
  32 #include <sys/types.h>
  33 
  34 #include <err.h>
  35 #include <errno.h>
  36 #include <inttypes.h>
  37 #include <limits.h>
  38 #include <stdio.h>
  39 #include <stdlib.h>
  40 #include <string.h>
  41 #include <unistd.h>
  42 #include <alloca.h>
  43 #include <ctype.h>
  44 #include <locale.h>
  45 #include <note.h>
  46 
  47 #define warnx1(a, b, c)         warnx(a)
  48 #define warnx2(a, b, c)         warnx(a, b)
  49 #define warnx3(a, b, c)         warnx(a, b, c)
  50 
  51 #define PTRDIFF(x, y)   ((uintptr_t)(x) - (uintptr_t)(y))
  52 
  53 #define _(x)    gettext(x)
  54 
  55 #define PF(f, func) do {                                                \
  56         char *b = NULL;                                                 \





  57         if (havewidth)                                                  \
  58                 if (haveprec)                                           \
  59                         (void) asprintf(&b, f, fieldwidth, precision, func); \
  60                 else                                                    \
  61                         (void) asprintf(&b, f, fieldwidth, func);   \
  62         else if (haveprec)                                              \
  63                 (void) asprintf(&b, f, precision, func);            \
  64         else                                                            \
  65                 (void) asprintf(&b, f, func);                               \
  66         if (b) {                                                        \
  67                 (void) fputs(b, stdout);                                \
  68                 free(b);                                                \
  69         }                                                               \


  70 _NOTE(CONSTCOND) } while (0)
  71 
  72 static int       asciicode(void);
  73 static char     *doformat(char *, int *);
  74 static int       escape(char *, int, size_t *);
  75 static int       getchr(void);
  76 static int       getfloating(long double *, int);
  77 static int       getint(int *);
  78 static int       getnum(intmax_t *, uintmax_t *, int);
  79 static const char
  80                 *getstr(void);
  81 static char     *mknum(char *, char);
  82 static void      usage(void);
  83 
  84 static const char digits[] = "0123456789";
  85 
  86 static int  myargc;
  87 static char **myargv;
  88 static char **gargv;
  89 static char **maxargv;
  90 
  91 int
  92 main(int argc, char *argv[])
  93 {
  94         size_t len;
  95         int end, rval;
  96         char *format, *fmt, *start;
  97 
  98         (void) setlocale(LC_ALL, "");
  99 
 100         argv++;
 101         argc--;
 102 
 103         /*
 104          * POSIX says: Standard utilities that do not accept options,
 105          * but that do accept operands, shall recognize "--" as a
 106          * first argument to be discarded.
 107          */
 108         if (argc && strcmp(argv[0], "--") == 0) {
 109                 argc--;
 110                 argv++;
 111         }
 112 
 113         if (argc < 1) {
 114                 usage();
 115                 return (1);
 116         }
 117 
 118         /*
 119          * Basic algorithm is to scan the format string for conversion
 120          * specifications -- once one is found, find out if the field
 121          * width or precision is a '*'; if it is, gather up value.  Note,
 122          * format strings are reused as necessary to use up the provided
 123          * arguments, arguments of zero/null string are provided to use
 124          * up the format string.
 125          */
 126         fmt = format = *argv;
 127         (void) escape(fmt, 1, &len);        /* backslash interpretation */
 128         rval = end = 0;
 129         gargv = ++argv;
 130 
 131         for (;;) {
 132                 maxargv = gargv;
 133 
 134                 myargv = gargv;
 135                 for (myargc = 0; gargv[myargc]; myargc++)
 136                         /* nop */;
 137                 start = fmt;
 138                 while (fmt < format + len) {
 139                         if (fmt[0] == '%') {
 140                                 (void) fwrite(start, 1, PTRDIFF(fmt, start),
 141                                     stdout);
 142                                 if (fmt[1] == '%') {
 143                                         /* %% prints a % */
 144                                         (void) putchar('%');
 145                                         fmt += 2;
 146                                 } else {
 147                                         fmt = doformat(fmt, &rval);
 148                                         if (fmt == NULL)
 149                                                 return (1);
 150                                         end = 0;
 151                                 }
 152                                 start = fmt;
 153                         } else
 154                                 fmt++;
 155                         if (gargv > maxargv)
 156                                 maxargv = gargv;
 157                 }
 158                 gargv = maxargv;
 159 
 160                 if (end == 1) {
 161                         warnx1(_("missing format character"), NULL, NULL);
 162                         return (1);
 163                 }
 164                 (void) fwrite(start, 1, PTRDIFF(fmt, start), stdout);
 165                 if (!*gargv)
 166                         return (rval);
 167                 /* Restart at the beginning of the format string. */
 168                 fmt = format;
 169                 end = 1;
 170         }
 171         /* NOTREACHED */
 172 }
 173 
 174 
 175 static char *
 176 doformat(char *fmt, int *rval)
 177 {
 178         static const char skip1[] = "#'-+ 0";


 179         int fieldwidth, haveprec, havewidth, mod_ldbl, precision;
 180         char convch, nextch;
 181         char *start;
 182         char **fargv;
 183         char *dptr;
 184         int l;
 185 
 186         start = alloca(strlen(fmt) + 1);
 187 
 188         dptr = start;
 189         *dptr++ = '%';
 190         *dptr = 0;
 191 
 192         fmt++;
 193 
 194         /* look for "n$" field index specifier */
 195         l = strspn(fmt, digits);
 196         if ((l > 0) && (fmt[l] == '$')) {
 197                 int idx = atoi(fmt);
 198                 if (idx <= myargc) {
 199                         gargv = &myargv[idx - 1];
 200                 } else {
 201                         gargv = &myargv[myargc];
 202                 }
 203                 if (gargv > maxargv) {
 204                         maxargv = gargv;
 205                 }
 206                 fmt += l + 1;
 207 
 208                 /* save format argument */
 209                 fargv = gargv;
 210         } else {
 211                 fargv = NULL;
 212         }
 213 
 214         /* skip to field width */
 215         while (strchr(skip1, *fmt) != NULL) {
 216                 *dptr++ = *fmt++;
 217                 *dptr = 0;
 218         }
 219 
 220 
 221         if (*fmt == '*') {
 222 
 223                 fmt++;
 224                 l = strspn(fmt, digits);
 225                 if ((l > 0) && (fmt[l] == '$')) {
 226                         int idx = atoi(fmt);
 227                         if (idx <= myargc) {
 228                                 gargv = &myargv[idx - 1];
 229                         } else {
 230                                 gargv = &myargv[myargc];
 231                         }
 232                         fmt += l + 1;
 233                 }
 234 
 235                 if (getint(&fieldwidth))
 236                         return (NULL);
 237                 if (gargv > maxargv) {
 238                         maxargv = gargv;
 239                 }
 240                 havewidth = 1;
 241 
 242                 *dptr++ = '*';
 243                 *dptr = 0;
 244         } else {
 245                 havewidth = 0;
 246 
 247                 /* skip to possible '.', get following precision */
 248                 while (isdigit(*fmt)) {
 249                         *dptr++ = *fmt++;
 250                         *dptr = 0;
 251                 }
 252         }
 253 
 254         if (*fmt == '.') {
 255                 /* precision present? */
 256                 fmt++;
 257                 *dptr++ = '.';
 258 
 259                 if (*fmt == '*') {
 260 
 261                         fmt++;
 262                         l = strspn(fmt, digits);
 263                         if ((l > 0) && (fmt[l] == '$')) {
 264                                 int idx = atoi(fmt);
 265                                 if (idx <= myargc) {
 266                                         gargv = &myargv[idx - 1];
 267                                 } else {
 268                                         gargv = &myargv[myargc];
 269                                 }
 270                                 fmt += l + 1;
 271                         }
 272 
 273                         if (getint(&precision))
 274                                 return (NULL);
 275                         if (gargv > maxargv) {
 276                                 maxargv = gargv;
 277                         }
 278                         haveprec = 1;
 279                         *dptr++ = '*';
 280                         *dptr = 0;
 281                 } else {
 282                         haveprec = 0;
 283 
 284                         /* skip to conversion char */
 285                         while (isdigit(*fmt)) {
 286                                 *dptr++ = *fmt++;
 287                                 *dptr = 0;
 288                         }
 289                 }
 290         } else
 291                 haveprec = 0;
 292         if (!*fmt) {
 293                 warnx1(_("missing format character"), NULL, NULL);
 294                 return (NULL);
 295         }
 296         *dptr++ = *fmt;
 297         *dptr = 0;
 298 
 299         /*
 300          * Look for a length modifier.  POSIX doesn't have these, so
 301          * we only support them for floating-point conversions, which
 302          * are extensions.  This is useful because the L modifier can
 303          * be used to gain extra range and precision, while omitting
 304          * it is more likely to produce consistent results on different
 305          * architectures.  This is not so important for integers
 306          * because overflow is the only bad thing that can happen to
 307          * them, but consider the command  printf %a 1.1
 308          */
 309         if (*fmt == 'L') {
 310                 mod_ldbl = 1;
 311                 fmt++;
 312                 if (!strchr("aAeEfFgG", *fmt)) {
 313                         warnx2(_("bad modifier L for %%%c"), *fmt, NULL);
 314                         return (NULL);
 315                 }
 316         } else {
 317                 mod_ldbl = 0;
 318         }
 319 
 320         /* save the current arg offset, and set to the format arg */
 321         if (fargv != NULL) {
 322                 gargv = fargv;
 323         }
 324 
 325         convch = *fmt;
 326         nextch = *++fmt;
 327 
 328         *fmt = '\0';
 329         switch (convch) {
 330         case 'b': {
 331                 size_t len;
 332                 char *p;
 333                 int getout;
 334 
 335                 p = strdup(getstr());
 336                 if (p == NULL) {
 337                         warnx2("%s", strerror(ENOMEM), NULL);
 338                         return (NULL);
 339                 }
 340                 getout = escape(p, 0, &len);
 341                 (void) fputs(p, stdout);


 342                 free(p);
 343 
 344                 if (getout)
 345                         exit(*rval);
 346                 break;
 347         }
 348         case 'c': {
 349                 char p;
 350 
 351                 p = getchr();
 352                 PF(start, p);
 353                 break;
 354         }
 355         case 's': {
 356                 const char *p;
 357 
 358                 p = getstr();
 359                 PF(start, p);
 360                 break;
 361         }
 362         case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
 363                 char *f;
 364                 intmax_t val;
 365                 uintmax_t uval;


 378         }
 379         case 'e': case 'E':
 380         case 'f': case 'F':
 381         case 'g': case 'G':
 382         case 'a': case 'A': {
 383                 long double p;
 384 
 385                 if (getfloating(&p, mod_ldbl))
 386                         *rval = 1;
 387                 if (mod_ldbl)
 388                         PF(start, p);
 389                 else
 390                         PF(start, (double)p);
 391                 break;
 392         }
 393         default:
 394                 warnx2(_("illegal format character %c"), convch, NULL);
 395                 return (NULL);
 396         }
 397         *fmt = nextch;
 398 
 399         /* return the gargv to the next element */
 400         return (fmt);
 401 }
 402 
 403 static char *
 404 mknum(char *str, char ch)
 405 {
 406         static char *copy;
 407         static size_t copy_size;
 408         char *newcopy;
 409         size_t len, newlen;
 410 
 411         len = strlen(str) + 2;
 412         if (len > copy_size) {
 413                 newlen = ((len + 1023) >> 10) << 10;
 414                 if ((newcopy = realloc(copy, newlen)) == NULL) {
 415                         warnx2("%s", strerror(ENOMEM), NULL);
 416                         return (NULL);
 417                 }
 418                 copy = newcopy;
 419                 copy_size = newlen;


 437                         *store = c;
 438                         continue;
 439                 }
 440                 switch (*++fmt) {
 441                 case '\0':              /* EOS, user error */
 442                         *store = '\\';
 443                         *++store = '\0';
 444                         *len = PTRDIFF(store, save);
 445                         return (0);
 446                 case '\\':              /* backslash */
 447                 case '\'':              /* single quote */
 448                         *store = *fmt;
 449                         break;
 450                 case 'a':               /* bell/alert */
 451                         *store = '\a';
 452                         break;
 453                 case 'b':               /* backspace */
 454                         *store = '\b';
 455                         break;
 456                 case 'c':
 457                         if (!percent) {
 458                                 *store = '\0';
 459                                 *len = PTRDIFF(store, save);
 460                                 return (1);
 461                         }
 462                         *store = 'c';
 463                         break;
 464                 case 'f':               /* form-feed */
 465                         *store = '\f';
 466                         break;
 467                 case 'n':               /* newline */
 468                         *store = '\n';
 469                         break;
 470                 case 'r':               /* carriage-return */
 471                         *store = '\r';
 472                         break;
 473                 case 't':               /* horizontal tab */
 474                         *store = '\t';
 475                         break;
 476                 case 'v':               /* vertical tab */
 477                         *store = '\v';
 478                         break;
 479                                         /* octal constant */
 480                 case '0': case '1': case '2': case '3':
 481                 case '4': case '5': case '6': case '7':
 482                         c = (!percent && *fmt == '0') ? 4 : 3;
 483                         for (value = 0;