1 #include <stdlib.h>
2 #include <string.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <utmp.h>
6 #include <_syslist.h>
7 #include <_ansi.h>
8 
9 static int utmp_fd = -2;
10 static char *utmp_file = UTMP_FILE;
11 
12 static struct utmp utmp_data;
13 
14 void
setutent()15 setutent ()
16 {
17   if (utmp_fd == -2)
18     {
19       utmp_fd = _open (utmp_file, O_RDONLY);
20     }
21   _lseek (utmp_fd, 0, SEEK_SET);
22 }
23 
24 void
endutent()25 endutent ()
26 {
27   _close (utmp_fd);
28   utmp_fd = -2;
29 }
30 
31 void
utmpname(_CONST char * file)32 utmpname (_CONST char *file)
33 {
34   utmp_file = strdup (file);
35 }
36 
37 struct utmp *
getutent()38 getutent ()
39 {
40   if (utmp_fd == -2)
41     setutent ();
42   if (_read (utmp_fd, &utmp_data, sizeof (utmp_data)) < sizeof (utmp_data))
43     return 0;
44   return &utmp_data;
45 }
46 
47 struct utmp *
getutid(struct utmp * id)48 getutid (struct utmp *id)
49 {
50   while (_read (utmp_fd, &utmp_data, sizeof (utmp_data)) == sizeof (utmp_data))
51     {
52       switch (id->ut_type)
53 	{
54 	case RUN_LVL:
55 	case BOOT_TIME:
56 	case OLD_TIME:
57 	case NEW_TIME:
58 	  if (id->ut_type == utmp_data.ut_type)
59 	    return &utmp_data;
60 	case INIT_PROCESS:
61 	case LOGIN_PROCESS:
62 	case USER_PROCESS:
63 	case DEAD_PROCESS:
64 	  if (id->ut_id == utmp_data.ut_id)
65 	    return &utmp_data;
66 	default:
67 	  abort ();
68 	}
69     }
70   return 0;
71 }
72 
73 struct utmp *
getutline(struct utmp * line)74 getutline (struct utmp *line)
75 {
76   while (_read (utmp_fd, &utmp_data, sizeof (utmp_data)) == sizeof (utmp_data))
77     {
78       if ((utmp_data.ut_type == LOGIN_PROCESS ||
79 	   utmp_data.ut_type == USER_PROCESS) &&
80 	  !strncmp (utmp_data.ut_line, line->ut_line,
81 		    sizeof (utmp_data.ut_line)))
82 	return &utmp_data;
83     }
84 
85   return 0;
86 }
87