diff options
author | Runxi Yu <me@runxiyu.org> | 2025-02-17 01:51:27 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-02-17 01:51:27 +0800 |
commit | 36c43d168d564fe311974efa6f6ac322afcf71d6 (patch) | |
tree | 8f2811c6d4439c8bfe1908dd0ef022e66ed9f116 | |
parent | resources.go: Embed git_hooks_client/* (diff) | |
download | forge-36c43d168d564fe311974efa6f6ac322afcf71d6.tar.gz forge-36c43d168d564fe311974efa6f6ac322afcf71d6.tar.zst forge-36c43d168d564fe311974efa6f6ac322afcf71d6.zip |
git_hooks_client: Splice stdin
Requires stdin to be a pipe. So `cat | ./git_hooks_client` works while
`./git_hooks_client` in a terminal directly does not (character devices
are not pipes).
-rw-r--r-- | git_hooks_client/Makefile | 2 | ||||
-rw-r--r-- | git_hooks_client/git_hooks_client.c | 23 |
2 files changed, 15 insertions, 10 deletions
diff --git a/git_hooks_client/Makefile b/git_hooks_client/Makefile index 0dd283e..f8f2c50 100644 --- a/git_hooks_client/Makefile +++ b/git_hooks_client/Makefile @@ -1 +1,3 @@ +CFLAGS = -Wall -Wextra -Werror -pedantic -std=c99 -D_GNU_SOURCE + git_hooks_client: diff --git a/git_hooks_client/git_hooks_client.c b/git_hooks_client/git_hooks_client.c index b274e03..ef6f3c8 100644 --- a/git_hooks_client/git_hooks_client.c +++ b/git_hooks_client/git_hooks_client.c @@ -4,21 +4,21 @@ #include <sys/socket.h> #include <sys/un.h> #include <string.h> +#include <fcntl.h> -int main() { +int main(void) { int sock; struct sockaddr_un addr; - const char *message = "hi"; const char *socket_path = getenv("LINDENII_FORGE_HOOKS_SOCKET_PATH"); if (socket_path == NULL) { - exit(EXIT_FAILURE); + return EXIT_FAILURE; } sock = socket(AF_UNIX, SOCK_STREAM, 0); if (sock == -1) { perror("socket"); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } memset(&addr, 0, sizeof(struct sockaddr_un)); @@ -28,16 +28,19 @@ int main() { if (connect(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) { perror("connect"); close(sock); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } - if (send(sock, message, strlen(message), 0) == -1) { - perror("send"); + ssize_t bytes_spliced; + while ((bytes_spliced = splice(STDIN_FILENO, NULL, sock, NULL, 1, SPLICE_F_MORE)) > 0) { + } + + if (bytes_spliced == -1) { + perror("splice"); close(sock); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } close(sock); - - return 0; + return EXIT_SUCCESS; } |