How should vwprintf() work?
Jakub Jelinek
jakub@redhat.com
Wed Jul 21 22:43:00 GMT 2004
On Wed, Jul 21, 2004 at 11:55:59PM +0200, Petter Reinholdtsen wrote:
>
> Based on this failure claim on
> <URL:http://www.dinkumware.com/conform_c.html>, I started looking on
> how vwprintf() work.
>
> - If L"" is passed as the format parameter to vwprintf a negative
> value is returned, indicating an error occurred.
>
> The vwprintf() function seem to terminate the program or block any
> printf() after it is called, or fail if a printf() was called first.
> Look at this example:
>
> % cat > x.c
> #include <stdio.h>
> #include <wchar.h>
> #include <stdarg.h>
> int main()
> {
> va_list args;
> int i;
> #if defined(TEST)
> printf("started\n");
> #endif /* TEST */
> i = vwprintf(L"", args);
> printf("retval: %d\n", i);
> printf("ended\n");
> return 0;
> }
> % cc -Wall x.c ; ./a.out
> x.c: In function `main':
> x.c:11: warning: implicit declaration of function `vwprintf'
> % cc -DTEST -Wall x.c ; ./a.out
> x.c: In function `main':
> x.c:11: warning: implicit declaration of function `vwprintf'
> started
> retval: -1
> ended
> %
>
> Is this a bug? I'm testing on Debian/Unstable.
No, that's correct behaviour.
See e.g. ISO C99, 7.19.2:
Each stream has an orientation. After a stream is associated with an
external file, but before any operations are performed on it, the stream is
without orientation. Once a wide character input/output function has been
applied to a stream without orientation, the stream becomes a wide-oriented
stream . Similarly, once a byte input/output function has been applied to a
stream without orientation, the stream becomes a byte-oriented stream . Only
a call to the freopen function or the fwide function can otherwise alter the
orientation of a stream. (A successful call to freopen removes any
orientation.)
If you compile without -DTEST, vwprintf sets wide orientation on stdout
and so later printf on it will fail (but e.g.
wprintf(L"retval: %d\n", i); wprintf(L"ended\n");
would work). If you compile with -DTEST, the first printf sets byte
orientation on stdout and thus vwprintf fails.
Jakub
More information about the Libc-alpha
mailing list