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