blob: 8d24875d7900cfcaf3c6057dc4d385fc7c3726ee [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>
26#include <pwd.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/types.h>
30#include <unistd.h>
31
32int gethomedir(char **dirp)
33{
34 char *homedir, *dir;
35 size_t len;
36 uid_t uid;
37 struct passwd *passwd;
38
39 assert(dirp != NULL && *dirp == NULL);
40
41#ifdef WIN32
42 /* Not supported, yet */
43 return -1;
44#endif
45
46 homedir = getenv("HOME");
47 if (homedir == NULL) {
48 uid = getuid();
49 passwd = getpwuid(uid);
50 if (passwd == NULL) {
51 /* Do we want emit error msg here? */
52 return -1;
53 }
54 homedir = passwd->pw_dir;
55 }
56
57 len = strlen(homedir) + 1;
58 dir = new char[len];
59 memcpy(dir, homedir, len);
60
61 *dirp = dir;
62 return 0;
63}
64