Platform-independent way to get the user's home directory in C++

902 Views Asked by At

Currently, when I want to get the user's home directory in C++, I do:

#include <pwd.h>

const char* get_home_directory() {
    struct passwd *pw = getpwuid(getuid());
    return (pw == nullptr ? nullptr : pw->pw_dir);
}

but this relies on getpwuid, which is a POSIX / BSD library function; and it's C rather than C++. Is there a platform-independent, hopefuly more C++'ish way to do the same?

Note: A commonly-used out-of-standard library, even non-Boost, could also work. It would have to support a wide range of platforms though.

0

There are 0 best solutions below