From a6ffeb5f93682ce7490c7d5fc5b376d07bc03cd9 Mon Sep 17 00:00:00 2001 From: Aaron Ball Date: Thu, 5 Jan 2017 08:28:38 -0700 Subject: Added detection for non-existent note directory Was segfaulting when note directory didn't exist. Now we just see a nice error message. --- src/main.c | 3 +++ src/note_list.c | 48 ++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/main.c b/src/main.c index 049de6d..aa51cdb 100644 --- a/src/main.c +++ b/src/main.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "note_list.h" #include "note.h" @@ -132,6 +133,8 @@ int main(int argc, char* argv []) { config_get(&c, "extension") ); + if(errno != 0) { return 1; } + if(strcmp(argv[1], "ls") == 0 || strcmp(argv[1], "list") == 0) { list_notes(&list); } else if(strcmp(argv[1], "new") == 0) { diff --git a/src/note_list.c b/src/note_list.c index d354257..f043866 100644 --- a/src/note_list.c +++ b/src/note_list.c @@ -27,6 +27,10 @@ int note_list_new(note_list* list, char* path, char* ext) { list->cursor = 0; // Open the dir fd list->dirp = opendir(path); + if(list->dirp == NULL) { + printf("Error: Could not open path '%s'.\n", path); + return 1; + } list->noteent = malloc(sizeof(note)); strcpy(list->path, path); strcpy(list->ext, ext); @@ -46,6 +50,7 @@ void note_list_free(note_list* list) { note* note_list_read(note_list* list) { struct dirent* de; + char fullpath[256]; char fext[64]; // Iterrate over dir entities @@ -64,14 +69,10 @@ note* note_list_read(note_list* list) { break; } - // Copy in the path - sprintf(list->noteent->path, "%s/%s", list->path, de->d_name); - // Copy in the extension - strcpy(list->noteent->extension, fext); - // Copy in the filename - strcpy(list->noteent->name, de->d_name); - // Overwrite . with null byte - list->noteent->name[strlen(de->d_name) - strlen(fext) - 1] = '\0'; + // Construct the note full path + sprintf(fullpath, "%s/%s", list->path, de->d_name); + // Create the note entity + note_new(list->noteent, fullpath); // Increment the file counter ++list->cursor; @@ -79,6 +80,37 @@ note* note_list_read(note_list* list) { } +/** + * Rewinds the note_list directory descriptior to zero. Basically executes + * rewinddir and reset the note cursor. + * + * @param list Note list to rewind + */ +void note_list_rewind(note_list* list) { + // Reset to dir fd to index 0 + rewinddir(list->dirp); + // Set list cursor to 0 + list->cursor = 0; +} + + +/** + * Seeks the note with the specified index (starting at 1). + * + * @param list Note list to seek through + * @param index Integer index to seek to + */ +note* note_list_seek(note_list* list, int index) { + // Reset dir to 0 + note_list_rewind(list); + + while(list->cursor < index) { + list->noteent = note_list_read(list); + } + return list->noteent; +} + + /** * Opens the specified note for editing * -- cgit v1.2.3