# Get current user path?



## kuroyume (Jun 16, 2008)

On a multi-user machine, how would one get the current user's folder path (i.e.: /Users/<user>) in Carbon only (Xcode C++).  Seems that using environmental variables (getenv()) returns nothing for "PWD".

Thanks,
Robert


----------



## ElDiabloConCaca (Jun 17, 2008)

Does this help?

http://lists.apple.com/archives/carbon-dev/2004/nov/msg00840.html


----------



## kuroyume (Jun 17, 2008)

I eventually found something but it took about twenty variations on wording (in the end, since the current user's Libraryreferences was my target, that helped).

So, here's my little code - works great:


```
#include "Folders.h"

// METHODS: MacUserPrefs
// Get Users Preferences folder
//*---------------------------------------------------------------------------*
String MacUserPrefs::GetUserPrefs()
//*---------------------------------------------------------------------------*
{
	// ask MacOS to find the users preferences folder (/Users/{username}/Library/Preferences/ on US-english MacOS X)
	// see http://developer.apple.com/samplecode/MoreFilesX/listing2.html
	// and http://developer.apple.com/documentation/Carbon/Reference/Folder_Manager/Reference/reference.html#//apple_ref/c/func/FSFindFolder
	// and http://developer.apple.com/samplecode/FileNotification/listing1.html
	FSRef	fsRef;
	char	path[1024];
	OSErr	tError =	noErr;

	tError =			FSFindFolder(kOnAppropriateDisk, kPreferencesFolderType, kDontCreateFolder, &fsRef);
	tError =			FSRefMakePath(&fsRef, (UInt8*)path, 1024);
	return	String(path);
}
```


----------



## thomodachi (Feb 4, 2009)

For code get user directory, go here:

http://developer.apple.com/qa/qa2007/qa1549.html


----------



## Mikuro (Feb 4, 2009)

kuroyume said:


> Seems that using environmental variables (getenv()) returns nothing for "PWD".



Try HOME instead of PWD. PWD is the current working directory, not the home folder (though that doesn't explain why it returns nothing).

I'm not sure if this is better than the solution you already have. Just FYI.


----------



## kuroyume (Feb 4, 2009)

thomodachi said:


> For code get user directory, go here:
> 
> http://developer.apple.com/qa/qa2007/qa1549.html



What does that have to do with this at all?  There is no mention of a 'tilde-based path' in the entire thread (???)


----------



## kuroyume (Feb 4, 2009)

Mikuro said:


> Try HOME instead of PWD. PWD is the current working directory, not the home folder (though that doesn't explain why it returns nothing).
> 
> I'm not sure if this is better than the solution you already have. Just FYI.



The SDK being used has since added a means to get the user's folder.  They're a little bit slow trying to keep up with Apple's changes. 

Thanks


----------



## thomodachi (Feb 4, 2009)

On any *nix machine tilde (~) is a shortcut to the user folder path.


----------

