added filesystem functions

This commit is contained in:
Richard Aas 2012-06-15 07:21:23 +00:00
parent d8dede46e8
commit 86ca6c5ac8
6 changed files with 156 additions and 0 deletions

View file

@ -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);

View file

@ -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

View file

@ -776,6 +776,9 @@
<File
RelativePath="..\..\src\sys\endian.c">
</File>
<File
RelativePath="..\..\src\sys\fs.c">
</File>
<File
RelativePath="..\..\src\sys\rand.c">
</File>

115
src/sys/fs.c Normal file
View file

@ -0,0 +1,115 @@
/**
* @file fs.c File-system functions
*
* Copyright (C) 2010 Creytiv.com
*/
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shlobj.h>
#include <direct.h>
#include <lmaccess.h>
#undef WIN32_LEAN_AND_MEAN
#endif
#include <re.h>
/**
* 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
}

View file

@ -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

View file

@ -3,10 +3,14 @@
*
* Copyright (C) 2010 Creytiv.com
*/
#include <stdlib.h>
#include <string.h>
#include <re_types.h>
#include <re_fmt.h>
#include <re_sys.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_UNAME
#include <sys/utsname.h>
#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
*