commit: 388be371d91938897924756e705fbaaf959b6361
parent 81e55d1675082f9f25146e95c018753f0aa34358
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Mon, 1 Apr 2024 20:38:15 +0200
cmd/cat: Write a clearer splice loop for fd_copy
- No more extra loop done with goto
- No more body-less while with a large function call as conditional
- Chain of if+default on errno changed to switch(errno)
Diffstat:
M | cmd/cat.c | 39 | ++++++++++++++++++++------------------- |
1 file changed, 20 insertions(+), 19 deletions(-)
diff --git a/cmd/cat.c b/cmd/cat.c
@@ -45,29 +45,30 @@ concat(int fd, const char *fdname)
static int
fd_copy(int fd, const char *fdname)
{
- ssize_t c = 0;
+ while(1)
+ {
+ assert(errno == 0);
+ ssize_t ret =
+ splice(fd, NULL, 1, NULL, SIZE_MAX, SPLICE_F_MOVE | SPLICE_F_NONBLOCK | SPLICE_F_MORE);
-again:
- assert(errno == 0);
- while((c = splice(
- fd, NULL, 1, NULL, SIZE_MAX, SPLICE_F_MOVE | SPLICE_F_NONBLOCK | SPLICE_F_MORE)) > 0)
- ;
+ if(ret == 0) break;
- if(c < 0)
- {
- if(errno == EINVAL)
+ if(ret < 0)
{
- errno = 0;
- return concat(fd, fdname);
- }
- if(errno == EAGAIN)
- {
- errno = 0;
- goto again;
+ switch(errno)
+ {
+ case EINVAL:
+ errno = 0;
+ return concat(fd, fdname);
+ case EAGAIN:
+ errno = 0;
+ continue;
+ default:
+ fprintf(stderr, "cat: Error copying ā%sā: %s\n", fdname, strerror(errno));
+ errno = 0;
+ return 1;
+ }
}
- fprintf(stderr, "cat: Error copying ā%sā: %s\n", fdname, strerror(errno));
- errno = 0;
- return 1;
}
return 0;