ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
well, reading the /etc/passwd is nice and universal, but well, you need to know the "another" variable in that case : $USER
This works on all machines. One possible solution would be to do:
Code:
system( "cd ; pwd > /tmp/home" ) ;
fp = fopen("/tmp/home" ,.... )
and read it ...
Newsflash: shell's cd relies on $HOME (if called with no arguments) as well so it's as reliable as chdir(getenv("HOME"));.
Quote:
Originally Posted by patrick295767
reading the /etc/passwd is nice and universal
No it isn't. /etc/passwd can be nearly empty and does not have to contain current user.
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.
/etc/passwd can be nearly empty and does not have to contain current user.
Yes, you are right, but on a home pc it is usually in use.
Quote:
Originally Posted by mina86
It sounds to me like you are trying to solve a problem that does not exist.
No, he only wants to chdir ~ in c, nothing else. Actually you are right, we should not solve a non-existent problemm just give a hint how to do that.
I prefer the solution of dwhitney67 (#5) using getpwuid
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.