Re: [pgsql-hackers-win32] Repleacement for src/port/snprintf.c

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Nicolai Tufar <ntufar(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Peter Eisentraut <peter_e(at)gmx(dot)net>, Magnus Hagander <mha(at)sollentuna(dot)net>, pgsql-hackers(at)postgresql(dot)org, pgsql-hackers-win32(at)postgresql(dot)org, pgsql-patches(at)postgresql(dot)org, devrim(at)gunduz(dot)org
Subject: Re: [pgsql-hackers-win32] Repleacement for src/port/snprintf.c
Date: 2005-02-14 00:06:34
Message-ID: 200502140006.j1E06Yd23803@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-hackers-win32 pgsql-patches

Nicolai Tufar wrote:
> Hello all,
>
> I would like to submit my changes to src/port/snprintf.c to
> enable %n$ format placeholder replacement in snprintf() and
> vsnprintf(). Additionally I implemented a trivial printf().
>
> I also attach a diff for configure.in to include snprintf.o
> in pgport but I am sure it is not the right thing to do.
> Could someone give a hint on where I need to place such a
> definition.
>
> Please review my patch. as Tom Lane pointed out there
> are 150 messages in the following files that do not print
> properly:

It took me a while to understand this but I get it now. This is the
best explanation I have seen, from Linux 2.6:

One can also specify explicitly which argument is taken, at each place
where an argument is required, by writing '%m$' instead of '%' and '*m$'
instead of '*', where the decimal integer m denotes the position in the
argument list of the desired argument, indexed starting from 1. Thus,

printf("%*d", width, num);

and

printf("%2$*1$d", width, num);

are equivalent. The second style allows repeated references to the
same argument. The C99 standard does not include the style using '$',
which comes from the Single Unix Specification. If the style using '$'
is used, it must be used throughout for all conversions taking an
argument and all width and precision arguments, but it may be mixed with
'%%' formats which do not consume an argument. There may be no gaps in
the numbers of arguments specified using '$'; for example, if
arguments 1 and 3 are specified, argument 2 must also be specified
somewhere in the format string.

I can see why this would be useful for translations because it uncouples
the order of the printf arguments from the printf string. However, I
have learned that Win32, HP-UX, NetBSD 2.0, and BSD/OS do not support
this. This is probably because it is not in C99 but in SUS (see above).

Anyway, this is too large to put into 8.0, but I am attaching a patch
for 8.1 that has the proper configure tests to check if the C library
supports this behavior. If it does not, the build will use our
port/snprintf.c.

One problem with that is that our snprintf.c is not thread-safe. Seems
the increases use of it will require us to fix this soon. I have added
to TODO:

* Make src/port/snprintf.c thread-safe

One change to the original port is that there was a define of a union
with no name:

+ union{
+ void* value;
+ long_long numvalue;
+ double fvalue;
+ int charvalue;
+ };

As far as I know a union with no name is illegal. I just removed the
"union {" and the closing brace.

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

Attachment Content-Type Size
unknown_filename text/plain 14.8 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2005-02-14 01:07:58 Re: Design notes for BufMgrLock rewrite
Previous Message Tom Lane 2005-02-13 23:56:47 Re: Design notes for BufMgrLock rewrite

Browse pgsql-hackers-win32 by date

  From Date Subject
Next Message Nicolai Tufar 2005-02-15 14:29:27 Re: [pgsql-hackers-win32] Repleacement for src/port/snprintf.c
Previous Message Nicolai Tufar 2005-02-09 07:59:28 Repleacement for src/port/snprintf.c

Browse pgsql-patches by date

  From Date Subject
Next Message Bruce Momjian 2005-02-14 23:04:02 Cleanup for gettext() calls
Previous Message Tom Lane 2005-02-13 19:00:06 Re: [BUGS] BUG #1466: #maintenace_work_mem = 16384