views:

208

answers:

3

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("~")
+6  A: 

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.

Marcelo Cantos
+1 This is more or less what Qt does in `QDir::homepath()` does: http://doc.trolltech.com/4.6/qdir.html#homePath. Probably check their source, too.
stephan
Note really. QT apparently returns `CSIDL_PROFILE`. And on my PC, that's a different directory. (`%HOMEDRIVE%` is on the fileserver, `CSIDL_PROFILE` isn't.)
MSalters
@MSalters: just checked the source of my slightly dated Qt installation: on Windows, checks "HOME", "USERPROFILE", "HOMEDRIVE". On Unix, just tries "HOME". If not set, returns the root path.
stephan
A: 

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.

Andy Mortimer
+2  A: 

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

  • Windows: env USERPROFILE or if this fails, concatenate HOMEDRIVE+HOMEPATH
  • Linux, Unix and OS X: env HOME or if this fails, use getpwuid() - the link is to some code :D

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().

Sorin Sbarnea