2 This file is not licenced under the GPL like the rest of the code.
3 Its is under the MIT license, to encourage reuse by cut-and-paste.
5 Copyright (c) 2007 Red Hat, inc
7 Permission is hereby granted, free of charge, to any person
8 obtaining a copy of this software and associated documentation files
9 (the "Software"), to deal in the Software without restriction,
10 including without limitation the rights to use, copy, modify, merge,
11 publish, distribute, sublicense, and/or sell copies of the Software,
12 and to permit persons to whom the Software is furnished to do so,
13 subject to the following conditions:
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * xdg_user_dir_lookup_with_fallback:
34 * @type: a string specifying the type of directory
35 * @fallback: value to use if the directory isn't specified by the user
36 * @returns: a newly allocated absolute pathname
38 * Looks up a XDG user directory of the specified type.
39 * Example of types are "DESKTOP" and "DOWNLOAD".
41 * In case the user hasn't specified any directory for the specified
42 * type the value returned is @fallback.
44 * The return value is newly allocated and must be freed with
45 * free(). The return value is never NULL if @fallback != NULL, unless
49 xdg_user_dir_lookup_with_fallback (const char *type
, const char *fallback
)
52 char *home_dir
, *config_home
, *config_file
;
59 home_dir
= getenv ("HOME");
64 config_home
= getenv ("XDG_CONFIG_HOME");
65 if (config_home
== NULL
|| config_home
[0] == 0)
67 config_file
= (char*) malloc (strlen (home_dir
) + strlen ("/.config/user-dirs.dirs") + 1);
68 if (config_file
== NULL
)
71 strcpy (config_file
, home_dir
);
72 strcat (config_file
, "/.config/user-dirs.dirs");
76 config_file
= (char*) malloc (strlen (config_home
) + strlen ("/user-dirs.dirs") + 1);
77 if (config_file
== NULL
)
80 strcpy (config_file
, config_home
);
81 strcat (config_file
, "/user-dirs.dirs");
84 file
= fopen (config_file
, "r");
90 while (fgets (buffer
, sizeof (buffer
), file
))
92 /* Remove newline at end */
93 len
= strlen (buffer
);
94 if (len
> 0 && buffer
[len
-1] == '\n')
98 while (*p
== ' ' || *p
== '\t')
101 if (strncmp (p
, "XDG_", 4) != 0)
104 if (strncmp (p
, type
, strlen (type
)) != 0)
107 if (strncmp (p
, "_DIR", 4) != 0)
111 while (*p
== ' ' || *p
== '\t')
118 while (*p
== ' ' || *p
== '\t')
126 if (strncmp (p
, "$HOME/", 6) == 0)
136 user_dir
= (char*) malloc (strlen (home_dir
) + 1 + strlen (p
) + 1);
137 if (user_dir
== NULL
)
140 strcpy (user_dir
, home_dir
);
141 strcat (user_dir
, "/");
145 user_dir
= (char*) malloc (strlen (p
) + 1);
146 if (user_dir
== NULL
)
152 d
= user_dir
+ strlen (user_dir
);
153 while (*p
&& *p
!= '"')
155 if ((*p
== '\\') && (*(p
+1) != 0))
169 return strdup (fallback
);
174 * xdg_user_dir_lookup:
175 * @type: a string specifying the type of directory
176 * @returns: a newly allocated absolute pathname
178 * Looks up a XDG user directory of the specified type.
179 * Example of types are "DESKTOP" and "DOWNLOAD".
181 * The return value is always != NULL (unless out of memory),
183 * for the type is not specified by the user the default
184 * is the home directory. Except for DESKTOP which defaults
187 * The return value is newly allocated and must be freed with
191 xdg_user_dir_lookup (const char *type
)
193 char *dir
, *home_dir
, *user_dir
;
195 dir
= xdg_user_dir_lookup_with_fallback (type
, NULL
);
199 home_dir
= getenv ("HOME");
201 if (home_dir
== NULL
)
202 return strdup ("/tmp");
204 /* Special case desktop for historical compatibility */
205 if (strcmp (type
, "DESKTOP") == 0)
207 user_dir
= (char*) malloc (strlen (home_dir
) + strlen ("/Desktop") + 1);
208 if (user_dir
== NULL
)
211 strcpy (user_dir
, home_dir
);
212 strcat (user_dir
, "/Desktop");
216 return strdup (home_dir
);
219 #ifdef STANDALONE_XDG_USER_DIR_LOOKUP
221 main (int argc
, char *argv
[])
225 fprintf (stderr
, "Usage %s <dir-type>\n", argv
[0]);
229 printf ("%s\n", xdg_user_dir_lookup (argv
[1]));