Given the following:
#include <string.h>
#include <stdlib.h>
#include <pwd.h>
#include <unistd.h>
#include <utmp.h>
#include <stdio.h>
#include <time.h>
int main(int argc, char *argv[])
{
struct utmp entry;
char *fd;
system("echo before adding entry:;who");
entry.ut_type=USER_PROCESS;
entry.ut_pid=getpid();
fd = ttyname(0);
printf("The current tty is: %s\n",fd);
strcpy(entry.ut_line,ttyname(0)+strlen("/dev/"));
/* only correct for ptys named /dev/tty[pqr][0-9a-z] */
strcpy(entry.ut_id,ttyname(0)+strlen("/dev/tty"));
time(&entry.ut_time);
strcpy(entry.ut_user,getpwuid(getuid())->pw_name);
memset(entry.ut_host,0,UT_HOSTSIZE);
entry.ut_addr=0;
setutent();
pututline(&entry);
system("echo after adding entry:;who");
entry.ut_type=DEAD_PROCESS;
memset(entry.ut_line,0,UT_LINESIZE);
entry.ut_time=0;
memset(entry.ut_user,0,UT_NAMESIZE);
setutent();
pututline(&entry);
system("echo after removing entry:;who");
endutent();
return 0;
On xterm, it shows /dev/pts/3. However, when I run the above code, I see the following:
before adding entry:
cda pts/1 2007-06-30 21:04 (:0.0)
cda pts/2 2007-06-30 20:28 (:0.0)
after adding entry:
cda pts/1 2007-06-30 21:04 (:0.0)
cda pts/2 2007-06-30 20:28 (:0.0)
after removing entry:
cda pts/1 2007-06-30 21:04 (:0.0)
cda pts/2 2007-06-30 20:28 (:0.0)
The question is, how come nothing gets added to utmp when I run it under xterm on Linux? Ie how come it doesn't show the line
cda pts/3 2007-06-30 20:28 (:0.0)