run_tracker/rtracker.c

133 lines
2.8 KiB
C
Raw Permalink Normal View History

2025-02-14 14:30:45 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
2025-02-14 18:11:50 +00:00
#include <stdarg.h>
2025-02-14 14:30:45 +00:00
#include "c_cross_pack/libs/threads.h"
#include "c_cross_pack/libs/time.h"
2025-02-14 14:30:45 +00:00
2025-02-14 18:11:50 +00:00
#define CL_RED "\033[1;31m"
#define CL_YELLOW "\033[1;33m"
#define CL_GREEN "\033[1;32m"
#define CL_NORMAL "\033[1;0m"
2025-02-14 14:30:45 +00:00
char comment[1000] = "";
char command[10000] = "";
2025-02-15 06:38:11 +00:00
#define OUT_LEN 1000 // 1000 items with 100 symbol len
int out_len = OUT_LEN;
2025-02-14 18:11:50 +00:00
// Output of command execution
2025-02-15 06:38:11 +00:00
char out[OUT_LEN][100];
2025-02-14 14:30:45 +00:00
int out_i = 0;
bool runCommand(char* command) {
FILE *fp;
char path[1024];
int status;
2025-02-14 18:11:50 +00:00
// Catch stdout and stderr
2025-02-14 14:30:45 +00:00
char fullCommand[1024];
snprintf(fullCommand, sizeof(fullCommand), "%s 2>&1", command);
2025-02-14 18:11:50 +00:00
// Open the command for reading
2025-02-14 14:30:45 +00:00
fp = popen(fullCommand, "r");
if (fp == NULL) {
printf("Failed to run command\n");
return false;
}
// Clean
out_i = 0;
memset(out, 0, sizeof(out));
2025-02-14 18:11:50 +00:00
// Read the output a line at a time and store it in the out array
2025-02-14 14:30:45 +00:00
while (fgets(path, sizeof(path), fp) != NULL) {
strncpy(out[out_i], path, 100);
out[out_i][99] = '\0'; // Ensure null termination
out_i++;
2025-02-15 06:38:11 +00:00
if (out_i >= out_len) {
2025-02-14 14:30:45 +00:00
break; // Prevent overflow
}
}
/* Close */
status = pclose(fp);
/* Check the return status of the command */
if (WIFEXITED(status)) {
int exit_status = WEXITSTATUS(status);
if (exit_status != 0) {
return false; // Command failed
}
} else {
return false; // Command did not exit normally
}
return true;
}
void* loadAnim() {
char anim[] = {'-', '\\', '|', '/'};
while (true) {
for (size_t i = 0; i < sizeof(anim)/sizeof(anim[0]); i++) {
2025-02-14 18:11:50 +00:00
printf("\r[%s%c%s] %s", CL_YELLOW, anim[i], CL_NORMAL, comment);
2025-02-14 14:30:45 +00:00
fflush(stdout);
wait(100);
2025-02-14 14:30:45 +00:00
}
}
}
void getOut() {
for (int i = 0; i <= out_i; i++) {
printf("%s", out[i]);
}
}
int main(int argc, char **argv) {
2025-02-14 18:11:50 +00:00
bool isCommand = false; // Flag for detect command section
2025-02-14 14:30:45 +00:00
bool forceOut = false;
bool justTry = false; // No out if error, just try
2025-02-14 14:30:45 +00:00
for(int i = 1; i < argc; i++) {
if (isCommand == false) {
if (strcmp(argv[i], "%o") == 0) {
isCommand = true;
forceOut = true;
} else if (strcmp(argv[i], "%t") == 0) {
isCommand = true;
justTry = true;
2025-02-14 18:11:50 +00:00
} else if (strcmp(argv[i], "%%") == 0) {
2025-02-14 14:30:45 +00:00
isCommand = true;
} else {
strcat(comment, argv[i]);
strcat(comment, " ");
}
} else {
strcat(command, argv[i]);
strcat(command, " ");
}
}
void* thread_handle;
start_thread(&thread_handle, loadAnim);
if (runCommand(command)) {
kill_thread(thread_handle);
2025-02-14 18:11:50 +00:00
printf("\r[%s🗸%s] %s\n", CL_GREEN, CL_NORMAL, comment);
2025-02-14 14:30:45 +00:00
if (forceOut == true) {
getOut();
}
} else {
kill_thread(thread_handle);
if (justTry) {
printf("\r[%s🗸%s] %s\n", CL_GREEN, CL_NORMAL, comment);
} else {
printf("\r[%s𐄂%s] %s\n", CL_RED, CL_NORMAL, comment);
// Get out of failed command
getOut();
return 1;
}
2025-02-14 14:30:45 +00:00
}
return 0;
}