libsysutils

changeset 2:36936815cc92 tip

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 08 May 2015 07:26:01 +0300
parents c1323f9f202d
children
files include/datapath.h src/datapath.c src/datapath_unix.c
diffstat 3 files changed, 61 insertions(+), 2 deletions(-) [+]
line diff
     1.1 --- a/include/datapath.h	Tue May 05 05:51:56 2015 +0300
     1.2 +++ b/include/datapath.h	Fri May 08 07:26:01 2015 +0300
     1.3 @@ -10,7 +10,7 @@
     1.4  	SYSU_CURRENT_DIR	/* current working directory */
     1.5  };
     1.6  
     1.7 -const char *sysu_get_directory(enum sysu_dir_type type);
     1.8 +int sysu_get_directory(enum sysu_dir_type type, char *buf, int sz);
     1.9  
    1.10  /* Writes the path to outpath, and returns the size of the path in bytes.
    1.11   * Call with len <= 0 and outpath == NULL to return required storage
     2.1 --- a/src/datapath.c	Tue May 05 05:51:56 2015 +0300
     2.2 +++ b/src/datapath.c	Fri May 08 07:26:01 2015 +0300
     2.3 @@ -1,5 +1,6 @@
     2.4  #include "datapath.h"
     2.5  
     2.6 -const char *sysu_get_directory(enum sysu_dir_type type)
     2.7 +int sysu_get_directory(enum sysu_dir_type type, char *buf, int sz)
     2.8  {
     2.9 +	return sysu_impl_getdir(type, buf, sz);
    2.10  }
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/datapath_unix.c	Fri May 08 07:26:01 2015 +0300
     3.3 @@ -0,0 +1,58 @@
     3.4 +#ifdef __unix__
     3.5 +
     3.6 +#include <stdio.h>
     3.7 +#include <stdlib.h>
     3.8 +#include <string.h>
     3.9 +#include <unistd.h>
    3.10 +#include <pwd.h>
    3.11 +#include "datapath.h"
    3.12 +#include "sysu_impl.h"
    3.13 +
    3.14 +int sysu_impl_getdir(enum sysu_dir_type type, char *buf, int sz)
    3.15 +{
    3.16 +	switch(type) {
    3.17 +	case SYSU_DATA_DIR:
    3.18 +		if(sysu_state.appname && sysu_state.prefix) {
    3.19 +			return snprintf(buf, sz, "%s/share/%s", sysu_state.prefix, sysu_state.appname);
    3.20 +		}
    3.21 +		break;
    3.22 +
    3.23 +	case SYSU_STORAGE_DIR:
    3.24 +	case SYSU_CONFIG_DIR:
    3.25 +		if(sysu_state.appname) {
    3.26 +			int homelen = sysu_impl_getdir(SYSU_HOME_DIR, buf, sz);
    3.27 +			return homelen + snprintf(buf + homelen, sz - homelen, "/.config/%s", sysu_state.appname);
    3.28 +		}
    3.29 +		break;
    3.30 +
    3.31 +	case SYSU_TMP_DIR:
    3.32 +		strncpy(buf, "/tmp", sz - 1);
    3.33 +		return 4;
    3.34 +
    3.35 +	case SYSU_HOME_DIR:
    3.36 +		{
    3.37 +			struct passwd pwd, *res;
    3.38 +			char tmp[512], *home;
    3.39 +			getpwuid_r(getuid(), &pwd, tmp, sizeof tmp, &res);
    3.40 +			if(res) {
    3.41 +				home = pwd.pw_dir;
    3.42 +			} else {
    3.43 +				home = getenv("HOME");
    3.44 +			}
    3.45 +			if(home) {
    3.46 +				strncpy(buf, home, sz - 1);
    3.47 +				return strlen(buf);
    3.48 +			}
    3.49 +		}
    3.50 +		break;
    3.51 +
    3.52 +	case SYSU_CURRENT_DIR:
    3.53 +		return strlen(getcwd(buf, sz));
    3.54 +	}
    3.55 +
    3.56 +	/* failed, return an empty path */
    3.57 +	*buf = 0;
    3.58 +	return 0;
    3.59 +}
    3.60 +
    3.61 +#endif