aboutsummaryrefslogtreecommitdiff
path: root/src/Main.c
blob: 55411db8a76e16f2541a2eface0df0a25b172686 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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;
}