pomo/pomo.c

75 lines
1.7 KiB
C
Raw Normal View History

2023-08-21 09:11:07 +00:00
#include <stdio.h>
#include <stdlib.h>
2023-08-20 11:32:54 +00:00
#include <time.h>
2023-08-21 09:11:07 +00:00
#include <unistd.h>
2023-08-20 09:46:34 +00:00
#define MAX_MSG_LEN 100
2023-08-21 09:11:07 +00:00
#define LEN(a) (sizeof(a) / sizeof(a[0]))
2023-08-20 09:46:34 +00:00
typedef struct {
2023-08-21 09:11:07 +00:00
unsigned int t;
char* msg;
2023-08-20 09:46:34 +00:00
} Timers;
static Timers timers[] = {
2023-08-21 09:11:07 +00:00
/* timer(s) comment */
{ 1500, "Time to start working!" },
2023-08-21 09:11:57 +00:00
{ 300, "Time to start resting!" },
2023-08-21 09:11:07 +00:00
{ 1500, "Time to start working!" },
2023-08-21 09:11:57 +00:00
{ 300, "Time to start resting!" },
2023-08-21 09:11:07 +00:00
{ 1500, "Time to start working!" },
2023-08-21 09:11:57 +00:00
{ 300, "Time to start resting!" },
2023-08-21 09:11:07 +00:00
{ 1500, "Time to start working!" },
2023-08-21 09:11:57 +00:00
{ 900, "Time to take a longer rest!" },
2023-08-20 09:46:34 +00:00
};
2023-08-21 09:11:07 +00:00
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);
perror(" failed");
exit(0);
}
2023-08-20 09:46:34 +00:00
}
2023-08-21 09:11:07 +00:00
void print_time_now()
{
2023-08-20 11:32:54 +00:00
time_t timer;
char buffer[26];
struct tm* tm_info;
timer = time(NULL);
tm_info = localtime(&timer);
strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
fprintf(stderr, "%s", buffer);
}
2023-08-21 09:11:07 +00:00
void display_message(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_MSG_LEN explicitly.
2023-08-20 09:46:34 +00:00
printf("%s", sh_command);
2023-08-21 09:11:07 +00:00
char* spawn_args[] = {
"/bin/sh",
"-c",
sh_command,
NULL
};
spawn(spawn_args);
print_time_now();
fprintf(stderr, " | %s\n", msg);
2023-08-20 09:46:34 +00:00
}
2023-08-21 09:11:07 +00:00
int main(int argc, char* argv[])
{
for (int i = 0;; i = (i + 1) % LEN(timers)) {
display_message(timers[i].msg);
sleep(timers[i].t);
}
2023-08-20 09:46:34 +00:00
}