script & lock

Kenneth Almquist ka at june.cs.washington.edu
Thu Jan 12 16:36:01 AEST 1989


chris at mimsy.UUCP (Chris Torek) writes:
> sme at computing-maths.cardiff.ac.uk (Simon Elliott) writes:
>> ... A program run in script is attached to [a] pseudo terminal so
>> getlogin() returns a pointer to the null string entry from utmp.
>> Getlogin returns the NULL pointer only if it called from a process
>> which is NOT attached to any terminal (even a pseudo one).
> 
> Not so.  If I may quote from getlogin():
> 
> 	#if defined(LIBC_SCCS) && !defined(lint)
> 	static char sccsid[] = "@(#)getlogin.c	5.3 (Berkeley) 5/9/86";
> 	#endif LIBC_SCCS and not lint
> 
> 	...
> 		if (ubuf.ut_name[0] == '\0')
> 			return (0);
> 	...
> 		return (ubuf.ut_name);
> 
> I believe *getlogin()==0 may be true under some broken Unixes.

The source may be the final reference, but there's no law against looking
things up in the manual.  If I may quote from the 4.3 BSD manual (dated
April 1986):

	Getlogin returns a pointer to the login name as found in
	/etc/utmp....

	If getlogin is called within a process that is not attached to
	a terminal or if there is no entry in /etc/utmp for the process's
	terminal, getlogin returns a NULL pointer (0).

A process running under script is attached to a pty, and there is
(presumably) a utmp entry for the pty, so getlogin should return the
user name from the /etc/utmp entry for the pty.  Since no user is
actually logged onto the pty, the name in the /etc/utmp entry is the
null string.

So unless Berkeley screwed up, code that depends upon getlogin
returning a NULL pointer when it encounters a null string won't be
portable to vanilla 4.3 BSD.  It certainly won't be portable to
Ultrix 2.3 (a 4.2 BSD derivative which we run here).  I will resist
commenting on whether 4.2 and 4.3 BSD can accurately be described
as "broken Unixes" and simply suggest that testing the results of
getlogin against both NULL and "" is good practice.
				Kenneth Almquist



More information about the Comp.unix.wizards mailing list