#include <ncbi.h>
#include <gishlib.h>

#ifdef OS_UNIX
#include <pwd.h>

#ifndef L_cuserid
#define L_cuserid	9
#endif

CharPtr
sys_whoami()
{
	char	PNTR getlogin();

	register CharPtr    cp;
	register struct passwd	PNTR pwp;
	static char	whoiam[L_cuserid+1];

	if (whoiam[0] != NULLB)
		return whoiam;

	if ((cp = getlogin()) != NULL && *cp != NULLB) {
		strncpy(whoiam, cp, sizeof(whoiam)-1);
		whoiam[sizeof(whoiam)-1] = NULLB;
		return whoiam;
	}

	/* Procedure when not logged in... */

	/* Rewind the passwd file */
	setpwent();
	if ((pwp = getpwuid(getuid())) != NULL) {
		strncpy(whoiam, pwp->pw_name, sizeof(whoiam)-1);
		whoiam[sizeof(whoiam)-1] = NULLB;
		return whoiam;
	}
	/* Who are you?? */
	return NULL;
}

#endif /* OS_UNIX */

#ifdef OS_VMS
#include <jpidef.h>

CharPtr
sys_whoami()
{
	int		Status;
	CharPtr	cp;
	static char	username[12];
	static int	User_Length;
	static struct {
		unsigned short	Size;
		unsigned short	Code;
		CharPtr	Name = username;
		int		PNTR Resultant_Size = &User_Length;
		} Item_List1[2] = {
			{32, JPI$_USERNAME, username, &User_Length},
			{0, 0, 0, 0}
		};

	Status = sys$getjpiw(0,0,0,Item_List1,0,0,0);
	if (!(Status & 1)) {
		/*
		*      Error
		*/
		if (Status == 0x3a4) {
			/*
			 * Process is swapped out!
			 */
			User_Length = 0;
			return NULL;
		}
	}
	return username;
}
#endif /* OS_VMS */
