forked from adrianlopezroche/fdupes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdgbase.c
69 lines (55 loc) · 1.51 KB
/
xdgbase.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* The contents of this file are in the public domain.
*/
#include "config.h"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <string.h>
#define XDG_CACHE_HOME_BASENAME ".cache"
#define XDG_CACHE_HOME_PERMISSIONS 0700
char *getcachehome(int createdefault)
{
char *cachedir;
char *homedir;
char *result;
size_t pathlength;
size_t defaultdirlength;
struct passwd *pw;
struct stat st;
cachedir = getenv("XDG_CACHE_HOME");
if (cachedir != 0)
{
pathlength = strlen(cachedir);
result = malloc(pathlength + 1);
if (result == 0)
return 0;
strcpy(result, cachedir);
}
else
{
homedir = getenv("HOME");
if (homedir == 0)
{
pw = getpwuid(getuid());
if (pw == 0)
return 0;
homedir = pw->pw_dir;
}
if (homedir == 0)
return 0;
pathlength = strlen(homedir);
defaultdirlength = strlen(XDG_CACHE_HOME_BASENAME);
result = malloc(pathlength + defaultdirlength + 2);
if (result == 0)
return 0;
memmove(result, homedir, pathlength);
memmove(result + pathlength + 1, XDG_CACHE_HOME_BASENAME, defaultdirlength);
result[pathlength] = '/';
result[pathlength + defaultdirlength + 1] = '\0';
if (createdefault && stat(result, &st) != 0)
mkdir(result, XDG_CACHE_HOME_PERMISSIONS);
}
return result;
}