From 76a7a2c292f8f5ba4ce6bf1cee095940a2eff401 Mon Sep 17 00:00:00 2001 From: iceyrazor Date: Fri, 9 Oct 2026 06:32:21 -0500 Subject: several changes: - config.c is now config.h - keepRunning is now sig_atomic_t - execlb now returns error from forked process - execlb now has buffer size - execlb now uses waitpid - buffers are no longer malloced and are 0 initialized - fixed includes - same stuff pretty much to type.c - removed size calculation from type.c --- .gitignore | 2 +- build.sh | 2 +- src/config.def.c | 11 ----------- src/config.def.h | 11 +++++++++++ src/main.c | 53 +++++++++++++++++++++++++++++------------------------ src/type.c | 38 ++++++++++++++++---------------------- 6 files changed, 58 insertions(+), 59 deletions(-) delete mode 100644 src/config.def.c create mode 100644 src/config.def.h diff --git a/.gitignore b/.gitignore index 3d81b6a..83e7ab0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ bin/** -src/config.c +src/config.h diff --git a/build.sh b/build.sh index 1322453..6aac001 100755 --- a/build.sh +++ b/build.sh @@ -17,6 +17,6 @@ shift $((OPTIND-1)) [ "${1:-}" = "--" ] && shift printf "\n\nmaking project\n" -[ ! -e "src/config.c" ] && cp src/config.def.c src/config.c; +[ ! -e "src/config.h" ] && cp src/config.def.h src/config.h; $CC -o bin/chat-loop src/main.c $LIB -lm $DEBUG $CC -o bin/type src/type.c $LIB -lm $DEBUG diff --git a/src/config.def.c b/src/config.def.c deleted file mode 100644 index 6de4b4e..0000000 --- a/src/config.def.c +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef config -#define config - -char *title="title"; //change the title here -char *arr[] = { - "message 1", - "message 2", - "message 3", -}; - -#endif diff --git a/src/config.def.h b/src/config.def.h new file mode 100644 index 0000000..6de4b4e --- /dev/null +++ b/src/config.def.h @@ -0,0 +1,11 @@ +#ifndef config +#define config + +char *title="title"; //change the title here +char *arr[] = { + "message 1", + "message 2", + "message 3", +}; + +#endif diff --git a/src/main.c b/src/main.c index a2ce6bc..f036234 100644 --- a/src/main.c +++ b/src/main.c @@ -10,10 +10,12 @@ #include #include -#include "config.c" +#include "config.h" #include #include +#define buf_size 2048 + /* chat-loop / main.c. sends text to vrchat using osc. Copyright (C) 2026 iceyrazor @@ -32,7 +34,7 @@ along with this program. If not, see . */ -static volatile bool keepRunning = true; +static volatile sig_atomic_t keepRunning = true; // handle Ctrl+C static void sigintHandler(int x) @@ -41,11 +43,11 @@ static void sigintHandler(int x) } // execute command and put output into ret_buf buffer -void execlb(char* cmd, char* ret_buf) +int execlb(const char* cmd, char* buffer, size_t buffer_size) { int link[2]; pid_t pid; - char buf[2048]; + int status; if (pipe(link) == -1) exit(-5); @@ -58,21 +60,20 @@ void execlb(char* cmd, char* ret_buf) close(link[0]); close(link[1]); execl("/bin/sh", "sh", "-c", cmd, (char*)0); - exit(0); + _exit(127); } else { close(link[1]); - int nbytes = read(link[0], buf, sizeof(buf)); - if (nbytes > 2047) - nbytes = 2047; - snprintf(ret_buf, nbytes + 1, "%s", buf); - wait(NULL); + int nbytes = read(link[0], buffer, buffer_size - 1); + buffer[nbytes] = '\0'; + waitpid(pid,&status,0); close(link[0]); + return status; } } int main() { - char wbuffer[2048]; // declare a 2Kb buffer to read packet data into + char wbuffer[2048] = {0}; // declare a 2Kb buffer to read packet data into int port = 9000; struct sockaddr_in servaddr; @@ -87,19 +88,27 @@ int main() servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Server IP // register the SIGINT handler (Ctrl+C) - signal(SIGINT, &sigintHandler); + signal(SIGINT, sigintHandler); - char* dist = (char*)malloc(sizeof(char) * 2048); - execlb("uname -r", dist); - dist[strlen(dist) - 1] = '\0'; + char dist[2048] = {0}; + int status=execlb("uname -r", dist, sizeof dist); + if (status != 0){ + fprintf(stderr,"ERROR WITH execlb %d", status); + exit(1); + } + dist[strcspn(dist,"\n")] = '\0'; - char* comb_string = (char*)malloc(sizeof(char) * 2048); - char* song_title = (char*)malloc(sizeof(char) * 2048); + char comb_string[2048] = {0}; + char song_title[2048] = {0}; size_t i = 0; while (keepRunning) { - execlb("src/title.sh", song_title); - sprintf(comb_string, "%s\nLinux %s\n%s\n%s", title, dist, arr[i], song_title); + int song_status=execlb("src/title.sh", song_title, sizeof song_title); + if (song_status != 0){ + fprintf(stderr,"ERROR WITH execlb %d", status); + exit(1); + } + snprintf(comb_string, sizeof comb_string, "%s\nLinux %s\n%s\n%s", title, dist, arr[i], song_title); uint32_t len = tosc_writeMessage(wbuffer, sizeof(wbuffer), "/chatbox/input", "sTF", comb_string); sendto(socketfd, wbuffer, len, MSG_CONFIRM, (const struct sockaddr*)&servaddr, sizeof(servaddr)); @@ -116,9 +125,5 @@ int main() sendto(socketfd, wbuffer, len, MSG_CONFIRM, (const struct sockaddr*)&servaddr, sizeof(servaddr)); // end - free(comb_string); - free(dist); - free(song_title); - return 0; -} +} \ No newline at end of file diff --git a/src/type.c b/src/type.c index 19548e9..b2512d6 100644 --- a/src/type.c +++ b/src/type.c @@ -1,14 +1,15 @@ #include -#include #include #include #include #include -#include +#include #include +#include +#include -#include "tinyosc.h" -#include "tinyosc.c" +#include +#include /* type.c. sends text to vrchat using osc from user input. @@ -28,10 +29,10 @@ along with this program. If not, see . */ - -int main(int argc, char **argv){ - char wbuffer[2048]; // declare a 2Kb buffer to read packet data into - int port=9000; +int main(int argc, char** argv) +{ + char wbuffer[2048] = { 0 }; // declare a 2Kb buffer to read packet data into + int port = 9000; struct sockaddr_in servaddr; int socketfd = socket(AF_INET, SOCK_DGRAM, 0); @@ -40,25 +41,18 @@ int main(int argc, char **argv){ return -1; } - servaddr.sin_family = AF_INET; // IPv4 - servaddr.sin_port = htons(port); // Server port + servaddr.sin_family = AF_INET; // IPv4 + servaddr.sin_port = htons(port); // Server port servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Server IP - size_t size=0; - for (int i=1; i < argc; i++){ - size+=strlen(argv[i])+1; - } - char *send_str=(char*)malloc(sizeof(char)*size+1); - send_str[0]='\0'; - for (int i=1; i < argc; i++){ - strcat(send_str,argv[i]); - strcat(send_str," "); + char send_str[2048] = { 0 }; + for (int i = 1; i < argc; i++) { + strcat(send_str, argv[i]); + strcat(send_str, " "); } int len = tosc_writeMessage(wbuffer, sizeof(wbuffer), "/chatbox/input", "sTF", send_str); - sendto(socketfd, wbuffer, len, MSG_CONFIRM, (const struct sockaddr *)&servaddr, sizeof(servaddr)); - - free(send_str); + sendto(socketfd, wbuffer, len, MSG_CONFIRM, (const struct sockaddr*)&servaddr, sizeof(servaddr)); return 0; } -- cgit v1.3