Debian Codegraph @

src/file/dirs_xdg.c

raw
1 /*
2 * This file is part of libbluray
3 * Copyright (C) 2013 Petri Hintukainen <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see
17 * <http://www.gnu.org/licenses/>.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23
24 #include "dirs.h"
25
26 #include "util/strutl.h"
27 #include "util/logging.h"
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 /*
33 * Based on XDG Base Directory Specification
34 * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
35 */
36
37 #define USER_CFG_DIR ".config"
38 #define USER_CACHE_DIR ".cache"
39 #define USER_DATA_DIR ".local/share"
40 #define SYSTEM_CFG_DIR "/etc/xdg"
41
42
43 char *file_get_config_home(void)
44 {
45 const char *xdg_home = getenv("XDG_CONFIG_HOME");
46 if (xdg_home && *xdg_home) {
47 return str_dup(xdg_home);
48 }
49
50 const char *user_home = getenv("HOME");
51 if (user_home && *user_home) {
52 return str_printf("%s/%s", user_home, USER_CFG_DIR);
53 }
54
55 BD_DEBUG(DBG_FILE, "Can't find user home directory ($HOME) !\n");
56 return NULL;
57 }
58
59 char *file_get_data_home(void)
60 {
61 const char *xdg_home = getenv("XDG_DATA_HOME");
62 if (xdg_home && *xdg_home) {
63 return str_dup(xdg_home);
64 }
65
66 const char *user_home = getenv("HOME");
67 if (user_home && *user_home) {
68 return str_printf("%s/%s", user_home, USER_DATA_DIR);
69 }
70
71 BD_DEBUG(DBG_FILE, "Can't find user home directory ($HOME) !\n");
72 return NULL;
73 }
74
75 char *file_get_cache_home(void)
76 {
77 const char *xdg_cache = getenv("XDG_CACHE_HOME");
78 if (xdg_cache && *xdg_cache) {
79 return str_dup(xdg_cache);
80 }
81
82 const char *user_home = getenv("HOME");
83 if (user_home && *user_home) {
84 return str_printf("%s/%s", user_home, USER_CACHE_DIR);
85 }
86
87 BD_DEBUG(DBG_FILE, "Can't find user home directory ($HOME) !\n");
88 return NULL;
89 }
90
91 const char *file_get_config_system(const char *dir)
92 {
93 static char *dirs = NULL; // "dir1\0dir2\0...\0dirN\0\0"
94
95 if (!dirs) {
96 const char *xdg_sys = getenv("XDG_CONFIG_DIRS");
97
98 if (xdg_sys && *xdg_sys) {
99
100 dirs = calloc(1, strlen(xdg_sys) + 2);
101 if (!dirs) {
102 return NULL;
103 }
104 strcpy(dirs, xdg_sys);
105
106 char *pt = dirs;
107 while (NULL != (pt = strchr(pt, ':'))) {
108 *pt++ = 0;
109 }
110
111 } else {
112 dirs = str_printf("%s%c%c", SYSTEM_CFG_DIR, 0, 0);
113 }
114 }
115
116 if (!dir) {
117 // first call
118 dir = dirs;
119 } else {
120 // next call
121 dir += strlen(dir) + 1;
122 if (!*dir) {
123 // end of list
124 dir = NULL;
125 }
126 }
127
128 return dir;
129 }