aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/easydir.c26
-rw-r--r--src/easydir.h5
-rw-r--r--src/implementation.c15
-rw-r--r--src/implementation.h2
-rw-r--r--src/main.c75
-rw-r--r--src/tree.c41
6 files changed, 65 insertions, 99 deletions
diff --git a/src/easydir.c b/src/easydir.c
index e1a4394..18f6ec8 100644
--- a/src/easydir.c
+++ b/src/easydir.c
@@ -4,17 +4,37 @@
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>
+#include <dirent.h>
#include "easydir.h"
#include "xstd.h"
-int checkFileExist(char *source)
+int file_exist(const char *path)
{
- FILE *file = fopen(source, "r+"); // r+ so that errno can equal EISDIR
+ FILE *file = fopen(path, "r+"); // r+ so that errno can equal EISDIR
if(!file)
return errno == EISDIR ? F_ISDIR : F_NOEXIST;
fclose(file);
- return F_NOEXIST;
+ return F_ISFILE;
+}
+
+int count_dir_entries(const char *path)
+{
+ int counter = 0;
+ DIR *dir;
+ struct dirent *dir_entry;
+
+ dir = opendir(path);
+ if(dir == NULL)
+ return 0;
+
+ while((dir_entry = readdir(dir))) {
+ if(dir_entry->d_name[0] == '.')
+ continue;
+ counter++;
+ }
+ closedir(dir);
+ return counter;
}
char *fileCropLineFeed(char *path, char *text, int maxlen)
diff --git a/src/easydir.h b/src/easydir.h
index 0ba5677..06a2109 100644
--- a/src/easydir.h
+++ b/src/easydir.h
@@ -2,12 +2,13 @@
#define EASYDIR_H
enum status_file {
- F_SUCCESS,
+ F_ISFILE,
F_NOEXIST,
F_ISDIR
};
-int checkFileExist(char *path_to_file);
+int file_exist(const char *path);
+int count_dir_entries(const char *path);
char *fileCropLineFeed(char *path, char *text, int maxlen);
#endif \ No newline at end of file
diff --git a/src/implementation.c b/src/implementation.c
index d8b0973..e499f0a 100644
--- a/src/implementation.c
+++ b/src/implementation.c
@@ -53,18 +53,13 @@ static void copyText(char *password)
}
/* check two dot in path */
-int checkForbiddenPaths(char *path)
+int check_sneaky_paths(const char *path)
{
- int i, length;
- int firstdot = 0;
- for(i = 0, length = strlen(path); i < length; i++)
+ int length = strlen(path), i;
+ for(i = 1; i < length; i++)
{
- if(path[i] == '.') {
- if(firstdot)
- return 1;
- firstdot++;
- }
- else firstdot = 0;
+ if(path[i-1] == '.' && path[i] == '.')
+ return 1;
}
return 0;
}
diff --git a/src/implementation.h b/src/implementation.h
index bcb6985..15f21ff 100644
--- a/src/implementation.h
+++ b/src/implementation.h
@@ -8,7 +8,7 @@ enum asnwers {
OW_NO = 1,
};
-int checkForbiddenPaths(char *path);
+int check_sneaky_paths(const char *path);
char *getGPGKey();
char* getPassword(char *path_pass, char *password, size_t size, int flag_copy);
void nonvisibleEnter(int status);
diff --git a/src/main.c b/src/main.c
index b1e1ff0..2a923d5 100644
--- a/src/main.c
+++ b/src/main.c
@@ -137,11 +137,11 @@ int cmd_insert(int argc, char *argv[])
if(path == NULL)
usageprint("%s", description);
- result = checkForbiddenPaths(path);
+ result = check_sneaky_paths(path);
if(result)
errprint("You have used forbidden paths\n");
- if(checkFileExist(path) == F_SUCCESS) {
+ if(file_exist(path) == F_ISFILE) {
if(!flag_force) {
if(getOverwriteAnswer(path) != OW_YES)
return 1;
@@ -224,13 +224,13 @@ int cmd_edit(int argc, char *argv[])
usageprint("%s", description);
dbgprint("passname: %s\n", argv[optind]);
- result = checkForbiddenPaths(path_to_password);
+ result = check_sneaky_paths(path_to_password);
if(result)
errprint("You have used forbidden paths\n");
globalSplitPath(path_to_password);
- result = checkFileExist(gPath_pass);
- if(result != F_SUCCESS) {
+ result = file_exist(gPath_pass);
+ if(result != F_ISFILE) {
if(result == F_ISDIR) errprint("It is a directory\n");
errprint("No such file exists\n");
}
@@ -314,12 +314,12 @@ int cmd_generate(int argc, char *argv[])
if(pass_length < minlen_pass || pass_length > maxlen_pass)
errprint("You typed an incorrect number\n");
- result = checkForbiddenPaths(path_to_password);
+ result = check_sneaky_paths(path_to_password);
if(result)
errprint("You have used forbidden paths\n");
globalSplitPath(path_to_password);
- if(checkFileExist(gPath_pass) == F_SUCCESS) {
+ if(file_exist(gPath_pass) == F_ISFILE) {
if(!flag_force) {
if(getOverwriteAnswer(path_to_password) != OW_YES)
return 1;
@@ -347,19 +347,19 @@ int cmd_remove(int argc, char *argv[])
if(!path)
usageprint("%s", description);
- result = checkForbiddenPaths(path);
+ result = check_sneaky_paths(path);
if(result)
errprint("You have used forbidden paths\n");
- globalSplitPath(path);
- result = checkFileExist(gPath_pass);
- if(result != F_SUCCESS) {
- if(result == F_ISDIR) errprint("It is a directory\n");
+ result = file_exist(path);
+ if(result == F_NOEXIST)
errprint("No such file exists\n");
+ if(result == F_ISDIR) {
+ if(count_dir_entries(path) != 0)
+ errprint("Directory not empty\n");
}
- if(unlink(gPath_pass) == 0)
- rmdir(gPath_subdir);
+ remove(path);
return 0;
}
@@ -386,46 +386,32 @@ int cmd_move(int argc, char *argv[])
if(optind < argc) optind++; // for skip "move"
if(!argv[optind] || !argv[optind+1])
usageprint("%s", description);
- dbgprint("old-path: %s\n", argv[optind]);
- dbgprint("new-path: %s\n", argv[optind+1]);
char *old_path = argv[optind];
- result = checkForbiddenPaths(old_path);
+ char *new_path = argv[optind+1];
+ dbgprint("old-path = %s\n", old_path);
+ dbgprint("new-path = %s\n", new_path);
+
+ result = check_sneaky_paths(old_path);
if(result)
errprint("You have used forbidden paths\n");
- globalSplitPath(old_path);
- result = checkFileExist(gPath_pass);
- if(result != F_SUCCESS) {
- if(result == F_ISDIR) errprint("It is a directory\n");
+ result = file_exist(old_path);
+ if(result == F_NOEXIST)
errprint("No such file exists\n");
- }
-
- char *old_path_gpg = gPath_pass;
- char *old_path_subdir = gPath_subdir;
- char *new_path = argv[optind+1];
- result = checkForbiddenPaths(new_path);
+ result = check_sneaky_paths(new_path);
if(result)
errprint("You have used forbidden paths\n");
- globalSplitPath(new_path);
-
- if(checkFileExist(new_path) == F_ISDIR)
- ;
- else if(checkFileExist(gPath_pass) == F_SUCCESS) {
+ result = file_exist(new_path);
+ if(result != F_NOEXIST) {
if(!flag_force) {
if(getOverwriteAnswer(new_path) != OW_YES)
return 1;
}
- new_path = gPath_pass;
}
- else errprint("No such new-path exists\n");
-
- char *arguments[] = {"mv", "-f", old_path_gpg, new_path, NULL};
- easyFork("mv", arguments);
- rmdir(old_path_subdir);
- free(old_path_subdir);
- free(old_path_gpg);
+ if(rename(old_path, new_path))
+ perror("rename");
return 0;
}
@@ -484,7 +470,7 @@ int cmd_showtree(int argc, char *argv[])
}
if(argv[optind]) {
- result = checkForbiddenPaths(argv[optind]);
+ result = check_sneaky_paths(argv[optind]);
if(result)
errprint("You have used forbidden paths\n");
path = malloc(sizeof(char) * (strlen(argv[optind]) + 1));
@@ -508,11 +494,12 @@ int cmd_showtree(int argc, char *argv[])
{
globalSplitPath(path);
- if(checkFileExist(gPath_pass) == F_SUCCESS)
+ if(file_exist(gPath_pass) == F_ISFILE)
{
char password[maxlen_pass];
getPassword(path, password, sizeof(char)*maxlen_pass, flag_copy);
- if(!flag_copy) printf("%s\n", password);
+ if(!flag_copy)
+ printf("%s\n", password);
}
else errprint("%s is not in the password storage\n", path);
}
@@ -550,7 +537,7 @@ static int goto_maindir()
}
free(rootdir);
- return 0;
+ return ret;
}
int main(int argc, char *argv[])
diff --git a/src/tree.c b/src/tree.c
index 387ef8b..1a746b2 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -7,6 +7,7 @@
#include "tree.h"
#include "xstd.h"
+#include "easydir.h"
#define ANSIC_RST "\x1B[0m"
#define ANSIC_BBLU "\x1B[34;1m"
@@ -28,40 +29,6 @@ static void entries_sort(char **entries, const int size)
}
}
-static int is_dir(const char *path)
-{
- struct stat buffer;
- if(stat(path, &buffer))
- return 0;
- return S_ISDIR(buffer.st_mode);
-}
-
-static int count_dir_entries(const char *path)
-{
- int counter = 0;
- DIR *dir;
- struct dirent *dir_entry;
-
- dir = opendir(path);
- if(dir == NULL) {
- fprintf(stderr, "opendir() failed\n");
- return -1;
- }
-
- errno = 0;
- while((dir_entry = readdir(dir))) {
- if(dir_entry->d_name[0] == '.')
- continue;
- counter++;
- }
- if(errno) {
- fprintf(stderr, "readdir() failed\n");
- return -1;
- }
- closedir(dir);
- return counter;
-}
-
int tree(const char *path, const char *prefix)
{
DIR *main_dir;
@@ -91,10 +58,6 @@ int tree(const char *path, const char *prefix)
i++;
}
closedir(main_dir);
- if(errno) {
- perror("opendir");
- return 1;
- }
entries_sort(entries, cnt_ent);
for(i = 0; i < cnt_ent; i++) {
@@ -109,7 +72,7 @@ int tree(const char *path, const char *prefix)
}
full_path = xstrcat(path, entries[i], "/");
- if(is_dir(full_path)) {
+ if(file_exist(full_path) == F_ISDIR) {
printf("%s%s%s%s%s\n", prefix, pointer, ANSIC_BBLU,
entries[i], ANSIC_RST);