Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Copyright (C) 2010 TightVNC Team. All Rights Reserved.
  2. * Copyright 2021-2023 Pierre Ossman for Cendio AB
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <os/os.h>
  23. #include <assert.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #ifndef WIN32
  27. #include <pwd.h>
  28. #include <limits.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #else
  34. #include <windows.h>
  35. #include <wininet.h> /* MinGW needs it */
  36. #include <shlobj.h>
  37. #define stat _stat
  38. #endif
  39. static const char* getvncdir(bool userDir, const char *xdg_env, const char *xdg_def)
  40. {
  41. static char dir[PATH_MAX], legacy[PATH_MAX];
  42. struct stat st;
  43. #ifndef WIN32
  44. char *homedir, *xdgdir;
  45. uid_t uid;
  46. struct passwd *passwd;
  47. #else
  48. BOOL ret;
  49. #endif
  50. #ifndef WIN32
  51. homedir = getenv("HOME");
  52. if (homedir == NULL) {
  53. uid = getuid();
  54. passwd = getpwuid(uid);
  55. if (passwd == NULL) {
  56. /* Do we want emit error msg here? */
  57. return NULL;
  58. }
  59. homedir = passwd->pw_dir;
  60. }
  61. if (userDir)
  62. return homedir;
  63. xdgdir = getenv(xdg_env);
  64. if (xdgdir != NULL && xdgdir[0] == '/')
  65. snprintf(dir, sizeof(dir), "%s/tigervnc", xdgdir);
  66. else
  67. snprintf(dir, sizeof(dir), "%s/%s/tigervnc", homedir, xdg_def);
  68. snprintf(legacy, sizeof(legacy), "%s/.vnc", homedir);
  69. #else
  70. (void) xdg_def;
  71. (void) xdg_env;
  72. if (userDir)
  73. ret = SHGetSpecialFolderPath(NULL, dir, CSIDL_PROFILE, FALSE);
  74. else
  75. ret = SHGetSpecialFolderPath(NULL, dir, CSIDL_APPDATA, FALSE);
  76. if (ret == FALSE)
  77. return NULL;
  78. if (userDir)
  79. return dir;
  80. ret = SHGetSpecialFolderPath(NULL, legacy, CSIDL_APPDATA, FALSE);
  81. if (ret == FALSE)
  82. return NULL;
  83. if (strlen(dir) + strlen("\\TigerVNC") >= sizeof(dir))
  84. return NULL;
  85. if (strlen(legacy) + strlen("\\vnc") >= sizeof(legacy))
  86. return NULL;
  87. strcat(dir, "\\TigerVNC");
  88. strcat(legacy, "\\vnc");
  89. #endif
  90. return (stat(dir, &st) != 0 && stat(legacy, &st) == 0) ? legacy : dir;
  91. }
  92. const char* os::getuserhomedir()
  93. {
  94. return getvncdir(true, NULL, NULL);
  95. }
  96. const char* os::getvncconfigdir()
  97. {
  98. return getvncdir(false, "XDG_CONFIG_HOME", ".config");
  99. }
  100. const char* os::getvncdatadir()
  101. {
  102. return getvncdir(false, "XDG_DATA_HOME", ".local/share");
  103. }
  104. const char* os::getvncstatedir()
  105. {
  106. return getvncdir(false, "XDG_STATE_HOME", ".local/state");
  107. }