在 Linux 中获取主目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2910377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get home directory in Linux
提问by Alex F
I need a way to get user home directory in C++ program running on Linux. If the same code works on Unix, it would be nice. I don't want to use HOME environment value.
我需要一种方法来在 Linux 上运行的 C++ 程序中获取用户主目录。如果相同的代码可以在 Unix 上运行,那就太好了。我不想使用 HOME 环境值。
AFAIK, root home directory is /root. Is it OK to create some files/folders in this directory, in the case my program is running by root user?
AFAIK,根主目录是/root。如果我的程序由 root 用户运行,是否可以在此目录中创建一些文件/文件夹?
采纳答案by R Samuel Klatchko
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:
您需要getuid
获取当前用户的用户 ID,然后getpwuid
获取该用户的密码条目(包括主目录):
#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.
注意:如果您在线程应用程序中需要它,您将需要使用它getpwuid_r
。
回答by Anthony
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.
如果您以 root 身份运行该程序,那么您将拥有对该目录的 rwx 访问权限。我想在里面创造东西很好。
回答by josch
You should first check the $HOME
environment variable, and if that does not exist, use getpwuid.
您应该首先检查$HOME
环境变量,如果该变量不存在,请使用 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.
另请注意,如果您希望主目录作为您编写的程序的一部分存储配置或缓存数据并希望分发给用户,您应该考虑遵循XDG 基本目录规范。例如,如果您想为您的应用程序创建一个配置目录,您应该首先检查$XDG_CONFIG_HOME
usinggetenv
如上所示,如果未设置变量,则仅回退到上面的代码。
If you require multi-thread safety, you should use getpwuid_r
instead of getpwuid
like this (from the getpwnam(3)
man page):
如果您需要多线程安全,您应该使用getpwuid_r
而不是getpwuid
这样(来自getpwnam(3)
手册页):
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;