OSDN Git Service

arm: move check for BX to its own header
[uclinux-h8/uClibc.git] / libutil / login.c
1 #include <errno.h>
2 #include <limits.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <utmp.h>
7
8 /* Write the given entry into utmp and wtmp.
9  * Note: the match in utmp is done against ut_id field,
10  * which is NOT set by this function - caller must set it.
11  */
12 void login(const struct utmp *entry)
13 {
14         struct utmp copy;
15         char tty_name[sizeof(copy.ut_line) + 6];
16         int fd;
17
18 // Manpage:
19 // login() takes the argument ut struct, fills the field ut->ut_type
20 // (if there is such a field) with the value USER_PROCESS,
21 // and fills the field ut->ut_pid (if there is such a field)
22 // with the process ID of the calling process.
23         copy = *entry;
24 #if _HAVE_UT_TYPE - 0
25         copy.ut_type = USER_PROCESS;
26 #endif
27 #if _HAVE_UT_PID - 0
28         copy.ut_pid = getpid();
29 #endif
30
31 // Then it tries to fill the field ut->ut_line. It takes the first of stdin,
32 // stdout, stderr that is a tty, and stores the corresponding pathname minus
33 // a possible leading /dev/ into this field, and then writes the struct
34 // to the utmp file. On the other hand, if no tty name was found,
35 // this field is filled with "???" and the struct is not written
36 // to the utmp file.
37         fd = 0;
38         while (fd != 3 && ttyname_r(fd, tty_name, sizeof(tty_name)) != 0)
39                 fd++;
40         if (fd != 3) {
41                 if (strncmp(tty_name, "/dev/", 5) == 0)
42                         strncpy(copy.ut_line, tty_name + 5, sizeof(copy.ut_line)-1);
43                 else
44                         strncpy(copy.ut_line, tty_name, sizeof(copy.ut_line)-1);
45                 copy.ut_line[sizeof(copy.ut_line)-1] = '\0';
46
47                 /* utmpname(_PATH_UTMP); - why?
48                  * this makes it impossible for caller to use other file!
49                  * Does any standard or historical precedent says this must be done? */
50                 setutent();
51                 /* Replaces record with matching ut_id, or appends new one: */
52                 pututline(&copy);
53                 endutent();
54         } else {
55                 strncpy(copy.ut_line, "???", sizeof(copy.ut_line));
56         }
57
58 // After this, the struct is written to the wtmp file.
59         updwtmp(_PATH_WTMP, &copy);
60 }