aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2025-01-04 01:44:25 +0100
committerdec05eba <dec05eba@protonmail.com>2025-01-04 01:44:25 +0100
commit36c7bbfda38d93c01b05d453276295ead289ea0e (patch)
tree74d6c5a4e924852d9f4dcb1ff4affe50258d2af8 /tools
parent9998db8afa1cd7997ab369f440661adf7f92f465 (diff)
Simplify gsr-ui-cli
Diffstat (limited to 'tools')
-rw-r--r--tools/gsr-ui-cli/main.c26
1 files changed, 6 insertions, 20 deletions
diff --git a/tools/gsr-ui-cli/main.c b/tools/gsr-ui-cli/main.c
index 8ff6829..e30867b 100644
--- a/tools/gsr-ui-cli/main.c
+++ b/tools/gsr-ui-cli/main.c
@@ -22,21 +22,8 @@ static void get_runtime_filepath(char *buffer, size_t buffer_size, const char *f
snprintf(buffer, buffer_size, "%s/%s", dir, filename);
}
-static FILE* fifo_open_non_blocking(const char *filepath) {
- const int fd = open(filepath, O_RDWR | O_NONBLOCK);
- if(fd <= 0)
- return NULL;
-
- FILE *file = fdopen(fd, "r+");
- if(!file) {
- close(fd);
- return NULL;
- }
- return file;
-}
-
/* Assumes |str| size is less than 256 */
-static void fifo_write_all(FILE *file, const char *str) {
+static void fifo_write_all(int file_fd, const char *str) {
char command[256];
const ssize_t command_size = snprintf(command, sizeof(command), "%s\n", str);
if(command_size >= (ssize_t)sizeof(command)) {
@@ -46,8 +33,7 @@ static void fifo_write_all(FILE *file, const char *str) {
ssize_t offset = 0;
while(offset < (ssize_t)command_size) {
- const ssize_t bytes_written = fwrite(str + offset, 1, command_size - offset, file);
- fflush(file);
+ const ssize_t bytes_written = write(file_fd, str + offset, command_size - offset);
if(bytes_written > 0)
offset += bytes_written;
}
@@ -106,13 +92,13 @@ int main(int argc, char **argv) {
char fifo_filepath[PATH_MAX];
get_runtime_filepath(fifo_filepath, sizeof(fifo_filepath), "gsr-ui");
- FILE *fifo_file = fifo_open_non_blocking(fifo_filepath);
- if(!fifo_file) {
+ const int fifo_fd = open(fifo_filepath, O_RDWR | O_NONBLOCK);
+ if(fifo_fd <= 0) {
fprintf(stderr, "Error: failed to open fifo file %s. Maybe gsr-ui is not running?\n", fifo_filepath);
exit(2);
}
- fifo_write_all(fifo_file, command);
- fclose(fifo_file);
+ fifo_write_all(fifo_fd, command);
+ close(fifo_fd);
return 0;
}