Is there a standard way of determining file locations under Linux? Even better, are there any POSIX API’s which allow the retrieval of standard file locations?
For example, how can I determine a user’s home directory? Or, how can I determine the proper location for system configuration files?
I know that typically these locations would be “/home/username” or “/etc/”. Should I just hardcode the paths as such?
The path to the current user’s home directory is in the environment variable
HOME. (I know systems where home dirs are spread over several partitions (say,/vol/vol[number]/[first letter]/[user name]) and not located in/home/.)For other users, there’s
getpwent(andgetpwent_r), which pull the home directory from the passwd entry.For the other directories, there is the File System Hierarchy Standard, which most Linux distros adhere to and some other OSen as well.
I don’t think there’s an API for this. Thus, if a system does things differently, you’re on your own — good luck! 😉