how to get current user home directory

This is a discussion on how to get current user home directory within the Linux Programming forums, part of the Platform Specific Boards category; Hello everyone, How to use Linux API to get current user home directory, e.g. for user foo, the home directory ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    how to get current user home directory

    Hello everyone,


    How to use Linux API to get current user home directory, e.g. for user foo, the home directory is /home/foo, for user root, the home directory is /root.


    thanks in advance,
    George

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    It's usually better to look at the HOME environment variable first. Only if that isn't set, then check the passwd file. Unless you have some specific reason for not believing/trusting the HOME variable (some security consideration) you should use it.

  4. #4
    .
    Join Date
    Nov 2003
    Posts
    307
    I agree completely with Salem. Rather than argue, assume there may be very good reasons to reconsider using HOME or other environment variables for a lot of things unless there is no recourse -- simply as de facto standard, good practice.

    Some casual points:

    env problems are a pain/impossible to diagnose after the fact. Eliminating sources of error upfront is considered good practice.

    Code re-use means that "inoccuous" code is put into a different environment where it can become less so.

    Consider p 211 of McConnell 'Code Complete' 2nd Ed.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by jim mcnamara View Post
    I agree completely with Salem. Rather than argue, assume there may be very good reasons to reconsider using HOME or other environment variables for a lot of things unless there is no recourse -- simply as de facto standard, good practice.
    So bash is broken?

    Code:
    me@host:/home/dir$ export HOME=/
    me@host:/home/dir$ cd
    me@host:/$
    I did suggest that there may be considerations in using HOME which the OP should probably look into. But ignoring HOME as standard practice pretty much means the variable shouldn't exist in the first place. If I had the book you refer to, I'd look there. Care to elaborate at all?

    EDIT: And as if getpwent() could be trusted! It can't. It's a library function so it can be trivially overriden by LD_PRELOAD hacking. Seems like the whole "benefit" is non-existent.
    Last edited by brewbuck; 05-08-2007 at 02:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 01:54 AM
  2. Problem with simple case statements
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 05-08-2006, 08:39 AM
  3. Newbie Help: Currency Converter
    By Ashfury in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 12:21 PM
  4. getting current directory
    By Corrington_j in forum Linux Programming
    Replies: 2
    Last Post: 09-07-2005, 10:04 PM
  5. illegal function overloading????
    By Mr_Jack in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2003, 12:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21