From a8fe7bce0ea33f71485cf8c2e4c8330831c849ce Mon Sep 17 00:00:00 2001 From: Joursoir Date: Fri, 26 Feb 2021 13:36:16 +0000 Subject: rename handerror to xstd; create and use some routines --- src/easydir.c | 2 +- src/handerror.c | 30 ------------------------------ src/handerror.h | 8 -------- src/implementation.c | 2 +- src/main.c | 30 +++++++++++++++--------------- src/tree.c | 15 +-------------- src/xstd.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/xstd.h | 10 ++++++++++ 8 files changed, 73 insertions(+), 69 deletions(-) delete mode 100644 src/handerror.c delete mode 100644 src/handerror.h create mode 100644 src/xstd.c create mode 100644 src/xstd.h diff --git a/src/easydir.c b/src/easydir.c index 6de77bc..e1a4394 100644 --- a/src/easydir.c +++ b/src/easydir.c @@ -6,7 +6,7 @@ #include #include "easydir.h" -#include "handerror.h" +#include "xstd.h" int checkFileExist(char *source) { diff --git a/src/handerror.c b/src/handerror.c deleted file mode 100644 index 218619d..0000000 --- a/src/handerror.c +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -#include -#include - -void callError(int num) -{ - fprintf(stderr, "lpass: Sorry, there was an error in the program [#%d]\n", num); - exit(3); -} - -void printError(const char *text) -{ - fprintf(stderr, "%s", text); - exit(4); -} - -void easyFork(char *name, char *arguments[]) -{ - int pid; - pid = fork(); - if(pid == -1) callError(100); - if(pid == 0) { /* new process */ - execvp(name, arguments); - perror(name); - exit(4); - } - wait(&pid); -} - diff --git a/src/handerror.h b/src/handerror.h deleted file mode 100644 index 09c5106..0000000 --- a/src/handerror.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef HANDERROR_H -#define HANDERROR_H - -void easyFork(char *name, char *arguments[]); -void callError(int num); -void printError(const char *text); - -#endif \ No newline at end of file diff --git a/src/implementation.c b/src/implementation.c index 334b7e1..d8b0973 100644 --- a/src/implementation.c +++ b/src/implementation.c @@ -8,7 +8,7 @@ #include #include -#include "handerror.h" +#include "xstd.h" #include "easydir.h" #include "implementation.h" diff --git a/src/main.c b/src/main.c index 4ac64a2..b1e1ff0 100644 --- a/src/main.c +++ b/src/main.c @@ -17,7 +17,7 @@ #include #include "easydir.h" -#include "handerror.h" +#include "xstd.h" #include "implementation.h" #include "exec-cmd.h" #include "tree.h" @@ -531,25 +531,25 @@ static struct cmd_struct *get_cmd(const char *name) return NULL; } -static int gotoMainDir() +static int goto_maindir() { - char *env_home = getenv("HOME"); - int len_rootdir = strlen(env_home) + 1 + strlen(LOCKPASS_DIR) + 1; - char *rootdir = malloc(sizeof(char) * len_rootdir); - sprintf(rootdir, "%s/%s", env_home, LOCKPASS_DIR); - + int ret = 0; + char *rootdir = xstrcat(getenv("HOME"), LOCKPASS_DIR, "/"); if(chdir(rootdir)) // failed { // create main directory: - if(mkdir(rootdir, S_IRWXU)) { - if(errno != EEXIST) - errprint("mkdir() failed\n"); + int res = mkdir(rootdir, S_IRWXU); + if(res) { + if(errno != EEXIST) { + perror("mkdir"); + ret = 1; + } } - - // try again: - return chdir(rootdir); + else // try again: + ret = chdir(rootdir); } + free(rootdir); return 0; } @@ -558,8 +558,8 @@ int main(int argc, char *argv[]) if(!isatty(STDIN_FILENO)) errprint("Please, use a terminal to run this application\n"); - if(gotoMainDir()) - errprint("chdir() failed\n"); + if(goto_maindir()) + perror("chdir"); int ret = 0; char *cmd = (argv[1] != NULL) ? argv[1] : ""; diff --git a/src/tree.c b/src/tree.c index 658b6cf..387ef8b 100644 --- a/src/tree.c +++ b/src/tree.c @@ -6,6 +6,7 @@ #include #include "tree.h" +#include "xstd.h" #define ANSIC_RST "\x1B[0m" #define ANSIC_BBLU "\x1B[34;1m" @@ -61,20 +62,6 @@ static int count_dir_entries(const char *path) return counter; } -static char *xstrcat(const char *first, const char *second, - const char *delimiter) -{ - size_t size = sizeof(char) * (strlen(first) + strlen(second) + 1); - if(delimiter) - size += sizeof(char) * strlen(delimiter); - char *res = malloc(size); - strcpy(res, first); - if(delimiter) - strcat(res, delimiter); - strcat(res, second); - return res; -} - int tree(const char *path, const char *prefix) { DIR *main_dir; diff --git a/src/xstd.c b/src/xstd.c new file mode 100644 index 0000000..5669e8f --- /dev/null +++ b/src/xstd.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include + +void callError(int num) +{ + fprintf(stderr, "lpass: Sorry, there was an error in the program [#%d]\n", num); + exit(3); +} + +void printError(const char *text) +{ + fprintf(stderr, "%s", text); + exit(4); +} + +void easyFork(char *name, char *arguments[]) +{ + int pid; + pid = fork(); + if(pid == -1) callError(100); + if(pid == 0) { /* new process */ + execvp(name, arguments); + perror(name); + exit(4); + } + wait(&pid); +} + +char *xstrcat(const char *first, const char *second, + const char *delimiter) +{ + size_t size = sizeof(char) * (strlen(first) + strlen(second) + 1); + if(delimiter) + size += sizeof(char) * strlen(delimiter); + char *res = malloc(size); + strcpy(res, first); + if(delimiter) + strcat(res, delimiter); + strcat(res, second); + return res; +} + diff --git a/src/xstd.h b/src/xstd.h new file mode 100644 index 0000000..5432390 --- /dev/null +++ b/src/xstd.h @@ -0,0 +1,10 @@ +#ifndef LPASS_XSTD_H +#define LPASS_XSTD_H + +void easyFork(char *name, char *arguments[]); +void callError(int num); +void printError(const char *text); +char *xstrcat(const char *first, const char *second, + const char *delimiter); + +#endif \ No newline at end of file -- cgit v1.2.3-18-g5258