Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 5 Jun 2018 13:53:37 +0000 (UTC)
From:      Konstantin Belousov <kib@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org
Subject:   svn commit: r334655 - stable/11/lib/libc/stdio
Message-ID:  <201806051353.w55Drb7X087344@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kib
Date: Tue Jun  5 13:53:37 2018
New Revision: 334655
URL: https://svnweb.freebsd.org/changeset/base/334655

Log:
  MFC r334031:
  Implement printf(3) family %m format string extension.

Modified:
  stable/11/lib/libc/stdio/printf.3
  stable/11/lib/libc/stdio/vfprintf.c
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/lib/libc/stdio/printf.3
==============================================================================
--- stable/11/lib/libc/stdio/printf.3	Tue Jun  5 13:46:18 2018	(r334654)
+++ stable/11/lib/libc/stdio/printf.3	Tue Jun  5 13:53:37 2018	(r334655)
@@ -32,7 +32,7 @@
 .\"     @(#)printf.3	8.1 (Berkeley) 6/4/93
 .\" $FreeBSD$
 .\"
-.Dd December 2, 2009
+.Dd May 22, 2018
 .Dt PRINTF 3
 .Os
 .Sh NAME
@@ -642,6 +642,12 @@ integer indicated by the
 .Vt "int *"
 (or variant) pointer argument.
 No argument is converted.
+.It Cm m
+Print the string representation of the error code stored in the
+.Dv errno
+variable at the beginning of the call, as returned by
+.Xr strerror 3 .
+No argument is taken.
 .It Cm %
 A
 .Ql %
@@ -749,6 +755,12 @@ and
 .Cm \&%U
 are not standard and
 are provided only for backward compatibility.
+The conversion format
+.Cm \&%m
+is also not standard and provides the popular extension from the
+.Tn GNU C
+library.
+.Pp
 The effect of padding the
 .Cm %p
 format with zeros (either by the
@@ -786,9 +798,11 @@ or the return value would be too large to be represent
 .El
 .Sh SEE ALSO
 .Xr printf 1 ,
+.Xr errno 2 ,
 .Xr fmtcheck 3 ,
 .Xr scanf 3 ,
 .Xr setlocale 3 ,
+.Xr strerror 3 ,
 .Xr wprintf 3
 .Sh STANDARDS
 Subject to the caveats noted in the
@@ -841,6 +855,12 @@ and
 .Fn vdprintf
 functions were added in
 .Fx 8.0 .
+The
+.Cm \&%m
+format extension first appeared in the
+.Tn GNU C
+library, and was implemented in
+.Fx 12.0 .
 .Sh BUGS
 The
 .Nm

Modified: stable/11/lib/libc/stdio/vfprintf.c
==============================================================================
--- stable/11/lib/libc/stdio/vfprintf.c	Tue Jun  5 13:46:18 2018	(r334654)
+++ stable/11/lib/libc/stdio/vfprintf.c	Tue Jun  5 13:53:37 2018	(r334655)
@@ -315,6 +315,7 @@ __vfprintf(FILE *fp, locale_t locale, const char *fmt0
 	int ret;		/* return value accumulator */
 	int width;		/* width from format (%8d), or 0 */
 	int prec;		/* precision from format; <0 for N/A */
+	int saved_errno;
 	char sign;		/* sign prefix (' ', '+', '-', or \0) */
 	struct grouping_state gs; /* thousands' grouping info */
 
@@ -464,6 +465,7 @@ __vfprintf(FILE *fp, locale_t locale, const char *fmt0
 	savserr = fp->_flags & __SERR;
 	fp->_flags &= ~__SERR;
 
+	saved_errno = errno;
 	convbuf = NULL;
 	fmt = (char *)fmt0;
 	argtable = NULL;
@@ -774,6 +776,11 @@ fp_common:
 			}
 			break;
 #endif /* !NO_FLOATING_POINT */
+		case 'm':
+			cp = strerror(saved_errno);
+			size = (prec >= 0) ? strnlen(cp, prec) : strlen(cp);
+			sign = '\0';
+			break;
 		case 'n':
 			/*
 			 * Assignment-like behavior is specified if the



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201806051353.w55Drb7X087344>