source: trunk/libs/newlib/src/newlib/libc/unix/getut.c @ 543

Last change on this file since 543 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 1.5 KB
Line 
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
11static int utmp_fd = -2;
12static char *utmp_file = UTMP_FILE;
13
14static struct utmp utmp_data;
15
16void
17setutent ()
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
26void
27endutent ()
28{
29  close (utmp_fd);
30  utmp_fd = -2;
31}
32
33void
34utmpname (const char *file)
35{
36  utmp_file = strdup (file);
37}
38
39struct utmp *
40getutent ()
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
49struct utmp *
50getutid (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
75struct utmp *
76getutline (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  */
Note: See TracBrowser for help on using the repository browser.