diff options
| author | dacctal <dacctal@symlinx.net> | 2026-05-24 10:23:38 +0000 |
|---|---|---|
| committer | dacctal <dacctal@symlinx.net> | 2026-05-24 10:23:38 +0000 |
| commit | aa0d78815004ae6b0c4a42b0e024f5c4ef555ae2 (patch) | |
| tree | 62f97b4c5acfdb98003568466f878fbd3d6b9dfb /src/cmd_out.c | |
| parent | 83d471f2c1d1b1fa6be51f41e4f1c36ab19d7094 (diff) | |
c rewrite
Diffstat (limited to 'src/cmd_out.c')
| -rw-r--r-- | src/cmd_out.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/cmd_out.c b/src/cmd_out.c new file mode 100644 index 0000000..b800149 --- /dev/null +++ b/src/cmd_out.c @@ -0,0 +1,42 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> + +#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 |
