chdir("~") to $HOME in C programming language?
Hello,
I would like to do some chdir to the ~ ($HOME) of the user account. I am using Debian (under Linux). http://stackoverflow.com/questions/9...home-directory not much answers that prompts to possible code :( Code:
chdir(getenv("HOME")) This works on all machines. One possible solution would be to do: Code:
system( "cd ; pwd > /tmp/home" ) ; Kind regards -- listening: http://www.youtube.com/watch?v=qycAC_6Bbto |
there are two choices: look for the implementation in bash (or whatever shell you prefer), you just need to take the source code.
second, open the /etc/passwd file and read that entry. |
Quote:
|
no, we do not need to know the name of the user, we always have the user id, that is enough for that
|
Using a pair of library functions, getuid() and getpwuid(), one can get the user's home directory.
Code:
#include <unistd.h> |
Quote:
Quote:
It sounds to me like you are trying to solve a problem that does not exist. If $HOME is not set in current environment just fail if user supplies paths starting with “~/” or use “/” as the default if you want. Whichever you prefer. Trying to manually figure out $HOME is waste of your time. |
Quote:
Quote:
I prefer the solution of dwhitney67 (#5) using getpwuid |
All times are GMT -5. The time now is 11:04 AM. |