Get the user name (BRL.System)

BlitzMax Forums/BlitzMax Module Tweaks/Get the user name (BRL.System)

Brucey(Posted 2007) [#1]
Get the login name of the current user.

system.bmx
Rem
bbdoc: Gets the user login name.
End Rem
Function GetUserName$()
Extern
	Function bbSystemGetUserName(buf:Byte Ptr)
End Extern
	Local buf:Byte[256]
	bbSystemGetUserName(buf)
	Return String.fromCString(buf)
End Function


system.c
void bbSystemGetUserName(char * buffer) {
#ifdef _WIN32
	int len = 256;
	GetUserName(buffer, &len);
#else
#include <sys/types.h>
#include <pwd.h>
	struct passwd * p = getpwuid(getuid());
	strcpy(buffer, p->pw_name);	
#endif
}


Win32 not tested yet (will do tomorrow) as I just googled to find out the API call to get the info, and tweaked things accordingly, so chances are I've missed something out and it won't work.


If someone can name me a better Module this should go into, please do. BRL.System seems the most appropriate.

Quite a useful function, me thinks...

(I'm up for more if anyone has any "cross-platform" suggestions)