如何在程序中不使用环境变量 (getenv
, ...) 来获取实际的“用户名”?该程序是基于C/C++和Linux操作系统。
getlogin_r()
函数在unistd.h
中定义,返回用户名。有关更多信息,请参见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 "";
}
#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");
}
使用在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;
}
更多细节请查看:
cuserid()
函数已经从POSIX标准中删除(参见https://pubs.opengroup.org/onlinepubs/9699919799/functions/getlogin.html#tag_16_203_08):原先在POSIX.1-1988标准中定义的`cuserid()`函数提供的信息可以通过以下方式获取:`getpwuid(geteuid())`。 - Andrew Henle正在逐渐研究现代C++规范
static auto whoAmI = [](){ struct passwd *tmp = getpwuid (geteuid ());
return tmp ? tmp->pw_name : "onlyGodKnows";
}
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
// Return username or nullptr if unknown.
const char* username() {
const passwd* pw = getpwuid(geteuid());
return pw == nullptr ? nullptr : pw->pw_name;
}
#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
getlogin_r
的包装器而已。 - Matteo Italiagetlogin_r
编写一个C++封装(使用std::string
等),而不是C风格函数(使用char *
等)。 - Matteo Italiacstdlib
等是C++的头文件(在std
命名空间中提供遗留的C库)。但是,POSIX头文件不包含在标准C中,它们是操作系统API,旨在从多种语言(主要是C和C++)中使用。 - Matteo Italia