/** * Dailytodo is an easy tool to review daily todo items * Copyright (C) 2022 Aaron Ball * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #define _XOPEN_SOURCE #include #include "common.h" #include "config.h" int print_section(char* path, char* prefix, char* title) { FILE* fd = NULL; char buf[4096] = {'\0'}; char header[512] = {'\0'}; char hprefix[32] = {'\0'}; int headerlen = 0; int hprefixlen = 0; int output = 0; snprintf(header, 512, "%s %s\n", prefix, title); snprintf(hprefix, 32, "%s \n", prefix); headerlen = strlen(header); hprefixlen = strlen(hprefix); fd = fopen(path, "r"); while(fgets(buf, 4096, fd) != NULL) { if(strncmp(buf, header, headerlen) == 0 ) { output = 1; continue; } else if(strncmp(buf, hprefix, hprefixlen) == 0 ) { output = 0; } if(output == 1) fputs(buf, stdout); } fclose(fd); return 0; } int main(int argc, char* argv[]) { char notepath[256] = {'\0'}; char date[128] = {'\0'}; char* prefix = "#"; char* title = "Todo"; int days = 0; int i = 0; struct config c; memset(&c, sizeof(struct config), 1); config_init(&c); if(argc > 1) days = strtol(argv[1], NULL, 10); // If user requests negative days, we want to skip non-existant days. So `-2` // is really "two entries ago" rather than two days ago, which could yield // creating an empty historical note if one does not already exist (for // example: on Monday, opening Friday's entry which is -3 days, but -1 // entry). if(days < 0) { while(days < 0) { i--; getpath(c.journaldir, i, notepath, c.extension); if(fexists(notepath)) days++; // Stop traversing after a year to avoid infinite loops when no notes // exist (or exist very long ago) if(i < -365) { fprintf(stderr, "Refusing to search further back than one year\n"); return 1; } } } else { getpath(c.journaldir, days, notepath, c.extension); } getdate(i, date, 128); printf("%s %s (%s)\n", prefix, title, date); print_section(notepath, prefix, title); return 0; }