diff options
| author | iceyrazor <iceyrazor@mailfence.com> | 2026-10-09 06:32:21 -0500 |
|---|---|---|
| committer | iceyrazor <iceyrazor@mailfence.com> | 2026-10-09 06:32:21 -0500 |
| commit | 76a7a2c292f8f5ba4ce6bf1cee095940a2eff401 (patch) | |
| tree | e5ef5568a3554dd6fc53da9180a66e50a9867efa | |
| parent | 1df6a629cddbdcd9c622ef5c4eefdca8f04c3555 (diff) | |
- 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
| -rw-r--r-- | .gitignore | 2 | ||||
| -rwxr-xr-x | build.sh | 2 | ||||
| -rw-r--r-- | src/config.def.h (renamed from src/config.def.c) | 0 | ||||
| -rw-r--r-- | src/main.c | 53 | ||||
| -rw-r--r-- | src/type.c | 38 |
5 files changed, 47 insertions, 48 deletions
@@ -1,2 +1,2 @@ bin/** -src/config.c +src/config.h @@ -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.h index 6de4b4e..6de4b4e 100644 --- a/src/config.def.c +++ b/src/config.def.h @@ -10,10 +10,12 @@ #include <sys/wait.h> #include <unistd.h> -#include "config.c" +#include "config.h" #include <tinyosc.c> #include <tinyosc.h> +#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 <https://www.gnu.org/licenses/>. */ -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 @@ -1,14 +1,15 @@ #include <arpa/inet.h> -#include <sys/select.h> #include <fcntl.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include <unistd.h> +#include <sys/select.h> #include <sys/wait.h> +#include <unistd.h> +#include <string.h> -#include "tinyosc.h" -#include "tinyosc.c" +#include <tinyosc.c> +#include <tinyosc.h> /* type.c. sends text to vrchat using osc from user input. @@ -28,10 +29,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ - -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; } |
