pomo.c (1664B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #include <unistd.h> 5 6 #define LEN(a) (sizeof(a) / sizeof(a[0])) 7 #define MAX_MSG_LEN 100 8 9 typedef struct { 10 unsigned int t; 11 char* msg; 12 } Timers; 13 14 static Timers timers[] = { 15 { 2400, "Time to start working!" }, 16 { 600, "Time to start resting!" }, 17 { 2400, "Time to start working!" }, 18 { 600, "Time to start resting!" }, 19 { 2400, "Time to start working!" }, 20 { 600, "Time to start resting!" }, 21 { 2400, "Time to start working!" }, 22 { 1800, "Time to take a longer rest!" }, 23 }; 24 25 void spawn(char* argv[]) 26 { 27 if (fork() == 0) { 28 // we need to fork the process so that 29 // when we exit the sent screen 30 // this program continues. 31 setsid(); 32 execvp(argv[0], argv); 33 perror(" failed"); 34 exit(0); 35 } 36 } 37 38 void print_time_now() 39 { 40 time_t timer; 41 char buffer[26]; 42 struct tm* tm_info; 43 44 timer = time(NULL); 45 tm_info = localtime(&timer); 46 47 strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info); 48 fprintf(stderr, "%s", buffer); 49 } 50 51 void display_message(char* msg) 52 { 53 char sh_command[MAX_MSG_LEN]; 54 snprintf(sh_command, MAX_MSG_LEN, "echo '%s' | sent", msg); // NOLINT: We are being carefull here by considering MAX_MSG_LEN explicitly. 55 printf("%s", sh_command); 56 char* spawn_args[] = { 57 "/bin/sh", 58 "-c", 59 sh_command, 60 NULL 61 }; 62 spawn(spawn_args); 63 print_time_now(); 64 fprintf(stderr, " | %s\n", msg); 65 } 66 67 int main(int argc, char* argv[]) 68 { 69 for (int i = 0;; i = (i + 1) % LEN(timers)) { 70 display_message(timers[i].msg); 71 sleep(timers[i].t); 72 } 73 }