ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/gethomedir.c
Revision: 1.3
Committed: Sun Jul 21 16:48:34 2019 UTC (4 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.2: +7 -12 lines
Log Message:
Replaced strncpy() and strncat() with strlcpy() and strlcat() and other fixes

File Contents

# User Rev Content
1 schorsch 1.1 #ifndef lint
2 greg 1.3 static const char RCSid[] = "$Id: gethomedir.c,v 1.2 2016/03/06 01:13:17 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 greg 1.3 strlcpy(path, cp, plen);
27 schorsch 1.1 return path;
28     }
29     /* now let's see what Windows thinks */
30     if ((cd = getenv("HOMEDRIVE")) != NULL
31     && (cp = getenv("HOMEPATH")) != NULL) {
32 greg 1.3 strlcpy(path, cd, plen);
33     strlcat(path, cp, plen);
34 schorsch 1.1 return path;
35     }
36     return NULL;
37     }
38     /* No idea how to find the home directory of another user */
39     return NULL;
40     }
41    
42 schorsch 1.2 #else /* _WIN32 || _WIN64 */
43 schorsch 1.1
44    
45     #include <unistd.h>
46     #include <pwd.h>
47     #include <sys/types.h>
48    
49     char *
50     gethomedir(char *uname, char *path, int plen)
51     {
52     struct passwd *pwent;
53     uid_t uid;
54     char *cp;
55    
56     if (uname == NULL || *uname == '\0') { /* ours */
57     if ((cp = getenv("HOME")) != NULL) {
58 greg 1.3 strlcpy(path, cp, plen);
59 schorsch 1.1 return path;
60     }
61     uid = getuid();
62     if ((pwent = getpwuid(uid)) == NULL)
63     return(NULL); /* we don't exist ?!? */
64 greg 1.3 strlcpy(path, pwent->pw_dir, plen);
65 schorsch 1.1 return path;
66     }
67     /* someone else */
68     if ((pwent = getpwnam(uname)) == NULL)
69     return(NULL); /* no such user */
70    
71 greg 1.3 strlcpy(path, pwent->pw_dir, plen);
72 schorsch 1.1 return path;
73     }
74    
75 schorsch 1.2 #endif /* _WIN32 || _WIN64 */
76 schorsch 1.1