blob: c08b57289c46adef28511779e008a48022b138ca [file] [log] [blame]
Adam Tkacb56a69c2010-10-29 12:07:14 +00001/* 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 Tkacb8ec9e82010-11-25 15:07:35 +000026
27#ifndef WIN32
Adam Tkacb56a69c2010-10-29 12:07:14 +000028#include <pwd.h>
29#include <stdlib.h>
30#include <string.h>
31#include <sys/types.h>
32#include <unistd.h>
Adam Tkacb8ec9e82010-11-25 15:07:35 +000033#else
34#include <windows.h>
35#include <wininet.h> /* MinGW needs it */
36#include <shlobj.h>
37#endif
Adam Tkacb56a69c2010-10-29 12:07:14 +000038
Adam Tkacaf081722011-02-07 10:45:15 +000039int getvnchomedir(char **dirp)
Adam Tkacb56a69c2010-10-29 12:07:14 +000040{
Adam Tkacb8ec9e82010-11-25 15:07:35 +000041#ifndef WIN32
Adam Tkacb56a69c2010-10-29 12:07:14 +000042 char *homedir, *dir;
43 size_t len;
44 uid_t uid;
45 struct passwd *passwd;
Adam Tkacb8ec9e82010-11-25 15:07:35 +000046#else
47 TCHAR *dir;
48 BOOL ret;
49#endif
Adam Tkacb56a69c2010-10-29 12:07:14 +000050
51 assert(dirp != NULL && *dirp == NULL);
52
Adam Tkacb8ec9e82010-11-25 15:07:35 +000053#ifndef WIN32
Adam Tkacb56a69c2010-10-29 12:07:14 +000054 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 Tkacaf081722011-02-07 10:45:15 +000065 len = strlen(homedir);
66 dir = new char[len+7];
Adam Tkacb8ec9e82010-11-25 15:07:35 +000067 if (dir == NULL)
68 return -1;
69
Adam Tkacb56a69c2010-10-29 12:07:14 +000070 memcpy(dir, homedir, len);
Adam Tkacaf081722011-02-07 10:45:15 +000071 memcpy(dir + len, "/.vnc/\0", 7);
Adam Tkacb8ec9e82010-11-25 15:07:35 +000072#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 Tkacaf081722011-02-07 10:45:15 +000082 memcpy(dir+strlen(dir), (TCHAR *)"\\vnc\\\0", 6);
Adam Tkacb8ec9e82010-11-25 15:07:35 +000083#endif
Adam Tkacb56a69c2010-10-29 12:07:14 +000084 *dirp = dir;
85 return 0;
86}
87