diff options
Diffstat (limited to 'src/Main.c')
| -rw-r--r-- | src/Main.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/Main.c b/src/Main.c new file mode 100644 index 0000000..f3f084d --- /dev/null +++ b/src/Main.c @@ -0,0 +1,54 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/wait.h> +#include <time.h> +#include "Messages.h" + +#define SUCCESS_COUNT (sizeof(success_messages) / sizeof(char*) - 1) +#define FAILURE_COUNT (sizeof(failure_messages) / sizeof(char*) - 1) + +static const char ANSI_RED[] = "\x1b[31m"; +static const char ANSI_RESET[] = "\x1b[0m"; + +static const char* get_random_message(const char* messages[], size_t count) { + return messages[rand() % count]; +} + +int main(int argc, char* argv[]) { + if (argc < 2) { + fprintf(stderr, "Usage: %s <command>\n", argv[0]); + return 1; + } + + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + srand((unsigned int)ts.tv_nsec); + + pid_t pid = fork(); + + if (pid < 0) { + perror("fork"); + return 1; + } + + if (pid == 0) { + execvp(argv[1], &argv[1]); + perror(argv[1]); + _exit(127); + } + + int status; + waitpid(pid, &status, 0); + + const char* message; + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { + message = get_random_message(success_messages, SUCCESS_COUNT); + } else { + message = get_random_message(failure_messages, FAILURE_COUNT); + } + + printf("%s%s<3%s\n", ANSI_RED, message, ANSI_RESET); + + return 0; +} |
