chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / sysdeps / unix / sysv / linux / s390 / s390-32 / utmp32.c
1 /* Copyright (C) 2008 Free Software Foundation, Inc.
2    Contributed by Andreas Krebbel <Andreas.Krebbel@de.ibm.com>.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library 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 GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <sys/types.h>
21 #include <utmp.h>
22 #include <errno.h>
23 #include <libc-symbols.h>
24
25 #include "utmp32.h"
26 #include "utmp-convert.h"
27
28 /* Allocate a static buffer to be returned to the caller.  As well as
29    with the existing version of these functions the caller has to be
30    aware that the contents of this buffer will change with subsequent
31    calls.  */
32 #define ALLOCATE_UTMP32_OUT(OUT)                        \
33   static struct utmp32 *OUT = NULL;                     \
34                                                         \
35   if (OUT == NULL)                                      \
36     {                                                   \
37       OUT = malloc (sizeof (struct utmp32));            \
38       if (OUT == NULL)                                  \
39         return NULL;                                    \
40     }
41
42 /* Perform a lookup for a utmp entry matching FIELD using function
43    FUNC.  FIELD is converted to a 64 bit utmp and the result is
44    converted back to 32 bit utmp.  */
45 #define ACCESS_UTMP_ENTRY(FUNC, FIELD)                  \
46   struct utmp in64;                                     \
47   struct utmp *out64;                                   \
48   ALLOCATE_UTMP32_OUT (out32);                          \
49                                                         \
50   utmp_convert32to64 (FIELD, &in64);                    \
51   out64 = FUNC (&in64);                                 \
52                                                         \
53   if (out64 == NULL)                                    \
54     return NULL;                                        \
55                                                         \
56   utmp_convert64to32 (out64, out32);                    \
57                                                         \
58   return out32;
59
60 /* Search forward from the current point in the utmp file until the
61    next entry with a ut_type matching ID->ut_type.  */
62 struct utmp32 *
63 getutid32 (const struct utmp32 *id)
64 {
65   ACCESS_UTMP_ENTRY (getutid, id)
66 }
67 symbol_version (getutid32, getutid, GLIBC_2.0);
68
69 /* Search forward from the current point in the utmp file until the
70    next entry with a ut_line matching LINE->ut_line.  */
71 struct utmp32 *
72 getutline32 (const struct utmp32 *line)
73 {
74   ACCESS_UTMP_ENTRY (getutline, line)
75 }
76 symbol_version (getutline32, getutline, GLIBC_2.0);
77
78 /* Write out entry pointed to by UTMP_PTR into the utmp file.  */
79 struct utmp32 *
80 pututline32 (const struct utmp32 *utmp_ptr)
81 {
82   ACCESS_UTMP_ENTRY (pututline, utmp_ptr)
83 }
84 symbol_version (pututline32, pututline, GLIBC_2.0);
85
86 /* Read next entry from a utmp-like file.  */
87 struct utmp32 *
88 getutent32 (void)
89 {
90   struct utmp *out64;
91   ALLOCATE_UTMP32_OUT (out32);
92
93   out64 = getutent ();
94   if (!out64)
95     return NULL;
96
97   utmp_convert64to32 (out64, out32);
98   return out32;
99 }
100 symbol_version (getutent32, getutent, GLIBC_2.0);
101
102 /* Reentrant versions of the file for handling utmp files.  */
103
104 int
105 getutent32_r (struct utmp32 *buffer, struct utmp32 **result)
106 {
107   struct utmp out64;
108   struct utmp *out64p;
109   int ret;
110
111   ret = getutent_r (&out64, &out64p);
112   if (ret == -1)
113     {
114       *result = NULL;
115       return -1;
116     }
117
118   utmp_convert64to32 (out64p, buffer);
119   *result = buffer;
120
121   return 0;
122 }
123 symbol_version (getutent32_r, getutent_r, GLIBC_2.0);
124
125 int
126 getutid32_r (const struct utmp32 *id, struct utmp32 *buffer,
127                struct utmp32 **result)
128 {
129   struct utmp in64;
130   struct utmp out64;
131   struct utmp *out64p;
132   int ret;
133
134   utmp_convert32to64 (id, &in64);
135
136   ret = getutid_r (&in64, &out64, &out64p);
137   if (ret == -1)
138     {
139       *result = NULL;
140       return -1;
141     }
142
143   utmp_convert64to32 (out64p, buffer);
144   *result = buffer;
145
146   return 0;
147 }
148 symbol_version (getutid32_r, getutid_r, GLIBC_2.0);
149
150 int
151 getutline32_r (const struct utmp32 *line,
152                  struct utmp32 *buffer, struct utmp32 **result)
153 {
154   struct utmp in64;
155   struct utmp out64;
156   struct utmp *out64p;
157   int ret;
158
159   utmp_convert32to64 (line, &in64);
160
161   ret = getutline_r (&in64, &out64, &out64p);
162   if (ret == -1)
163     {
164       *result = NULL;
165       return -1;
166     }
167
168   utmp_convert64to32 (out64p, buffer);
169   *result = buffer;
170
171   return 0;
172
173 }
174 symbol_version (getutline32_r, getutline_r, GLIBC_2.0);
175
176 /* Append entry UTMP to the wtmp-like file WTMP_FILE.  */
177 void
178 updwtmp32 (const char *wtmp_file, const struct utmp32 *utmp)
179 {
180   struct utmp in32;
181
182   utmp_convert32to64 (utmp, &in32);
183   updwtmp (wtmp_file, &in32);
184 }
185 symbol_version (updwtmp32, updwtmp, GLIBC_2.0);