/****************************************************************************** * Journal Wizard * * Program using a text editor--defined by macro EDITOR--to generate dated * journal entries organized into directories by year and month. Places this * inside journal_wizard directory created in Documents directory of user's * home directory. * * * program: journal_wizard * date : 20 Oct 2022 * by : yetimach * * note: set the editor to whatever you like: "vim", "neovim", "nano", * "libreoffice --writer". Just be sure you have it installed. * Just here below *****************************************************************************/ #define EDITOR "vim" #include #include #include #include #include #include #include #define MAX_YEAR 12 #define MAX_MONTH 12 #define MAX_MONTH_NAME 4 #define MAX_DAY 3 #define MAX_DATE 11 #define MAX_PATH 100 #define MAX_COMMAND 125 int main(void){ char current_year[MAX_YEAR]; char current_month[MAX_MONTH]; char current_day[MAX_MONTH]; char date_string[MAX_DATE]; /* generate localtime struct and handle possible errors */ time_t rawtime; struct tm *info; time(&rawtime); if (rawtime == ((time_t)-1)){ fprintf(stderr, "Not able to access current time.\n"); exit(EXIT_FAILURE); } info = localtime(&rawtime); if (info == NULL){ fprintf(stderr, "Not able to generate localtime struct.\n"); exit(EXIT_FAILURE); } sprintf(current_year, "%d", (info->tm_year + 1900)); sprintf(current_month, "%d", info->tm_mon + 1); sprintf(current_day, "%d", info->tm_mday); /* make date string */ strcpy(date_string, current_month); strcat(date_string, "_"); strcat(date_string, current_day); strcat(date_string, "_"); strcat(date_string, current_year); /* begin checking for necessary dirs and making them if they don't exist */ char path_to_Documents[MAX_PATH]; strcpy(path_to_Documents, getenv("HOME")); strcat(path_to_Documents, "/Documents"); DIR* dir = opendir(path_to_Documents); if (dir) { /* Directory exists. */ closedir(dir); }else if (ENOENT == errno) { /* Directory does not exist so make it. */ mkdir(path_to_Documents, 0751); }else{ fprintf(stderr, "problem detecting or creating '%s'.\n", path_to_Documents); exit(EXIT_FAILURE); } char path_to_journal_wizard[MAX_PATH]; strcpy(path_to_journal_wizard, path_to_Documents); strcat(path_to_journal_wizard, "/journal_wizard"); dir = opendir(path_to_journal_wizard); if (dir) { /* Directory exists. */ closedir(dir); }else if (ENOENT == errno) { /* Directory does not exist so make it. */ mkdir(path_to_journal_wizard, 0751); }else{ fprintf(stderr, "problem detecting or creating '%s'.\n", path_to_journal_wizard); exit(EXIT_FAILURE); } char path_to_year_dir[MAX_PATH]; strcpy(path_to_year_dir, path_to_journal_wizard); strcat(path_to_year_dir, "/"); strcat(path_to_year_dir, current_year); dir = opendir(path_to_year_dir); if (dir) { /* Directory exists. */ closedir(dir); }else if (ENOENT == errno) { /* Directory does not exist so make it. */ mkdir(path_to_year_dir, 0751); }else{ fprintf(stderr, "problem detecting or creating '%s'.\n", path_to_year_dir); exit(EXIT_FAILURE); } char month_name[MAX_MONTH_NAME]; char date_and_time_str[30]; strcpy(date_and_time_str, asctime(info)); int p = 0; for (int i = 4; i <= 6; i++) month_name[p++] = date_and_time_str[i]; month_name[p] = '\0'; char path_to_month_dir[MAX_PATH]; strcpy(path_to_month_dir, path_to_year_dir); strcat(path_to_month_dir, "/"); strcat(path_to_month_dir, month_name); dir = opendir(path_to_month_dir); if (dir) { /* Directory exists. */ closedir(dir); }else if (ENOENT == errno) { /* Directory does not exist so make it. */ mkdir(path_to_month_dir, 0751); }else{ fprintf(stderr, "problem detecting or creating '%s'.\n", path_to_month_dir); exit(EXIT_FAILURE); } /* check if entry for day alredy exists if not make it */ char path_to_current_entry[MAX_PATH]; strcpy(path_to_current_entry, path_to_month_dir); strcat(path_to_current_entry, "/"); strcat(path_to_current_entry, date_string); strcat(path_to_current_entry, ".txt"); FILE *current_entry; if((current_entry = fopen(path_to_current_entry, "a"))){ /* add date and time stamp to journal entry */ fprintf(current_entry, "\n%s\n", date_and_time_str); fclose(current_entry); }else{ fprintf(stderr, "Unable to create file '%s'.\n", path_to_current_entry); exit(EXIT_FAILURE); } char editor_command[MAX_COMMAND]; strcpy(editor_command, EDITOR); strcat(editor_command, " "); strcat(editor_command, path_to_current_entry); system(editor_command); exit(EXIT_SUCCESS); }