How can I find the user\'s home directory in a cross platform manner in C++? i.e. /home/user in Linux, C:\\Users\\user\\ on Windows Vista, C:\\Documents And Settings\\user\\
I don't think it's possible to completely hide the Windows/Unix divide with this one (unless, maybe, Boost has something).
The most portable way would have to be getenv("HOME")
on Unix and concatenating the results of getenv("HOMEDRIVE")
and getenv("HOMEPATH")
on Windows.
const static volatile char A = 'a'; // All this is to prevent reverse engineering
#ifdef unix
HomeDirectory = getenv((char[]){A-25, A-18, A-20, A-28, 0});
#elif defined(_WIN32)
HomeDirectory = getenv((char[]){A-25, A-18, A-20, A-28, A-29, A-15, A-24, A-11, A-28, 0});
const char*Homepath = getenv((char[]){A-25, A-18, A-20, A-28, A-17, A-32, A-13, A-25, 0});
HomeDirectory = malloc(strlen(HomeDirectory)+strlen(Homepath)+1);
strcat(HomeDirectory, Homepath);
#endif
The home directory isn't really a cross-platform concept. Your suggestion of the root of the profile directory (%USERPROFILE%) is a fair analogy, but depending what you want to do once you have the directory, you might want one of the Application Data directories, or the user's My Documents. On UNIX, you might create a hidden ".myapp" in the home directory to keep your files in, but that's not right on Windows.
Your best bet is to write specific code for each platform, to get at the directory you want in each case. Depending how correct you want to be, it might be enough to use env vars: HOME on UNIX, USERPROFILE or APPDATA (depending what you need) on Windows.
On UNIX at least (any Windows folks care to comment?), it's usually good practice to use the HOME environment variable if it's set, even if it disagrees with the directory specific in the password file. Then, on the odd occasion when users want all apps to read their data from a different directory, it will still work.
This is possible, and the best way to find it is to study the source code of os.path.expanduser("~")
, it is really easy to replicate the same functionality in C.
You'll have to add some #ifdef
directives to cover different systems.
Here are the rules that will provide you the HOME directory
USERPROFILE
or if this fails, concatenate HOMEDRIVE
+HOMEPATH
HOME
or if this fails, use getpwuid() (example code)Important remark: many people are assuming that HOME
environment variable is always available on Unix but this is not true, one good example would be OS X.
On OS X when you run an application from GUI (not console) this will not have this variable set so you need to use the getpwuid().