script & lock
Simon Elliott
sme at computing-maths.cardiff.ac.uk
Mon Jan 9 20:16:19 AEST 1989
Getlogin returns a pointer to the login name as found in /etc/utmp.
/etc/utmp contains a record for each terminal on the system (including
pseudo-terminals (ttyp?). The login name part is blank or null until
login fills it it. When you run script, your controlling terminal is a
pseudo-terminal (eg. /dev/ttyp0). Your login name is not put into
/etc/utmp for this pseudo-terminal so the entry is still null. A program
run in script is attached to the 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).
-------cut here-------
#include <stdio.h>
#include <pwd.h>
main(ac,av)
char *av[];
{
char *uname, getlogin();
struct passwd *pwd, getpwnam(), getpwuid();
if ((uname = getlogin()) == NULL) {
pwd = getpwuid(getuid());
fprintf(stderr, "uid: %d\n", getuid());
}
else {
pwd = getpwnam(uname);
fprintf(stderr, "uname: *(char *)0x%lx = \"%s\"\n", uname, uname);
}
if (pwd == NULL) {
fprintf(stderr, "%s: cannot locate passwd entry!\007\n", av[0]);
exit(1);
}
}
---------cut here-------
Try running this a) from shell prompt;
b) under 'script' (uses pseudo-tty);
c) using 'at' (detaches from terminal).
--
--------------------------------------------------------------------------
Simon Elliott Internet: sme%v1.cm.cf.ac.uk at cunyvm.cuny.edu
UWCC Computer Centre JANET: sme at uk.ac.cf.cm.v1
40/41 Park Place UUCP: {backbones}!mcvax!ukc!reading!cf-cm!sme
Cardiff, Wales PHONE: +44 222 874300
More information about the Comp.unix.wizards
mailing list