-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxdg.c
More file actions
43 lines (36 loc) · 1.14 KB
/
xdg.c
File metadata and controls
43 lines (36 loc) · 1.14 KB
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
#include <pwd.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "utils.h"
char *xdg_config_dir(char *postfix) {
char *config_home;
if ((config_home = getenv("XDG_CONFIG_HOME"))) {
size_t config_home_len = strlen(config_home);
char *buf = malloc(config_home_len + (postfix ? strlen(postfix) : 0) + 2);
strcpy(buf, config_home);
while (config_home[config_home_len - 1] == '/') {
buf[--config_home_len] = 0;
}
if (postfix) {
buf[config_home_len] = '/';
strcpy(buf+config_home_len+1, postfix);
}
return buf;
} else {
char *name = get_user_name();
size_t name_len = strlen(name);
char *buf = malloc(strlen("/home/") + name_len + strlen("/.config/") +
(postfix ? strlen(postfix) : 0) + 1);
strcpy(buf, "/home/");
strcat(buf, name);
strcat(buf, "/.config");
if (postfix) {
size_t end = strlen("/home/") + name_len + strlen("/.config");
buf[end] = '/';
strcpy(buf+end+1, postfix);
}
return buf;
}
}