Add utils.c

This commit is contained in:
augustin64 2023-07-16 13:04:37 +02:00
parent 114c5f901f
commit c5b616f5e7
4 changed files with 71 additions and 3 deletions

View File

@ -1,5 +1,5 @@
FLAGS = -g -Wall -Wextra -DLOG_USE_COLOR `pkg-config --cflags --libs libnotify`
LD_FLAGS = -lsqlite3
LD_FLAGS = -lsqlite3 -lpthread
$(shell mkdir -p out)
@ -17,10 +17,10 @@ out/%.o: src/main/%.c
out/%.o: src/log/%.c
$(CC) -c $(FLAGS) $^ -o $@
takl: out/main.o out/db.o out/tasks.o
takl: out/main.o out/db.o out/tasks.o out/utils.o
$(CC) $(FLAGS) $(LD_FLAGS) $^ -o $@
takl-daemon: out/daemon.o out/db.o out/tasks.o out/log.o
takl-daemon: out/daemon.o out/db.o out/tasks.o out/utils.o out/log.o
$(CC) $(FLAGS) $(LD_FLAGS) $^ -o $@

View File

@ -6,6 +6,9 @@
// By default, $HOME/.config/takl.sqlite3 is used. You can change this behaviour here
//#define DB_FILE "takl.sqlite3"
// By default, /tmp/takl.$USER is used. You can change this behaviour here
//#define SOCKET_FILE "takl.sock"
#define MAX_TASK_ID 10000 // max is set to MAX_TASK_ID-1
#define NEW_TASK_ID_MAX_RETRIES 10000 // number of retries before giving up

14
src/main/include/utils.h Normal file
View File

@ -0,0 +1,14 @@
#include <pthread.h>
#include "struct.h"
/*
Renvoie le chemin d'accès au socket (/tmp/takl.$USER sauf si défini différemment dans la config)
*/
char* get_socket_path();
/*
Renvoie l'élément arrivant dans le moins de temps d'une liste de tâches.
*/
task_t next_task(task_list_t* tasks);

51
src/main/utils.c Normal file
View File

@ -0,0 +1,51 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <pthread.h>
#include "include/config.h"
#include "include/struct.h"
char* get_socket_path() {
#ifndef SOCKET_FILE
char* base_path = "/tmp/takl";
char* username = getenv("USER");
assert(username != NULL);
char* socket_path = malloc(sizeof(char)*(strlen(base_path)+strlen(username)+1));
sprintf(socket_path, "%s.%s", base_path, username);
return socket_path;
#else
char* socket_path = malloc(sizeof(char)*(strlen(SOCKET_FILE)+1));
memcpy(socket_path, SOCKET_FILE, sizeof(char)*(strlen(SOCKET_FILE)+1));
return socket_path;
#endif
}
task_t next_task(task_list_t* tasks) {
time_t now = time(0);
task_t next;
next.due_to = 0;
task_list_t* cur = tasks;
while (cur) {
task_t t = cur->task;
if (!t.done && (difftime(now, t.due_to) <= 0 || t.due_to == 0)) { // La tâche a une échéance, qui n'est pas passée
if (next.due_to == 0 || difftime(next.due_to, t.due_to) >= 0) {
next = t;
}
}
cur = cur->next;
}
return next;
}