2000-08-25 02:51:09 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2000-02-18 03:39:52 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <utmp.h>
|
|
|
|
#include <_syslist.h>
|
2000-08-23 03:04:49 +08:00
|
|
|
#include <_ansi.h>
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
static int utmp_fd = -2;
|
|
|
|
static char *utmp_file = UTMP_FILE;
|
|
|
|
|
|
|
|
static struct utmp utmp_data;
|
|
|
|
|
|
|
|
void
|
|
|
|
setutent ()
|
|
|
|
{
|
|
|
|
if (utmp_fd == -2)
|
|
|
|
{
|
2005-11-04 04:47:50 +08:00
|
|
|
utmp_fd = open (utmp_file, O_RDONLY);
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
2005-11-04 04:47:50 +08:00
|
|
|
lseek (utmp_fd, 0, SEEK_SET);
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
endutent ()
|
|
|
|
{
|
2005-11-04 04:47:50 +08:00
|
|
|
close (utmp_fd);
|
2000-02-18 03:39:52 +08:00
|
|
|
utmp_fd = -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-08-23 03:04:49 +08:00
|
|
|
utmpname (_CONST char *file)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
utmp_file = strdup (file);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct utmp *
|
|
|
|
getutent ()
|
|
|
|
{
|
|
|
|
if (utmp_fd == -2)
|
|
|
|
setutent ();
|
2005-11-04 04:47:50 +08:00
|
|
|
if (read (utmp_fd, &utmp_data, sizeof (utmp_data)) < sizeof (utmp_data))
|
2000-02-18 03:39:52 +08:00
|
|
|
return 0;
|
|
|
|
return &utmp_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct utmp *
|
|
|
|
getutid (struct utmp *id)
|
|
|
|
{
|
2005-11-04 04:47:50 +08:00
|
|
|
while (read (utmp_fd, &utmp_data, sizeof (utmp_data)) == sizeof (utmp_data))
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
switch (id->ut_type)
|
|
|
|
{
|
|
|
|
case RUN_LVL:
|
|
|
|
case BOOT_TIME:
|
|
|
|
case OLD_TIME:
|
|
|
|
case NEW_TIME:
|
|
|
|
if (id->ut_type == utmp_data.ut_type)
|
|
|
|
return &utmp_data;
|
|
|
|
case INIT_PROCESS:
|
|
|
|
case LOGIN_PROCESS:
|
|
|
|
case USER_PROCESS:
|
|
|
|
case DEAD_PROCESS:
|
|
|
|
if (id->ut_id == utmp_data.ut_id)
|
|
|
|
return &utmp_data;
|
|
|
|
default:
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct utmp *
|
|
|
|
getutline (struct utmp *line)
|
|
|
|
{
|
2005-11-04 04:47:50 +08:00
|
|
|
while (read (utmp_fd, &utmp_data, sizeof (utmp_data)) == sizeof (utmp_data))
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
if ((utmp_data.ut_type == LOGIN_PROCESS ||
|
|
|
|
utmp_data.ut_type == USER_PROCESS) &&
|
|
|
|
!strncmp (utmp_data.ut_line, line->ut_line,
|
|
|
|
sizeof (utmp_data.ut_line)))
|
|
|
|
return &utmp_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|