From 1af9cbf7bb9e89fdc9de704c0b68209762ab8c80 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Sat, 12 Jun 2021 19:25:17 +0000 Subject: rename main.c to lpass.c --- Makefile | 3 +- src/lpass.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 91 ------------------------------------------------------------- 3 files changed, 93 insertions(+), 92 deletions(-) create mode 100644 src/lpass.c delete mode 100644 src/main.c diff --git a/Makefile b/Makefile index 7c7c2ed..bc9348b 100755 --- a/Makefile +++ b/Makefile @@ -3,7 +3,8 @@ CC = gcc CFLAGS = -Wall -g #-DDEBUG LIBS = $(shell gpgme-config --cflags --libs) MAN_PATH = /usr/share/man/man1 -SOURCES = src/main.c \ +SOURCES = \ + src/lpass.c \ src/exec-cmd.c \ src/routines.c \ src/easydir.c \ diff --git a/src/lpass.c b/src/lpass.c new file mode 100644 index 0000000..22e9eb8 --- /dev/null +++ b/src/lpass.c @@ -0,0 +1,91 @@ +/*** + This file is part of LockPassword + Copyright (C) 2020-2021 Aleksandr D. Goncharov (Joursoir) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +***/ + +#include +#include +#include +#include +#include +#include + +#include "constants.h" +#include "exec-cmd.h" +#include "xstd.h" + +struct cmd_struct { + const char *cmd; + int (*func)(int, char **); +}; + +static struct cmd_struct commands[] = { + { "init", cmd_init }, + { "insert", cmd_insert }, + { "edit", cmd_edit }, + { "generate", cmd_generate }, + { "rm", cmd_remove }, + { "mv", cmd_move }, + { "help", cmd_help }, + { "version", cmd_version }, + { NULL, NULL } +}; + +static struct cmd_struct *get_cmd(const char *name) +{ + struct cmd_struct *ptr; + for(ptr = commands; ptr->cmd; ptr++) { + if(strcmp(name, ptr->cmd) == 0) + return ptr; + } + return NULL; +} + +static int goto_maindir() +{ + int retval = 0, result; + char *rootdir = xstrcat(getenv("HOME"), LOCKPASS_DIR, "/"); + if(chdir(rootdir)) // failed + { + // create main directory: + result = mkdir(rootdir, S_IRWXU); + if(result) { + if(errno != EEXIST) + errprint_ptr(&retval, 1, "%s", strerror(errno)); + } + else // try again: + retval = chdir(rootdir); + } + + free(rootdir); + return retval; +} + +int main(int argc, char *argv[]) +{ + if(!isatty(STDIN_FILENO)) + errprint_r(1, "Please, use a terminal to run this application\n"); + + if(goto_maindir()) + errprint_r(1, "%s", strerror(errno)); + + char *cmd = (argv[1] != NULL) ? argv[1] : ""; + struct cmd_struct *ptr; + if((ptr = get_cmd(cmd))) + return ptr->func(argc, argv); + + return cmd_showtree(argc, argv); +} \ No newline at end of file diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 22e9eb8..0000000 --- a/src/main.c +++ /dev/null @@ -1,91 +0,0 @@ -/*** - This file is part of LockPassword - Copyright (C) 2020-2021 Aleksandr D. Goncharov (Joursoir) - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -***/ - -#include -#include -#include -#include -#include -#include - -#include "constants.h" -#include "exec-cmd.h" -#include "xstd.h" - -struct cmd_struct { - const char *cmd; - int (*func)(int, char **); -}; - -static struct cmd_struct commands[] = { - { "init", cmd_init }, - { "insert", cmd_insert }, - { "edit", cmd_edit }, - { "generate", cmd_generate }, - { "rm", cmd_remove }, - { "mv", cmd_move }, - { "help", cmd_help }, - { "version", cmd_version }, - { NULL, NULL } -}; - -static struct cmd_struct *get_cmd(const char *name) -{ - struct cmd_struct *ptr; - for(ptr = commands; ptr->cmd; ptr++) { - if(strcmp(name, ptr->cmd) == 0) - return ptr; - } - return NULL; -} - -static int goto_maindir() -{ - int retval = 0, result; - char *rootdir = xstrcat(getenv("HOME"), LOCKPASS_DIR, "/"); - if(chdir(rootdir)) // failed - { - // create main directory: - result = mkdir(rootdir, S_IRWXU); - if(result) { - if(errno != EEXIST) - errprint_ptr(&retval, 1, "%s", strerror(errno)); - } - else // try again: - retval = chdir(rootdir); - } - - free(rootdir); - return retval; -} - -int main(int argc, char *argv[]) -{ - if(!isatty(STDIN_FILENO)) - errprint_r(1, "Please, use a terminal to run this application\n"); - - if(goto_maindir()) - errprint_r(1, "%s", strerror(errno)); - - char *cmd = (argv[1] != NULL) ? argv[1] : ""; - struct cmd_struct *ptr; - if((ptr = get_cmd(cmd))) - return ptr->func(argc, argv); - - return cmd_showtree(argc, argv); -} \ No newline at end of file -- cgit v1.2.3-18-g5258