aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/easydir.c2
-rw-r--r--src/handerror.h8
-rw-r--r--src/implementation.c2
-rw-r--r--src/main.c30
-rw-r--r--src/tree.c15
-rw-r--r--src/xstd.c (renamed from src/handerror.c)15
-rw-r--r--src/xstd.h10
7 files changed, 43 insertions, 39 deletions
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 <errno.h>
#include "easydir.h"
-#include "handerror.h"
+#include "xstd.h"
int checkFileExist(char *source)
{
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 <errno.h>
#include <sys/stat.h>
-#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 <sys/stat.h>
#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 <sys/stat.h>
#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/handerror.c b/src/xstd.c
index 218619d..5669e8f 100644
--- a/src/handerror.c
+++ b/src/xstd.c
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
+#include <string.h>
#include <sys/wait.h>
void callError(int num)
@@ -28,3 +29,17 @@ void easyFork(char *name, char *arguments[])
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