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\ on Windows XP, and whatever it is that Macs use. (I think it's /User/user)
Basically, what I'm looking for is a C++ way of doing this (example in python)
os.path.expanduser("~")
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, concatenateHOMEDRIVE
+HOMEPATH
HOME
or if this fails, usegetpwuid()
(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().
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 ofgetenv("HOMEDRIVE")
andgetenv("HOMEPATH")
on Windows.