fstat issues

From: James Moss (moss@brutesquad.org)
Date: Sat Dec 08 2001 - 13:17:04 EST

I noticed recently that the following no longer works. I was wondering if
this is internal to the kernel or if perhaps this is a glibc issue. Also
wondering if this was an intential change. It did 'used to work' but I
realize that may hold little to no ground since it does seem to be on a per
implementation basis. Anyway looking forward to hearing back, feel free to
show a better way to go about doing what I'm attempting to do.
     -James Moss

#include <stdio.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <fcntl.h>

main(argc, argv)
int argc;
char **argv;
    struct stat sb;
    if (0 == fstat( fileno(stdin), &sb )) {
        printf( "size %d\n", sb.st_size );
    } else {
        printf( "size %d\n", sb.st_size );
    exit( 0 );

On HP, Solaris, etc...
cc foo.c
echo "abc" > ftext.txt
./a.out < ftext.txt
size 4
cat ftext.txt | ./a.out
size 4
On Debian Unstable, latest release of Suse, and latest release of Redhat...
gcc foo.c
echo "abc" > ftext.txt
./a.out < ftext.txt
size 4
cat ftext.txt | ./a.out
size 0

Page 90, 4.12 "File Size"
Advanced Programming in the UNIX Environment states that it's posix compliant
in SVR4 to have the ability to read file size from a pipe and the st_size of
the sb struct is defined.... does Linux break SVR4 compliance? Is there a
better or new way to do this?

SVID Vol: 1a Version 4

Since a pipe is bi-directional, there are two separate flows of data.
Therefore, the size (st_size) returned by a call to fstat with argument
fildes[0] or fildes[1] is the number of bytes available for reading from
fildes[0] or fildes[1] respectively.

June 15, 1995
File: ba_os/pipe
Page: 219
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

This archive was generated by hypermail 2b29 : Sat Dec 15 2001 - 21:00:13 EST