From 2eb15227c79e3a08ce8eed80d41ef07da5b90a78 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Sat, 5 Sep 2020 14:37:33 +0200 Subject: move fs related functions to common, add more file related functions --- src/common.c | 36 ++++++++++++++++++++++++++++++++++++ src/common.h | 20 +++++++++++++++++++- src/config.c | 11 +---------- src/config.h | 4 +--- 4 files changed, 57 insertions(+), 14 deletions(-) diff --git a/src/common.c b/src/common.c index ca08f07..6d687ee 100644 --- a/src/common.c +++ b/src/common.c @@ -1,5 +1,7 @@ #include #include +#include +#include #include "common.h" @@ -19,3 +21,37 @@ void print_help(const struct Command* commands, const size_t size) printf("\t%-*s\t %s\n", longestCommand, commands[i].name, commands[i].description); } } + +struct stat getStat(const char* path) +{ + struct stat sb; + + stat(path, &sb); + + return sb; +} + +bool isFile(const char* path) +{ + struct stat sb = getStat(path); + + return S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode); +} + +bool isDir(const char* path) +{ + struct stat sb = getStat(path); + + return S_ISDIR(sb.st_mode) || S_ISLNK(sb.st_mode); +} + +void makeDir(const char* path) +{ + struct stat st = {0}; + + if (stat(path, &st) == -1) + { + mkdir(path, 0755); + } +} + diff --git a/src/common.h b/src/common.h index fba45cd..f33c152 100644 --- a/src/common.h +++ b/src/common.h @@ -3,6 +3,8 @@ #include #include +#include +#include #define ARRAY_LEN(arr) sizeof(arr) / sizeof(arr[0]) @@ -10,6 +12,16 @@ #define DXVK_API "https://api.github.com/repos/lutris/dxvk/releases" #define INSTALLER_API "https://lutris.net/api/installers/" +#ifndef NAME +#warning "no name specified, setting it to polecat" +#define NAME "polecat" +#endif + +#ifndef VERSION +#warning "no version specified, setting it to 0.0.0" +#define VERSION "0.0.0" +#endif + #define USER_AGENT NAME "/" VERSION #define USAGE_STR "Usage: " NAME @@ -27,4 +39,10 @@ struct Command { void print_help(const struct Command*, size_t); -#endif \ No newline at end of file +struct stat getStat(const char* path); +bool isFile(const char*); +bool isDir(const char*); + +void makeDir(const char* path); + +#endif diff --git a/src/config.c b/src/config.c index b728e10..4309d87 100644 --- a/src/config.c +++ b/src/config.c @@ -13,7 +13,7 @@ static void getXDGDir(const char* envvar, const char* homeext, char* config, con if (xdg_var) { - strcpy(config, xdg_var); + strncpy(config, xdg_var, size); } else { @@ -38,12 +38,3 @@ void getCacheDir(char* config, const size_t size) getXDGDir("XDG_CACHE_HOME", "/.cache/" NAME, config, size); } -void makeDir(const char* path) -{ - struct stat st = {0}; - - if (stat(path, &st) == -1) - { - mkdir(path, 0755); - } -} \ No newline at end of file diff --git a/src/config.h b/src/config.h index 07f0dc6..84c3fd0 100644 --- a/src/config.h +++ b/src/config.h @@ -7,6 +7,4 @@ void getConfigDir(char*, const size_t); void getDataDir(char*, const size_t); void getCacheDir(char*, const size_t); -void makeDir(const char* path); - -#endif \ No newline at end of file +#endif -- cgit v1.2.3