Sync from coreutils.
[gnulib.git] / lib / readutmp.h
1 /* Declarations for GNU's read utmp module.
2
3    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4    2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 /* Written by jla; revised by djm */
21
22 #ifndef __READUTMP_H__
23 # define __READUTMP_H__
24
25 # include <sys/types.h>
26
27 /* AIX 4.3.3 has both utmp.h and utmpx.h, but only struct utmp
28    has the ut_exit member.  */
29 # if (HAVE_UTMPX_H && HAVE_UTMP_H && HAVE_STRUCT_UTMP_UT_EXIT \
30       && ! HAVE_STRUCT_UTMPX_UT_EXIT)
31 #  undef HAVE_UTMPX_H
32 # endif
33
34 # ifdef HAVE_UTMPX_H
35 #  ifdef HAVE_UTMP_H
36     /* HPUX 10.20 needs utmp.h, for the definition of e.g., UTMP_FILE.  */
37 #   include <utmp.h>
38 #  endif
39 #  include <utmpx.h>
40 #  define UTMP_STRUCT_NAME utmpx
41 #  define UT_TIME_MEMBER(UT_PTR) ((UT_PTR)->ut_tv.tv_sec)
42 #  define SET_UTMP_ENT setutxent
43 #  define GET_UTMP_ENT getutxent
44 #  define END_UTMP_ENT endutxent
45 #  ifdef HAVE_UTMPXNAME
46 #   define UTMP_NAME_FUNCTION utmpxname
47 #  endif
48
49 #  if HAVE_STRUCT_UTMPX_UT_EXIT_E_TERMINATION
50 #   define UT_EXIT_E_TERMINATION(U) ((U)->ut_exit.e_termination)
51 #  else
52 #   if HAVE_STRUCT_UTMPX_UT_EXIT_UT_TERMINATION
53 #    define UT_EXIT_E_TERMINATION(U) ((U)->ut_exit.ut_termination)
54 #   else
55 #    define UT_EXIT_E_TERMINATION(U) 0
56 #   endif
57 #  endif
58
59 #  if HAVE_STRUCT_UTMPX_UT_EXIT_E_EXIT
60 #   define UT_EXIT_E_EXIT(U) ((U)->ut_exit.e_exit)
61 #  else
62 #   if HAVE_STRUCT_UTMPX_UT_EXIT_UT_EXIT
63 #    define UT_EXIT_E_EXIT(U) ((U)->ut_exit.ut_exit)
64 #   else
65 #    define UT_EXIT_E_EXIT(U) 0
66 #   endif
67 #  endif
68
69 # else
70 #  include <utmp.h>
71 #  if !HAVE_DECL_GETUTENT
72     struct utmp *getutent();
73 #  endif
74 #  define UTMP_STRUCT_NAME utmp
75 #  define UT_TIME_MEMBER(UT_PTR) ((UT_PTR)->ut_time)
76 #  define SET_UTMP_ENT setutent
77 #  define GET_UTMP_ENT getutent
78 #  define END_UTMP_ENT endutent
79 #  ifdef HAVE_UTMPNAME
80 #   define UTMP_NAME_FUNCTION utmpname
81 #  endif
82
83 #  if HAVE_STRUCT_UTMP_UT_EXIT_E_TERMINATION
84 #   define UT_EXIT_E_TERMINATION(U) ((U)->ut_exit.e_termination)
85 #  else
86 #   if HAVE_STRUCT_UTMP_UT_EXIT_UT_TERMINATION
87 #    define UT_EXIT_E_TERMINATION(U) ((U)->ut_exit.ut_termination)
88 #   else
89 #    define UT_EXIT_E_TERMINATION(U) 0
90 #   endif
91 #  endif
92
93 #  if HAVE_STRUCT_UTMP_UT_EXIT_E_EXIT
94 #   define UT_EXIT_E_EXIT(U) ((U)->ut_exit.e_exit)
95 #  else
96 #   if HAVE_STRUCT_UTMP_UT_EXIT_UT_EXIT
97 #    define UT_EXIT_E_EXIT(U) ((U)->ut_exit.ut_exit)
98 #   else
99 #    define UT_EXIT_E_EXIT(U) 0
100 #   endif
101 #  endif
102
103 # endif
104
105 /* Accessor macro for the member named ut_user or ut_name.  */
106 # ifdef HAVE_UTMPX_H
107
108 #  if HAVE_STRUCT_UTMPX_UT_USER
109 #   define UT_USER(Utmp) ((Utmp)->ut_user)
110 #  endif
111 #  if HAVE_STRUCT_UTMPX_UT_NAME
112 #   undef UT_USER
113 #   define UT_USER(Utmp) ((Utmp)->ut_name)
114 #  endif
115
116 # else
117
118 #  if HAVE_STRUCT_UTMP_UT_USER
119 #   define UT_USER(Utmp) ((Utmp)->ut_user)
120 #  endif
121 #  if HAVE_STRUCT_UTMP_UT_NAME
122 #   undef UT_USER
123 #   define UT_USER(Utmp) ((Utmp)->ut_name)
124 #  endif
125
126 # endif
127
128 # define HAVE_STRUCT_XTMP_UT_EXIT \
129     (HAVE_STRUCT_UTMP_UT_EXIT \
130      || HAVE_STRUCT_UTMPX_UT_EXIT)
131
132 # define HAVE_STRUCT_XTMP_UT_ID \
133     (HAVE_STRUCT_UTMP_UT_ID \
134      || HAVE_STRUCT_UTMPX_UT_ID)
135
136 # define HAVE_STRUCT_XTMP_UT_PID \
137     (HAVE_STRUCT_UTMP_UT_PID \
138      || HAVE_STRUCT_UTMPX_UT_PID)
139
140 typedef struct UTMP_STRUCT_NAME STRUCT_UTMP;
141
142 enum { UT_USER_SIZE = sizeof UT_USER ((STRUCT_UTMP *) 0) };
143
144 # if !defined (UTMP_FILE) && defined (_PATH_UTMP)
145 #  define UTMP_FILE _PATH_UTMP
146 # endif
147
148 # if !defined (WTMP_FILE) && defined (_PATH_WTMP)
149 #  define WTMP_FILE _PATH_WTMP
150 # endif
151
152 # ifdef UTMPX_FILE /* Solaris, SysVr4 */
153 #  undef UTMP_FILE
154 #  define UTMP_FILE UTMPX_FILE
155 # endif
156
157 # ifdef WTMPX_FILE /* Solaris, SysVr4 */
158 #  undef WTMP_FILE
159 #  define WTMP_FILE WTMPX_FILE
160 # endif
161
162 # ifndef UTMP_FILE
163 #  define UTMP_FILE "/etc/utmp"
164 # endif
165
166 # ifndef WTMP_FILE
167 #  define WTMP_FILE "/etc/wtmp"
168 # endif
169
170 # if HAVE_STRUCT_XTMP_UT_PID
171 #  define UT_PID(U) ((U)->ut_pid)
172 # else
173 #  define UT_PID(U) 0
174 # endif
175
176 # if HAVE_STRUCT_UTMP_UT_TYPE || HAVE_STRUCT_UTMPX_UT_TYPE
177 #  define UT_TYPE_EQ(U, V) ((U)->ut_type == (V))
178 #  define UT_TYPE_NOT_DEFINED 0
179 # else
180 #  define UT_TYPE_EQ(U, V) 0
181 #  define UT_TYPE_NOT_DEFINED 1
182 # endif
183
184 # ifdef BOOT_TIME
185 #  define UT_TYPE_BOOT_TIME(U) UT_TYPE_EQ (U, BOOT_TIME)
186 # else
187 #  define UT_TYPE_BOOT_TIME(U) 0
188 # endif
189
190 # ifdef USER_PROCESS
191 #  define UT_TYPE_USER_PROCESS(U) UT_TYPE_EQ (U, USER_PROCESS)
192 # else
193 #  define UT_TYPE_USER_PROCESS(U) 0
194 # endif
195
196 # define IS_USER_PROCESS(U)                                     \
197    (UT_USER (U)[0]                                              \
198     && (UT_TYPE_USER_PROCESS (U)                                \
199         || (UT_TYPE_NOT_DEFINED && UT_TIME_MEMBER (U) != 0)))
200
201 /* Options for read_utmp.  */
202 enum
203   {
204     READ_UTMP_CHECK_PIDS = 1
205   };
206
207 char *extract_trimmed_name (const STRUCT_UTMP *ut);
208 int read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,
209                int options);
210
211 #endif /* __READUTMP_H__ */