Path: utzoo!attcan!uunet!lll-winken!ames!mailrus!bbn!mit-eddie!uw-beaver!uw-june!ka From: ka@june.cs.washington.edu (Kenneth Almquist) Newsgroups: comp.unix.wizards Subject: Re: script & lock Keywords: getlogin(3) Message-ID: <6925@june.cs.washington.edu> Date: 12 Jan 89 06:36:01 GMT References: <17972@adm.BRL.MIL> <596@cf-cm.UUCP> <15399@mimsy.UUCP> Organization: U of Washington, Computer Science, Seattle Lines: 46 chris@mimsy.UUCP (Chris Torek) writes: > sme@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