blob: 5d41055349d0cb261f7625fb10f2a98d81637922 [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
Adam Tkacd6276672010-10-29 12:57:35 +000023#ifndef WIN32
Adam Tkacb56a69c2010-10-29 12:07:14 +000024#include <os/os.h>
25
26#include <assert.h>
27#include <pwd.h>
28#include <stdlib.h>
29#include <string.h>
30#include <sys/types.h>
31#include <unistd.h>
32
33int gethomedir(char **dirp)
34{
35 char *homedir, *dir;
36 size_t len;
37 uid_t uid;
38 struct passwd *passwd;
39
40 assert(dirp != NULL && *dirp == NULL);
41
42#ifdef WIN32
43 /* Not supported, yet */
44 return -1;
45#endif
46
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
58 len = strlen(homedir) + 1;
59 dir = new char[len];
60 memcpy(dir, homedir, len);
61
62 *dirp = dir;
63 return 0;
64}
65
Adam Tkacd6276672010-10-29 12:57:35 +000066#endif
67