OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/username.h" | 5 #include "remoting/host/username.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 | 10 |
11 #if defined(OS_POSIX) | 11 #if defined(OS_POSIX) |
12 #include <pwd.h> | 12 #include <pwd.h> |
13 #include <sys/types.h> | 13 #include <sys/types.h> |
14 #include <unistd.h> | 14 #include <unistd.h> |
15 #endif // defined(OS_POSIX) | 15 #endif // defined(OS_POSIX) |
16 | 16 |
17 namespace remoting { | 17 namespace remoting { |
18 | 18 |
19 std::string GetUsername() { | 19 std::string GetUsername() { |
20 #if defined(OS_POSIX) | 20 #if defined(OS_POSIX) |
21 long buf_size = sysconf(_SC_GETPW_R_SIZE_MAX); | 21 long buf_size = sysconf(_SC_GETPW_R_SIZE_MAX); |
22 if (buf_size <= 0) | 22 if (buf_size <= 0) |
23 return std::string(); | 23 return std::string(); |
24 | 24 |
25 std::vector<char> buf(buf_size); | 25 std::vector<char> buf(buf_size); |
26 struct passwd passwd; | 26 struct passwd passwd; |
27 struct passwd* passwd_result = NULL; | 27 struct passwd* passwd_result = nullptr; |
28 getpwuid_r(getuid(), &passwd, &(buf[0]), buf_size, &passwd_result); | 28 getpwuid_r(getuid(), &passwd, &(buf[0]), buf_size, &passwd_result); |
29 return passwd_result ? passwd_result->pw_name : std::string(); | 29 return passwd_result ? passwd_result->pw_name : std::string(); |
30 #else // !defined(OS_POSIX) | 30 #else // !defined(OS_POSIX) |
31 NOTIMPLEMENTED(); | 31 NOTIMPLEMENTED(); |
32 return std::string(); | 32 return std::string(); |
33 #endif // defined(OS_POSIX) | 33 #endif // defined(OS_POSIX) |
34 } | 34 } |
35 | 35 |
36 } // namespace remoting | 36 } // namespace remoting |
OLD | NEW |