Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / toolkit / components / startup / src / nsUserInfoUnix.cpp
blobeddf0997d3acc4bfec868657e9fc7b4717de5329
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Mozilla Communicator client code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Seth Spitzer <sspitzer@netscape.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsUserInfo.h"
40 #include "nsCRT.h"
42 #include <pwd.h>
43 #include <sys/types.h>
44 #include <unistd.h>
45 #include <sys/utsname.h>
47 #include "nsString.h"
48 #include "nsXPIDLString.h"
49 #include "nsReadableUtils.h"
51 /* Some UNIXy platforms don't have pw_gecos. In this case we use pw_name */
52 #if defined(NO_PW_GECOS)
53 #define PW_GECOS pw_name
54 #else
55 #define PW_GECOS pw_gecos
56 #endif
58 nsUserInfo::nsUserInfo()
62 nsUserInfo::~nsUserInfo()
66 NS_IMPL_ISUPPORTS1(nsUserInfo,nsIUserInfo)
68 NS_IMETHODIMP
69 nsUserInfo::GetFullname(PRUnichar **aFullname)
71 struct passwd *pw = nsnull;
73 pw = getpwuid (geteuid());
75 if (!pw || !pw->PW_GECOS) return NS_ERROR_FAILURE;
77 #ifdef DEBUG_sspitzer
78 printf("fullname = %s\n", pw->PW_GECOS);
79 #endif
81 nsCAutoString fullname(pw->PW_GECOS);
83 // now try to parse the GECOS information, which will be in the form
84 // Full Name, <other stuff> - eliminate the ", <other stuff>
85 // also, sometimes GECOS uses "&" to mean "the user name" so do
86 // the appropriate substitution
88 // truncate at first comma (field delimiter)
89 PRInt32 index;
90 if ((index = fullname.Find(",")) != kNotFound)
91 fullname.Truncate(index);
93 // replace ampersand with username
94 if (pw->pw_name) {
95 nsCAutoString username(pw->pw_name);
96 if (!username.IsEmpty() && nsCRT::IsLower(username.CharAt(0)))
97 username.SetCharAt(nsCRT::ToUpper(username.CharAt(0)), 0);
99 fullname.ReplaceSubstring("&", username.get());
102 *aFullname = ToNewUnicode(fullname);
104 if (*aFullname)
105 return NS_OK;
107 return NS_ERROR_FAILURE;
110 NS_IMETHODIMP
111 nsUserInfo::GetUsername(char * *aUsername)
113 struct passwd *pw = nsnull;
115 // is this portable? those are POSIX compliant calls, but I need to check
116 pw = getpwuid(geteuid());
118 if (!pw || !pw->pw_name) return NS_ERROR_FAILURE;
120 #ifdef DEBUG_sspitzer
121 printf("username = %s\n", pw->pw_name);
122 #endif
124 *aUsername = nsCRT::strdup(pw->pw_name);
126 return NS_OK;
129 NS_IMETHODIMP
130 nsUserInfo::GetDomain(char * *aDomain)
132 nsresult rv = NS_ERROR_FAILURE;
134 struct utsname buf;
135 char *domainname = nsnull;
137 // is this portable? that is a POSIX compliant call, but I need to check
138 if (uname(&buf)) {
139 return rv;
142 #if defined(HAVE_UNAME_DOMAINNAME_FIELD)
143 domainname = buf.domainname;
144 #elif defined(HAVE_UNAME_US_DOMAINNAME_FIELD)
145 domainname = buf.__domainname;
146 #endif
148 if (domainname && domainname[0]) {
149 *aDomain = nsCRT::strdup(domainname);
150 rv = NS_OK;
152 else {
153 // try to get the hostname from the nodename
154 // on machines that use DHCP, domainname may not be set
155 // but the nodename might.
156 if (buf.nodename && buf.nodename[0]) {
157 // if the nodename is foo.bar.org, use bar.org as the domain
158 char *pos = strchr(buf.nodename,'.');
159 if (pos) {
160 *aDomain = nsCRT::strdup(pos+1);
161 rv = NS_OK;
166 return rv;
169 NS_IMETHODIMP
170 nsUserInfo::GetEmailAddress(char * *aEmailAddress)
172 // use username + "@" + domain for the email address
174 nsresult rv;
176 nsCAutoString emailAddress;
177 nsXPIDLCString username;
178 nsXPIDLCString domain;
180 rv = GetUsername(getter_Copies(username));
181 if (NS_FAILED(rv)) return rv;
183 rv = GetDomain(getter_Copies(domain));
184 if (NS_FAILED(rv)) return rv;
186 if (!username.IsEmpty() && !domain.IsEmpty()) {
187 emailAddress = (const char *)username;
188 emailAddress += "@";
189 emailAddress += (const char *)domain;
191 else {
192 return NS_ERROR_FAILURE;
195 *aEmailAddress = ToNewCString(emailAddress);
197 return NS_OK;