LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   chdir("~") to $HOME in C programming language? (https://www.linuxquestions.org/questions/programming-9/chdir-%7E-to-%24home-in-c-programming-language-4175457202/)

Xeratul 04-07-2013 03:34 AM

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"))
is not universal or reliable.

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 ...

I am looking forward to hearing.

Kind regards

--
listening:
http://www.youtube.com/watch?v=qycAC_6Bbto

pan64 04-07-2013 04:12 AM

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.

patrick295767 04-07-2013 04:13 AM

Quote:

Originally Posted by pan64 (Post 4926733)
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 :(

pan64 04-07-2013 04:18 AM

no, we do not need to know the name of the user, we always have the user id, that is enough for that

dwhitney67 04-07-2013 07:01 AM

Using a pair of library functions, getuid() and getpwuid(), one can get the user's home directory.

Code:

#include <unistd.h>
#include <pwd.h>
#include <stdio.h>

int main(void)
{
    uid_t uid = getuid();
    struct passwd* pwd = getpwuid(uid);

    if (!pwd)
    {
        printf("User with %u ID is unknown.\n", uid);
        return -1;
    }

    printf("User '%s' has home directory at '%s'\n", pwd->pw_name, pwd->pw_dir);

    if (chdir(pwd->pw_dir) != 0)
    {
        printf("Unable to change directory to '%s'\n", pwd->pw_dir);
        return -1;
    }

    return 0;
}


mina86 04-08-2013 08:57 AM

Quote:

Originally Posted by Xeratul (Post 4926715)
Code:

chdir(getenv("HOME"))
is not universal or reliable.

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 (Post 4926735)
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.

pan64 04-09-2013 12:23 AM

Quote:

Originally Posted by mina86 (Post 4927536)
/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 (Post 4927536)
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


All times are GMT -5. The time now is 11:04 AM.