1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: gethomedir.c,v 1.3 2019/07/21 16:48:34 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* gethomedir.c - search for a users home directory |
6 |
* |
7 |
*/ |
8 |
|
9 |
#include "copyright.h" |
10 |
|
11 |
#include <stdlib.h> |
12 |
|
13 |
#include "rtio.h" |
14 |
|
15 |
#if defined(_WIN32) || defined(_WIN64) |
16 |
|
17 |
char * |
18 |
gethomedir(char *uname, char *path, int plen) |
19 |
{ |
20 |
char *cd, *cp; |
21 |
|
22 |
if (uname == NULL || *uname == '\0') { /* ours */ |
23 |
/* pretend we're on unix first (eg. for Cygwin) */ |
24 |
if ((cp = getenv("HOME")) != NULL) { |
25 |
strlcpy(path, cp, plen); |
26 |
return path; |
27 |
} |
28 |
/* now let's see what Windows thinks */ |
29 |
if ((cd = getenv("HOMEDRIVE")) != NULL |
30 |
&& (cp = getenv("HOMEPATH")) != NULL) { |
31 |
strlcpy(path, cd, plen); |
32 |
strlcat(path, cp, plen); |
33 |
return path; |
34 |
} |
35 |
return NULL; |
36 |
} |
37 |
/* No idea how to find the home directory of another user */ |
38 |
return NULL; |
39 |
} |
40 |
|
41 |
#else /* _WIN32 || _WIN64 */ |
42 |
|
43 |
|
44 |
#include <unistd.h> |
45 |
#include <pwd.h> |
46 |
#include <sys/types.h> |
47 |
|
48 |
char * |
49 |
gethomedir(char *uname, char *path, int plen) |
50 |
{ |
51 |
struct passwd *pwent; |
52 |
uid_t uid; |
53 |
char *cp; |
54 |
|
55 |
if (uname == NULL || *uname == '\0') { /* ours */ |
56 |
if ((cp = getenv("HOME")) != NULL) { |
57 |
strlcpy(path, cp, plen); |
58 |
return path; |
59 |
} |
60 |
uid = getuid(); |
61 |
if ((pwent = getpwuid(uid)) == NULL) |
62 |
return(NULL); /* we don't exist ?!? */ |
63 |
strlcpy(path, pwent->pw_dir, plen); |
64 |
return path; |
65 |
} |
66 |
/* someone else */ |
67 |
if ((pwent = getpwnam(uname)) == NULL) |
68 |
return(NULL); /* no such user */ |
69 |
|
70 |
strlcpy(path, pwent->pw_dir, plen); |
71 |
return path; |
72 |
} |
73 |
|
74 |
#endif /* _WIN32 || _WIN64 */ |
75 |
|