9

How can I get the path of home directory in Mac OS X using C language in XCode editor.

sorin
  • 137,198
  • 150
  • 472
  • 707
boom
  • 5,194
  • 21
  • 52
  • 88
  • Current accepted answer is invalid but there is another similar question that is still open http://stackoverflow.com/questions/3726113/getting-current-user-home-directory-on-os-x – sorin Sep 16 '10 at 11:31

3 Answers3

13

This should work under Linux, Unix and OS X, for Windows you need to make a slight modification.

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

int main(void)
{
    const char *homeDir = getenv("HOME");

    if (!homeDir) {
        struct passwd* pwd = getpwuid(getuid());
        if (pwd)
           homeDir = pwd->pw_dir;
    }
    printf("Home directory is %s\n", homeDir);
    return 0;
}
Dmytro
  • 941
  • 11
  • 18
sorin
  • 137,198
  • 150
  • 472
  • 707
8

with FSFindFolder:

UInt8 path[1024];
FSRef file;
FSFindFolder( kOnAppropriateDisk , kCurrentUserFolderType , kCreateFolder , &file );
FSRefMakePath( &file , path , sizeof(path) );

with CSCopyUserName:

char path[1024];
CFStringRef name = CSCopyUserName( true );
CFStringRef full = CFStringCreateWithFormat( NULL , NULL , CFSTR( "/Users/%@" ) , name );
CFStringGetCString( full , path , sizeof(path) , kCFStringEncodingUTF8 );
// release strings

with NSHomeDirectory:

char path[1024];
CFStringGetCString( (CFStringRef)NSHomeDirectory() , path , sizeof(path) , kCFStringEncodingUTF8 );

note that the path can use UTF8 characters.

drawnonward
  • 52,256
  • 15
  • 103
  • 110
6
#include <stdlib.h>
#include <stdio.h>    

int main(void)
{
    const char *homeDir = getenv("HOME");

    if (homeDir)
        printf("Home directory is %s\n", homeDir);
    else
        printf("Couldn't figure it out.\n");

    return 0;
}
dreamlax
  • 89,489
  • 28
  • 156
  • 207
  • 3
    `$ HOME=erased; ./app` = Home directory is erased – jweyrich Jun 11 '10 at 05:13
  • 7
    As jweyrich says, this is not reliable. There's no guarantee that HOME is set to the home directory or that it is set at all. Server processes often delete their environment for security reasons. You should use `getpwuid()` which returns the password structure including the home directory from the user database. http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man3/getpwuid.3.html – JeremyP Jun 11 '10 at 08:22
  • POSIX guarantees that it is set, see here: [`HOME` The system shall initialize this variable at the time of login to be a pathname of the user's home directory](http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html). Note that on that page it also says "If the variables in the following two sections are present in the environment during the execution of an application or utility, they shall be given the meaning described below.". – dreamlax May 24 '14 at 00:48
  • 1
    @JeremyP: there's no guarantee that a valid home directory is set in the passwd file either (on my system, mysql has a home directory of `/nonexistent`). It could be argued that anyone deliberately changing their `HOME` would want applications to continue to treat it as if it were their actual home directory. – dreamlax Sep 02 '14 at 02:49
  • @dreamlax POSIX does not guarantee that $HOME is set, only that it is set in the login shell of a user and then only if the user hasn't messed about with it. So the question is do you need the user's real home directory or do you want the home directory that the user tells you is their home directory (I accept there are legitimate use cases for the latter as well as the former). – JeremyP Sep 05 '14 at 14:04