Problems with getlogin()

 

Intoduction:
It seems that the Solaris getlogin() function is buggy. Specifically, when
you run a program that contains a call to getlogin() is launched from a shell
that isn't attached to a terminal, the program will dump core. This means,
for example, that if you try and run your program inside of script (as
recommend for the assignment), it will dump core, which is bad.

I verified this problem on Solaris 2.5.1/SPARC and Solaris 2.6/x86.

As a workaround, I recommend using the cuserid() system call. It seems
to be much more robust. You can read the manpage in order to learn about
this function, but I have also included a sample program in order to
demonstrate how it can be used.
Sample Program:
/****************************************************************************/
/* ECES 338, Assignment #1 Sample Program #1                                */
/*	'cuserid.c' - Demonstration of the cuserid() system call.           */
/* by Andy Reitz (ajr9@po.cwru.edu)                                         */
/* Date: 2/2/99                                                             */
/****************************************************************************/
#include < stdio.h >

int main (void)
{
	char nm_str[L_cuserid];
	char *nm_ptr;

	/* Method one just squirts the userid into a local array. */
	cuserid (nm_str);
	printf ("1: Local Variable\t--\t%s\n", nm_str);
	
	/*
	 * Method two relies on the internal static area allocated by
	 * cuserid().
	 */
	printf ("2: Internal Static Area\t--\t%s\n", cuserid (NULL));

	/*
	 * Method three involves dynamically allocating memory via malloc().
	 * I _highly_ recommend that you error-check your malloc() calls
	 * like I do.
	 */
	if ((nm_ptr = (char *) malloc (sizeof (char) * L_cuserid)) == NULL)
		{
		perror ("Malloc failed; Reason: ");
		exit (1);
		}

	cuserid (nm_ptr);
	printf ("3: Via Malloc'd Memory\t--\t%s\n", nm_ptr);

	/* It's also imperitave to free() any memory that you malloc(). */
	free (nm_ptr);

	return (0);
} /* End main(). */