2023-08-20 10:11:47 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2023-08-20 09:46:34 +00:00
|
|
|
|
|
|
|
#define MAX_MSG_LEN 100
|
|
|
|
#define LEN(a) (sizeof(a) / sizeof(a[0]))
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned int t;
|
|
|
|
char *msg;
|
|
|
|
} Timers;
|
|
|
|
|
|
|
|
static Timers timers[] = {
|
|
|
|
/* timer(s) comment */
|
|
|
|
{ 1500, "Time to start working!"},
|
|
|
|
{ 300, "Time to start resting!"},
|
|
|
|
{ 1500, "Time to start working!"},
|
|
|
|
{ 300, "Time to start resting!"},
|
|
|
|
{ 1500, "Time to start working!"},
|
|
|
|
{ 300, "Time to start resting!"},
|
|
|
|
{ 1500, "Time to start working!"},
|
|
|
|
{ 900, "Time to take a longer rest!" },
|
|
|
|
};
|
|
|
|
|
2023-08-20 10:11:47 +00:00
|
|
|
void spawn(char *argv[]){
|
2023-08-20 09:46:34 +00:00
|
|
|
if (fork() == 0) {
|
|
|
|
// we need to fork the process so that
|
|
|
|
// when we exit the sent screen
|
|
|
|
// this program continues.
|
|
|
|
setsid();
|
|
|
|
execvp(argv[0], argv);
|
|
|
|
perror(" failed");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-20 10:11:47 +00:00
|
|
|
void display_message(char *msg){
|
2023-08-20 09:46:34 +00:00
|
|
|
char sh_command[MAX_MSG_LEN];
|
2023-08-20 10:11:47 +00:00
|
|
|
snprintf(sh_command, MAX_MSG_LEN, "echo '%s' | sent", msg); // NOLINT: We are being carefull here by considering MAX_MSG_LEN explicitly.
|
2023-08-20 09:46:34 +00:00
|
|
|
printf("%s", sh_command);
|
|
|
|
char *spawn_args[] = {
|
2023-08-20 10:11:47 +00:00
|
|
|
"/bin/sh",
|
2023-08-20 09:46:34 +00:00
|
|
|
"-c",
|
|
|
|
sh_command,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
spawn(spawn_args);
|
2023-08-20 10:11:47 +00:00
|
|
|
// fprintf(stderr, "%s\n", msg);
|
2023-08-20 09:46:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]){
|
|
|
|
for(int i=0; ; i = (i+1) % LEN(timers)){
|
2023-08-20 10:11:47 +00:00
|
|
|
display_message(timers[i].msg);
|
2023-08-20 09:46:34 +00:00
|
|
|
sleep(timers[i].t);
|
|
|
|
}
|
|
|
|
}
|