Print this page
4854 printf(1) doesn't support %b and \c properly

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/printf/printf.c
          +++ new/usr/src/cmd/printf/printf.c
↓ open down ↓ 84 lines elided ↑ open up ↑
  85   85  
  86   86  static int  myargc;
  87   87  static char **myargv;
  88   88  static char **gargv;
  89   89  static char **maxargv;
  90   90  
  91   91  int
  92   92  main(int argc, char *argv[])
  93   93  {
  94   94          size_t len;
  95      -        int chopped, end, rval;
       95 +        int end, rval;
  96   96          char *format, *fmt, *start;
  97   97  
  98   98          (void) setlocale(LC_ALL, "");
  99   99  
 100  100          argv++;
 101  101          argc--;
 102  102  
 103  103          /*
 104  104           * POSIX says: Standard utilities that do not accept options,
 105  105           * but that do accept operands, shall recognize "--" as a
↓ open down ↓ 11 lines elided ↑ open up ↑
 117  117  
 118  118          /*
 119  119           * Basic algorithm is to scan the format string for conversion
 120  120           * specifications -- once one is found, find out if the field
 121  121           * width or precision is a '*'; if it is, gather up value.  Note,
 122  122           * format strings are reused as necessary to use up the provided
 123  123           * arguments, arguments of zero/null string are provided to use
 124  124           * up the format string.
 125  125           */
 126  126          fmt = format = *argv;
 127      -        chopped = escape(fmt, 1, &len);         /* backslash interpretation */
      127 +        (void) escape(fmt, 1, &len);    /* backslash interpretation */
 128  128          rval = end = 0;
 129  129          gargv = ++argv;
 130  130  
 131  131          for (;;) {
 132  132                  maxargv = gargv;
 133  133  
 134  134                  myargv = gargv;
 135  135                  for (myargc = 0; gargv[myargc]; myargc++)
 136  136                          /* nop */;
 137  137                  start = fmt;
↓ open down ↓ 17 lines elided ↑ open up ↑
 155  155                          if (gargv > maxargv)
 156  156                                  maxargv = gargv;
 157  157                  }
 158  158                  gargv = maxargv;
 159  159  
 160  160                  if (end == 1) {
 161  161                          warnx1(_("missing format character"), NULL, NULL);
 162  162                          return (1);
 163  163                  }
 164  164                  (void) fwrite(start, 1, PTRDIFF(fmt, start), stdout);
 165      -                if (chopped || !*gargv)
      165 +                if (!*gargv)
 166  166                          return (rval);
 167  167                  /* Restart at the beginning of the format string. */
 168  168                  fmt = format;
 169  169                  end = 1;
 170  170          }
 171  171          /* NOTREACHED */
 172  172  }
 173  173  
 174  174  
 175  175  static char *
↓ open down ↓ 155 lines elided ↑ open up ↑
 331  331                  size_t len;
 332  332                  char *p;
 333  333                  int getout;
 334  334  
 335  335                  p = strdup(getstr());
 336  336                  if (p == NULL) {
 337  337                          warnx2("%s", strerror(ENOMEM), NULL);
 338  338                          return (NULL);
 339  339                  }
 340  340                  getout = escape(p, 0, &len);
 341      -                *(fmt - 1) = 's';
 342      -                PF(start, p);
 343      -                *(fmt - 1) = 'b';
      341 +                (void) fputs(p, stdout);
 344  342                  free(p);
 345  343  
 346  344                  if (getout)
 347      -                        return (fmt);
      345 +                        exit(*rval);
 348  346                  break;
 349  347          }
 350  348          case 'c': {
 351  349                  char p;
 352  350  
 353  351                  p = getchr();
 354  352                  PF(start, p);
 355  353                  break;
 356  354          }
 357  355          case 's': {
↓ open down ↓ 91 lines elided ↑ open up ↑
 449  447                  case '\'':              /* single quote */
 450  448                          *store = *fmt;
 451  449                          break;
 452  450                  case 'a':               /* bell/alert */
 453  451                          *store = '\a';
 454  452                          break;
 455  453                  case 'b':               /* backspace */
 456  454                          *store = '\b';
 457  455                          break;
 458  456                  case 'c':
 459      -                        *store = '\0';
 460      -                        *len = PTRDIFF(store, save);
 461      -                        return (1);
      457 +                        if (!percent) {
      458 +                                *store = '\0';
      459 +                                *len = PTRDIFF(store, save);
      460 +                                return (1);
      461 +                        }
      462 +                        *store = 'c';
      463 +                        break;
 462  464                  case 'f':               /* form-feed */
 463  465                          *store = '\f';
 464  466                          break;
 465  467                  case 'n':               /* newline */
 466  468                          *store = '\n';
 467  469                          break;
 468  470                  case 'r':               /* carriage-return */
 469  471                          *store = '\r';
 470  472                          break;
 471  473                  case 't':               /* horizontal tab */
↓ open down ↓ 155 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX