Thread: How to get the user's home directory

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    228

    How to get the user's home directory

    This is somewhat related to this, as I decided, after considering grumpy's reply, to write the log file to the home directory.
    That raised another question, and it felt like it deserves a post of its own (I hope I'm not wrong).

    So I want to obtain the user's home directory.
    I know it can be done with getenv("HOME"), but if I'll inspect the value of the enviroment variable HOME, it might work when I run it now, but it won't work on Windows, since windows uses HOMEPATH to save the user's home directory.

    Is there a different, more cross-platform way to do it?

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    You could use macros. Can't you write specific checks to see if you're on a Windows architecture or not?

    It'd be like
    Code:
    #if Windows
    ...
    #endif
    
    #if Linux
    ...
    #endif
    I know that I've seen architecture checks before so I'm really just assuming here.

    Alright, I found this : http://sourceforge.net/p/predef/wiki/OperatingSystems/

    Basically, these are just the names of the different operating systems.

    So you'd type something like
    Code:
    #if __linux__
    ...
    #endif
    Last edited by MutantJohn; 10-20-2014 at 02:23 PM.

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    228
    Wow, that's really cool!
    I didn't know you can do that with preprocessing macros.
    The only preprocessing conditional I knew there was is #ifndef ...

    Thanks.
    Last edited by Absurd; 10-20-2014 at 02:51 PM.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    For Windows you need to get both HOMEDRIVE and HOMEPATH to put the result together to get the home directory. See this article from MSDN
    GetWindowsDirectory function (Windows)

    Here is a way you can use the symbol _WIN32 to make a somewhat portable function to get the home directory of the current user

    Code:
    char *get_homedir(void)
    {
        char homedir[MAX_PATH];
    #ifdef _WIN32
        snprintf(homedir, MAX_PATH, "%s%s", getenv("HOMEDRIVE"), getenv("HOMEPATH"));
    #else
        snprintf(homedir, MAX_PATH, "%s", getenv("HOME"));
    #endif
        return strdup(homedir);
    }

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Windows 7 has the "HOME" environment variable, that shows the full path (C:\users\username) of the home directory. It's likely that Vista had it as well, although I don't have the ability to check at the moment, and it stands to reason that newer (ie. 8, 8.1) versions also define "HOME"
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Some extra suggestions to augment c99tutorial's example:

    1. I'm not sure if getenv("HOME") is right for all non-Windows platforms, i.e. maybe you should check
    2. getenv can return NULL (getenv(3): environment variable - Linux man page). You should check for this before trying to snprintf it. Especially if you want to support multiple platforms, which may or may not store home dir in the env var HOME.
    3. Consider checking the return value of snprintf to make sure that it wasn't truncated, i.e. that homedir contains the whole path.
    4. Consider using sizeof(homedir) instead of MAX_PATH in snprintf calls. That way, if you change the size of homedir, you don't need to worry about changing the snprintf calls.
    5. strdup is not a standard function however it is an incredibly useful function, so I recommend defining your own if it's not available. Use preprocessor checks like #ifndef to determine whether it will be compile in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Not able to access directory of home folder
    By Bargi in forum Linux Programming
    Replies: 1
    Last Post: 02-06-2008, 02:45 AM
  2. how to get current user home directory
    By George2 in forum Linux Programming
    Replies: 4
    Last Post: 05-08-2007, 02:46 PM
  3. Accessing the 'home' directory path?
    By smoothdogg00 in forum C Programming
    Replies: 3
    Last Post: 04-30-2006, 10:51 AM
  4. Home Directory
    By codez in forum C Programming
    Replies: 2
    Last Post: 01-07-2006, 01:45 PM
  5. getting the home directory
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 10-17-2005, 07:14 AM