如何在Linux中用C/C++获取用户名?

问题描述 投票:0回答:7

如何在程序中不使用环境(

getenv
,...)来获取实际的“用户名”?环境是C/C++,Linux。

c++ c linux posix
7个回答
66
投票

getlogin_r()

中定义的函数
unistd.h
返回用户名。请参阅 
man getlogin_r
了解更多信息。

它的签名是:

int getlogin_r(char *buf, size_t bufsize);

不用说,这个函数可以很容易地在 C 或 C++ 中调用。


59
投票

来自 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 "";
}

3
投票
#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");
}

2
投票

使用

char *cuserid(char *s)
中找到的
stdio.h

#include <stdio.h>

#define MAX_USERID_LENGTH 32

int main()
{
  char username[MAX_USERID_LENGTH];
  cuserid(username);
  printf("%s\n", username);
  return 0;
}

更多详情请参阅:

  1. https://pubs.opengroup.org/onlinepubs/007908799/xsh/cuserid.html
  2. https://serverfault.com/questions/294121/what-is-the-maximum-username-length-on-current-gnu-linux-systems

1
投票

通过现代 C++ 规范进行一些工作

static auto whoAmI = [](){ struct passwd *tmp = getpwuid (geteuid ());
  return tmp ? tmp->pw_name : "onlyGodKnows"; 
}

0
投票
#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;
}

-2
投票

今天我必须做同样的事情,但不喜欢包含任何操作系统特定的标头。因此,您可以通过跨平台方式执行以下操作,而无需求助于任何 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

© www.soinside.com 2019 - 2024. All rights reserved.