libsysutils
view 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 source
1 #ifdef __unix__
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <pwd.h>
8 #include "datapath.h"
9 #include "sysu_impl.h"
11 int sysu_impl_getdir(enum sysu_dir_type type, char *buf, int sz)
12 {
13 switch(type) {
14 case SYSU_DATA_DIR:
15 if(sysu_state.appname && sysu_state.prefix) {
16 return snprintf(buf, sz, "%s/share/%s", sysu_state.prefix, sysu_state.appname);
17 }
18 break;
20 case SYSU_STORAGE_DIR:
21 case SYSU_CONFIG_DIR:
22 if(sysu_state.appname) {
23 int homelen = sysu_impl_getdir(SYSU_HOME_DIR, buf, sz);
24 return homelen + snprintf(buf + homelen, sz - homelen, "/.config/%s", sysu_state.appname);
25 }
26 break;
28 case SYSU_TMP_DIR:
29 strncpy(buf, "/tmp", sz - 1);
30 return 4;
32 case SYSU_HOME_DIR:
33 {
34 struct passwd pwd, *res;
35 char tmp[512], *home;
36 getpwuid_r(getuid(), &pwd, tmp, sizeof tmp, &res);
37 if(res) {
38 home = pwd.pw_dir;
39 } else {
40 home = getenv("HOME");
41 }
42 if(home) {
43 strncpy(buf, home, sz - 1);
44 return strlen(buf);
45 }
46 }
47 break;
49 case SYSU_CURRENT_DIR:
50 return strlen(getcwd(buf, sz));
51 }
53 /* failed, return an empty path */
54 *buf = 0;
55 return 0;
56 }
58 #endif