ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/gethomedir.c
Revision: 1.1
Committed: Mon Oct 27 10:20:58 2003 UTC (20 years, 6 months ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad5R0, rad3R7P2, rad3R7P1, rad4R2, rad4R1, rad4R0, rad3R6, rad3R6P1, rad3R8, rad3R9, rad4R2P1
Log Message:
Now really adding gethomedir.c.

File Contents

# User Rev Content
1 schorsch 1.1 #ifndef lint
2     static const char RCSid[] = "$Id$";
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     #include <string.h>
13    
14     #include "rtio.h"
15    
16     #ifdef _WIN32
17    
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     #else /* _WIN32 */
45    
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     #endif /* _WIN32 */
81