summaryrefslogtreecommitdiff
path: root/src/journal.c
blob: eb38db2ed4c50271eeab579db1614ca8a6ef2256 (plain)
    1 /**
    2  * Dailyjournal is an easy tool to log daily activities
    3  * Copyright (C) 2022  Aaron Ball <nullspoon@oper.io>
    4  *
    5  * This program is free software: you can redistribute it and/or modify
    6  * it under the terms of the GNU General Public License as published by
    7  * the Free Software Foundation, either version 3 of the License, or
    8  * (at your option) any later version.
    9  *
   10  * This program is distributed in the hope that it will be useful,
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  * GNU General Public License for more details.
   14  *
   15  * You should have received a copy of the GNU General Public License
   16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   17  */
   18 #define _XOPEN_SOURCE
   19 #include <stdio.h>
   20 
   21 #include "common.h"
   22 #include "config.h"
   23 
   24 int main(int argc, char* argv[]) {
   25   char notepath[256]     = {'\0'};
   26   int i = 0;
   27   struct config c;
   28 
   29   memset(&c, sizeof(struct config), 1);
   30   config_init(&c);
   31 
   32   int days = 0;
   33   if(argc > 1)
   34     days = strtol(argv[1], NULL, 10);
   35 
   36   // If user requests negative days, we want to skip non-existant days. So `-2`
   37   // is really "two entries ago" rather than two days ago, which could yield
   38   // creating an empty historical note if one does not already exist (for
   39   // example: on Monday, opening Friday's entry which is -3 days, but -1
   40   // entry).
   41   if(days < 0) {
   42     while(days < 0) {
   43       i--;
   44       getpath(c.journaldir, i, notepath, c.extension);
   45       if(fexists(notepath))
   46         days++;
   47       // Stop traversing after a year to avoid infinite loops when no notes
   48       // exist (or exist very long ago)
   49       if(i < -365) {
   50         fprintf(stderr, "Refusing to search further back than one year\n");
   51         return 1;
   52       }
   53     }
   54   } else {
   55     getpath(c.journaldir, days, notepath, c.extension);
   56   }
   57 
   58   if(safe_cp(c.templatepath, notepath) == -2) {
   59     printf("No template found at %s\n", c.templatepath);
   60     return 2;
   61   }
   62   return fedit(notepath);
   63 }

Generated by cgit