commit 10466e416041df9dfc931d2b40515838af5b9d82
parent d93042afe023d288f614cbd04b17c5fce214bcd1
Author: tongong <tongong@gmx.net>
Date: Tue, 8 Jun 2021 18:50:03 +0200
added exit handler
Diffstat:
M | timetracker-save.c | | | 155 | +++++++++++++++++++++++++++++++++++++++++-------------------------------------- |
1 file changed, 81 insertions(+), 74 deletions(-)
diff --git a/timetracker-save.c b/timetracker-save.c
@@ -13,19 +13,6 @@
#define FILENAME "/timetracker/data" /* relative to $XDG_DATA_HOME */
#define FILENAME2 "/.local/share/timetracker/data" /* relative to $HOME */
-/* TODO */
-/* exit handler -> should write to data file
- * should not interfere with normal write */
-
-struct program {
- char name[MAX_PROGRAM_NAME];
- int time;
-};
-
-
-void exithandler(int dummy) {
- printf("stop");
-}
/* returns path to data file; should be free()d */
char * filelocation() {
@@ -46,6 +33,7 @@ char * filelocation() {
return loc;
}
+
/* creates .local/share/timetracker (or $XDG_DATA_HOME/timetracker) */
/* assumes .local/share is already there */
void createdatadir() {
@@ -62,26 +50,89 @@ void createdatadir() {
}
-int main() {
- char lastline[MAX_PROGRAM_NAME];
- time_t lasttime = 0;
+struct program {
+ char name[MAX_PROGRAM_NAME];
+ int time;
+};
+
+char lastline[MAX_PROGRAM_NAME];
+time_t lasttime = 0;
- time_t writetime = time(NULL) + WRITE_INTERVAL;
+time_t writetime;
- struct program *programs = NULL;
- int programnum = 0; /* number of programs stored in the programs array */
+struct program *programs = NULL;
+int programnum = 0; /* number of programs stored in the programs array */
+
+/* returns current time */
+/* this function was created to be able to add programs/write data file from
+ * inside and outside the while loop in main(); the current solution with many
+ * global variables is rather dirty but it works */
+time_t addprogram(_Bool forcewrite) {
+ time_t newtime = time(NULL);
+
+ if (lasttime) {
+ /* is the program already in the list? (and where?) */
+ int index = -1;
+ for (int i = 0; i < programnum; i++) {
+ if (!strcmp(lastline, programs[i].name)) {
+ index = i;
+ }
+ }
+ if (index == -1) {
+ /* extend/create list if necessary */
+ if (programnum % 10 == 0) {
+ programs = realloc(programs, sizeof(struct program) *
+ (10 + programnum));
+ }
+ /* add new program to list */
+ strncpy(programs[programnum].name, lastline, MAX_PROGRAM_NAME);
+ programs[programnum].time = newtime - lasttime;
+ programnum++;
+ } else {
+ /* update time for existing program */
+ programs[index].time += newtime - lasttime;
+ }
+
+ if (writetime <= newtime || forcewrite) {
+ /* write to data file */
+ char * loc = filelocation();
+ FILE *dataFile = fopen(loc, "a");;
+ free(loc);
+
+ fprintf(dataFile, "%d,", newtime);
+ for (int i = 0; i < programnum - 1; i++) {
+ fprintf(dataFile, "%s:%d,", programs[i].name, programs[i].time);
+ }
+ fprintf(dataFile, "%s:%d\n", programs[programnum - 1].name, programs[programnum - 1].time);
+ fclose(dataFile);
+
+ /* reset everything */
+ free(programs);
+ programnum = 0;
+ writetime = newtime + WRITE_INTERVAL;
+ }
+ }
+ return newtime;
+}
+
+
+int main() {
createdatadir();
- signal(SIGINT, exithandler);
+ /* ignore SIGINT and SIGTERM; only exit at the end of stdin */
+ /* i don't know if this is a terrible idea */
+ signal(SIGINT, SIG_IGN);
+ signal(SIGTERM, SIG_IGN);
/* ensures that no two timestamps in output file are identical */
sleep(1);
- while (1) {
- size_t size = 0; /* i do not need this value but getline does */
- char *line = NULL; /* current line / program name */
- if (getline(&line, &size, stdin) == -1) exit(1);
+ writetime = time(NULL) + WRITE_INTERVAL;
+
+ size_t size = 0; /* i do not need this value but getline does */
+ char *line = NULL; /* current line / program name */
+ while (getline(&line, &size, stdin) != -1) {
/* remove \n at the end of the line */
line[strlen(line) - 1] = '\0';
/* remove ":" and "," from program name because they would mess up
@@ -90,59 +141,15 @@ int main() {
if (line[i] == ':' || line[i] == ',') line[i] = '-';
}
- time_t newtime = time(NULL);
-
- if (lasttime) {
- /* is the program already in the list? (and where?) */
- int index = -1;
- for (int i = 0; i < programnum; i++) {
- if (!strcmp(lastline, programs[i].name)) {
- index = i;
- }
- }
- if (index == -1) {
- /* extend/create list if necessary */
- if (programnum % 10 == 0) {
- programs = realloc(programs, sizeof(struct program) *
- (10 + programnum));
- }
- /* add new program to list */
- strncpy(programs[programnum].name, lastline, MAX_PROGRAM_NAME);
- programs[programnum].time = newtime - lasttime;
- programnum++;
- } else {
- /* update time for existing program */
- programs[index].time += newtime - lasttime;
- }
-
- if (writetime <= newtime) {
- /* printf("writing to data file\n"); */
- /* write to data file */
- char * loc = filelocation();
- FILE *dataFile = fopen(loc, "a");;
- free(loc);
-
- fprintf(dataFile, "%d,", newtime);
- for (int i = 0; i < programnum - 1; i++) {
- fprintf(dataFile, "%s:%d,", programs[i].name, programs[i].time);
- }
- fprintf(dataFile, "%s:%d\n", programs[programnum - 1].name, programs[programnum - 1].time);
- fclose(dataFile);
-
- /* reset everything */
- free(programs);
- programnum = 0;
- writetime = newtime + WRITE_INTERVAL;
- }
- }
-
- /* for the next interation */
- lasttime = newtime;
+ /* for the next iteration */
+ lasttime = addprogram(0);
strncpy(lastline, line, MAX_PROGRAM_NAME);
lastline[MAX_PROGRAM_NAME - 1] = '\0';
- free(line);
}
+ free(line);
+ addprogram(1);
- /* will never be reached so can be deleted? */
+ /* is this successful or unsuccessful? the program is meant to operate in a
+ * loop and be quit by end of stdin */
return 0;
}