Project

General

Profile

Bug #3248 » 0005-printf-1-sync-with-FreeBSD.patch

piecuch, 09/16/2020 11:16 AM

View differences:

usr.bin/printf/printf.1
.\" @(#)printf.1 8.1 (Berkeley) 6/6/93
.\" $FreeBSD: head/usr.bin/printf/printf.1 264743 2014-04-21 22:47:18Z pfg $
.\"
.Dd January 11, 2016
.Dd July 1, 2020
.Dt PRINTF 1
.Os
.Sh NAME
......
Write a <bell> character.
.It Cm \eb
Write a <backspace> character.
.It Cm \ec
Ignore remaining characters in this string.
.It Cm \ef
Write a <form-feed> character.
.It Cm \en
......
octal escapes are
.Cm \e0 Ns Ar num
instead of
.Cm \e Ns Ar num .
.Cm \e Ns Ar num
and that an additional escape sequence
.Cm \ec
stops further output from this
.Nm
invocation.
.It Cm n$
Allows reordering of the output according to
.Ar argument .
......
manual page.
.Sh EXIT STATUS
.Ex -std
.Sh EXAMPLES
Print the string
.Qq hello :
.Bd -literal -offset indent
$ printf "%s\en" hello
hello
.Ed
.Pp
Same as above, but notice that the format string is not quoted and hence we
do not get the expected behavior:
.Bd -literal -offset indent
$ printf %s\en hello
hellon$
.Ed
.Pp
Print arguments forcing sign only for the first argument:
.Bd -literal -offset indent
$ printf "%+d\en%d\en%d\en" 1 -2 13
+1
-2
13
.Ed
.Pp
Same as above, but the single format string will be applied to the three
arguments:
.Bd -literal -offset indent
$ printf "%+d\en" 1 -2 13
+1
-2
+13
.Ed
.Pp
Print number using only two digits after the decimal point:
.Bd -literal -offset indent
$ printf "%.2f\en" 31.7456
31.75
.Ed
.Sh COMPATIBILITY
The traditional
.Bx
behavior of converting arguments of numeric formats not beginning
with a digit to the
.Tn ASCII
with a digit to the ASCII
code of the first character is not supported.
.Sh SEE ALSO
.Xr builtin 1 ,
......
after the standard library function,
.Xr printf 3 .
.Sh CAVEATS
.Tn ANSI
hexadecimal character constants were deliberately not provided.
ANSI hexadecimal character constants were deliberately not provided.
.Pp
If the locale contains multibyte characters
(such as UTF-8),
......
formats with a precision
may not operate as expected.
.Sh BUGS
Since the floating point numbers are translated from
.Tn ASCII
to floating-point and
then back again, floating-point precision may be lost.
Since the floating point numbers are translated from ASCII
to floating-point and then back again, floating-point precision may be lost.
(By default, the number is translated to an IEEE-754 double-precision
value before being printed.
The
usr.bin/printf/printf.c
/*-
* 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
......
#ifdef SHELL
#define main printfcmd
#include "bltin/bltin.h"
#include "error.h"
#include "options.h"
#endif
#define PF(f, func) do { \
char *b = NULL; \
if (havewidth) \
if (haveprec) \
asprintf(&b, f, fieldwidth, precision, func); \
printf(f, fieldwidth, precision, func); \
else \
asprintf(&b, f, fieldwidth, func); \
printf(f, fieldwidth, func); \
else if (haveprec) \
asprintf(&b, f, precision, func); \
printf(f, precision, func); \
else \
asprintf(&b, f, func); \
if (b) { \
fputs(b, stdout); \
free(b); \
} \
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);
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': {
(9-9/10)