This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[RESEND] Error in exemple code in glibc manual on section 6.3.5
- From: André Goddard Rosa <andre dot goddard at gmail dot com>
- To: libc-alpha at sources dot redhat dot com
- Date: Fri, 29 May 2009 13:34:30 -0300
- Subject: [RESEND] Error in exemple code in glibc manual on section 6.3.5
- References: <b8bf37780809090802x356993d8se77a356a747ad426@mail.gmail.com>
Hi, glibc mailing list!
I found an error in the glibc manual. Do I need to file a bug
for fixing the manual?
I did not find this information here:
http://www.gnu.org/software/libc/bugs.html
--
On section 6.3.5 - A Complete Multibyte Conversion Example the manual
describes the following algorithm:
int
file_mbsrtowcs (int input, int output)
{
/* Note the use of MB_LEN_MAX.
MB_CUR_MAX cannot portably be used here. */
char buffer[BUFSIZ + MB_LEN_MAX];
mbstate_t state;
int filled = 0;
int eof = 0;
/* Initialize the state. */
memset (&state, '\0', sizeof (state));
while (!eof)
{
ssize_t nread;
ssize_t nwrite;
char *inp = buffer;
wchar_t outbuf[BUFSIZ];
wchar_t *outp = outbuf;
/* Fill up the buffer from the input file. */
nread = read (input, buffer + filled, BUFSIZ);
if (nread < 0)
{
perror ("read");
return 0;
}
/* If we reach end of file, make a note to read no more. */
if (nread == 0)
eof = 1;
/* filled is now the number of bytes in buffer. */
filled += nread;
/* Convert those bytes to wide characters--as many as we can. */
while (1)
{
size_t thislen = mbrtowc (outp, inp, filled, &state);
/* Stop converting at invalid character;
this can mean we have read just the first part
of a valid character. */
if (thislen == (size_t) -1)
break;
/* We want to handle embedded NUL bytes
but the return value is 0. Correct this. */
if (thislen == 0)
thislen = 1;
/* Advance past this character. */
inp += thislen;
filled -= thislen;
++outp;
}
/* Write the wide characters we just made. */
nwrite = write (output, outbuf,
(outp - outbuf) * sizeof (wchar_t));
if (nwrite < 0)
{
perror ("write");
return 0;
}
/* See if we have a real invalid character. */
if ((eof && filled > 0) || filled >= MB_CUR_MAX)
{
error (0, 0, "invalid multibyte character");
return 0;
}
/* If any characters must be carried forward,
put them at the beginning of buffer. */
if (filled > 0)
memmove (inp, buffer, filled);
}
return 1;
}
ERROR:
The memmove() listed above has its parameters inverted, because it`s
not putting the remaining characters
in the beginning of the buffer.
So, it should be:
memmove (buffer, inp, filled);
DESCRIPTION
memmove(void *s1, const void *s2, size_t n);
The memmove() function copies n bytes from string s2 to string s1.
The
two strings may overlap; the copy is always done in a non-destructive
manner.
Thanks,
--
[]s,
André Goddard