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