Get current user path?

kuroyume

Registered
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
 
I eventually found something but it took about twenty variations on wording (in the end, since the current user's Library:preferences was my target, that helped).

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

Code:
#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);
}
 
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.
 
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
 
Back
Top