How can I get the path of home directory in Mac OS X
using C
language in XCode
editor.
Asked
Active
Viewed 1.1k times
9
-
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 Answers
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;
}
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.
![](../../users/profiles/306253.webp)
drawnonward
- 52,256
- 15
- 103
- 110
-
Note that `NSHomeDirectory` is misleading. It actually returns the application directory, not the current user homedir. – jweyrich Jun 11 '10 at 05:26
-
2@jweyrich that only applies to iPhone. On normal MacOS it is the equivalent of the others. None of these work on iPhone. – drawnonward Jun 11 '10 at 08:42
-
@drawnonward Aye! Sorry, I thought it worked the same way. Thanks for correcting me. – jweyrich Jun 11 '10 at 10:35
-
1Note that `CSCopyUserName()` is _not_ a solution for finding a user's home folder. It won't find mine, for instance, which is at /Network/Users/leeg. – Sep 16 '10 at 11:25
-
1
-
None of these are C, right? Don't they require the file to be a .m Objective-C file? – Vortico Sep 26 '17 at 13:09
-
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;
}
![](../../users/profiles/10320.webp)
dreamlax
- 89,489
- 28
- 156
- 207
-
3
-
7As 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