I wrote a shell and whenever user gives an argument like cd -blabla it changes directory to bla bla.But when I enter cd I want it to go to my home directory.Assigning my home directory is easy.I just wrote whereis home to terminal to find my home directory but I can change my home directory.If I enter my adress into the function chdir() manually then it will always change to that directory.How can I find the most updated home directory in C?(which changes everytime I change my home directory)
Asked
Active
Viewed 1,466 times
0
-
This is a duplicate of [this question](http://stackoverflow.com/questions/2910377/get-home-directory-in-linux-c) – JJF Mar 12 '16 at 14:49
-
1What "most updated home directory" means ??? Home directory is not something that changes frequently, it is usually considered as constant over time. Anyway, either you can get the `HOME` environment variable or read `passwd` db to get the user dir entry. – Jean-Baptiste Yunès Mar 12 '16 at 16:36
1 Answers
0
You should look at pwd.h
. You can get information about a user by the UID or the name.
Example by id
struct passwd *info = getpwuid(1000);
Change 1000 to the UID you need. You could use getuid() defined in unistd.h to get the current user.
The field
info->pw_dir
Contains the home path of the user.

mame98
- 1,271
- 12
- 26