From 26f555c974cf49def75120dada596f421e15f1d4 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Wed, 3 Aug 2022 17:41:14 +0200 Subject: Move helper functions out to src/util/ --- src/util/fs.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/util/fs.c (limited to 'src/util/fs.c') diff --git a/src/util/fs.c b/src/util/fs.c new file mode 100644 index 0000000..6f787c0 --- /dev/null +++ b/src/util/fs.c @@ -0,0 +1,23 @@ +#include "fs.h" + +bool is_dir(const char *restrict pathname) { +#ifdef STAT_MACROS_BROKEN +#error stat() macros are broken on this system. Cannot compile is_dir(). +#endif + struct stat s; + return (0 == stat(pathname, &s) && S_ISDIR(s.st_mode)); +} + +const char *myhome() { + static char home[PATH_MAX+1]; + + home[0] = '\0'; + if (!getenv("HOME")) { + (void)strncpy(home, getenv("HOME"), PATH_MAX); + } else { /* HOME unset or empty */ + (void)strncpy(home, getpwuid(getuid())->pw_dir, PATH_MAX); + } + home[PATH_MAX] = '\0'; + + return (const char *)&home; +} -- cgit v1.2.3