Subject: standards/19556: printf: %n$ type format strings not supported
To: None <gnats-bugs@gnats.netbsd.org>
From: None <wiz@netbsd.org>
List: netbsd-bugs
Date: 12/25/2002 05:41:06
>Number:         19556
>Category:       standards
>Synopsis:       printf: %n$ type format strings not supported
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    standards-manager
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Dec 24 20:42:00 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     Thomas Klausner
>Release:        NetBSD 1.6K
>Organization:
>Environment:
System: NetBSD hiro.dynup.net 1.6K NetBSD 1.6K (HIRO) #0: Sat Dec 21 04:53:27 CET 2002 wiz@hiro.dynup.net:/disk/archive/cvs/src/sys/arch/i386/compile/HIRO i386
Architecture: i386
Machine: i386
>Description:
The %n$ conversion character is not supported by our printf functions.
An excerpt from SUSv2 describing it:

Conversions can be applied to the nth argument after the format in the argument 
list, rather than to the next unused argument. In this case, the conversion
character % (see below) is replaced by the sequence %n$ where n is a decimal
integer in the range [1, {NL_ARGMAX}], giving the position of the argument in
the argument list. This feature provides for the definition of format strings
that select arguments in an order appropriate to specific languages (see the
EXAMPLES section). 

In format strings containing the %n$ form of conversion specifications, numbered
arguments in the argument list can be referenced from the format string as many 
times as required.

>How-To-Repeat:
Compile the following program (based on a glib2 configure test):

#include <stdio.h>

int
main (void)
{
    char buffer[128];

    sprintf (buffer, "%2\$d %3\$d %1\$d", 1, 2, 3);
    if (strcmp ("2 3 1", buffer) == 0) {
	printf("Success!\n");
	exit(0);
    }
    printf("Result is not 2 3 1, but ``%s''\n", buffer);
    exit (1);
}

Output is:
Result is not 2 3 1, but `` $d   $d $d''

>Fix:
Not provided.
>Release-Note:
>Audit-Trail:
>Unformatted: