aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/add_repo.c52
-rw-r--r--src/cmd_out.c66
2 files changed, 63 insertions, 55 deletions
diff --git a/src/add_repo.c b/src/add_repo.c
index 52afc80..949c992 100644
--- a/src/add_repo.c
+++ b/src/add_repo.c
@@ -26,26 +26,32 @@
#include "vars.h"
void add_repo(const char *repo, const char *repo_name) {
- bool is_previous_repos = false;
- char rfile_line[1024];
- char rfile_contents[8192] = {0};
-
- if (file_exists(repo_file)) {
- FILE *rfile = fopen(repo_file, "r");
- if (rfile) {
- while (fgets(rfile_line, sizeof(rfile_line), rfile)) {
- strcat(rfile_contents, rfile_line);
- }
- fclose(rfile);
- is_previous_repos = true;
- }
- }
-
- char *previous_repos = is_previous_repos ? rfile_contents : "";
-
- FILE *wfile = fopen(repo_file, "w");
- if (wfile) {
- fprintf(wfile, "%srepositories[\"%s\"] = { url = \"%s\" }\n", previous_repos, repo_name, repo);
- fclose(wfile);
- }
-} \ No newline at end of file
+ bool is_previous_repos = false;
+ char rfile_line[1024];
+ char rfile_contents[8192] = {0};
+
+ if (file_exists(repo_file)) {
+ FILE *rfile = fopen(repo_file, "r");
+ if (rfile) {
+ while (fgets(rfile_line, sizeof(rfile_line), rfile)) {
+ size_t current_len = strlen(rfile_contents);
+ size_t remaining = sizeof(rfile_contents) - current_len;
+ if (remaining > 1) {
+ snprintf(rfile_contents + current_len, remaining, "%s", rfile_line);
+ } else {
+ break;
+ }
+ }
+ fclose(rfile);
+ is_previous_repos = true;
+ }
+ }
+
+ char *previous_repos = is_previous_repos ? rfile_contents : "";
+
+ FILE *wfile = fopen(repo_file, "w");
+ if (wfile) {
+ fprintf(wfile, "%srepositories[\"%s\"] = { url = \"%s\" }\n", previous_repos, repo_name, repo);
+ fclose(wfile);
+ }
+}
diff --git a/src/cmd_out.c b/src/cmd_out.c
index bc371e0..f2d6bf4 100644
--- a/src/cmd_out.c
+++ b/src/cmd_out.c
@@ -26,35 +26,37 @@
#include "cmd_out.h"
char* cmd_out(const char *cmd) {
- FILE *pipe = popen(cmd, "r");
- if (!pipe) return strdup("");
-
- char buffer[128];
- size_t total_size = 0;
- size_t capacity = 256;
- char *result = malloc(capacity);
- if (!result) {
- pclose(pipe);
- return strdup("");
- }
- result[0] = '\0';
-
- while (fgets(buffer, sizeof(buffer), pipe) != NULL) {
- size_t len = strlen(buffer);
- if (total_size + len + 1 > capacity) {
- capacity *= 2;
- char *new_result = realloc(result, capacity);
- if (!new_result) {
- free(result);
- pclose(pipe);
- return strdup("");
- }
- result = new_result;
- }
- strcat(result, buffer);
- total_size += len;
- }
-
- pclose(pipe);
- return result;
-} \ No newline at end of file
+ FILE *pipe = popen(cmd, "r");
+ if (!pipe) return strdup("");
+
+ char buffer[128];
+ size_t total_size = 0;
+ size_t capacity = 256;
+ char *result = malloc(capacity);
+ if (!result) {
+ pclose(pipe);
+ return strdup("");
+ }
+ result[0] = '\0';
+
+ while (fgets(buffer, sizeof(buffer), pipe) != NULL) {
+ size_t len = strlen(buffer);
+ // Ensure enough space for the new chunk + 1 byte for '\0'
+ if (total_size + len + 1 > capacity) {
+ capacity *= 2;
+ char *new_result = realloc(result, capacity);
+ if (!new_result) {
+ free(result);
+ pclose(pipe);
+ return strdup("");
+ }
+ result = new_result;
+ }
+
+ snprintf(result + total_size, capacity - total_size, "%s", buffer);
+ total_size += len;
+ }
+
+ pclose(pipe);
+ return result;
+}