Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the user's home dir in a cross platform manner, using C++?

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("~") 
like image 970
Macha Avatar asked Mar 31 '10 11:03

Macha


1 Answers

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 
like image 112
Marcelo Cantos Avatar answered Oct 04 '22 13:10

Marcelo Cantos