[英]How to get the username in C/C++ in Linux?
如何在不使用程序中的环境( getenv
,...)的情况下获取实际的“用户名”? 环境是 C/C++ 和 Linux。
unistd.h
定义的函数getlogin_r()
返回用户名。 有关更多信息,请参阅man getlogin_r
。
它的签名是:
int getlogin_r(char *buf, size_t bufsize);
不用说,这个函数可以很容易地在 C 或 C++ 中调用。
从http://www.unix.com/programming/21041-getting-username-c-program-unix.html :
/* whoami.c */
#define _PROGRAM_NAME "whoami"
#include <stdlib.h>
#include <pwd.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
register struct passwd *pw;
register uid_t uid;
int c;
uid = geteuid ();
pw = getpwuid (uid);
if (pw)
{
puts (pw->pw_name);
exit (EXIT_SUCCESS);
}
fprintf (stderr,"%s: cannot find username for UID %u\n",
_PROGRAM_NAME, (unsigned) uid);
exit (EXIT_FAILURE);
}
只需将主线封装在类中即可:
class Env{
public:
static std::string getUserName()
{
uid_t uid = geteuid ();
struct passwd *pw = getpwuid (uid);
if (pw)
{
return std::string(pw->pw_name);
}
return {};
}
};
仅适用于 C:
const char *getUserName()
{
uid_t uid = geteuid();
struct passwd *pw = getpwuid(uid);
if (pw)
{
return pw->pw_name;
}
return "";
}
使用在stdio.h
找到的char *cuserid(char *s)
。
#include <stdio.h>
#define MAX_USERID_LENGTH 32
int main()
{
char username[MAX_USERID_LENGTH];
cuserid(username);
printf("%s\n", username);
return 0;
}
查看更多详情:
#include <iostream>
#include <unistd.h>
int main()
{
std::string Username = getlogin();
std::cout << Username << std::endl;
return 0 ;
}
另一种方式是这样——
#include <iostream>
using namespace std;
int main()
{
cout << system("whoami");
}
通过现代 C++ 规范进行一些工作
static auto whoAmI = [](){ struct passwd *tmp = getpwuid (geteuid ());
return tmp ? tmp->pw_name : "onlyGodKnows";
}
今天我不得不做同样的事情,但不喜欢包含任何操作系统特定的头文件。 因此,您可以跨平台方式执行以下操作,而无需求助于任何 Linux/Windows 特定标头:
#include <stdio.h>
#include <memory>
#include <stdexcept>
#include <array>
#include <regex>
std::string execute_command(std::string cmd)
{
std::array<char, 128> buffer;
std::string result;
#if defined(_WIN32)
#define POPEN _popen
#define PCLOSE _pclose
#elif defined(unix) || defined(__unix__) || defined(__unix)
#define POPEN popen
#define PCLOSE pclose
#endif
std::unique_ptr<FILE, decltype(&PCLOSE)> pipe(POPEN(cmd.c_str(), "r"), PCLOSE);
if (!pipe)
{
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
{
result += buffer.data();
}
return result;
}
std::string get_current_username()
{
#if defined(_WIN32)
// whoami works on windows as well but it returns the name
// in the format of `computer_name\user_name` so instead
// we use %USERNAME% which gives us the exact username.
#define USERNAME_QUERY "echo %USERNAME%"
#elif defined(unix) || defined(__unix__) || defined(__unix)
#define USERNAME_QUERY "whoami"
#endif
auto username = execute_command(USERNAME_QUERY);
// this line removes the white spaces (such as newline, etc)
// from the username.
username = std::regex_replace(username, std::regex("\\s"), "");
return username;
}
这在 Linux 和 Windows 上都可以正常工作,并且与 C++11 兼容!
在线测试: https : //paiza.io/projects/e/xmBuf3rD7MhYca02v5V2dw?theme=twilight
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.