(read_utmp): Guard with `#ifdef UTMP_NAME_FUNCTION',
[gnulib.git] / lib / readutmp.c
1 /* GNU's read utmp module.
2    Copyright (C) 1992-2000 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 <stdio.h>
23
24 #include <sys/stat.h>
25 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
26 # include <string.h>
27 #else
28 # include <strings.h>
29 #endif /* STDC_HEADERS || HAVE_STRING_H */
30
31 #include "readutmp.h"
32
33 char *xmalloc ();
34 char *realloc ();
35
36 /* Copy UT->ut_name into storage obtained from malloc.  Then remove any
37    trailing spaces from the copy, NUL terminate it, and return the copy.  */
38
39 char *
40 extract_trimmed_name (const STRUCT_UTMP *ut)
41 {
42   char *p, *trimmed_name;
43
44   trimmed_name = xmalloc (sizeof (UT_USER (ut)) + 1);
45   strncpy (trimmed_name, UT_USER (ut), sizeof (UT_USER (ut)));
46   /* Append a trailing space character.  Some systems pad names shorter than
47      the maximum with spaces, others pad with NULs.  Remove any spaces.  */
48   trimmed_name[sizeof (UT_USER (ut))] = ' ';
49   p = strchr (trimmed_name, ' ');
50   if (p != NULL)
51     *p = '\0';
52   return trimmed_name;
53 }
54
55 /* Read the utmp entries corresponding to file FILENAME into freshly-
56    malloc'd storage, set *UTMP_BUF to that pointer, set *N_ENTRIES to
57    the number of entries, and return zero.  If there is any error,
58    return non-zero and don't modify the parameters.  */
59
60 #ifdef UTMP_NAME_FUNCTION
61
62 int
63 read_utmp (const char *filename, int *n_entries, STRUCT_UTMP **utmp_buf)
64 {
65   int n_read;
66   STRUCT_UTMP *u;
67   STRUCT_UTMP *utmp = NULL;
68
69   /* Ignore the return value for now.
70      Solaris' utmpname returns 1 upon success -- which is contrary
71      to what the GNU libc version does.  In addition, older GNU libc
72      versions are actually void.   */
73   UTMP_NAME_FUNCTION (filename);
74
75   SET_UTMP_ENT ();
76
77   n_read = 0;
78   while ((u = GET_UTMP_ENT ()) != NULL)
79     {
80       ++n_read;
81       utmp = (STRUCT_UTMP *) realloc (utmp, n_read * sizeof (STRUCT_UTMP));
82       if (utmp == NULL)
83         return 1;
84       utmp[n_read - 1] = *u;
85     }
86
87   END_UTMP_ENT ();
88
89   *n_entries = n_read;
90   *utmp_buf = utmp;
91
92   return 0;
93 }
94
95 #else
96
97 int
98 read_utmp (const char *filename, int *n_entries, STRUCT_UTMP **utmp_buf)
99 {
100   FILE *utmp;
101   struct stat file_stats;
102   size_t n_read;
103   size_t size;
104   STRUCT_UTMP *buf;
105
106   utmp = fopen (filename, "r");
107   if (utmp == NULL)
108     return 1;
109
110   fstat (fileno (utmp), &file_stats);
111   size = file_stats.st_size;
112   if (size > 0)
113     buf = (STRUCT_UTMP *) xmalloc (size);
114   else
115     {
116       fclose (utmp);
117       return 1;
118     }
119
120   /* Use < instead of != in case the utmp just grew.  */
121   n_read = fread (buf, 1, size, utmp);
122   if (ferror (utmp) || fclose (utmp) == EOF
123       || n_read < size)
124     return 1;
125
126   *n_entries = size / sizeof (STRUCT_UTMP);
127   *utmp_buf = buf;
128
129   return 0;
130 }
131
132 #endif /* HAVE_UTMPNAME */