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