You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

os.cxx 2.2KB

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