cygwin getlogin_r() is missing

Martin Fuchs kde-cygwin@mail.kde.org
Sat, 30 Nov 2002 12:02:34 +0100


This is a multi-part message in MIME format.

------=_NextPart_000_0004_01C29868.5E45F920
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hi Ralf!

> Is anyone there, how is willing to contribute such a function the the
cygwin
> library ?

I think the attached code should help you here.

    Martin

------=_NextPart_000_0004_01C29868.5E45F920
Content-Type: text/plain;
	name="getlogin_r.c"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="getlogin_r.c"

#define	WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <malloc.h>
#include <stdio.h>
#include <errno.h>

#define	EOK		0

#define	SUCCESS	EOK
#define	FAILURE	ERANGE

int getlogin_r(char* name, size_t namesize)
{
	HANDLE hToken;
	DWORD tu_len;
	TOKEN_USER* ptu;
	TCHAR domain[256];
	DWORD domain_len;
	DWORD name_len;
	SID_NAME_USE name_use;

	if (OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &hToken) ||
		(GetLastError()=3D=3DERROR_NO_TOKEN && =
OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))) {

		ptu =3D 0;

		if (GetTokenInformation(hToken, TokenUser, 0, 0, &tu_len))
			return FAILURE;

		if (GetLastError() !=3D ERROR_INSUFFICIENT_BUFFER)
			return FAILURE;

		if (!(ptu =3D (TOKEN_USER*)alloca(tu_len)))
			return FAILURE;

		if (!GetTokenInformation(hToken, TokenUser, ptu, tu_len, &tu_len))
			return FAILURE;

		domain_len =3D 256;
		name_len =3D namesize;

		if (!LookupAccountSid(0, ptu->User.Sid, name, &name_len, domain, =
&domain_len, &name_use))
			return FAILURE;

		CloseHandle(hToken);
	}

	return SUCCESS;
}

main()
{
	char buffer[1024];

	int ret =3D getlogin_r(buffer, sizeof(buffer));

	printf("getlogin_r() returned: %d\nbuffer =3D %s\n", ret, buffer);

	return 0;
}

------=_NextPart_000_0004_01C29868.5E45F920--