Subject: Re: new stdlib?
To: Paul Sander <paul@wakawaka.com>
From: Michael Richardson <mcr@sandelman.ottawa.on.ca>
List: netbsd-help
Date: 02/09/2000 21:14:58
>>>>> "Paul" == Paul Sander <paul@wakawaka.com> writes:
Paul> The %m$nd format has to do with the XPG4 standard, as defined by The Open
Paul> Group (formerly X/Open and OSF, I believe). It allows the rearrangement of
Paul> the arguments with regard to localization.
My understanding is that this only works if you can guarantee that all
arguments are of the same size, which is not the case in ANSI C anymore since
you don't have to do default promotion rules.
I wonder how they get it to work?
Paul> An example of this usage would be localizing the display of a date. To
Paul> show a date in American format, you could use something like this:
Paul> sprintf( c_datestring, "%2$02d/%1$02d/%3$04d",
Paul> (int)tmdate.tm_mday, (int)tmdate.tm_mon+1,
Paul> (int)tmdate.tm_year+1900 );
Paul> But the European format reverses the month and day, like this:
Paul> sprintf( c_datestring, "%1$02d/%2$02d/%3$04d",
Paul> (int)tmdate.tm_mday, (int)tmdate.tm_mon+1,
Paul> (int)tmdate.tm_year+1900 );
Paul> The value of this becomes apparent when the format string itself is
Paul> read from a message catalog.
Paul> You can find details on www.opengroup.org and searching for sprintf in
Paul> "The Single Unix Specification" (which you must hunt for, and register
Paul> to read for free).
Paul> --- Forwarded mail from wulf@ping.net.au
Paul> does anyone know the following construct? It was found in anteater
Paul> which segfaults at that point.
Paul> [...]
Paul> sprintf( c_datestring, "year %1$02d, day %2$02d, month %3$04d",
Paul> (int)tmdate.tm_mday, (int)tmdate.tm_mon+1,
Paul> (int)tmdate.tm_year+1900 );
Paul> string newdate = c_datestring;
Paul> return newdate;
Paul> I've expanded the format string which is constructed earlier
Paul> in the code. I've never seen the use of a "%1$02d" format string nor have I
Paul> found any references in books and manuals. When changed to
Paul> "%02d" the program runs fine and displays the correct date format.
Paul> Is there a new stdlib with extentions of the current format string? I am
Paul> using egcs-1.1.1 and standard libraries as shipped with NetBSD-1.4.1.
Paul> --- End of forwarded message from wulf@ping.net.au