Reputation: 23
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?
Upvotes: 2
Views: 118
Reputation: 24393
I know that you didn't ask this, however if you're looking to find the location of an executable, you can use which
Upvotes: -1
Reputation: 231293
The current user's home directory can be found in the HOME environment variable. For other users, you can use the getpwnam or getpwuid functions (or the _r variants) to look up another specified user's home directory, among other things.
Upvotes: 1
Reputation: 4264
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
(and getpwent_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! ;-)
Upvotes: 5