* New version 2.26
[alpine.git] / pith / osdep / pw_stuff.c
blobba42571bbcd73d94f6d32d66eaccac71fde78620
1 /*
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 * ========================================================================
15 #include <system.h>
16 #include <general.h>
18 #include "../charconv/utf8.h"
19 #include "../charconv/filesys.h"
20 #include "pw_stuff.h"
23 * internal prototypes
25 #ifndef _WINDOWS
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
37 ----*/
38 static char *
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);
51 gcoscp++){
53 if(*gcoscp == '&'){
54 for(lncp = logname; *lncp && fncp < end; fncp++, lncp++)
55 *fncp = (lncp == logname) ? toupper((unsigned char) (*lncp))
56 : (*lncp);
57 }else
58 *fncp++ = *gcoscp;
61 *fncp = '\0';
62 return(fullname);
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
72 in the caller.
74 Args: ui -- struct pointer to pass back answers
76 Result: fills in the fields
77 ----*/
78 void
79 get_user_info(struct user_info *ui)
81 #ifndef _WINDOWS
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));
89 ui->login[0] = '\0';
90 ui->fullname = (char *) malloc(sizeof(char));
91 ui->fullname[0] = '\0';
92 }else {
93 char *s;
94 size_t len;
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));
111 #else /* _WINDOWS */
112 char buf[_MAX_PATH], *p, *q;
113 TCHAR lptstr_buf[_MAX_PATH];
114 int len = _MAX_PATH;
116 if(GetUserName(lptstr_buf, &len))
117 ui->login = lptstr_to_utf8(lptstr_buf);
118 else
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);
124 else
125 snprintf(buf, sizeof(buf), "%c:\\", '@' + _getdrive());
127 if(p)
128 free((void *)p);
130 if(q)
131 free((void *)q);
133 ui->homedir = (char *) malloc((strlen(buf)+1) * sizeof(char));
134 if(ui->homedir){
135 strncpy(ui->homedir, buf, strlen(buf));
136 ui->homedir[strlen(buf)] = '\0';
139 ui->fullname = (char *) malloc(sizeof(char));
140 if(ui->fullname)
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.
152 ----*/
153 char *
154 local_name_lookup(char *name)
156 #ifndef _WINDOWS
157 struct passwd *pw = getpwnam(name);
159 if(pw == NULL){
160 char *p;
162 for(p = name; *p; p++)
163 if(isupper((unsigned char)*p))
164 break;
166 /* try changing it to all lower case */
167 if(p && *p){
168 char lcase[256];
169 size_t l;
171 snprintf(lcase, sizeof(lcase), "%s", name);
173 l = strlen(name);
174 for(p = lcase; *p; p++)
175 if(isupper((unsigned char)*p))
176 *p = tolower((unsigned char)*p);
178 pw = getpwnam(lcase);
180 if(pw){
181 strncpy(name, lcase, l+1);
182 name[l] = '\0';
187 if(pw != NULL){
188 char *gn, *s = NULL;
189 size_t l;
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);
195 return(s);
197 else
198 return((char *) NULL);
199 #else /* _WINDOWS */
200 return(NULL);
201 #endif /* _WINDOWS */