#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
*/
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]);
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);
}
+ if (pipe(pipefd) == -1) {
+ perror("Cannot create buffer pipe");
+ 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);
+ 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) {
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);
- if (nwrite == 0) continue;
+ 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) {
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);
- } while (nread);
+ debug("left %d bytes\n", nread);
+ }
}
return EXIT_SUCCESS;
}
-
-
-
/*
* routine to print usage info and exit
*/