Varie correzioni, completata revisione capitolo sull'I/O su file
[gapil.git] / sources / splicecp.c
index c19066fad7de0ee16208e3897bda299773ce3331..cbaa4e89725c1746f4a542ecd6aa1bcf5fe4e088 100644 (file)
@@ -89,10 +89,6 @@ int main(int argc, char *argv[])
         usage();
     }
     /* open pipe, input and output file */
-    if (pipe(pipefd) == -1) {
-       perror("Cannot create buffer pipe"); 
-       exit(EXIT_FAILURE); 
-    }
     in_fd = open(argv[optind], O_RDONLY);
     if (in_fd < 0) {
        printf("Input error %s on %s\n", strerror(errno), argv[optind]);
@@ -103,10 +99,15 @@ int main(int argc, char *argv[])
        printf("Cannot open %s, error %s\n", argv[optind+1], strerror(errno));
        exit(EXIT_FAILURE); 
     }
+    if (pipe(pipefd) == -1) {
+       perror("Cannot create buffer pipe"); 
+       exit(EXIT_FAILURE); 
+    }
     /* copy loop */
     debug("Size %d\n", size);
     while (1) {
-       nread = splice(in_fd, NULL, pipefd[1], NULL, size, 0);
+       nread = splice(in_fd, NULL, pipefd[1], NULL, size, 
+                      SPLICE_F_MOVE|SPLICE_F_MORE);
        debug("read %d bytes\n", nread);
        if (nread == 0) break;
        if (nread < 0) {
@@ -117,10 +118,10 @@ int main(int argc, char *argv[])
                exit(EXIT_FAILURE);
            } 
        }
-       do {
-           nwrite = splice(pipefd[0], NULL, out_fd, NULL, nread, 0);
+       while (nread > 0) {
+           nwrite = splice(pipefd[0], NULL, out_fd, NULL, nread, 
+                           SPLICE_F_MOVE|SPLICE_F_MORE);
            debug("write %d bytes\n", nwrite);
-           if (nwrite == 0) continue;
            if (nwrite < 0) {
                if (errno == EINTR)
                    continue;
@@ -131,7 +132,7 @@ int main(int argc, char *argv[])
            }
            nread -= nwrite;
            debug("left %d bytes\n", nread);
-       } while (nread);
+       }
     }
     return EXIT_SUCCESS;
 }