LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-07-2013, 03:34 AM   #1
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,658

Rep: Reputation: 255Reputation: 255Reputation: 255
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

Last edited by Xeratul; 04-07-2013 at 03:36 AM.
 
Old 04-07-2013, 04:12 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
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.
 
Old 04-07-2013, 04:13 AM   #3
patrick295767
Member
 
Registered: Feb 2006
Distribution: FreeBSD, Linux, Slackware, LFS, Gparted
Posts: 664

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by pan64 View Post
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
 
Old 04-07-2013, 04:18 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
no, we do not need to know the name of the user, we always have the user id, that is enough for that
 
Old 04-07-2013, 07:01 AM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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;
}
 
Old 04-08-2013, 08:57 AM   #6
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by Xeratul View Post
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 View Post
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.

Last edited by mina86; 04-08-2013 at 08:58 AM.
 
1 members found this post helpful.
Old 04-09-2013, 12:23 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
Quote:
Originally Posted by mina86 View Post
/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 View Post
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
NFS problem: "Could not chdir to home directory /home/USER: Permission denied" sumanc Linux - Server 4 07-22-2010 04:12 PM
"Genie" programming language, want to learn... 01micko Programming 6 06-12-2009 10:09 PM
"The C Programming Language" by K&R, can't get a program working, section 1.5.2 Romanus81 Programming 9 11-28-2008 01:01 PM
Is K&R's "The C Programming Language" outdated? frankie_DJ Programming 3 09-21-2006 07:39 AM
Exim4 undeliverable emails"failed to chdir to /home/paul" PK2K Linux - Software 0 05-15-2006 06:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration