From 5a8218b13fbe21e4c85a484933ca201b666323ee Mon Sep 17 00:00:00 2001 From: Joursoir Date: Wed, 21 Oct 2020 19:26:03 +0000 Subject: fix bug: work outside working dir; refactor --- easydir.c | 100 +++++--------------------------------------------------------- 1 file changed, 8 insertions(+), 92 deletions(-) (limited to 'easydir.c') diff --git a/easydir.c b/easydir.c index 921549e..08cac0f 100644 --- a/easydir.c +++ b/easydir.c @@ -7,106 +7,22 @@ #include "handerror.h" -/* Buff size: source == path = file -splitPath thinks, that in end path always stay FILE, not directory */ -char* splitPath(char *source, char *path, char *file) -{ - int fSymbol = 0, f = 0; - - char *main_path = malloc( sizeof(char) * (strlen(source)+1) ); - char *file_path = malloc( sizeof(char) * (strlen(source)+1) ); - - for(int i=0; i < strlen(source); i++) - { - if(fSymbol == 1) - { - switch(source[i]) - { - case '/': - { - fSymbol = 0; - f = 0; - - strcat(main_path, file_path); - strcat(main_path, "/"); - file_path[0] = '\0'; - break; - } - default: - { - file_path[f] = source[i]; - file_path[f+1] = '\0'; - f++; - - break; - } - - } - } - else // if it's beginning of string - { - // handling first symbol - switch(source[i]) - { - case '.': - case '\\': - case '/': - { - printError("lpass: You can't use these symbol at the beginning: '.', '/', '\\' \n"); - break; - } - default: - fSymbol = 1; - - // enter first symbol - file_path[0] = source[i]; - file_path[1] = '\0'; - f++; - - break; - } - } - } - - strcpy(path, main_path); - strcpy(file, file_path); - free(main_path); - free(file_path); - - if(*file) return file; - return NULL; -} - int deleteFile(char *file_path) { - int pid; - pid = fork(); - if(pid == -1) callError(112); - if(pid == 0) { /* new process */ - execlp("rm", "rm", file_path, NULL); - perror("rm"); - exit(4); - } - wait(&pid); + char *arguments[] = {"rm", file_path, NULL}; + easyFork("rm", arguments); return 1; } int deleteEmptyDir(char *dir_path) { - int pid; - pid = fork(); - if(pid == -1) callError(113); - if(pid == 0) { /* new process */ - #if defined(DEBUG) - execlp("rmdir", "rmdir", "-p", dir_path, NULL); - #else - execlp("rmdir", "rmdir", "-p", "--ignore-fail-on-non-empty", dir_path, NULL); - #endif - perror("rmdir"); - exit(4); - } - wait(&pid); + #if defined(DEBUG) + char *arguments[] = {"rmdir", "-p", dir_path, NULL}; + #else + char *arguments[] = {"rmdir", "-p", "--ignore-fail-on-non-empty", dir_path, NULL}; + #endif + easyFork("rmdir", arguments); return 1; } -- cgit v1.2.3-18-g5258