libsysutils
diff src/datapath_unix.c @ 2:36936815cc92
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 08 May 2015 07:26:01 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/datapath_unix.c Fri May 08 07:26:01 2015 +0300 1.3 @@ -0,0 +1,58 @@ 1.4 +#ifdef __unix__ 1.5 + 1.6 +#include <stdio.h> 1.7 +#include <stdlib.h> 1.8 +#include <string.h> 1.9 +#include <unistd.h> 1.10 +#include <pwd.h> 1.11 +#include "datapath.h" 1.12 +#include "sysu_impl.h" 1.13 + 1.14 +int sysu_impl_getdir(enum sysu_dir_type type, char *buf, int sz) 1.15 +{ 1.16 + switch(type) { 1.17 + case SYSU_DATA_DIR: 1.18 + if(sysu_state.appname && sysu_state.prefix) { 1.19 + return snprintf(buf, sz, "%s/share/%s", sysu_state.prefix, sysu_state.appname); 1.20 + } 1.21 + break; 1.22 + 1.23 + case SYSU_STORAGE_DIR: 1.24 + case SYSU_CONFIG_DIR: 1.25 + if(sysu_state.appname) { 1.26 + int homelen = sysu_impl_getdir(SYSU_HOME_DIR, buf, sz); 1.27 + return homelen + snprintf(buf + homelen, sz - homelen, "/.config/%s", sysu_state.appname); 1.28 + } 1.29 + break; 1.30 + 1.31 + case SYSU_TMP_DIR: 1.32 + strncpy(buf, "/tmp", sz - 1); 1.33 + return 4; 1.34 + 1.35 + case SYSU_HOME_DIR: 1.36 + { 1.37 + struct passwd pwd, *res; 1.38 + char tmp[512], *home; 1.39 + getpwuid_r(getuid(), &pwd, tmp, sizeof tmp, &res); 1.40 + if(res) { 1.41 + home = pwd.pw_dir; 1.42 + } else { 1.43 + home = getenv("HOME"); 1.44 + } 1.45 + if(home) { 1.46 + strncpy(buf, home, sz - 1); 1.47 + return strlen(buf); 1.48 + } 1.49 + } 1.50 + break; 1.51 + 1.52 + case SYSU_CURRENT_DIR: 1.53 + return strlen(getcwd(buf, sz)); 1.54 + } 1.55 + 1.56 + /* failed, return an empty path */ 1.57 + *buf = 0; 1.58 + return 0; 1.59 +} 1.60 + 1.61 +#endif