pomo/spt.c

59 lines
1.4 KiB
C

#include <unistd.h> // for the "sleep" & exec functions
#include <stdio.h> // snprintf
#include <stdlib.h> // exit
#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!" },
};
void spawn(char *argv[])
{
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);
// printfn("spt: execvp %s\n", argv[0]);
perror(" failed");
exit(0);
}
}
void display_message_with_sent(char *msg){
char sh_command[MAX_MSG_LEN];
snprintf(sh_command, MAX_MSG_LEN, "echo '%s' | sent", msg); // NOLINT: We are being carefull here by considering MAX_ERROR_LENGTH explicitly.
printf("%s", sh_command);
char *spawn_args[] = {
"/bin/bash",
"-c",
sh_command,
NULL
};
fprintf(stderr, "%s\n", msg);
spawn(spawn_args);
}
int main(int argc, char *argv[]){
for(int i=0; ; i = (i+1) % LEN(timers)){
display_message_with_sent(timers[i].msg);
sleep(timers[i].t);
}
}