#include "types.h"
#include "stat.h"

struct stat sbuf;
char buf[100];

_main()
{
    int fda[2];
    int ret;

    if (pipe(fda) != 0)
        perror("pipe");

	printf("pipe returns fd[0] %d fd[1] %d\n",fda[0],fda[1]);

	fstat(fda[0], &sbuf);
	printf("pipe mode 0%o nlink %d size %d + %d dev %d ino %d\n",sbuf.st_mode,
		sbuf.st_nlink,sbuf.st_size.o_blkno,sbuf.st_size.o_offset,
		sbuf.st_dev,sbuf.st_ino);

	ret = write(fda[1],"Hello, world\n",13);
	if (ret == -1)
		perror("write");
	else
		printf("wrote %d bytes\n",ret);

	fstat(fda[0], &sbuf);
	printf("pipe mode 0%o nlink %d size %d + %d\n",sbuf.st_mode,
		sbuf.st_nlink,sbuf.st_size.o_blkno,sbuf.st_size.o_offset);

	ret = read(fda[0],buf,5);
	if (ret == -1)
		perror("read");
	else
		printf("read %d bytes\n",ret);

	fstat(fda[0], &sbuf);
	printf("pipe mode 0%o nlink %d size %d + %d\n",sbuf.st_mode,
		sbuf.st_nlink,sbuf.st_size.o_blkno,sbuf.st_size.o_offset);
}
