From 887e32a0e087ace3aba61eddd49249135a8570c7 Mon Sep 17 00:00:00 2001 From: Robert Hawdon Date: Thu, 21 Feb 2019 22:23:30 +0000 Subject: [PATCH 01/10] Moved slit directory to its own function --- configure.ac | 2 +- src/showfunctions.c | 114 +++++++++++++++++--------------------------- src/showfunctions.h | 5 ++ 3 files changed, 49 insertions(+), 72 deletions(-) diff --git a/configure.ac b/configure.ac index 0ac71a6..dcf8ead 100644 --- a/configure.ac +++ b/configure.ac @@ -1,4 +1,4 @@ -AC_INIT([dfshow], [0.6.1], [https://github.com/roberthawdon/dfshow/issues]) +AC_INIT([dfshow], [0.6.2-dev], [https://github.com/roberthawdon/dfshow/issues]) AC_GNU_SOURCE AM_INIT_AUTOMAKE([subdir-objects]) AC_PROG_CC diff --git a/src/showfunctions.c b/src/showfunctions.c index c339d8d..0273aff 100644 --- a/src/showfunctions.c +++ b/src/showfunctions.c @@ -152,103 +152,75 @@ int checkRunningEnv(){ return i; } -char *getRelativePath(char *file, char *target) -{ - typedef struct { - char directories[256]; - } path; - - char *result = malloc(sizeof(char) + 1); - int i, j, e, c, resultLen, targetUp, fileUp; - path *fileStruct, *targetStruct; - int currentFileIndex, currentTargetIndex, fileLen, targetLen, commonPath = 0; +int splitPath(pathDirs **dirStruct, char *path){ + int e, i, j, c; + pathDirs *tmp; - targetUp = fileUp = 0; - - fileStruct = malloc(sizeof(path)); - targetStruct = malloc(sizeof(path)); + tmp = malloc(sizeof(pathDirs)); + if (tmp){ + *dirStruct = tmp; + } - // Store sections of file in structure e = -1; j = 0; - c = strlen(file); + c = strlen(path); for(i = 0; i < c; i++){ - if (file[i] == '/'){ + if (path[i] == '/'){ if (e > -1){ - fileStruct[e].directories[j] = '\0'; - if(!strcmp(fileStruct[e].directories, "..")){ + (*dirStruct)[e].directories[j] = '\0'; + if(!strcmp((*dirStruct)[e].directories, "..")){ // assmue .. and remove the element before - fileStruct[e] = fileStruct[e - 1]; - fileStruct[e - 1] = fileStruct[e - 2]; + (*dirStruct)[e] = (*dirStruct)[e - 1]; + (*dirStruct)[e - 1] = (*dirStruct)[e - 2]; e--; - fileStruct = realloc(fileStruct, sizeof(path) * (2 + e)); - } else if (!strcmp(fileStruct[e].directories, ".")){ + (*dirStruct) = realloc((*dirStruct), sizeof(pathDirs) * (2 + e)); + } else if (!strcmp((*dirStruct)[e].directories, ".")){ // strip single . - strcpy(fileStruct[e].directories, "\0"); + strcpy((*dirStruct)[e].directories, "\0"); } else { // If element created is NOT .. e++; - fileStruct = realloc(fileStruct, sizeof(path) * (2 + e)); + (*dirStruct) = realloc((*dirStruct), sizeof(pathDirs) * (2 + e)); } } else { e++; - fileStruct = realloc(fileStruct, sizeof(path) * (2 + e)); + (*dirStruct) = realloc((*dirStruct), sizeof(pathDirs) * (2 + e)); } j=0; } else { - fileStruct[e].directories[j] = file[i]; + (*dirStruct)[e].directories[j] = path[i]; j++; } } - fileStruct[e].directories[j] = '\0'; - if (!strcmp(fileStruct[e].directories, ".")){ - strcpy(fileStruct[e].directories, ""); + (*dirStruct)[e].directories[j] = '\0'; + if (!strcmp((*dirStruct)[e].directories, ".")){ + strcpy((*dirStruct)[e].directories, ""); e--; } + + return(e); +} + +char *getRelativePath(char *file, char *target) +{ + char *result = malloc(sizeof(char) + 1); + int i, j, e, c, resultLen, targetUp, fileUp; + pathDirs *fileStruct, *targetStruct; + int fileLen, targetLen, commonPath = 0; + + targetUp = fileUp = 0; + + // fileStruct = malloc(sizeof(path)); + // targetStruct = malloc(sizeof(path)); + + // Store sections of file in structure + e = splitPath(&fileStruct, file); fileLen = e + 1; - currentFileIndex = e; // Store sections of target in structure - e = -1; - j = 0; - c = strlen(target); - - for(i = 0; i < c; i++){ - if (target[i] == '/'){ - if (e > -1){ - targetStruct[e].directories[j] = '\0'; - if(!strcmp(targetStruct[e].directories, "..")){ - // assmue .. and remove the element before - targetStruct[e] = targetStruct[e - 1]; - targetStruct[e - 1] = targetStruct[e - 2]; - e--; - targetStruct = realloc(targetStruct, sizeof(path) * (2 + e)); - } else if (!strcmp(targetStruct[e].directories, ".")){ - // strip single . - strcpy(targetStruct[e].directories, "\0"); - } else { - // If element created is NOT .. - e++; - targetStruct = realloc(targetStruct, sizeof(path) * (2 + e)); - } - } else { - e++; - targetStruct = realloc(targetStruct, sizeof(path) * (2 + e)); - } - j=0; - } else { - targetStruct[e].directories[j] = target[i]; - j++; - } - } - targetStruct[e].directories[j] = '\0'; - if (!strcmp(targetStruct[e].directories, ".")){ - strcpy(targetStruct[e].directories, ""); - e--; - } + e = splitPath(&targetStruct, target); targetLen = e + 1; - currentTargetIndex = e; // Find the smallest of our structures if (fileLen > targetLen){ @@ -303,9 +275,9 @@ char *getRelativePath(char *file, char *target) } } else { // Assume we're in the same directory at this point - j = strlen(fileStruct[currentFileIndex].directories); + j = strlen(fileStruct[fileLen - 1].directories); result = realloc(result, sizeof(char) * (j + 1)); - sprintf(result, "%s", fileStruct[currentFileIndex].directories); + sprintf(result, "%s", fileStruct[fileLen - 1].directories); } // result[resultLen - 1] = '\0'; // This seems to cause no end of grief on FreeBSD and I can't even remember why it's here. diff --git a/src/showfunctions.h b/src/showfunctions.h index fbd21fc..c8e771e 100644 --- a/src/showfunctions.h +++ b/src/showfunctions.h @@ -28,7 +28,12 @@ typedef struct { int selected; } history; +typedef struct { + char directories[256]; +} pathDirs; + int checkRunningEnv(); +int splitPath(pathDirs **dirStruct, char *path); char *getRelativePath(char *file, char *target); int wildcard(const char *value, char *wcard); int findResultByName(results *ob, char *name); From 18c912d02cdf729829a34a05dc3b1c49b6e5a42d Mon Sep 17 00:00:00 2001 From: Robert Hawdon Date: Thu, 21 Feb 2019 23:16:11 +0000 Subject: [PATCH 02/10] Add create parent directory function --- src/showfunctions.c | 24 +++++++++++++++++++++--- src/showfunctions.h | 1 + src/showmenus.c | 3 +++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/showfunctions.c b/src/showfunctions.c index 0273aff..b3c0f32 100644 --- a/src/showfunctions.c +++ b/src/showfunctions.c @@ -211,9 +211,6 @@ char *getRelativePath(char *file, char *target) targetUp = fileUp = 0; - // fileStruct = malloc(sizeof(path)); - // targetStruct = malloc(sizeof(path)); - // Store sections of file in structure e = splitPath(&fileStruct, file); fileLen = e + 1; @@ -289,6 +286,27 @@ char *getRelativePath(char *file, char *target) return(result); } +void createParentDirs(char *path){ + pathDirs *targetPath; + char *tempPath = malloc(sizeof(char) + 1); + int e, i = 0; + + e = splitPath(&targetPath, path); + + strcpy(tempPath, ""); + for (i = 0; i < e; i++){ + tempPath = realloc(tempPath, sizeof(char) * (strlen(tempPath) + strlen(targetPath[i].directories) + 2)); + sprintf(tempPath, "%s/%s", tempPath, targetPath[i].directories); + if (!check_dir(tempPath)){ + mk_dir(tempPath); + } + } + + free(targetPath); + free(tempPath); + return; +} + int wildcard(const char *value, char *wcard) { int vsize = (int)strlen(value); diff --git a/src/showfunctions.h b/src/showfunctions.h index c8e771e..c7d32f9 100644 --- a/src/showfunctions.h +++ b/src/showfunctions.h @@ -34,6 +34,7 @@ typedef struct { int checkRunningEnv(); int splitPath(pathDirs **dirStruct, char *path); +void createParentDirs(char *path); char *getRelativePath(char *file, char *target); int wildcard(const char *value, char *wcard); int findResultByName(results *ob, char *name); diff --git a/src/showmenus.c b/src/showmenus.c index 74c484d..5adc495 100644 --- a/src/showmenus.c +++ b/src/showmenus.c @@ -1174,6 +1174,7 @@ void linktext_input(char *file, int symbolic) free(rewrite); } + //relSymlink: if (check_dir(dirFromPath(target))){ if (check_file(target)){ topLineMessage("Error: File exists."); @@ -1195,6 +1196,8 @@ void linktext_input(char *file, int symbolic) } } else { topLineMessage("Error: Directory Not Found."); + //createParentDirs(target); + //goto relSymlink; } } directory_view_menu_inputs(); From 3727cdc84fec2d9d8769687920e8c06b7ae31279 Mon Sep 17 00:00:00 2001 From: Robert Hawdon Date: Fri, 22 Feb 2019 23:36:50 +0000 Subject: [PATCH 03/10] Create parent directories menu and implementation on links --- src/showmenus.c | 44 +++++++++++++++++++++++++++++++++++++++----- src/showmenus.h | 1 + 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/showmenus.c b/src/showmenus.c index 5adc495..dba1c34 100644 --- a/src/showmenus.c +++ b/src/showmenus.c @@ -763,6 +763,36 @@ char * execute_argument_input(const char *exec) return strout; } +int createParentsInput(char *path) +{ + int result = 0; + int messageLen; + wchar_t *message = malloc(sizeof(wchar_t) * 1); + + messageLen = (strlen(path) + 64); + + message = realloc(message, sizeof(wchar_t) * (messageLen + 1)); + + swprintf(message, messageLen, L"Directory [<%s>] does not exist. Create it? !Yes/!No (enter = no)", path); + wPrintMenu(0,0, message); + while(1) + { + *pc = getch10th(); + if (*pc == 'y'){ + result = 1; + break; + } else if ((*pc == 'n') || (*pc == 10)){ + result = 0; + break; + } else if (*pc == 27){ + result = -1; + break; + } + } + free(message); + return(result); +} + int huntCaseSelectInput() { int result = 0; @@ -1141,7 +1171,7 @@ void linktext_input(char *file, int symbolic) char inputmessage[32]; char typeText[9]; char target[1024]; - int relative; + int relative, e; char *relativeFile; char tempDebug[1024]; strcpy(target, currentpwd); @@ -1174,7 +1204,7 @@ void linktext_input(char *file, int symbolic) free(rewrite); } - //relSymlink: + relSymlink: if (check_dir(dirFromPath(target))){ if (check_file(target)){ topLineMessage("Error: File exists."); @@ -1195,9 +1225,13 @@ void linktext_input(char *file, int symbolic) refreshDirectory(sortmode, 0, selected, 0); } } else { - topLineMessage("Error: Directory Not Found."); - //createParentDirs(target); - //goto relSymlink; + e = createParentsInput(dirFromPath(target)); + if (e == 1){ + createParentDirs(target); + goto relSymlink; + } else { + topLineMessage("Error: Directory Not Found."); + } } } directory_view_menu_inputs(); diff --git a/src/showmenus.h b/src/showmenus.h index e6de197..9f64842 100644 --- a/src/showmenus.h +++ b/src/showmenus.h @@ -4,6 +4,7 @@ void unloadMenuLabels(); int touchType(); time_t touchTimeInput(int type); int symLinkLocation(); +int createParentsInput(char *path); void linktext_input(int selected, int symbolic); void link_key_menu_inputs(); void modify_key_menu_inputs(); From 2852f7d340badb8fdc2a3cebe4997034ac5a2ceb Mon Sep 17 00:00:00 2001 From: Robert Hawdon Date: Sat, 23 Feb 2019 16:07:43 +0000 Subject: [PATCH 04/10] Create parent directories on most file creation conditions --- src/showmenus.c | 129 +++++++++++++++++++++++++++++++----------------- 1 file changed, 83 insertions(+), 46 deletions(-) diff --git a/src/showmenus.c b/src/showmenus.c index dba1c34..34b4b10 100644 --- a/src/showmenus.c +++ b/src/showmenus.c @@ -273,6 +273,36 @@ int sanitizeTopFileRef(int topfileref) return topfileref; } +int createParentsInput(char *path) +{ + int result = 0; + int messageLen; + wchar_t *message = malloc(sizeof(wchar_t) * 1); + + messageLen = (strlen(path) + 64); + + message = realloc(message, sizeof(wchar_t) * (messageLen + 1)); + + swprintf(message, messageLen, L"Directory [<%s>] does not exist. Create it? !Yes/!No (enter = no)", path); + wPrintMenu(0,0, message); + while(1) + { + *pc = getch10th(); + if (*pc == 'y'){ + result = 1; + break; + } else if ((*pc == 'n') || (*pc == 10)){ + result = 0; + break; + } else if (*pc == 27){ + result = -1; + break; + } + } + free(message); + return(result); +} + void refreshDirectory(char *sortmode, int origtopfileref, int origselected, int destructive) { char currentselectname[512]; @@ -399,6 +429,7 @@ void copy_file_input(char *file) { // YUCK, repetition, this needs sorting char newfile[1024]; + int e; move(0,0); clrtoeol(); mvprintw(0, 0, "Copy file to:"); @@ -413,6 +444,7 @@ void copy_file_input(char *file) strcpy(newfile, rewrite); free(rewrite); } + copyFile: if ( check_dir(dirFromPath(newfile))){ if ( check_file(newfile) ) { @@ -426,7 +458,13 @@ void copy_file_input(char *file) refreshDirectory(sortmode, 0, selected, 0); } } else { - topLineMessage("Error: Directory Not Found."); + e = createParentsInput(dirFromPath(newfile)); + if (e == 1){ + createParentDirs(newfile); + goto copyFile; + } else { + topLineMessage("Error: Directory Not Found."); + } } } directory_view_menu_inputs(); @@ -434,7 +472,7 @@ void copy_file_input(char *file) void copy_multi_file_input(results* ob, char *input) { - int i; + int i, e; char dest[1024]; char destfile[1024]; @@ -451,6 +489,7 @@ void copy_multi_file_input(results* ob, char *input) strcpy(dest, rewrite); free(rewrite); } + copyMultiFile: if ( check_dir(dest) ){ for (i = 0; i < totalfilecount; i++) { @@ -477,8 +516,16 @@ void copy_multi_file_input(results* ob, char *input) } } } + refreshDirectory(sortmode, 0, selected, 0); } else { - topLineMessage("Error: Directory Not Found."); + e = createParentsInput(dest); + if (e == 1){ + createParentDirs(dest); + mk_dir(dest); // Needed as the final element is omitted by the above + goto copyMultiFile; + } else { + topLineMessage("Error: Directory Not Found."); + } } } directory_view_menu_inputs(); @@ -486,7 +533,7 @@ void copy_multi_file_input(results* ob, char *input) void rename_multi_file_input(results* ob, char *input) { - int i; + int i, e; char dest[1024]; char destfile[1024]; @@ -503,6 +550,7 @@ void rename_multi_file_input(results* ob, char *input) strcpy(dest, rewrite); free(rewrite); } + renameMultiFile: if ( check_dir(dest) ){ for (i = 0; i < totalfilecount; i++) { @@ -530,7 +578,14 @@ void rename_multi_file_input(results* ob, char *input) } } } else { - topLineMessage("Error: Directory Not Found."); + e = createParentsInput(dest); + if (e == 1){ + createParentDirs(dest); + mk_dir(dest); // Needed as the final element is omitted by the above + goto renameMultiFile; + } else { + topLineMessage("Error: Directory Not Found."); + } } refreshDirectory(sortmode, 0, selected, 1); } @@ -556,6 +611,7 @@ void rename_file_input(char *file) { // YUCK, repetition, this needs sorting char dest[1024]; + int e; move(0,0); clrtoeol(); mvprintw(0, 0, "Rename file to:"); @@ -569,18 +625,29 @@ void rename_file_input(char *file) strcpy(dest, rewrite); free(rewrite); } - if ( check_file(dest) ) - { - if ( replace_file_confirm_input(dest) ) - { - RenameObject(file, dest); - strcpy(currentfilename, objectFromPath(dest)); - refreshDirectory(sortmode, 0, selected, 2); - } + renameFile: + if ( check_dir(dirFromPath(dest))){ + if ( check_file(dest) ) + { + if ( replace_file_confirm_input(dest) ) + { + RenameObject(file, dest); + strcpy(currentfilename, objectFromPath(dest)); + refreshDirectory(sortmode, 0, selected, 2); + } + } else { + RenameObject(file, dest); + strcpy(currentfilename, objectFromPath(dest)); + refreshDirectory(sortmode, 0, selected, 2); + } + } else { + e = createParentsInput(dirFromPath(dest)); + if (e == 1){ + createParentDirs(dest); + goto renameFile; } else { - RenameObject(file, dest); - strcpy(currentfilename, objectFromPath(dest)); - refreshDirectory(sortmode, 0, selected, 2); + topLineMessage("Error: Directory Not Found."); + } } } directory_view_menu_inputs(); @@ -763,36 +830,6 @@ char * execute_argument_input(const char *exec) return strout; } -int createParentsInput(char *path) -{ - int result = 0; - int messageLen; - wchar_t *message = malloc(sizeof(wchar_t) * 1); - - messageLen = (strlen(path) + 64); - - message = realloc(message, sizeof(wchar_t) * (messageLen + 1)); - - swprintf(message, messageLen, L"Directory [<%s>] does not exist. Create it? !Yes/!No (enter = no)", path); - wPrintMenu(0,0, message); - while(1) - { - *pc = getch10th(); - if (*pc == 'y'){ - result = 1; - break; - } else if ((*pc == 'n') || (*pc == 10)){ - result = 0; - break; - } else if (*pc == 27){ - result = -1; - break; - } - } - free(message); - return(result); -} - int huntCaseSelectInput() { int result = 0; From a8db28432e4c9736a85fc75922d95670255e49aa Mon Sep 17 00:00:00 2001 From: Robert Hawdon Date: Sat, 23 Feb 2019 22:52:28 +0000 Subject: [PATCH 05/10] Added creation of parent directories on make directory function --- src/showmenus.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/showmenus.c b/src/showmenus.c index 34b4b10..9425723 100644 --- a/src/showmenus.c +++ b/src/showmenus.c @@ -656,6 +656,7 @@ void rename_file_input(char *file) void make_directory_input() { char newdir[1024]; + int e; move(0,0); clrtoeol(); mvprintw(0, 0, "Make Directory - Enter pathname:"); @@ -671,8 +672,18 @@ void make_directory_input() strcpy(newdir, rewrite); free(rewrite); } + makeDir: if (access(dirFromPath(newdir), W_OK) == 0){ mk_dir(newdir); + } else if (access(dirFromPath(newdir), W_OK) == -1) { + e = createParentsInput(dirFromPath(newdir)); + if (e == 1){ + createParentDirs(newdir); + goto makeDir; + } else { + sprintf(errmessage, "Error: %s", strerror(errno)); + topLineMessage(errmessage); + } } else { sprintf(errmessage, "Error: %s", strerror(errno)); topLineMessage(errmessage); From d199cab4cd41bd784f40f75db89572d71fc30f9e Mon Sep 17 00:00:00 2001 From: Robert Hawdon Date: Sun, 24 Feb 2019 23:50:17 +0000 Subject: [PATCH 06/10] Added parent file check to other functions --- src/colors.c | 31 ++++++++--- src/common.c | 101 ++++++++++++++++++++++++++++++++++++ src/common.h | 7 +++ src/showfunctions.c | 71 -------------------------- src/showfunctions.h | 6 --- src/showmenus.c | 122 +++++++++++++++++++++++--------------------- src/showmenus.h | 1 - 7 files changed, 195 insertions(+), 144 deletions(-) diff --git a/src/colors.c b/src/colors.c index dcbe988..b309e29 100644 --- a/src/colors.c +++ b/src/colors.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "common.h" #include "colors.h" @@ -41,6 +42,8 @@ colorPairs colors[256]; char fgbgLabel[11]; +char errmessage[256]; + extern int colormode; extern int c; extern int * pc; @@ -220,7 +223,7 @@ void refreshColors(){ void saveTheme(){ config_t cfg; config_setting_t *root, *setting, *group, *array; - int e, i; + int e, f, i; char filename[1024]; char * rewrite; move(0,0); @@ -249,7 +252,8 @@ void saveTheme(){ setting = config_setting_add(array, NULL, CONFIG_TYPE_INT); config_setting_set_int(setting, colors[i].bold); } - if (check_dir(dirFromPath(filename))){ + saveTheme: + if (access(dirFromPath(filename), W_OK) == 0){ if (check_file(filename)){ curs_set(FALSE); printMenu(0,0, "File exists. Replace? (!Yes/!No)"); @@ -274,11 +278,24 @@ void saveTheme(){ setenv("DFS_THEME", objectFromPath(filename), 1); } } else { - curs_set(FALSE); - mk_dir(dirFromPath(filename)); - config_write_file(&cfg, filename); - // topLineMessage("Error: Unable to write file"); - curs_set(TRUE); + if (errno == ENOENT){ + // curs_set(FALSE); + // mk_dir(dirFromPath(filename)); + // config_write_file(&cfg, filename); + // // topLineMessage("Error: Unable to write file"); + // curs_set(TRUE); + f = createParentsInput(dirFromPath(filename)); + if (f == 1){ + createParentDirs(filename); + goto saveTheme; + } else { + sprintf(errmessage, "Error: %s", strerror(errno)); + topLineMessage(errmessage); + } + } else { + sprintf(errmessage, "Error: %s", strerror(errno)); + topLineMessage(errmessage); + } } config_destroy(&cfg); } diff --git a/src/common.c b/src/common.c index ed44d68..31370e8 100644 --- a/src/common.c +++ b/src/common.c @@ -67,6 +67,107 @@ int getch10th (void) { return ch; } +int splitPath(pathDirs **dirStruct, char *path){ + int e, i, j, c; + pathDirs *tmp; + + tmp = malloc(sizeof(pathDirs)); + if (tmp){ + *dirStruct = tmp; + } + + e = -1; + j = 0; + c = strlen(path); + + for(i = 0; i < c; i++){ + if (path[i] == '/'){ + if (e > -1){ + (*dirStruct)[e].directories[j] = '\0'; + if(!strcmp((*dirStruct)[e].directories, "..")){ + // assmue .. and remove the element before + (*dirStruct)[e] = (*dirStruct)[e - 1]; + (*dirStruct)[e - 1] = (*dirStruct)[e - 2]; + e--; + (*dirStruct) = realloc((*dirStruct), sizeof(pathDirs) * (2 + e)); + } else if (!strcmp((*dirStruct)[e].directories, ".")){ + // strip single . + strcpy((*dirStruct)[e].directories, "\0"); + } else { + // If element created is NOT .. + e++; + (*dirStruct) = realloc((*dirStruct), sizeof(pathDirs) * (2 + e)); + } + } else { + e++; + (*dirStruct) = realloc((*dirStruct), sizeof(pathDirs) * (2 + e)); + } + j=0; + } else { + (*dirStruct)[e].directories[j] = path[i]; + j++; + } + } + (*dirStruct)[e].directories[j] = '\0'; + if (!strcmp((*dirStruct)[e].directories, ".")){ + strcpy((*dirStruct)[e].directories, ""); + e--; + } + + return(e); +} + +int createParentsInput(char *path) +{ + int result = 0; + int messageLen; + wchar_t *message = malloc(sizeof(wchar_t) * 1); + + messageLen = (strlen(path) + 64); + + message = realloc(message, sizeof(wchar_t) * (messageLen + 1)); + + swprintf(message, messageLen, L"Directory [<%s>] does not exist. Create it? !Yes/!No (enter = no)", path); + wPrintMenu(0,0, message); + while(1) + { + *pc = getch10th(); + if (*pc == 'y'){ + result = 1; + break; + } else if ((*pc == 'n') || (*pc == 10)){ + result = 0; + break; + } else if (*pc == 27){ + result = -1; + break; + } + } + free(message); + return(result); +} + +void createParentDirs(char *path){ + pathDirs *targetPath; + char *tempPath = malloc(sizeof(char) + 1); + int e, i = 0; + + e = splitPath(&targetPath, path); + + strcpy(tempPath, ""); + for (i = 0; i < e; i++){ + tempPath = realloc(tempPath, sizeof(char) * (strlen(tempPath) + strlen(targetPath[i].directories) + 2)); + sprintf(tempPath, "%s/%s", tempPath, targetPath[i].directories); + if (!check_dir(tempPath)){ + mk_dir(tempPath); + } + } + + free(targetPath); + free(tempPath); + return; +} + int cmp_menu_ref(const void *lhs, const void *rhs) { diff --git a/src/common.h b/src/common.h index 37afbd9..87c2046 100644 --- a/src/common.h +++ b/src/common.h @@ -10,8 +10,15 @@ typedef struct { int displayLabelSize; } menuDef; +typedef struct { + char directories[256]; +} pathDirs; + int getch10th (void); int cmp_menu_ref(const void *lhs, const void *rhs); +int splitPath(pathDirs **dirStruct, char *path); +int createParentsInput(char *path); +void createParentDirs(char *path); void addMenuItem(menuDef **dfMenu, int *pos, char* refLabel, wchar_t* displayLabel, int hotKey); void updateMenuItem(menuDef **dfMenu, int *menuSize, char* refLabel, wchar_t* displayLabel); wchar_t * genMenuDisplayLabel(wchar_t* preMenu, menuDef* dfMenu, int size, wchar_t* postMenu, int comma); diff --git a/src/showfunctions.c b/src/showfunctions.c index b3c0f32..aea0a5b 100644 --- a/src/showfunctions.c +++ b/src/showfunctions.c @@ -152,56 +152,6 @@ int checkRunningEnv(){ return i; } -int splitPath(pathDirs **dirStruct, char *path){ - int e, i, j, c; - pathDirs *tmp; - - tmp = malloc(sizeof(pathDirs)); - if (tmp){ - *dirStruct = tmp; - } - - e = -1; - j = 0; - c = strlen(path); - - for(i = 0; i < c; i++){ - if (path[i] == '/'){ - if (e > -1){ - (*dirStruct)[e].directories[j] = '\0'; - if(!strcmp((*dirStruct)[e].directories, "..")){ - // assmue .. and remove the element before - (*dirStruct)[e] = (*dirStruct)[e - 1]; - (*dirStruct)[e - 1] = (*dirStruct)[e - 2]; - e--; - (*dirStruct) = realloc((*dirStruct), sizeof(pathDirs) * (2 + e)); - } else if (!strcmp((*dirStruct)[e].directories, ".")){ - // strip single . - strcpy((*dirStruct)[e].directories, "\0"); - } else { - // If element created is NOT .. - e++; - (*dirStruct) = realloc((*dirStruct), sizeof(pathDirs) * (2 + e)); - } - } else { - e++; - (*dirStruct) = realloc((*dirStruct), sizeof(pathDirs) * (2 + e)); - } - j=0; - } else { - (*dirStruct)[e].directories[j] = path[i]; - j++; - } - } - (*dirStruct)[e].directories[j] = '\0'; - if (!strcmp((*dirStruct)[e].directories, ".")){ - strcpy((*dirStruct)[e].directories, ""); - e--; - } - - return(e); -} - char *getRelativePath(char *file, char *target) { char *result = malloc(sizeof(char) + 1); @@ -286,27 +236,6 @@ char *getRelativePath(char *file, char *target) return(result); } -void createParentDirs(char *path){ - pathDirs *targetPath; - char *tempPath = malloc(sizeof(char) + 1); - int e, i = 0; - - e = splitPath(&targetPath, path); - - strcpy(tempPath, ""); - for (i = 0; i < e; i++){ - tempPath = realloc(tempPath, sizeof(char) * (strlen(tempPath) + strlen(targetPath[i].directories) + 2)); - sprintf(tempPath, "%s/%s", tempPath, targetPath[i].directories); - if (!check_dir(tempPath)){ - mk_dir(tempPath); - } - } - - free(targetPath); - free(tempPath); - return; -} - int wildcard(const char *value, char *wcard) { int vsize = (int)strlen(value); diff --git a/src/showfunctions.h b/src/showfunctions.h index c7d32f9..fbd21fc 100644 --- a/src/showfunctions.h +++ b/src/showfunctions.h @@ -28,13 +28,7 @@ typedef struct { int selected; } history; -typedef struct { - char directories[256]; -} pathDirs; - int checkRunningEnv(); -int splitPath(pathDirs **dirStruct, char *path); -void createParentDirs(char *path); char *getRelativePath(char *file, char *target); int wildcard(const char *value, char *wcard); int findResultByName(results *ob, char *name); diff --git a/src/showmenus.c b/src/showmenus.c index 9425723..e22dc8d 100644 --- a/src/showmenus.c +++ b/src/showmenus.c @@ -273,36 +273,6 @@ int sanitizeTopFileRef(int topfileref) return topfileref; } -int createParentsInput(char *path) -{ - int result = 0; - int messageLen; - wchar_t *message = malloc(sizeof(wchar_t) * 1); - - messageLen = (strlen(path) + 64); - - message = realloc(message, sizeof(wchar_t) * (messageLen + 1)); - - swprintf(message, messageLen, L"Directory [<%s>] does not exist. Create it? !Yes/!No (enter = no)", path); - wPrintMenu(0,0, message); - while(1) - { - *pc = getch10th(); - if (*pc == 'y'){ - result = 1; - break; - } else if ((*pc == 'n') || (*pc == 10)){ - result = 0; - break; - } else if (*pc == 27){ - result = -1; - break; - } - } - free(message); - return(result); -} - void refreshDirectory(char *sortmode, int origtopfileref, int origselected, int destructive) { char currentselectname[512]; @@ -445,7 +415,7 @@ void copy_file_input(char *file) free(rewrite); } copyFile: - if ( check_dir(dirFromPath(newfile))){ + if (access(dirFromPath(newfile), W_OK) == 0){ if ( check_file(newfile) ) { if ( replace_file_confirm_input(newfile) ) @@ -458,12 +428,18 @@ void copy_file_input(char *file) refreshDirectory(sortmode, 0, selected, 0); } } else { - e = createParentsInput(dirFromPath(newfile)); - if (e == 1){ - createParentDirs(newfile); - goto copyFile; + if (errno == ENOENT){ + e = createParentsInput(dirFromPath(newfile)); + if (e == 1){ + createParentDirs(newfile); + goto copyFile; + } else { + sprintf(errmessage, "Error: %s", strerror(errno)); + topLineMessage(errmessage); + } } else { - topLineMessage("Error: Directory Not Found."); + sprintf(errmessage, "Error: %s", strerror(errno)); + topLineMessage(errmessage); } } } @@ -626,7 +602,7 @@ void rename_file_input(char *file) free(rewrite); } renameFile: - if ( check_dir(dirFromPath(dest))){ + if (access(dirFromPath(dest), W_OK) == 0){ if ( check_file(dest) ) { if ( replace_file_confirm_input(dest) ) @@ -641,14 +617,21 @@ void rename_file_input(char *file) refreshDirectory(sortmode, 0, selected, 2); } } else { - e = createParentsInput(dirFromPath(dest)); - if (e == 1){ - createParentDirs(dest); - goto renameFile; + if (errno == ENOENT){ + e = createParentsInput(dirFromPath(dest)); + if (e == 1){ + createParentDirs(dest); + goto renameFile; + } else { + sprintf(errmessage, "Error: %s", strerror(errno)); + topLineMessage(errmessage); + } } else { - topLineMessage("Error: Directory Not Found."); + sprintf(errmessage, "Error: %s", strerror(errno)); + topLineMessage(errmessage); } } + refreshDirectory(sortmode, 0, selected, 0); } directory_view_menu_inputs(); } @@ -675,18 +658,20 @@ void make_directory_input() makeDir: if (access(dirFromPath(newdir), W_OK) == 0){ mk_dir(newdir); - } else if (access(dirFromPath(newdir), W_OK) == -1) { - e = createParentsInput(dirFromPath(newdir)); - if (e == 1){ - createParentDirs(newdir); - goto makeDir; + } else { + if (errno == ENOENT){ + e = createParentsInput(dirFromPath(newdir)); + if (e == 1){ + createParentDirs(newdir); + goto makeDir; + } else { + sprintf(errmessage, "Error: %s", strerror(errno)); + topLineMessage(errmessage); + } } else { sprintf(errmessage, "Error: %s", strerror(errno)); topLineMessage(errmessage); } - } else { - sprintf(errmessage, "Error: %s", strerror(errno)); - topLineMessage(errmessage); } // curs_set(FALSE); refreshDirectory(sortmode, 0, selected, 0); @@ -771,6 +756,7 @@ void touch_file_input() char touchFile[1024]; FILE* touchFileObject; int setDateFlag = -1; + int e; move(0,0); clrtoeol(); strcpy(menuTitle, "Touch File - Enter pathname:"); @@ -798,6 +784,7 @@ void touch_file_input() } } // Do something + touchFile: if (access(dirFromPath(touchFile), W_OK) == 0) { if (check_object(touchFile) == 0){ touchFileObject = fopen(touchFile, "w"); @@ -816,8 +803,19 @@ void touch_file_input() } } } else { - sprintf(errmessage, "Error: %s", strerror(errno)); - topLineMessage(errmessage); + if (errno == ENOENT){ + e = createParentsInput(dirFromPath(touchFile)); + if (e == 1){ + createParentDirs(touchFile); + goto touchFile; + } else { + sprintf(errmessage, "Error: %s", strerror(errno)); + topLineMessage(errmessage); + } + } else { + sprintf(errmessage, "Error: %s", strerror(errno)); + topLineMessage(errmessage); + } } refreshDirectory(sortmode, 0, selected, 0); } @@ -1252,8 +1250,8 @@ void linktext_input(char *file, int symbolic) free(rewrite); } - relSymlink: - if (check_dir(dirFromPath(target))){ + makeSymlink: + if (access(dirFromPath(target), W_OK) == 0){ if (check_file(target)){ topLineMessage("Error: File exists."); } else { @@ -1273,12 +1271,18 @@ void linktext_input(char *file, int symbolic) refreshDirectory(sortmode, 0, selected, 0); } } else { - e = createParentsInput(dirFromPath(target)); - if (e == 1){ - createParentDirs(target); - goto relSymlink; + if (errno == ENOENT){ + e = createParentsInput(dirFromPath(target)); + if (e == 1){ + createParentDirs(target); + goto makeSymlink; + } else { + sprintf(errmessage, "Error: %s", strerror(errno)); + topLineMessage(errmessage); + } } else { - topLineMessage("Error: Directory Not Found."); + sprintf(errmessage, "Error: %s", strerror(errno)); + topLineMessage(errmessage); } } } diff --git a/src/showmenus.h b/src/showmenus.h index 9f64842..e6de197 100644 --- a/src/showmenus.h +++ b/src/showmenus.h @@ -4,7 +4,6 @@ void unloadMenuLabels(); int touchType(); time_t touchTimeInput(int type); int symLinkLocation(); -int createParentsInput(char *path); void linktext_input(int selected, int symbolic); void link_key_menu_inputs(); void modify_key_menu_inputs(); From 086847612407d081cad80b1995fab389b3ca9b39 Mon Sep 17 00:00:00 2001 From: Robert Hawdon Date: Mon, 25 Feb 2019 22:31:15 +0000 Subject: [PATCH 07/10] Ability to delete single, empty, directories --- src/showmenus.c | 26 ++++++++++++++++++++++++++ src/showmenus.h | 1 + 2 files changed, 27 insertions(+) diff --git a/src/showmenus.c b/src/showmenus.c index e22dc8d..7956740 100644 --- a/src/showmenus.c +++ b/src/showmenus.c @@ -939,6 +939,30 @@ void delete_file_confirm_input(char *file) } } +void delete_directory_confirm_input(char *directory) +{ + int e; + printMenu(0,0, "Delete directory? (!Yes/!No)"); + while(1) + { + *pc = getch10th(); + switch(*pc) + { + case 'y': + e = rmdir(directory); + if (e != 0){ + sprintf(errmessage, "Error: %s", strerror(errno)); + topLineMessage(errmessage); + } + refreshDirectory(sortmode, topfileref, selected, 1); + // Not breaking here, intentionally dropping through to the default + default: + directory_view_menu_inputs(); + break; + } + } +} + void delete_multi_file_confirm_input(results* ob) { int i, k; @@ -1378,6 +1402,8 @@ void directory_view_menu_inputs() strcat(selfile, ob[selected].name); if (!check_dir(selfile) || (strcmp(ob[selected].slink, ""))){ delete_file_confirm_input(selfile); + } else if (check_dir(selfile)){ + delete_directory_confirm_input(selfile); } } } else if (*pc == menuHotkeyLookup(fileMenu, "f_edit", fileMenuSize)){ diff --git a/src/showmenus.h b/src/showmenus.h index e6de197..66c3944 100644 --- a/src/showmenus.h +++ b/src/showmenus.h @@ -21,6 +21,7 @@ void edit_file_input(); void make_directory_input(); void touch_file_input(); void delete_file_confirm_input(char *file); +void delete_directory_confirm_input(char *directory); void delete_multi_file_confirm_input(results* ob); void copy_multi_file_input(results* ob, char *input); void copy_multi_file(results* ob, char *dest); From 5884215988195a732dcc38ccb5d122f57a3b92c2 Mon Sep 17 00:00:00 2001 From: Robert Hawdon Date: Sat, 2 Mar 2019 17:43:14 +0000 Subject: [PATCH 08/10] Skip ability to go back to deleted directories --- src/showmenus.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/showmenus.c b/src/showmenus.c index 7956740..26fe012 100644 --- a/src/showmenus.c +++ b/src/showmenus.c @@ -1439,6 +1439,7 @@ void directory_view_menu_inputs() //printMenu(0, 0, modifyMenuText); modify_key_menu_inputs(); } else if (*pc == menuHotkeyLookup(fileMenu, "f_quit", fileMenuSize)){ + handleMissingDir: if (historyref > 1){ strcpy(chpwd, hs[historyref - 2].path); objectWild = hs[historyref - 2].objectWild; @@ -1453,6 +1454,10 @@ void directory_view_menu_inputs() topfileref = sanitizeTopFileRef(hs[historyref].topfileref); clear_workspace(); display_dir(currentpwd, ob, topfileref, selected); + } else { + // Skip removed directories + historyref--; + goto handleMissingDir; } } else { historyref = 0; // Reset historyref here. A hacky workaround due to the value occasionally dipping to minus numbers. From 923423a6d66bcd3c925cd3d0201a45a95d068816 Mon Sep 17 00:00:00 2001 From: Robert Hawdon Date: Sat, 2 Mar 2019 18:10:25 +0000 Subject: [PATCH 09/10] Fixing seg fault when deleting a directory from "Run command" * Now `show` will return the user to the last valid directory in the history. * Addresses bug #75 --- src/showmenus.c | 57 ++++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/src/showmenus.c b/src/showmenus.c index 26fe012..a6f3846 100644 --- a/src/showmenus.c +++ b/src/showmenus.c @@ -277,36 +277,45 @@ void refreshDirectory(char *sortmode, int origtopfileref, int origselected, int { char currentselectname[512]; int i; - if (invalidstart) { - strcpy(currentselectname, ""); - exitCode = 0; - invalidstart = 0; - } else { - if (destructive == 2){ - strcpy(currentselectname, currentfilename); + handleMissingDir: + if (check_dir(currentpwd)){ + if (invalidstart) { + strcpy(currentselectname, ""); + exitCode = 0; + invalidstart = 0; } else { - strcpy(currentselectname, ob[origselected].name); + if (destructive == 2){ + strcpy(currentselectname, currentfilename); + } else { + strcpy(currentselectname, ob[origselected].name); + } } - } - if (destructive != -1){ - free(ob); - ob = get_dir(currentpwd); - clear_workspace(); - reorder_ob(ob, sortmode); - } - if (destructive > 0){ - i = findResultByName(ob, currentselectname); - if (i != 0){ - selected = i; - } else { - if (selected > totalfilecount - 1){ - selected = totalfilecount - 1; + if (destructive != -1){ + free(ob); + ob = get_dir(currentpwd); + clear_workspace(); + reorder_ob(ob, sortmode); + } + if (destructive > 0){ + i = findResultByName(ob, currentselectname); + if (i != 0){ + selected = i; } else { - selected = origselected; + if (selected > totalfilecount - 1){ + selected = totalfilecount - 1; + } else { + selected = origselected; + } } + } else { + selected = findResultByName(ob, currentselectname); } } else { - selected = findResultByName(ob, currentselectname); + strcpy(currentpwd, hs[historyref - 2].path); + objectWild = hs[historyref - 2].objectWild; + historyref--; + chdir(currentpwd); + goto handleMissingDir; } topfileref = sanitizeTopFileRef(origtopfileref); display_dir(currentpwd, ob, topfileref, selected); From b015e6ec8219a3806b11af9b74636963951863ae Mon Sep 17 00:00:00 2001 From: Robert Hawdon Date: Sat, 2 Mar 2019 18:16:07 +0000 Subject: [PATCH 10/10] Updated version to 0.6.2 with updated documentation --- README.rst | 3 ++- configure.ac | 2 +- docs/source/conf.py | 2 +- docs/source/installation.rst | 3 ++- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 54ebd46..4bbccc6 100644 --- a/README.rst +++ b/README.rst @@ -123,7 +123,8 @@ To install DF-SHOW via Homebrew, run the following in your terminal: .. code-block:: bash - brew install roberthawdon/homebrew-dfshow/dfshow + brew tap roberthawdon/dfshow + brew install dfshow Building from Git ----------------- diff --git a/configure.ac b/configure.ac index dcf8ead..5a18c3e 100644 --- a/configure.ac +++ b/configure.ac @@ -1,4 +1,4 @@ -AC_INIT([dfshow], [0.6.2-dev], [https://github.com/roberthawdon/dfshow/issues]) +AC_INIT([dfshow], [0.6.2], [https://github.com/roberthawdon/dfshow/issues]) AC_GNU_SOURCE AM_INIT_AUTOMAKE([subdir-objects]) AC_PROG_CC diff --git a/docs/source/conf.py b/docs/source/conf.py index e3284ec..3ae102d 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -26,7 +26,7 @@ # The short X.Y version version = '' # The full version, including alpha/beta/rc tags -release = '0.6.1-alpha' +release = '0.6.2-alpha' # -- General configuration --------------------------------------------------- diff --git a/docs/source/installation.rst b/docs/source/installation.rst index b80143a..19204ff 100644 --- a/docs/source/installation.rst +++ b/docs/source/installation.rst @@ -88,7 +88,8 @@ To install DF-SHOW via Homebrew, run the following in your terminal: .. code-block:: bash - brew install roberthawdon/homebrew-dfshow/dfshow + brew tap roberthawdon/dfshow + brew install dfshow Building from Source --------------------