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 /src/main.c | |
| 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
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 53 |
1 files changed, 29 insertions, 24 deletions
@@ -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 |
