From 36c43d168d564fe311974efa6f6ac322afcf71d6 Mon Sep 17 00:00:00 2001
From: Runxi Yu <me@runxiyu.org>
Date: Mon, 17 Feb 2025 01:51:27 +0800
Subject: [PATCH] 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).

---
 git_hooks_client/Makefile           |  2 ++
 git_hooks_client/git_hooks_client.c | 23 +++++++++++++----------

diff --git a/git_hooks_client/Makefile b/git_hooks_client/Makefile
index 0dd283e80f87d1aa98bd8dd7c0646f52f2d8e028..f8f2c50e4016cce2bd45bbd5cb10f71c017c0807 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 b274e0340d5baaa01d70f093578a385ad374e22f..ef6f3c86b089e6ea8210262a11f3f41dbabbfa63 100644
--- a/git_hooks_client/git_hooks_client.c
+++ b/git_hooks_client/git_hooks_client.c
@@ -4,21 +4,21 @@ #include <unistd.h>
 #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 @@
 	if (connect(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) {
 		perror("connect");
 		close(sock);
-		exit(EXIT_FAILURE);
+		return EXIT_FAILURE;
+	}
+
+	ssize_t bytes_spliced;
+	while ((bytes_spliced = splice(STDIN_FILENO, NULL, sock, NULL, 1, SPLICE_F_MORE)) > 0) {
 	}
 
-	if (send(sock, message, strlen(message), 0) == -1) {
-		perror("send");
+	if (bytes_spliced == -1) {
+		perror("splice");
 		close(sock);
-		exit(EXIT_FAILURE);
+		return EXIT_FAILURE;
 	}
 
 	close(sock);
-
-	return 0;
+	return EXIT_SUCCESS;
 }

-- 
2.48.1