linuxglue.c 6.7 KB

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