Risistemata versione del programma di esempio di {{{splice}}},
[gapil.git] / sources / splicecp.c
index feda8e6b962fc243cf956a4d8949056772650ff1..c19066fad7de0ee16208e3897bda299773ce3331 100644 (file)
 #include <errno.h>       /* error definitions and routines */
 #include <stdio.h>       /* standard I/O library */
 #include <string.h>      /* C strings library */
-#include <sys/stat.h>
-#include <sys/types.h>
+#include <sys/stat.h>    /* file characteristics constants and functions */
+#include <sys/types.h>   /* primitive system data types */
 
+#include "macros.h"
 /* 
  * Function and globals definitions
  */
 void usage(void);  /* Help printing routine */
-
 /*
  * Main program
  */
@@ -89,7 +89,7 @@ int main(int argc, char *argv[])
         usage();
     }
     /* open pipe, input and output file */
-    if (pipe(pipefd) == -1) { 
+    if (pipe(pipefd) == -1) {
        perror("Cannot create buffer pipe"); 
        exit(EXIT_FAILURE); 
     }
@@ -98,45 +98,43 @@ int main(int argc, char *argv[])
        printf("Input error %s on %s\n", strerror(errno), argv[optind]);
        exit(EXIT_FAILURE); 
     }
-    out_fd = open(argv[optind+1], O_CREAT|O_RDWR, 0644);
+    out_fd = open(argv[optind+1], O_CREAT|O_RDWR|O_TRUNC, 0644);
     if (out_fd < 0) {
        printf("Cannot open %s, error %s\n", argv[optind+1], strerror(errno));
        exit(EXIT_FAILURE); 
     }
     /* copy loop */
-    printf("Size %d\n", size);
+    debug("Size %d\n", size);
     while (1) {
        nread = splice(in_fd, NULL, pipefd[1], NULL, size, 0);
-       printf("read %d bytes\n", nread);
+       debug("read %d bytes\n", nread);
        if (nread == 0) break;
        if (nread < 0) {
            if (errno == EINTR) {
                continue;
            } else {
                perror("read error");
-               exit(EXIT_FAILURE); 
+               exit(EXIT_FAILURE);
            } 
        }
        do {
-           nwrite = splice(pipefd[0], NULL, out_fd, NULL, nread, 0); 
-           printf("write %d bytes\n", nwrite);
+           nwrite = splice(pipefd[0], NULL, out_fd, NULL, nread, 0);
+           debug("write %d bytes\n", nwrite);
            if (nwrite == 0) continue;
            if (nwrite < 0) {
                if (errno == EINTR)
                    continue;
-           } else {
-               perror("write error");
-               exit(EXIT_FAILURE); 
+               else {
+                   perror("write error");
+                   exit(EXIT_FAILURE);
+               }
            }
            nread -= nwrite;
-           printf("left %d bytes", nread);
+           debug("left %d bytes\n", nread);
        } while (nread);
     }
     return EXIT_SUCCESS;
 }
-
-
-
 /*
  * routine to print usage info and exit 
  */