2 * ========================================================================
3 * Copyright 2013-2022 Eduardo Chappa
4 * Copyright 2006 University of Washington
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * ========================================================================
18 #include "../charconv/utf8.h"
19 #include "../charconv/filesys.h"
27 static char *gcos_name(char *, char *);
30 /*----------------------------------------------------------------------
31 Pull the name out of the gcos field if we have that sort of /etc/passwd
33 Args: gcos_field -- The long name or GCOS field to be parsed
34 logname -- Replaces occurrences of & with logname string
36 Result: returns pointer to buffer with name
39 gcos_name(char *gcos_field
, char *logname
)
41 static char fullname
[MAX_FULLNAME
+1];
42 register char *fncp
, *gcoscp
, *lncp
, *end
;
45 * Full name is all chars up to first ',' (or whole gcos, if no ',').
46 * Replace any & with Logname.
49 for(fncp
= fullname
, gcoscp
= gcos_field
, end
= fullname
+ MAX_FULLNAME
;
50 (*gcoscp
!= ',' && *gcoscp
!= '\0' && fncp
< end
);
54 for(lncp
= logname
; *lncp
&& fncp
< end
; fncp
++, lncp
++)
55 *fncp
= (lncp
== logname
) ? toupper((unsigned char) (*lncp
))
65 #endif /* !_WINDOWS */
69 /*----------------------------------------------------------------------
70 Fill in homedir, login, and fullname for the logged in user.
71 These are all pointers to static storage so need to be copied
74 Args: ui -- struct pointer to pass back answers
76 Result: fills in the fields
79 get_user_info(struct user_info
*ui
)
82 struct passwd
*unix_pwd
;
84 unix_pwd
= getpwuid(getuid());
85 if(unix_pwd
== NULL
) {
86 ui
->homedir
= (char *) malloc(sizeof(char));
87 ui
->homedir
[0] = '\0';
88 ui
->login
= (char *) malloc(sizeof(char));
90 ui
->fullname
= (char *) malloc(sizeof(char));
91 ui
->fullname
[0] = '\0';
96 len
= strlen(fname_to_utf8(unix_pwd
->pw_dir
));
97 ui
->homedir
= (char *) malloc((len
+1) * sizeof(char));
98 snprintf(ui
->homedir
, len
+1, "%s", fname_to_utf8(unix_pwd
->pw_dir
));
100 len
= strlen(fname_to_utf8(unix_pwd
->pw_name
));
101 ui
->login
= (char *) malloc((len
+1) * sizeof(char));
102 snprintf(ui
->login
, len
+1, "%s", fname_to_utf8(unix_pwd
->pw_name
));
104 if((s
= gcos_name(unix_pwd
->pw_gecos
, unix_pwd
->pw_name
)) != NULL
){
105 len
= strlen(fname_to_utf8(s
));
106 ui
->fullname
= (char *) malloc((len
+1) * sizeof(char));
107 snprintf(ui
->fullname
, len
+1, "%s", fname_to_utf8(s
));
112 char buf
[_MAX_PATH
], *p
, *q
;
113 TCHAR lptstr_buf
[_MAX_PATH
];
116 if(GetUserName(lptstr_buf
, &len
))
117 ui
->login
= lptstr_to_utf8(lptstr_buf
);
119 ui
->login
= our_getenv("USERNAME");
121 if((p
= our_getenv("HOMEDRIVE"))
122 && (q
= our_getenv("HOMEPATH")))
123 snprintf(buf
, sizeof(buf
), "%s%s", p
, q
);
125 snprintf(buf
, sizeof(buf
), "%c:\\", '@' + _getdrive());
133 ui
->homedir
= (char *) malloc((strlen(buf
)+1) * sizeof(char));
135 strncpy(ui
->homedir
, buf
, strlen(buf
));
136 ui
->homedir
[strlen(buf
)] = '\0';
139 ui
->fullname
= (char *) malloc(sizeof(char));
141 ui
->fullname
[0] = '\0';
142 #endif /* _WINDOWS */
146 /*----------------------------------------------------------------------
147 Look up a userid on the local system and return rfc822 address
149 Args: name -- possible login name on local system
151 Result: returns NULL or pointer to alloc'd string rfc822 address.
154 local_name_lookup(char *name
)
157 struct passwd
*pw
= getpwnam(name
);
162 for(p
= name
; *p
; p
++)
163 if(isupper((unsigned char)*p
))
166 /* try changing it to all lower case */
171 snprintf(lcase
, sizeof(lcase
), "%s", name
);
174 for(p
= lcase
; *p
; p
++)
175 if(isupper((unsigned char)*p
))
176 *p
= tolower((unsigned char)*p
);
178 pw
= getpwnam(lcase
);
181 strncpy(name
, lcase
, l
+1);
191 if((gn
= gcos_name(pw
->pw_gecos
, name
)) != NULL
192 && (s
= (char *) malloc(l
= ((strlen(gn
) + 1) * sizeof(char)))) != NULL
)
193 snprintf(s
, l
, "%s", gn
);
198 return((char *) NULL
);
201 #endif /* _WINDOWS */