在 Linux 中获取主目录

我需要一种在 Linux 上运行的 C + + 程序中获得用户主目录的方法。如果相同的代码可以在 Unix 上运行,那就太好了。我不想使用 HOME 环境值。

AFAIK,root 主目录是/root。如果我的程序是由 root 用户运行的,那么在这个目录中创建一些文件/文件夹可以吗?

96271 次浏览

You need getuid to get the user id of the current user and then getpwuid to get the password entry (which includes the home directory) of that user:

#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>


struct passwd *pw = getpwuid(getuid());


const char *homedir = pw->pw_dir;

Note: if you need this in a threaded application, you'll want to use getpwuid_r instead.

If you're running the program as root then you'll have rwx access to this directory. Creating stuff inside it is fine, i suppose.

You should first check the $HOME environment variable, and if that does not exist, use getpwuid.

#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>


const char *homedir;


if ((homedir = getenv("HOME")) == NULL) {
homedir = getpwuid(getuid())->pw_dir;
}

Also note, that if you want the home directory to store configuration or cache data as part of a program you write and want to distribute to users, you should consider following the XDG Base Directory Specification. For example if you want to create a configuration directory for your application, you should first check $XDG_CONFIG_HOME using getenv as shown above and only fall back to the code above if the variable is not set.

If you require multi-thread safety, you should use getpwuid_r instead of getpwuid like this (from the getpwnam(3) man page):

struct passwd pwd;
struct passwd *result;
char *buf;
size_t bufsize;
int s;
bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
if (bufsize == -1)
bufsize = 0x4000; // = all zeroes with the 14th bit set (1 << 14)
buf = malloc(bufsize);
if (buf == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
s = getpwuid_r(getuid(), &pwd, buf, bufsize, &result);
if (result == NULL) {
if (s == 0)
printf("Not found\n");
else {
errno = s;
perror("getpwnam_r");
}
exit(EXIT_FAILURE);
}
char *homedir = result.pw_dir;

you can get home dir in c++ use this code

std::string home_dir = getenv("HOME");