From 49b68ef708c873513d216836afb95c13e5c6ab23 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Thu, 20 Apr 2006 14:26:49 +0200 Subject: [PATCH] [PATCH] Use error() throughout examples --- Makefile | 7 +++---- splice-cp.c | 26 +++++++++----------------- splice-in.c | 19 +++++++------------ splice-net.c | 44 ++++++++++++++++---------------------------- splice-out.c | 13 +++++-------- 5 files changed, 40 insertions(+), 69 deletions(-) diff --git a/Makefile b/Makefile index 82d4ec4..e820dce 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,14 @@ CC = gcc -CFLAGS = -Wall -O2 -g -ALL_CFLAGS = $(CFLAGS) -D_GNU_SOURCE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 +CFLAGS = -Wall -O2 -g -D_GNU_SOURCE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 PROGS = ktee splice-cp splice-in splice-out splice-net splice-test4c splice-test4s all: depend $(PROGS) %.o: %.c - $(CC) -o $*.o -c $(ALL_CFLAGS) $< + $(CC) -o $*.o -c $(CFLAGS) $< depend: - @$(CC) -MM $(ALL_CFLAGS) *.c 1> .depend + @$(CC) -MM $(CFLAGS) *.c 1> .depend clean: -rm -f *.o $(PROGS) .depend diff --git a/splice-cp.c b/splice-cp.c index ec640f3..79c2882 100644 --- a/splice-cp.c +++ b/splice-cp.c @@ -24,29 +24,21 @@ int main(int argc, char *argv[]) } in_fd = open(argv[1], O_RDONLY); - if (in_fd < 0) { - perror("open in"); - return 1; - } + if (in_fd < 0) + return error("open input"); - if (fstat(in_fd, &sb) < 0) { - perror("stat"); - return 1; - } + if (fstat(in_fd, &sb) < 0) + return error("stat input"); out_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (out_fd < 0) { - perror("open out"); - return 1; - } + if (out_fd < 0) + return error("open output"); - if (pipe(pfds) < 0) { - perror("pipe"); - return 1; - } + if (pipe(pfds) < 0) + return error("pipe"); do { - int this_len = min(BS, sb.st_size); + int this_len = min((off_t) BS, sb.st_size); int ret = splice(in_fd, NULL, pfds[1], NULL, this_len, SPLICE_F_NONBLOCK); if (ret <= 0) diff --git a/splice-in.c b/splice-in.c index 8a8cd61..8ac851b 100644 --- a/splice-in.c +++ b/splice-in.c @@ -21,23 +21,18 @@ int main(int argc, char *argv[]) } fd = open(argv[1], O_RDONLY); - if (fd < 0) { - perror("open"); - return 1; - } + if (fd < 0) + return error("open input"); - if (fstat(fd, &sb) < 0) { - perror("stat"); - return 1; - } + if (fstat(fd, &sb) < 0) + return error("stat input"); do { int ret = splice(fd, NULL, STDOUT_FILENO, NULL, sb.st_size, 0); - if (ret < 0) { - perror("splice"); - break; - } else if (!ret) + if (ret < 0) + return error("splice"); + else if (!ret) break; sb.st_size -= ret; diff --git a/splice-net.c b/splice-net.c index f8ba43b..f6441c5 100644 --- a/splice-net.c +++ b/splice-net.c @@ -74,10 +74,8 @@ int main(int argc, char *argv[]) if (inet_aton(argv[2], &addr.sin_addr) != 1) { struct hostent *hent = gethostbyname(argv[2]); - if (!hent) { - perror("gethostbyname"); - return 1; - } + if (!hent) + return error("gethostbyname"); memcpy(&addr.sin_addr, hent->h_addr, 4); } @@ -85,26 +83,18 @@ int main(int argc, char *argv[]) printf("Connecting to %s/%d\n", argv[2], port); fd = socket(AF_INET, SOCK_STREAM, 0); - if (fd < 0) { - perror("socket"); - return 1; - } + if (fd < 0) + return error("socket"); - if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { - perror("connect"); - return 1; - } + if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) + return error("connect"); - if (pipe(pfd) < 0) { - perror("pipe"); - return 1; - } + if (pipe(pfd) < 0) + return error("pipe"); ffd = open(argv[1], O_RDWR); - if (ffd < 0) { - perror("open input"); - return 1; - } + if (ffd < 0) + return error("open input"); signal(SIGINT, show_rate); gettimeofday(&start_time, NULL); @@ -122,10 +112,9 @@ int main(int argc, char *argv[]) if (!bla) printf("spliced %d\n", ret); - if (ret < 0) { - perror("splice"); - break; - } else if (!ret) { + if (ret < 0) + return error("splice"); + else if (!ret) { break; } b_sent += ret; @@ -134,10 +123,9 @@ int main(int argc, char *argv[]) while (ret > 0) { int flags = 0; int written = splice(pfd[0], NULL, fd, NULL, ret, flags); - if (written < 0) { - perror("splice-out"); - break; - } else if (!written) + if (written < 0) + return error("splice-out"); + else if (!written) break; ret -= written; } diff --git a/splice-out.c b/splice-out.c index 1080594..f5aa97e 100644 --- a/splice-out.c +++ b/splice-out.c @@ -18,18 +18,15 @@ int main(int argc, char *argv[]) } fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (fd < 0) { - perror("open"); - return 1; - } + if (fd < 0) + return error("open"); do { int ret = splice(STDIN_FILENO, NULL, fd, NULL, SPLICE_SIZE, 0); - if (ret < 0) { - perror("splice"); - break; - } else if (!ret) + if (ret < 0) + return error("splice"); + else if (!ret) break; } while (1); -- 2.25.1