diff options
author | Aaron Ball <nullspoon@oper.io> | 2017-01-05 08:28:38 -0700 |
---|---|---|
committer | Aaron Ball <nullspoon@oper.io> | 2017-01-05 08:30:55 -0700 |
commit | a6ffeb5f93682ce7490c7d5fc5b376d07bc03cd9 (patch) | |
tree | e62f2f096daf9fb58847f8b0e53c2989754a1991 | |
parent | 32e422d04fee8a01c2547e113cab516824ff6eee (diff) | |
download | noteless-a6ffeb5f93682ce7490c7d5fc5b376d07bc03cd9.tar.gz noteless-a6ffeb5f93682ce7490c7d5fc5b376d07bc03cd9.tar.xz |
Added detection for non-existent note directory
Was segfaulting when note directory didn't exist. Now we just see a nice
error message.
-rw-r--r-- | src/main.c | 3 | ||||
-rw-r--r-- | src/note_list.c | 48 |
2 files changed, 43 insertions, 8 deletions
@@ -19,6 +19,7 @@ #include <dirent.h> #include <string.h> #include <time.h> +#include <errno.h> #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; @@ -80,6 +81,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 * * @param list Note list the note-to-be-edited belongs to |