-------foo.c------------
#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.
FINAL COPY
June 15, 1995
File: ba_os/pipe
svid
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/