2 #include <fcntl.h> /* file control functions */
4 int main(int argc, char *argv[])
10 /* check argument, open destination file and check stdin and stdout */
11 if ((argc - optind) != 1) { /* There must be one argument */
12 printf("Wrong number of arguments %d\n", argc - optind);
15 fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0644);
17 printf("opening file %s falied: %s", argv[1], strerror(errno));
20 if (fstat(STDIN_FILENO, &fdata) < 0) {
21 perror("cannot stat stdin");
24 if (!S_ISFIFO(fdata.st_mode)) {
25 fprintf(stderr, "stdin must be a pipe\n");
28 if (fstat(STDOUT_FILENO, &fdata) < 0) {
29 perror("cannot stat stdout");
32 if (!S_ISFIFO(fdata.st_mode)) {
33 fprintf(stderr, "stdout must be a pipe\n");
38 /* copy stdin to stdout */
39 len = tee(STDIN_FILENO, STDOUT_FILENO, size, 0);
42 if (errno == EAGAIN) {
45 perror("error on tee stdin to stdout");
49 /* write data to the file using splice */
51 nwrite = splice(STDIN_FILENO, NULL, fd, NULL, len, SPLICE_F_MOVE);
53 perror("error on splice stdin to file");