Another libio bug.
H.J. Lu
hjl@lucon.org
Tue Nov 3 14:40:00 GMT 1998
Here is another stdio bug. On glibc 2, I got
# gcc f.c
# a.out
4096, 4120
4120
# a.out 1
fwrite: Resource temporarily unavailable
4096, 4120
0
On Solaris,
# a.out
5120, 5144
5144
# a.out 1
fwrite: Resource temporarily unavailable
5120, 5144
5120
On HP-UX,
# a.out
fwrite: Resource temporarily unavailable
8192, 8216
8192
# a.out 1
fwrite: Resource temporarily unavailable
8192, 8216
8192
I believe glibc 2 is wrong. Either Solaris or HP is ok. Ulrich, could
you please take a look? It seems a nasty bug to me.
Thanks.
--
H.J. Lu (hjl@gnu.org)
---
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
main (int argc, char **argv)
{
int pipe_fd [2];
int flags;
int pipe_buf;
int bufsiz1, bufsiz2;
FILE *fp;
char *bp;
int nitems;
if (pipe(pipe_fd))
{
perror ("pipe");
return 1;
}
if ((flags = fcntl(pipe_fd[1], F_GETFL, 0)) == -1)
{
perror ("fcntl");
return 1;
}
flags |= O_NONBLOCK;
if (fcntl(pipe_fd[1], F_SETFL, flags) == -1)
{
perror ("fcntl");
return 1;
}
if ((pipe_buf = fpathconf(pipe_fd[1], _PC_PIPE_BUF)) <= 0L)
{
perror ("fpathconf");
return 1;
}
bufsiz1 = pipe_buf + 16;
bufsiz2 = bufsiz1 + 8;
if (!(fp = fdopen(pipe_fd[1], "w")))
{
perror ("fdopen");
return 1;
}
if (argc > 1)
setbuf(fp, (char *) NULL);
bp = (char *) malloc(bufsiz2);
while (write(pipe_fd [1], bp, pipe_buf / 2) == pipe_buf / 2);
read (pipe_fd [0], bp, pipe_buf);
nitems = fwrite((void *) bp, (size_t) 1, (size_t) bufsiz2, fp);
if (ferror (fp))
perror ("fwrite");
printf ("%d, %d\n", pipe_buf, bufsiz2);
printf ("%d\n", nitems);
}
More information about the Libc-hacker
mailing list