# HG changeset patch # User John Tsiombikas # Date 1431059161 -10800 # Node ID 36936815cc9273bba4774ba7e28132ae671ec2a4 # Parent c1323f9f202d23140187af34da0cb51993f4a5a8 foo diff -r c1323f9f202d -r 36936815cc92 include/datapath.h --- a/include/datapath.h Tue May 05 05:51:56 2015 +0300 +++ b/include/datapath.h Fri May 08 07:26:01 2015 +0300 @@ -10,7 +10,7 @@ SYSU_CURRENT_DIR /* current working directory */ }; -const char *sysu_get_directory(enum sysu_dir_type type); +int sysu_get_directory(enum sysu_dir_type type, char *buf, int sz); /* Writes the path to outpath, and returns the size of the path in bytes. * Call with len <= 0 and outpath == NULL to return required storage diff -r c1323f9f202d -r 36936815cc92 src/datapath.c --- a/src/datapath.c Tue May 05 05:51:56 2015 +0300 +++ b/src/datapath.c Fri May 08 07:26:01 2015 +0300 @@ -1,5 +1,6 @@ #include "datapath.h" -const char *sysu_get_directory(enum sysu_dir_type type) +int sysu_get_directory(enum sysu_dir_type type, char *buf, int sz) { + return sysu_impl_getdir(type, buf, sz); } diff -r c1323f9f202d -r 36936815cc92 src/datapath_unix.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/datapath_unix.c Fri May 08 07:26:01 2015 +0300 @@ -0,0 +1,58 @@ +#ifdef __unix__ + +#include +#include +#include +#include +#include +#include "datapath.h" +#include "sysu_impl.h" + +int sysu_impl_getdir(enum sysu_dir_type type, char *buf, int sz) +{ + switch(type) { + case SYSU_DATA_DIR: + if(sysu_state.appname && sysu_state.prefix) { + return snprintf(buf, sz, "%s/share/%s", sysu_state.prefix, sysu_state.appname); + } + break; + + case SYSU_STORAGE_DIR: + case SYSU_CONFIG_DIR: + if(sysu_state.appname) { + int homelen = sysu_impl_getdir(SYSU_HOME_DIR, buf, sz); + return homelen + snprintf(buf + homelen, sz - homelen, "/.config/%s", sysu_state.appname); + } + break; + + case SYSU_TMP_DIR: + strncpy(buf, "/tmp", sz - 1); + return 4; + + case SYSU_HOME_DIR: + { + struct passwd pwd, *res; + char tmp[512], *home; + getpwuid_r(getuid(), &pwd, tmp, sizeof tmp, &res); + if(res) { + home = pwd.pw_dir; + } else { + home = getenv("HOME"); + } + if(home) { + strncpy(buf, home, sz - 1); + return strlen(buf); + } + } + break; + + case SYSU_CURRENT_DIR: + return strlen(getcwd(buf, sz)); + } + + /* failed, return an empty path */ + *buf = 0; + return 0; +} + +#endif