1 #if !defined(lint) && !defined(DOS)
2 static char rcsid
[] = "$Id: pw_stuff.c 763 2007-10-23 23:37:34Z hubert@u.washington.edu $";
6 * ========================================================================
7 * Copyright 2013-2018 Eduardo Chappa
8 * Copyright 2006 University of Washington
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
22 #include "../charconv/utf8.h"
23 #include "../charconv/filesys.h"
31 static char *gcos_name(char *, char *);
34 /*----------------------------------------------------------------------
35 Pull the name out of the gcos field if we have that sort of /etc/passwd
37 Args: gcos_field -- The long name or GCOS field to be parsed
38 logname -- Replaces occurances of & with logname string
40 Result: returns pointer to buffer with name
43 gcos_name(char *gcos_field
, char *logname
)
45 static char fullname
[MAX_FULLNAME
+1];
46 register char *fncp
, *gcoscp
, *lncp
, *end
;
49 * Full name is all chars up to first ',' (or whole gcos, if no ',').
50 * Replace any & with Logname.
53 for(fncp
= fullname
, gcoscp
= gcos_field
, end
= fullname
+ MAX_FULLNAME
;
54 (*gcoscp
!= ',' && *gcoscp
!= '\0' && fncp
< end
);
58 for(lncp
= logname
; *lncp
&& fncp
< end
; fncp
++, lncp
++)
59 *fncp
= (lncp
== logname
) ? toupper((unsigned char) (*lncp
))
69 #endif /* !_WINDOWS */
73 /*----------------------------------------------------------------------
74 Fill in homedir, login, and fullname for the logged in user.
75 These are all pointers to static storage so need to be copied
78 Args: ui -- struct pointer to pass back answers
80 Result: fills in the fields
83 get_user_info(struct user_info
*ui
)
86 struct passwd
*unix_pwd
;
88 unix_pwd
= getpwuid(getuid());
89 if(unix_pwd
== NULL
) {
90 ui
->homedir
= (char *) malloc(sizeof(char));
91 ui
->homedir
[0] = '\0';
92 ui
->login
= (char *) malloc(sizeof(char));
94 ui
->fullname
= (char *) malloc(sizeof(char));
95 ui
->fullname
[0] = '\0';
100 len
= strlen(fname_to_utf8(unix_pwd
->pw_dir
));
101 ui
->homedir
= (char *) malloc((len
+1) * sizeof(char));
102 snprintf(ui
->homedir
, len
+1, "%s", fname_to_utf8(unix_pwd
->pw_dir
));
104 len
= strlen(fname_to_utf8(unix_pwd
->pw_name
));
105 ui
->login
= (char *) malloc((len
+1) * sizeof(char));
106 snprintf(ui
->login
, len
+1, "%s", fname_to_utf8(unix_pwd
->pw_name
));
108 if((s
= gcos_name(unix_pwd
->pw_gecos
, unix_pwd
->pw_name
)) != NULL
){
109 len
= strlen(fname_to_utf8(s
));
110 ui
->fullname
= (char *) malloc((len
+1) * sizeof(char));
111 snprintf(ui
->fullname
, len
+1, "%s", fname_to_utf8(s
));
116 char buf
[_MAX_PATH
], *p
, *q
;
117 TCHAR lptstr_buf
[_MAX_PATH
];
120 if(GetUserName(lptstr_buf
, &len
))
121 ui
->login
= lptstr_to_utf8(lptstr_buf
);
123 ui
->login
= our_getenv("USERNAME");
125 if((p
= our_getenv("HOMEDRIVE"))
126 && (q
= our_getenv("HOMEPATH")))
127 snprintf(buf
, sizeof(buf
), "%s%s", p
, q
);
129 snprintf(buf
, sizeof(buf
), "%c:\\", '@' + _getdrive());
137 ui
->homedir
= (char *) malloc((strlen(buf
)+1) * sizeof(char));
139 strncpy(ui
->homedir
, buf
, strlen(buf
));
140 ui
->homedir
[strlen(buf
)] = '\0';
143 ui
->fullname
= (char *) malloc(sizeof(char));
145 ui
->fullname
[0] = '\0';
146 #endif /* _WINDOWS */
150 /*----------------------------------------------------------------------
151 Look up a userid on the local system and return rfc822 address
153 Args: name -- possible login name on local system
155 Result: returns NULL or pointer to alloc'd string rfc822 address.
158 local_name_lookup(char *name
)
161 struct passwd
*pw
= getpwnam(name
);
166 for(p
= name
; *p
; p
++)
167 if(isupper((unsigned char)*p
))
170 /* try changing it to all lower case */
175 snprintf(lcase
, sizeof(lcase
), "%s", name
);
178 for(p
= lcase
; *p
; p
++)
179 if(isupper((unsigned char)*p
))
180 *p
= tolower((unsigned char)*p
);
182 pw
= getpwnam(lcase
);
185 strncpy(name
, lcase
, l
+1);
195 if((gn
= gcos_name(pw
->pw_gecos
, name
)) != NULL
196 && (s
= (char *) malloc(l
= ((strlen(gn
) + 1) * sizeof(char)))) != NULL
)
197 snprintf(s
, l
, "%s", gn
);
202 return((char *) NULL
);
205 #endif /* _WINDOWS */