#include #include #include #include #include #include int main(int argc, char *argv[]) { int pipes[2]; int flags; long pipe_buf; size_t bufsize1, bufsize2, nitems; FILE *fp; char *bp; if (pipe(pipes) != 0) { fprintf(stderr, "error: could not create pipe\n"); return 1; } flags = fcntl(pipes[1], F_GETFL, 0); flags |= O_NONBLOCK; fcntl(pipes[1], F_SETFL, flags); pipe_buf = fpathconf(pipes[1], _PC_PIPE_BUF); if (pipe_buf <= 0L) { printf("cannot test: unlimited pipe buffer\n"); return 0; } bufsize1 = (size_t) pipe_buf + 16; bufsize2 = bufsize1 + 8; fp = fdopen(pipes[1], "w"); if (fp == NULL) { fprintf(stderr, "error: could not reopen pipe\n"); return 1; } setbuf(fp, (char *) NULL); bp = (char *) malloc(bufsize2); if (bp == NULL) { fprintf(stderr, "error: could not allocate memory\n"); return 1; } while (write(pipes[1], bp, (unsigned) pipe_buf/2) == pipe_buf/2) ; read(pipes[0], bp, (unsigned) pipe_buf); /* here we go */ nitems = fwrite((void *) bp, 1, (size_t) bufsize2, fp); if (nitems > 0) { printf("the write appeared to succeed\n"); } else if (ferror(fp)) { if (errno == EAGAIN) { printf("the write failed; EAGAIN seen\n"); } else { printf("the write failed; error: %s\n", strerror(errno)); } } else { printf("something weird happened\n"); } return 0; }