(read_utmp): Add variant for systems that have the utmpname function.
[gnulib.git] / lib / readutmp.c
1 /* GNU's read utmp module.
2    Copyright (C) 92, 93, 94, 95, 96, 1997, 1998 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* Written by jla; revised by djm */
19
20 #include <config.h>
21
22 #include <sys/stat.h>
23 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
24 # include <string.h>
25 #else
26 # include <strings.h>
27 #endif /* STDC_HEADERS || HAVE_STRING_H */
28
29 #include "readutmp.h"
30
31 char *xmalloc ();
32
33 /* Copy UT->ut_name into storage obtained from malloc.  Then remove any
34    trailing spaces from the copy, NUL terminate it, and return the copy.  */
35
36 char *
37 extract_trimmed_name (const STRUCT_UTMP *ut)
38 {
39   char *p, *trimmed_name;
40
41   trimmed_name = xmalloc (sizeof (ut->ut_name) + 1);
42   strncpy (trimmed_name, ut->ut_name, sizeof (ut->ut_name));
43   /* Append a trailing space character.  Some systems pad names shorter than
44      the maximum with spaces, others pad with NULs.  Remove any spaces.  */
45   trimmed_name[sizeof (ut->ut_name)] = ' ';
46   p = strchr (trimmed_name, ' ');
47   if (p != NULL)
48     *p = '\0';
49   return trimmed_name;
50 }
51
52 /* Read the utmp file FILENAME into *UTMP_BUF, set *N_ENTRIES to the
53    number of entries read, and return zero.  If there is any error,
54    return non-zero and don't modify the parameters.  */
55
56 #ifdef HAVE_UTMPNAME
57
58 int
59 read_utmp (const char *filename, int *n_entries, STRUCT_UTMP **utmp_buf)
60 {
61   int count_utmp = 0;
62   int n_read;
63   STRUCT_UTMP *u;
64   STRUCT_UTMP *uptr;
65   STRUCT_UTMP *utmp_contents;
66
67   if (utmpname (filename))
68     {
69       return 1;
70     }
71
72   /* FIXME: going through the list twice is wasteful. */
73
74   /* count the entries in utmp */
75   setutent ();
76   while ((u = getutent ()) != NULL)
77     ++count_utmp;
78
79   if (count_utmp == 0)
80     return 0;
81
82   utmp_contents = (STRUCT_UTMP *) xmalloc (count_utmp * sizeof (STRUCT_UTMP));
83
84   /* read the entries in utmp */
85
86   /* FIXME: can this fail? */
87   setutent ();
88
89   n_read = 0;
90   uptr = utmp_contents;
91   while ((u = getutent ()) != NULL)
92     {
93       ++n_read;
94       if (n_read > count_utmp)
95         {
96           STRUCT_UTMP *old_utmp_contents = utmp_contents;
97           ++count_utmp;
98           utmp_contents = (STRUCT_UTMP *) xrealloc (utmp_contents,
99                                                     (count_utmp
100                                                      * sizeof (STRUCT_UTMP)));
101           uptr = utmp_contents + (uptr - old_utmp_contents);
102         }
103       *uptr = *u;
104       ++uptr;
105     }
106
107   if (n_read != count_utmp)
108     utmp_contents = (STRUCT_UTMP *) xrealloc (utmp_contents,
109                                               n_read * sizeof (STRUCT_UTMP));
110
111   /* FIXME: can this fail? */
112   endutent ();
113
114   *n_entries = n_read;
115   *utmp_buf = utmp_contents;
116
117   return 0;
118 }
119
120 #else
121
122 int
123 read_utmp (const char *filename, int *n_entries, STRUCT_UTMP **utmp_buf)
124 {
125   FILE *utmp;
126   struct stat file_stats;
127   size_t n_read;
128   size_t size;
129   STRUCT_UTMP *buf;
130
131   utmp = fopen (filename, "r");
132   if (utmp == NULL)
133     return 1;
134
135   fstat (fileno (utmp), &file_stats);
136   size = file_stats.st_size;
137   if (size > 0)
138     buf = (STRUCT_UTMP *) xmalloc (size);
139   else
140     {
141       fclose (utmp);
142       return 1;
143     }
144
145   /* Use < instead of != in case the utmp just grew.  */
146   n_read = fread (buf, 1, size, utmp);
147   if (ferror (utmp) || fclose (utmp) == EOF
148       || n_read < size)
149     return 1;
150
151   *n_entries = size / sizeof (STRUCT_UTMP);
152   *utmp_buf = buf;
153
154   return 0;
155 }
156
157 #endif /* HAVE_UTMPNAME */