diff --git a/src/main.c b/src/main.c index 301caa0..19c1e80 100644 --- a/src/main.c +++ b/src/main.c @@ -54,14 +54,17 @@ int main(int argc, char *argv[]) { // loads and read the config file //gets the size fseek(f, 0, SEEK_END); - long size = ftell(f); + size_t size = ftell(f); rewind(f); //gets the data char *data = malloc(size+1); error(!data, "program", "malloc failed allocating memory for the variable data."); size_t readBytes = fread(data, 1, size, f); // 1 --> size of each item - if (readBytes!=size) {free(data);fclose(f);} + if (readBytes!=size) { + free(data); + fclose(f); + } error(readBytes!=size, "program", "Failed to read config file (%s) (%zu bytes read, expected %ld)", configPath, readBytes, size); data[size] = '\0'; fclose(f); @@ -277,7 +280,7 @@ int main(int argc, char *argv[]) { // select a vault char *vaultSelected = NULL; - size_t vaultsCount = 0; + int vaultsCount = 0; char **vaultsArray = getVaultsFromDirectory(notesDirectoryString, &vaultsCount, shouldDebug); // bypass if -v or --vault is set @@ -298,7 +301,7 @@ int main(int argc, char *argv[]) { qsort(vaultsArray, vaultsCount, sizeof(const char *), compareString); // sorts the vaults alphabetically debug("Available vaults"); if (shouldDebug) { - for (size_t i = 0; i < vaultsCount; i++) { + for (int i = 0; i < vaultsCount; i++) { altDebug("%s\n", vaultsArray[i]); } altDebug("└ ------------------------------\n"); @@ -344,7 +347,7 @@ note_selection: filesCount = filesCount + journalCount; debug("Available notes and journals:"); if (shouldDebug) { - for (size_t i = 0; i < filesCount; i++) { + for (int i = 0; i < filesCount; i++) { altDebug("%s\n", filesArray[i]); } altDebug("└------------------------------\n"); @@ -413,7 +416,7 @@ note_creation: char pathToRMRF[PATH_MAX]; sprintf(pathToRMRF, "%s/%s", notesDirectoryString, vaultSelected); debug("Removed the directory: %s", pathToRMRF); - rmrf(pathToRMRF); + rmrf(pathToRMRF, shouldDebug); shouldChangeVault = 1; } } else if (strcmp(noteSelected,"Quit (Ctrl+C)") == 0) { diff --git a/src/notes.c b/src/notes.c index 720aeae..f49580c 100644 --- a/src/notes.c +++ b/src/notes.c @@ -10,7 +10,7 @@ char **getJournalsFromVault(char *pathToVault, char *vault, char *journalRegex, DIR *vaultDirectory = opendir(tempPath); error(vaultDirectory==NULL, "program", "Could not open directory %s", tempPath); char **journalsArray = NULL; // will contain all the notes - size_t journalsCount = 0; // we need to count how many notes there is to always readjust how many memory we alloc + int journalsCount = 0; // we need to count how many notes there is to always readjust how many memory we alloc // https://stackoverflow.com/a/1085120 for regex code regex_t regex; @@ -57,7 +57,7 @@ char** getNotesFromVault(char *pathToVault, char *vault, char *journalRegex, int DIR *vaultDirectory = opendir(tempPath); error(vaultDirectory==NULL, "program", "Could not open directory %s", tempPath); char **notesArray = NULL; // will contain all the notes - size_t notesCount = 0; // we need to count how many notes there is to always readjust how many memory we alloc + int notesCount = 0; // we need to count how many notes there is to always readjust how many memory we alloc // Refer https://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html // for readdir() debug("┌------------------------------\nDetected Files and dirs from the vault:"); @@ -91,7 +91,7 @@ char** getNotesFromVault(char *pathToVault, char *vault, char *journalRegex, int return notesArray; } -char **getVaultsFromDirectory(char *dirString, size_t *count, int shouldDebug) { +char **getVaultsFromDirectory(char *dirString, int *count, int shouldDebug) { // (TODO LATER) it might be a good idea to check if these directories exist // (TODO LATER) expand ~ as it does not work with opendir() // this function is inputed a path to a directory (which comes usually from the config file) and outpus all the suitable directories (so not the hidden ones) which will serve as separate vaults for notes @@ -101,7 +101,7 @@ char **getVaultsFromDirectory(char *dirString, size_t *count, int shouldDebug) { DIR *vaultsDirectory = opendir(dirString); error(vaultsDirectory==NULL, "program", "Could not open directory %s", dirString); char **dirsArray = NULL; // will contain all the dirs/vaults - size_t dirsCount = 0; // we need to count how many dirs there is to always readjust how many memory we alloc + int dirsCount = 0; // we need to count how many dirs there is to always readjust how many memory we alloc // Refer https://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html // for readdir() debug("┌------------------------------\n Detected files and dirs from the directory:"); @@ -166,7 +166,7 @@ char *updateJournal(char *path, char *journal, char *timeFormat, int shouldDebug snprintf(createEntryMessage, PATH_MAX, "Create new entry for the journal %s", journal); entryArray[0] = createEntryMessage; entryArray[1] = "Open random entry"; - size_t entryCount = extraOptions; // we need to count how many dirs there is to always readjust how many memory we alloc + int entryCount = extraOptions; // we need to count how many dirs there is to always readjust how many memory we alloc // Refer https://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html // for readdir() debug("┌------------------------------\n Detected files and dirs from %s:", path); diff --git a/src/notes.h b/src/notes.h index 2d772a2..9bd6b57 100644 --- a/src/notes.h +++ b/src/notes.h @@ -10,7 +10,7 @@ char **getJournalsFromVault(char *pathToVault, char *vault, char *journalRegex, // journalRegex is the regex code for the journals. If a note matches this code, it is treated as a journal and it is not outputed from this function. char **getNotesFromVault(char *pathToVault, char *vault, char *journalRegex, int *count, int shouldDebug); // this function is inputed a path to a directory (which comes usually from the config file) and outpus all the suitable directories (so not the hidden ones) which will serve as separate vaults for notes. -char **getVaultsFromDirectory(char *dirString, size_t *count, int shouldDebug); +char **getVaultsFromDirectory(char *dirString, int *count, int shouldDebug); // path is the path to the file. // journal is the name of the file. // handles both type of journal (divided and unified). diff --git a/src/ui.c b/src/ui.c index 2c9eb8c..5e0b1ff 100644 --- a/src/ui.c +++ b/src/ui.c @@ -145,7 +145,7 @@ char *createNewNote(char dirToVault[PATH_MAX], char *vaultFromDir, int bypass, c return fileName; } -char* ncursesSelect(char **options, char *optionsText, size_t optionsNumber, size_t extraOptionsNumber, char *bottomText, char *middleText, char *topText, int shouldDebug) { +char* ncursesSelect(char **options, char *optionsText, int optionsNumber, int extraOptionsNumber, char *bottomText, char *middleText, char *topText, int shouldDebug) { int highlight = 0; //curently highlighted option int key; @@ -221,5 +221,7 @@ char* ncursesSelect(char **options, char *optionsText, size_t optionsNumber, siz } end_loop: endwin(); // end ncurses mode + fflush(stderr); + debug("Selected option: %s", options[highlight]); return options[highlight]; } diff --git a/src/ui.h b/src/ui.h index b95579e..f61e169 100644 --- a/src/ui.h +++ b/src/ui.h @@ -36,5 +36,5 @@ middleText (usally \n) is printed between options and extraOptions. bottomText is printed bellow. topText and middle must be exactly one line. If you want empty lines use " " and not "". returns the selected option. */ -char* ncursesSelect(char **options, char *optionsText, size_t optionsNumber, size_t extraOptionsNumber, char *bottomText, char *middleText, char *topText, int debug); +char* ncursesSelect(char **options, char *optionsText, int optionsNumber, int extraOptionsNumber, char *bottomText, char *middleText, char *topText, int debug); #endif diff --git a/src/utils.c b/src/utils.c index 415a21b..a6afaa1 100644 --- a/src/utils.c +++ b/src/utils.c @@ -102,8 +102,8 @@ static void ensureDir(const char *path, const int shouldDebug) { } void initAppFilesAndDirs(const char *home, const int shouldDebug) { - char config_dir[512]; - char cache_dir[512]; + char config_dir[PATH_MAX]; + char cache_dir[PATH_MAX]; snprintf(config_dir, sizeof(config_dir), "%s/.config/notewrapper", home); @@ -114,9 +114,8 @@ void initAppFilesAndDirs(const char *home, const int shouldDebug) { ensureDir(config_dir, shouldDebug); ensureDir(cache_dir, shouldDebug); - char config_file[512]; - snprintf(config_file, sizeof(config_file), - "%s/config.json", config_dir); + char config_file[PATH_MAX+12]; + snprintf(config_file, sizeof(config_file), "%s/config.json", config_dir); FILE *f = fopen(config_file, "r"); if (f) { @@ -288,7 +287,7 @@ void appendToFile(const char *path, const char *string, const int shouldDebug) { } void sanitize(char *string) { - for (int i = 0; i < strlen(string); i++) { + for (size_t i = 0; i < strlen(string); i++) { if ((!isalnum((unsigned char)string[i]) && strchr("/\\:*?\"\'<>\n\r\t", string[i])) || ((i == 0 || i == 1) && string[i] == '.')) { // replace unwanted chars by '_'. '.' is replaced if it is only the first two chars // (TODO LATER fixe case where it "*.*", "..." and so on string[i] = '_'; @@ -302,15 +301,19 @@ void sanitize(char *string) { // so it walks the file tree and deletes it's content before removing the directory // (TODO LATER) this seems safe, but it's maybe a good idea to add some checks to not remove something it should not remove int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) { + (void)sb; + (void)typeflag; + (void)ftwbuf; + int shouldDebug = 1; //normally shouldDebug should be passed to the function. However, it's to difficult here and the best it to set it to 1. int rv = remove(fpath); error(rv, "program", "remove() failed to delete %s", fpath); return rv; } -int rmrf(char *path) { - // (TODO LATER) Check if it not a root directory or something like this - return nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS); +int rmrf(char *path, int shouldDebug) { + error(strcmp(path, "/") == 0, "program", "Refusing to delete root directory"); + return nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS); } diff --git a/src/utils.h b/src/utils.h index e886c42..9fc070a 100644 --- a/src/utils.h +++ b/src/utils.h @@ -17,6 +17,7 @@ #include #include #include +#include #define BUFFER_SIZE 256 //standard buffer size. // the three supported values are "daily" "weekly" and "monthly" (for months we use the avarage lenght of a month in a non leap year). All values were calculated on my loyal TI-30 ECO RS #define DAILY 86400 @@ -65,7 +66,7 @@ void appendToFile(const char *path, const char *string, const int shouldDebug); void sanitize(char *string); //from https://stackoverflow.com/a/5467788. //deletes an entire directory. Use with parsimony and carefullness. -int rmrf(char *path); +int rmrf(char *path, int shouldDebug); // Inputs are the path to the file, the editor to open and some rendering option. // render: if we render the .md file with Vivify. // shouldJumpToEndOfFile: if we put the cursor at the end of the file when opening.