aboutsummaryrefslogtreecommitdiff
path: root/newlib/libc/unix/getut.c
blob: d8e3e2fec9d8dfb2be81f8547b8a3adbed92fd2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <fcntl.h>
#include <unistd.h>
#include <utmp.h>
#include <_syslist.h>
#include <_ansi.h>

static int utmp_fd = -2;
static char *utmp_file = UTMP_FILE;

static struct utmp utmp_data;

void
setutent ()
{
  if (utmp_fd == -2)
    {
      utmp_fd = _open (utmp_file, O_RDONLY);
    }
  _lseek (utmp_fd, 0, SEEK_SET);
}

void
endutent ()
{
  _close (utmp_fd);
  utmp_fd = -2;
}

void
utmpname (_CONST char *file)
{
  extern char *strdup (char *);

  utmp_file = strdup (file);
}

struct utmp *
getutent ()
{
  if (utmp_fd == -2)
    setutent ();
  if (_read (utmp_fd, &utmp_data, sizeof (utmp_data)) < sizeof (utmp_data))
    return 0;
  return &utmp_data;
}

struct utmp *
getutid (struct utmp *id)
{
  while (_read (utmp_fd, &utmp_data, sizeof (utmp_data)) == sizeof (utmp_data))
    {
      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)
{
  while (_read (utmp_fd, &utmp_data, sizeof (utmp_data)) == sizeof (utmp_data))
    {
      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;
}