Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / unix / getut.c
blob71a8c8f4f107d165e5ddf86d5878de3870467006
1 #ifndef _NO_GETUT
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>
11 static int utmp_fd = -2;
12 static char *utmp_file = UTMP_FILE;
14 static struct utmp utmp_data;
16 void
17 setutent ()
19 if (utmp_fd == -2)
21 utmp_fd = open (utmp_file, O_RDONLY);
23 lseek (utmp_fd, 0, SEEK_SET);
26 void
27 endutent ()
29 close (utmp_fd);
30 utmp_fd = -2;
33 void
34 utmpname (const char *file)
36 utmp_file = strdup (file);
39 struct utmp *
40 getutent ()
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;
49 struct utmp *
50 getutid (struct utmp *id)
52 while (read (utmp_fd, &utmp_data, sizeof (utmp_data)) == sizeof (utmp_data))
54 switch (id->ut_type)
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 (!strncmp (id->ut_id, utmp_data.ut_id, sizeof (utmp_data.ut_id)))
67 return &utmp_data;
68 break;
69 default:
70 abort ();
73 return 0;
76 struct utmp *
77 getutline (struct utmp *line)
79 while (read (utmp_fd, &utmp_data, sizeof (utmp_data)) == sizeof (utmp_data))
81 if ((utmp_data.ut_type == LOGIN_PROCESS ||
82 utmp_data.ut_type == USER_PROCESS) &&
83 !strncmp (utmp_data.ut_line, line->ut_line,
84 sizeof (utmp_data.ut_line)))
85 return &utmp_data;
88 return 0;
91 #endif /* !_NO_GETUT */