diff --git a/include/re_sys.h b/include/re_sys.h index 68cc56b..7c97930 100644 --- a/include/re_sys.h +++ b/include/re_sys.h @@ -55,6 +55,7 @@ int sys_build_get(struct re_printf *pf, void *unused); const char *sys_arch_get(void); const char *sys_os_get(void); const char *sys_libre_version_get(void); +const char *sys_username(void); int sys_coredump_set(bool enable); int sys_daemon(void); void sys_usleep(unsigned int us); @@ -81,3 +82,8 @@ uint64_t rand_u64(void); char rand_char(void); void rand_str(char *str, size_t size); void rand_bytes(uint8_t *p, size_t size); + + +/* File-System */ +int fs_mkdir(const char *path, uint16_t mode); +int fs_gethome(char *path, size_t sz); diff --git a/mk/symbian/re.mmp b/mk/symbian/re.mmp index 0cbf504..af46ce7 100644 --- a/mk/symbian/re.mmp +++ b/mk/symbian/re.mmp @@ -118,6 +118,7 @@ SOURCE sha1.c SOURCEPATH ..\..\src\sys SOURCE daemon.c SOURCE endian.c +SOURCE fs.c SOURCE rand.c SOURCE sys.c SOURCE symbian\sleep.cpp diff --git a/mk/win32/re.vcproj b/mk/win32/re.vcproj index 66c714b..ffc9835 100644 --- a/mk/win32/re.vcproj +++ b/mk/win32/re.vcproj @@ -776,6 +776,9 @@ + + diff --git a/src/sys/fs.c b/src/sys/fs.c new file mode 100644 index 0000000..bcdff7b --- /dev/null +++ b/src/sys/fs.c @@ -0,0 +1,115 @@ +/** + * @file fs.c File-system functions + * + * Copyright (C) 2010 Creytiv.com + */ +#include +#include +#include +#include +#include +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_PWD_H +#include +#endif +#ifdef WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#include +#include +#include +#undef WIN32_LEAN_AND_MEAN +#endif +#include + + +/** + * Create a directory with full path + * + * @param path Directory path + * @param mode Access permissions + * + * @return 0 if success, otherwise errorcode + */ +int fs_mkdir(const char *path, uint16_t mode) +{ + int ret; + + if (!path) + return EINVAL; + +#if defined (WIN32) + (void)mode; + ret = _mkdir(path); +#else + ret = mkdir(path, mode); +#endif + if (ret < 0) + return errno; + + return 0; +} + + +/** + * Get the home directory for the current user + * + * @param path String to write home directory + * @param sz Size of path string + * + * @return 0 if success, otherwise errorcode + */ +int fs_gethome(char *path, size_t sz) +{ +#ifdef WIN32 + char win32_path[MAX_PATH]; + + if (!path || !sz) + return EINVAL; + + if (S_OK != SHGetFolderPath(NULL, + CSIDL_APPDATA | CSIDL_FLAG_CREATE, + NULL, + 0, + win32_path)) { + return ENOENT; + } + + str_ncpy(path, win32_path, sz); + + return 0; + +#elif defined(__SYMBIAN32__) + if (!path || !sz) + return EINVAL; + + str_ncpy(path, "c:\\Data", sz); + + return 0; + +#elif defined(HAVE_PWD_H) + const char *loginname; + struct passwd *pw; + + if (!path || !sz) + return EINVAL; + + loginname = sys_username(); + if (!loginname) + return ENOENT; + + pw = getpwnam(loginname); + if (!pw) + return errno; + + str_ncpy(path, pw->pw_dir, sz); + + return 0; +#else + (void)path; + (void)sz; + return ENOSYS; +#endif +} diff --git a/src/sys/mod.mk b/src/sys/mod.mk index e005b45..616751e 100644 --- a/src/sys/mod.mk +++ b/src/sys/mod.mk @@ -6,6 +6,7 @@ SRCS += sys/daemon.c SRCS += sys/endian.c +SRCS += sys/fs.c SRCS += sys/rand.c SRCS += sys/sleep.c SRCS += sys/sys.c diff --git a/src/sys/sys.c b/src/sys/sys.c index 7f91860..f9432e7 100644 --- a/src/sys/sys.c +++ b/src/sys/sys.c @@ -3,10 +3,14 @@ * * Copyright (C) 2010 Creytiv.com */ +#include #include #include #include #include +#ifdef HAVE_UNISTD_H +#include +#endif #ifdef HAVE_UNAME #include #endif @@ -181,6 +185,32 @@ const char *sys_libre_version_get(void) } +/** + * Return the username (login name) for the current user + * + * @return Username or NULL if not available + */ +const char *sys_username(void) +{ +#ifdef HAVE_PWD_H + char *login; + + login = getenv("LOGNAME"); + if (!login) + login = getenv("USER"); +#if defined (HAVE_UNISTD_H) && !defined (__SYMBIAN32__) + if (!login) { + login = getlogin(); + } +#endif + + return str_isset(login) ? login : NULL; +#else + return NULL; +#endif +} + + /** * Enable or disable coredump *