OSDN Git Service

- Avoid warning about undefined preprocessor token. No obj-code changes.
[uclinux-h8/uClibc.git] / libutil / logwtmp.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * wtmp support rubbish (i.e. complete crap)
4  * Copyright (C) 2000-2006 by Erik Andersen <andersen@uclibc.org>
5  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
6  */
7
8 #include <string.h>
9 #include <sys/time.h>
10 #include <time.h>
11 #include <unistd.h>
12 #include <utmp.h>
13 #include <fcntl.h>
14 #include <sys/file.h>
15
16
17 void logwtmp (const char *line, const char *name, const char *host)
18 {
19     struct utmp lutmp;
20     memset (&(lutmp), 0, sizeof (struct utmp));
21
22     lutmp.ut_type = (name && *name)? USER_PROCESS : DEAD_PROCESS;
23     lutmp.ut_pid = getpid();
24     strncpy(lutmp.ut_line, line, sizeof(lutmp.ut_line)-1);
25     strncpy(lutmp.ut_name, name, sizeof(lutmp.ut_name)-1);
26     strncpy(lutmp.ut_host, host, sizeof(lutmp.ut_host)-1);
27 #if !defined __WORDSIZE_COMPAT32 || __WORDSIZE_COMPAT32 == 0
28     gettimeofday(&(lutmp.ut_tv), NULL);
29 #else
30     {
31       struct timeval tv;
32       gettimeofday (&tv, NULL);
33       lutmp.ut_tv.tv_sec = tv.tv_sec;
34       lutmp.ut_tv.tv_usec = tv.tv_usec;
35     }
36 #endif
37
38     updwtmp(_PATH_WTMP, &(lutmp));
39 }
40
41 #if 0
42 /* This is enabled in uClibc/libc/misc/utmp/wtent.c */
43 extern void updwtmp(const char *wtmp_file, const struct utmp *lutmp)
44 {
45     int fd;
46
47     fd = open(wtmp_file, O_APPEND | O_WRONLY, 0);
48     if (fd >= 0) {
49         if (lockf(fd, F_LOCK, 0)==0) {
50             write(fd, (const char *) lutmp, sizeof(struct utmp));
51             lockf(fd, F_ULOCK, 0);
52             close(fd);
53         }
54     }
55 }
56 #endif