diff options
Diffstat (limited to 'src/vars.c')
| -rw-r--r-- | src/vars.c | 195 |
1 files changed, 98 insertions, 97 deletions
@@ -1,10 +1,10 @@ +#include <errno.h> +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <stdbool.h> -#include <unistd.h> #include <sys/stat.h> -#include <errno.h> +#include <unistd.h> #include "vars.h" @@ -35,21 +35,22 @@ size_t cached_repos_count = 0; Map cached_build_systems = {0}; char home_config_buf[MAX_PATH_LEN] = {0}; -char* home_config() { - snprintf(home_config_buf, MAX_PATH_LEN, "%s/.config/pkgit/init.lua", home_dir); - return home_config_buf; +char *home_config() { + snprintf(home_config_buf, MAX_PATH_LEN, "%s/.config/pkgit/init.lua", + home_dir); + return home_config_buf; } -const char* get_install_dir(const char *key) { - for (size_t i = 0; i < cached_install_directories.size; i++) { - if (strcmp(cached_install_directories.items[i].key, key) == 0) { - return cached_install_directories.items[i].value; - } +const char *get_install_dir(const char *key) { + for (size_t i = 0; i < cached_install_directories.size; i++) { + if (strcmp(cached_install_directories.items[i].key, key) == 0) { + return cached_install_directories.items[i].value; } - return ""; + } + return ""; } -const char *version = "1.0.0"; +const char *version = "1.0.0_INDEV"; const char *red = "\e[0;31m"; const char *green = "\e[0;32m"; @@ -90,100 +91,100 @@ const char *print_warning; const char *print_error; int mkdir_p(const char *path) { - char tmp[MAX_PATH_LEN]; - char *p = NULL; - size_t len; - - snprintf(tmp, sizeof(tmp), "%s", path); - len = strlen(tmp); - if (tmp[len - 1] == '/') { - tmp[len - 1] = 0; - } - - for (p = tmp + 1; *p; p++) { - if (*p == '/') { - *p = 0; - if (mkdir(tmp, 0755) != 0 && errno != EEXIST) { - return -1; - } - *p = '/'; - } - } - - if (mkdir(tmp, 0755) != 0 && errno != EEXIST) { + char tmp[MAX_PATH_LEN]; + char *p = NULL; + size_t len; + + snprintf(tmp, sizeof(tmp), "%s", path); + len = strlen(tmp); + if (tmp[len - 1] == '/') { + tmp[len - 1] = 0; + } + + for (p = tmp + 1; *p; p++) { + if (*p == '/') { + *p = 0; + if (mkdir(tmp, 0755) != 0 && errno != EEXIST) { return -1; + } + *p = '/'; } + } + + if (mkdir(tmp, 0755) != 0 && errno != EEXIST) { + return -1; + } - return 0; + return 0; } bool file_exists(const char *path) { - struct stat buffer; - return (stat(path, &buffer) == 0); + struct stat buffer; + return (stat(path, &buffer) == 0); } bool is_directory(const char *path) { - struct stat statbuf; - if (stat(path, &statbuf) != 0) { - return false; - } - return S_ISDIR(statbuf.st_mode); + struct stat statbuf; + if (stat(path, &statbuf) != 0) { + return false; + } + return S_ISDIR(statbuf.st_mode); } void init_vars() { - char *home = getenv("HOME"); - if (home) { - snprintf(home_dir, MAX_PATH_LEN, "%s", home); - } else { - snprintf(home_dir, MAX_PATH_LEN, "/root"); - } - - is_root_config = file_exists(root_config); - - if (is_root_config) { - snprintf(config_dir, MAX_PATH_LEN, "/etc/pkgit"); - } else { - snprintf(config_dir, MAX_PATH_LEN, "%s/.config/pkgit", home_dir); - } - - snprintf(config_file, MAX_PATH_LEN, "%s/init.lua", config_dir); - snprintf(repo_file, MAX_PATH_LEN, "%s/repos.lua", config_dir); - - config_exists = file_exists(root_config) || file_exists(home_config()); - - snprintf(bin, MAX_PATH_LEN, "%s/.local/bin", home_dir); - snprintf(lib, MAX_PATH_LEN, "%s/.local/lib", home_dir); - snprintf(inc, MAX_PATH_LEN, "%s/.local/include", home_dir); - snprintf(src, MAX_PATH_LEN, "%s/.local/share/pkgit", home_dir); - - install_directories[0] = config_dir; - install_directories[1] = strdup(get_install_dir("bin")); - install_directories[2] = strdup(get_install_dir("lib")); - install_directories[3] = strdup(get_install_dir("inc")); - install_directories[4] = strdup(get_install_dir("src")); - - static char print_pkgit_buf[256]; - snprintf(print_pkgit_buf, sizeof(print_pkgit_buf), "%s[%s%s%s]%s", - bold_yellow, bold_magenta, "pkgit", bold_yellow, color_reset); - print_pkgit = print_pkgit_buf; - - static char print_success_buf[256]; - snprintf(print_success_buf, sizeof(print_success_buf), "%s%s [SUCCESS]%s", - print_pkgit, green, color_reset); - print_success = print_success_buf; - - static char print_skipped_buf[256]; - snprintf(print_skipped_buf, sizeof(print_skipped_buf), "%s%s [SKIP]%s", - print_pkgit, blue, color_reset); - print_skipped = print_skipped_buf; - - static char print_warning_buf[256]; - snprintf(print_warning_buf, sizeof(print_warning_buf), "%s%s [WARN]%s", - print_pkgit, yellow, color_reset); - print_warning = print_warning_buf; - - static char print_error_buf[256]; - snprintf(print_error_buf, sizeof(print_error_buf), "%s%s [ERROR]%s", - print_pkgit, red, color_reset); - print_error = print_error_buf; + char *home = getenv("HOME"); + if (home) { + snprintf(home_dir, MAX_PATH_LEN, "%s", home); + } else { + snprintf(home_dir, MAX_PATH_LEN, "/root"); + } + + is_root_config = file_exists(root_config); + + if (is_root_config) { + snprintf(config_dir, MAX_PATH_LEN, "/etc/pkgit"); + } else { + snprintf(config_dir, MAX_PATH_LEN, "%s/.config/pkgit", home_dir); + } + + snprintf(config_file, MAX_PATH_LEN, "%s/init.lua", config_dir); + snprintf(repo_file, MAX_PATH_LEN, "%s/repos.lua", config_dir); + + config_exists = file_exists(root_config) || file_exists(home_config()); + + snprintf(bin, MAX_PATH_LEN, "%s/.local/bin", home_dir); + snprintf(lib, MAX_PATH_LEN, "%s/.local/lib", home_dir); + snprintf(inc, MAX_PATH_LEN, "%s/.local/include", home_dir); + snprintf(src, MAX_PATH_LEN, "%s/.local/share/pkgit", home_dir); + + install_directories[0] = config_dir; + install_directories[1] = strdup(get_install_dir("bin")); + install_directories[2] = strdup(get_install_dir("lib")); + install_directories[3] = strdup(get_install_dir("inc")); + install_directories[4] = strdup(get_install_dir("src")); + + static char print_pkgit_buf[256]; + snprintf(print_pkgit_buf, sizeof(print_pkgit_buf), "%s[%s%s%s]%s", + bold_yellow, bold_magenta, "pkgit", bold_yellow, color_reset); + print_pkgit = print_pkgit_buf; + + static char print_success_buf[256]; + snprintf(print_success_buf, sizeof(print_success_buf), "%s%s [SUCCESS]%s", + print_pkgit, green, color_reset); + print_success = print_success_buf; + + static char print_skipped_buf[256]; + snprintf(print_skipped_buf, sizeof(print_skipped_buf), "%s%s [SKIP]%s", + print_pkgit, blue, color_reset); + print_skipped = print_skipped_buf; + + static char print_warning_buf[256]; + snprintf(print_warning_buf, sizeof(print_warning_buf), "%s%s [WARN]%s", + print_pkgit, yellow, color_reset); + print_warning = print_warning_buf; + + static char print_error_buf[256]; + snprintf(print_error_buf, sizeof(print_error_buf), "%s%s [ERROR]%s", + print_pkgit, red, color_reset); + print_error = print_error_buf; } |
