xdg_user_dir_lookup.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  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.
  4. Copyright (c) 2007 Red Hat, inc
  5. Permission is hereby granted, free of charge, to any person
  6. obtaining a copy of this software and associated documentation files
  7. (the "Software"), to deal in the Software without restriction,
  8. including without limitation the rights to use, copy, modify, merge,
  9. publish, distribute, sublicense, and/or sell copies of the Software,
  10. and to permit persons to whom the Software is furnished to do so,
  11. subject to the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. /**
  27. * xdg_user_dir_lookup_with_fallback:
  28. * @type: a string specifying the type of directory
  29. * @fallback: value to use if the directory isn't specified by the user
  30. * @returns: a newly allocated absolute pathname
  31. *
  32. * Looks up a XDG user directory of the specified type.
  33. * Example of types are "DESKTOP" and "DOWNLOAD".
  34. *
  35. * In case the user hasn't specified any directory for the specified
  36. * type the value returned is @fallback.
  37. *
  38. * The return value is newly allocated and must be freed with
  39. * free(). The return value is never NULL if @fallback != NULL, unless
  40. * out of memory.
  41. **/
  42. static char *
  43. xdg_user_dir_lookup_with_fallback (const char *type, const char *fallback)
  44. {
  45. FILE *file;
  46. char *home_dir, *config_home, *config_file;
  47. char buffer[512];
  48. char *user_dir;
  49. char *p, *d;
  50. int len;
  51. int relative;
  52. home_dir = getenv ("HOME");
  53. if (home_dir == NULL)
  54. goto error;
  55. config_home = getenv ("XDG_CONFIG_HOME");
  56. if (config_home == NULL || config_home[0] == 0)
  57. {
  58. config_file = (char*) malloc (strlen (home_dir) + strlen ("/.config/user-dirs.dirs") + 1);
  59. if (config_file == NULL)
  60. goto error;
  61. strcpy (config_file, home_dir);
  62. strcat (config_file, "/.config/user-dirs.dirs");
  63. }
  64. else
  65. {
  66. config_file = (char*) malloc (strlen (config_home) + strlen ("/user-dirs.dirs") + 1);
  67. if (config_file == NULL)
  68. goto error;
  69. strcpy (config_file, config_home);
  70. strcat (config_file, "/user-dirs.dirs");
  71. }
  72. file = fopen (config_file, "r");
  73. free (config_file);
  74. if (file == NULL)
  75. goto error;
  76. user_dir = NULL;
  77. while (fgets (buffer, sizeof (buffer), file))
  78. {
  79. /* Remove newline at end */
  80. len = strlen (buffer);
  81. if (len > 0 && buffer[len-1] == '\n')
  82. buffer[len-1] = 0;
  83. p = buffer;
  84. while (*p == ' ' || *p == '\t')
  85. p++;
  86. if (strncmp (p, "XDG_", 4) != 0)
  87. continue;
  88. p += 4;
  89. if (strncmp (p, type, strlen (type)) != 0)
  90. continue;
  91. p += strlen (type);
  92. if (strncmp (p, "_DIR", 4) != 0)
  93. continue;
  94. p += 4;
  95. while (*p == ' ' || *p == '\t')
  96. p++;
  97. if (*p != '=')
  98. continue;
  99. p++;
  100. while (*p == ' ' || *p == '\t')
  101. p++;
  102. if (*p != '"')
  103. continue;
  104. p++;
  105. relative = 0;
  106. if (strncmp (p, "$HOME/", 6) == 0)
  107. {
  108. p += 6;
  109. relative = 1;
  110. }
  111. else if (*p != '/')
  112. continue;
  113. if (relative)
  114. {
  115. user_dir = (char*) malloc (strlen (home_dir) + 1 + strlen (p) + 1);
  116. if (user_dir == NULL)
  117. goto error2;
  118. strcpy (user_dir, home_dir);
  119. strcat (user_dir, "/");
  120. }
  121. else
  122. {
  123. user_dir = (char*) malloc (strlen (p) + 1);
  124. if (user_dir == NULL)
  125. goto error2;
  126. *user_dir = 0;
  127. }
  128. d = user_dir + strlen (user_dir);
  129. while (*p && *p != '"')
  130. {
  131. if ((*p == '\\') && (*(p+1) != 0))
  132. p++;
  133. *d++ = *p++;
  134. }
  135. *d = 0;
  136. }
  137. error2:
  138. fclose (file);
  139. if (user_dir)
  140. return user_dir;
  141. error:
  142. if (fallback)
  143. return strdup (fallback);
  144. return NULL;
  145. }
  146. /**
  147. * xdg_user_dir_lookup:
  148. * @type: a string specifying the type of directory
  149. * @returns: a newly allocated absolute pathname
  150. *
  151. * Looks up a XDG user directory of the specified type.
  152. * Example of types are "DESKTOP" and "DOWNLOAD".
  153. *
  154. * The return value is always != NULL (unless out of memory),
  155. * and if a directory
  156. * for the type is not specified by the user the default
  157. * is the home directory. Except for DESKTOP which defaults
  158. * to ~/Desktop.
  159. *
  160. * The return value is newly allocated and must be freed with
  161. * free().
  162. **/
  163. char *
  164. xdg_user_dir_lookup (const char *type)
  165. {
  166. char *dir, *home_dir, *user_dir;
  167. dir = xdg_user_dir_lookup_with_fallback (type, NULL);
  168. if (dir != NULL)
  169. return dir;
  170. home_dir = getenv ("HOME");
  171. if (home_dir == NULL)
  172. return strdup ("/tmp");
  173. /* Special case desktop for historical compatibility */
  174. if (strcmp (type, "DESKTOP") == 0)
  175. {
  176. user_dir = (char*) malloc (strlen (home_dir) + strlen ("/Desktop") + 1);
  177. if (user_dir == NULL)
  178. return NULL;
  179. strcpy (user_dir, home_dir);
  180. strcat (user_dir, "/Desktop");
  181. return user_dir;
  182. }
  183. return strdup (home_dir);
  184. }
  185. #ifdef STANDALONE_XDG_USER_DIR_LOOKUP
  186. int
  187. main (int argc, char *argv[])
  188. {
  189. if (argc != 2)
  190. {
  191. fprintf (stderr, "Usage %s <dir-type>\n", argv[0]);
  192. exit (1);
  193. }
  194. printf ("%s\n", xdg_user_dir_lookup (argv[1]));
  195. return 0;
  196. }
  197. #endif