Adam Tkac | b56a69c | 2010-10-29 12:07:14 +0000 | [diff] [blame] | 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 | |
| 19 | #ifdef HAVE_CONFIG_H |
| 20 | #include <config.h> |
| 21 | #endif |
| 22 | |
| 23 | #include <os/os.h> |
| 24 | |
| 25 | #include <assert.h> |
Adam Tkac | b8ec9e8 | 2010-11-25 15:07:35 +0000 | [diff] [blame] | 26 | |
| 27 | #ifndef WIN32 |
Adam Tkac | b56a69c | 2010-10-29 12:07:14 +0000 | [diff] [blame] | 28 | #include <pwd.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <unistd.h> |
Adam Tkac | b8ec9e8 | 2010-11-25 15:07:35 +0000 | [diff] [blame] | 33 | #else |
| 34 | #include <windows.h> |
| 35 | #include <wininet.h> /* MinGW needs it */ |
| 36 | #include <shlobj.h> |
| 37 | #endif |
Adam Tkac | b56a69c | 2010-10-29 12:07:14 +0000 | [diff] [blame] | 38 | |
Adam Tkac | af08172 | 2011-02-07 10:45:15 +0000 | [diff] [blame] | 39 | int getvnchomedir(char **dirp) |
Adam Tkac | b56a69c | 2010-10-29 12:07:14 +0000 | [diff] [blame] | 40 | { |
Adam Tkac | b8ec9e8 | 2010-11-25 15:07:35 +0000 | [diff] [blame] | 41 | #ifndef WIN32 |
Adam Tkac | b56a69c | 2010-10-29 12:07:14 +0000 | [diff] [blame] | 42 | char *homedir, *dir; |
| 43 | size_t len; |
| 44 | uid_t uid; |
| 45 | struct passwd *passwd; |
Adam Tkac | b8ec9e8 | 2010-11-25 15:07:35 +0000 | [diff] [blame] | 46 | #else |
| 47 | TCHAR *dir; |
| 48 | BOOL ret; |
| 49 | #endif |
Adam Tkac | b56a69c | 2010-10-29 12:07:14 +0000 | [diff] [blame] | 50 | |
| 51 | assert(dirp != NULL && *dirp == NULL); |
| 52 | |
Adam Tkac | b8ec9e8 | 2010-11-25 15:07:35 +0000 | [diff] [blame] | 53 | #ifndef WIN32 |
Adam Tkac | b56a69c | 2010-10-29 12:07:14 +0000 | [diff] [blame] | 54 | homedir = getenv("HOME"); |
| 55 | if (homedir == NULL) { |
| 56 | uid = getuid(); |
| 57 | passwd = getpwuid(uid); |
| 58 | if (passwd == NULL) { |
| 59 | /* Do we want emit error msg here? */ |
| 60 | return -1; |
| 61 | } |
| 62 | homedir = passwd->pw_dir; |
| 63 | } |
| 64 | |
Adam Tkac | af08172 | 2011-02-07 10:45:15 +0000 | [diff] [blame] | 65 | len = strlen(homedir); |
| 66 | dir = new char[len+7]; |
Adam Tkac | b8ec9e8 | 2010-11-25 15:07:35 +0000 | [diff] [blame] | 67 | if (dir == NULL) |
| 68 | return -1; |
| 69 | |
Adam Tkac | b56a69c | 2010-10-29 12:07:14 +0000 | [diff] [blame] | 70 | memcpy(dir, homedir, len); |
Adam Tkac | af08172 | 2011-02-07 10:45:15 +0000 | [diff] [blame] | 71 | memcpy(dir + len, "/.vnc/\0", 7); |
Adam Tkac | b8ec9e8 | 2010-11-25 15:07:35 +0000 | [diff] [blame] | 72 | #else |
| 73 | dir = new TCHAR[MAX_PATH]; |
| 74 | if (dir == NULL) |
| 75 | return -1; |
| 76 | |
| 77 | ret = SHGetSpecialFolderPath(NULL, dir, CSIDL_APPDATA, FALSE); |
| 78 | if (ret == FALSE) { |
| 79 | delete [] dir; |
| 80 | return -1; |
| 81 | } |
Adam Tkac | af08172 | 2011-02-07 10:45:15 +0000 | [diff] [blame] | 82 | memcpy(dir+strlen(dir), (TCHAR *)"\\vnc\\\0", 6); |
Adam Tkac | b8ec9e8 | 2010-11-25 15:07:35 +0000 | [diff] [blame] | 83 | #endif |
Adam Tkac | b56a69c | 2010-10-29 12:07:14 +0000 | [diff] [blame] | 84 | *dirp = dir; |
| 85 | return 0; |
| 86 | } |
| 87 | |