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

不想你离开。 提交于 2019-11-27 10:31:43

问题


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("~")

回答1:


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



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/2552416/how-can-i-find-the-users-home-dir-in-a-cross-platform-manner-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!