Print this page
4818 printf(1) should support n$ width and precision specifiers
4854 printf(1) doesn't support %b and \c properly
Reviewed by: Keith Wesolowski <keith.wesolowski@joyent.com>
Approved by: TBD

Split Close
Expand all
Collapse all
          --- old/usr/src/man/man1/printf.1
          +++ new/usr/src/man/man1/printf.1
   1    1  '\" te
        2 +.\" Copyright 2014 Garrett D'Amore <garrett@damore.org>
   2    3  .\" Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved
   3    4  .\" Copyright 1992, X/Open Company Limited All Rights Reserved
   4    5  .\" Portions Copyright (c) 1982-2007 AT&T Knowledge Ventures
   5      -.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at  http://www.opengroup.org/bookstore/.
   6      -.\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text
   7      -.\" are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical
   8      -.\" and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
   9      -.\"  This notice shall appear on any product containing this material.
  10      -.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License. You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.
  11      -.\"  See the License for the specific language governing permissions and limitations under the License. When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with
  12      -.\" the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
  13      -.TH PRINTF 1 "Aug 11, 2009"
        6 +.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
        7 +.\" permission to reproduce portions of its copyrighted documentation.
        8 +.\" Original documentation from The Open Group can be obtained online at
        9 +.\" http://www.opengroup.org/bookstore/.
       10 +.\" The Institute of Electrical and Electronics Engineers and The Open Group,
       11 +.\" have given us permission to reprint portions of their documentation. In the
       12 +.\" following statement, the phrase "this text" refers to portions of the
       13 +.\" system documentation. Portions of this text are reprinted and reproduced in
       14 +.\" electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004
       15 +.\" Edition, Standard for Information Technology -- Portable Operating System
       16 +.\" Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright
       17 +.\" (C) 2001-2004 by the Institute of Electrical and Electronics Engineers,
       18 +.\" Inc and The Open Group. In the event of any discrepancy between these
       19 +.\" versions and the original IEEE and The Open Group Standard, the original
       20 +.\" IEEE and The Open Group Standard is the referee document. The original
       21 +.\" Standard can be obtained online at
       22 +.\" http://www.opengroup.org/unix/online.html.
       23 +.\" This notice shall appear on any product containing this material.
       24 +.\" The contents of this file are subject to the terms of the Common
       25 +.\" Development and Distribution License (the "License").  You may not use this
       26 +.\" file except in compliance with the License.
       27 +.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or
       28 +.\" http://www.opensolaris.org/os/licensing.  See the License for the specific
       29 +.\" language governing permissions and limitations under the License.
       30 +.\" When distributing Covered Code, include this CDDL HEADER in each file and
       31 +.\" include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable,
       32 +.\" add the following below this CDDL HEADER, with the fields enclosed by
       33 +.\" brackets "[]" replaced with your own identifying information:
       34 +.\" Portions Copyright [yyyy] [name of copyright owner]
       35 +.TH PRINTF 1 "May 11, 2014"
  14   36  .SH NAME
  15   37  printf \- write formatted output
  16   38  .SH SYNOPSIS
  17   39  .SS "/usr/bin/printf"
  18   40  .LP
  19   41  .nf
  20   42  \fBprintf\fR \fIformat\fR [\fIargument\fR]...
  21   43  .fi
  22   44  
  23   45  .SS "ksh93"
↓ open down ↓ 59 lines elided ↑ open up ↑
  83  105  .TP
  84  106  .ie t \(bu
  85  107  .el o
  86  108  The program does not precede output from the \fBo\fR conversion specification
  87  109  with zeros not specified by the \fIformat\fR operand.
  88  110  .RE
  89  111  .RS +4
  90  112  .TP
  91  113  .ie t \(bu
  92  114  .el o
      115 +The argument used for the conversion character (or width or precision
      116 +parameters, see below) may be taken from the \fIn\fRnth argument instead
      117 +of the next unused argument, by specifying \fIn\fR\fB$\fR immediately following
      118 +the \fB%\fR character, or the \fB*\fR character (for width or precision
      119 +arguments).
      120 +If \fIn\fR\fB$\fR appears in any conversions in the format string,
      121 +then it must be used for all conversions, including any variable width or
      122 +precision specifiers.
      123 +.RE
      124 +.RS +4
      125 +.TP
      126 +.ie t \(bu
      127 +.el o
      128 +The special character \fB*\fR may be used instead of a string of decimal digits
      129 +to indicate a minimum field width or a precision.  In this case the next
      130 +available argument is used (or the \fIn\fRth if the form \fIn\fR\fB$\fR is
      131 +used), treating its value as a decimal string. 
      132 +.RE
      133 +.RS +4
      134 +.TP
      135 +.ie t \(bu
      136 +.el o
  93  137  An additional conversion character, \fBb\fR, is supported as follows. The
  94  138  argument is taken to be a string that can contain backslash-escape sequences.
  95  139  The following backslash-escape sequences are supported:
  96  140  .RS +4
  97  141  .TP
  98  142  .ie t \(bu
  99  143  .el o
 100  144  the escape sequences listed on the \fBformats\fR(5) manual page (\fB\e\e\fR,
 101  145  \fB\ea\fR, \fB\eb\fR, \fB\ef\fR, \fB\en\fR, \fB\er\fR, \fB\et\fR, \fB\ev\fR),
 102  146  which are converted to the characters they represent
↓ open down ↓ 20 lines elided ↑ open up ↑
 123  167  .sp
 124  168  Bytes from the converted string are written until the end of the string or the
 125  169  number of bytes indicated by the precision specification is reached. If the
 126  170  precision is omitted, it is taken to be infinite, so all bytes up to the end of
 127  171  the converted string are written. For each specification that consumes an
 128  172  argument, the next argument operand is evaluated and converted to the
 129  173  appropriate type for the conversion as specified below. The \fIformat\fR
 130  174  operand is reused as often as necessary to satisfy the argument operands. Any
 131  175  extra \fBc\fR or \fBs\fR conversion specifications are evaluated as if a null
 132  176  string argument were supplied; other extra conversion specifications are
 133      -evaluated as if a zero argument were supplied. If the \fIformat\fR operand
      177 +evaluated as if a zero argument were supplied.
      178 +.sp
      179 +When there are more argument operands than format specifiers, and the
      180 +format includes \fIn\fR\fB$\fR position indicators, then the format is
      181 +reprocessed from the beginning as above, but with the argument list starting
      182 +from the next argument after the highest \fIn\fRth argument previously
      183 +encountered.
      184 +.sp
      185 +If the \fIformat\fR operand
 134  186  contains no conversion specifications and \fIargument\fR operands are present,
 135  187  the results are unspecified. If a character sequence in the \fIformat\fR
 136  188  operand begins with a \fB%\fR character, but does not form a valid conversion
 137  189  specification, the behavior is unspecified.
 138  190  .RE
 139  191  
 140  192  .sp
 141  193  .ne 2
 142  194  .na
 143  195  \fB\fIargument\fR\fR
↓ open down ↓ 235 lines elided ↑ open up ↑
 379  431  .TP
 380  432  .ie t \(bu
 381  433  .el o
 382  434  The escape sequence \fB\eC[.\fR\fIname\fR\fB\&.]\fR expands to the collating
 383  435  element \fIname\fR.
 384  436  .RE
 385  437  .RS +4
 386  438  .TP
 387  439  .ie t \(bu
 388  440  .el o
 389      -The escape sequence \fB\ex{hex}\fRexpands to the character corresponding to the
 390      -hexadecimal value \fBhex\fR.
      441 +The escape sequence \fB\ex{hex}\fR expands to the character corresponding to
      442 +the hexadecimal value \fBhex\fR.
 391  443  .RE
 392  444  .RS +4
 393  445  .TP
 394  446  .ie t \(bu
 395  447  .el o
 396  448  The format modifier flag = can be used to center a field to a specified width.
 397  449  When the output is a terminal, the character width is used rather than the
 398  450  number of bytes.
 399  451  .RE
 400  452  .RS +4
↓ open down ↓ 23 lines elided ↑ open up ↑
 424  476  .RE
 425  477  .sp
 426  478  .LP
 427  479  If there are more \fIstring\fR operands than format specifiers, the format
 428  480  string is reprocessed from the beginning. If there are fewer \fIstring\fR
 429  481  operands than format specifiers, then \fIstring\fR specifiers are treated as if
 430  482  empty strings were supplied, numeric conversions are treated as if \fB0\fR was
 431  483  supplied, and time conversions are treated as if \fBnow\fR was supplied.
 432  484  .sp
 433  485  .LP
      486 +When there are more argument operands than format specifiers, and the
      487 +format includes \fIn\fR\fB$\fR position indicators, then the format is
      488 +reprocessed from the beginning as above, but with the argument list starting
      489 +from the next argument after the highest \fIn\fRth argument previously
      490 +encountered.
      491 +.sp
      492 +.LP
 434  493  \fB/usr/bin/printf\fR is equivalent to \fBksh93\fR's \fBprintf\fR built-in and
 435  494  \fBprint -f\fR, which allows additional options to be specified.
 436  495  .SH USAGE
 437  496  .SS "/usr/bin/printf"
 438  497  .sp
 439  498  .LP
 440  499  The \fBprintf\fR utility, like the \fBprintf\fR(3C) function on which it is
 441  500  based, makes no special provision for dealing with multi-byte characters when
 442  501  using the \fB%c\fR conversion specification. Applications should be extremely
 443  502  cautious using either of these features when there are multi-byte characters in
 444  503  the character set.
 445  504  .sp
 446  505  .LP
 447      -Field widths and precisions cannot be specified as \fB*\fR.
 448      -.sp
 449      -.LP
 450  506  The \fB%b\fR conversion specification is not part of the ISO C standard; it has
 451  507  been added here as a portable way to process backslash escapes expanded in
 452  508  string operands as provided by the \fBecho\fR utility. See also the USAGE
 453  509  section of the \fBecho\fR(1) manual page for ways to use \fBprintf\fR as a
 454  510  replacement for all of the traditional versions of the \fBecho\fR utility.
 455  511  .sp
 456  512  .LP
 457  513  If an argument cannot be parsed correctly for the corresponding conversion
 458  514  specification, the \fBprintf\fR utility reports an error. Thus, overflow and
 459  515  extraneous characters at the end of an argument being used for a numeric
↓ open down ↓ 371 lines elided ↑ open up ↑
 831  887  .in -2
 832  888  .sp
 833  889  
 834  890  .sp
 835  891  .LP
 836  892  Note that the '$' characters must be properly escaped, such as
 837  893  
 838  894  .sp
 839  895  .in +2
 840  896  .nf
 841      -"%1\$s, %3\$d. %2\$s, %4\$d:%5\$.2d\en" in this case
      897 +"%1\e$s, %3\e$d. %2\e$s, %4\e$d:%5\e$.2d\en" in this case
 842  898  .fi
 843  899  .in -2
 844  900  .sp
 845  901  
 846  902  .sp
 847  903  .LP
 848  904  producing the message:
 849  905  
 850  906  .sp
 851  907  .in +2
↓ open down ↓ 93 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX