(read_utmp) [UTMP_NAME_FUNCTION]: Upon realloc failure,
[gnulib.git] / lib / readutmp.c
1 /* GNU's read utmp module.
2    Copyright (C) 1992-2001, 2003, 2004 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/types.h>
25 #include <sys/stat.h>
26 #include <string.h>
27 #include <stdlib.h>
28
29 #include "readutmp.h"
30 #include "unlocked-io.h"
31 #include "xalloc.h"
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_USER (ut)) + 1);
42   strncpy (trimmed_name, UT_USER (ut), sizeof (UT_USER (ut)));
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_USER (ut))] = ' ';
46   p = strchr (trimmed_name, ' ');
47   if (p != NULL)
48     *p = '\0';
49   return trimmed_name;
50 }
51
52 /* Read the utmp entries corresponding to file FILENAME into freshly-
53    malloc'd storage, set *UTMP_BUF to that pointer, set *N_ENTRIES to
54    the number of entries, and return zero.  If there is any error,
55    return non-zero and don't modify the parameters.  */
56
57 #ifdef UTMP_NAME_FUNCTION
58
59 int
60 read_utmp (const char *filename, int *n_entries, STRUCT_UTMP **utmp_buf)
61 {
62   int n_read;
63   STRUCT_UTMP *u;
64   STRUCT_UTMP *utmp = NULL;
65
66   /* Ignore the return value for now.
67      Solaris' utmpname returns 1 upon success -- which is contrary
68      to what the GNU libc version does.  In addition, older GNU libc
69      versions are actually void.   */
70   UTMP_NAME_FUNCTION (filename);
71
72   SET_UTMP_ENT ();
73
74   n_read = 0;
75   while ((u = GET_UTMP_ENT ()) != NULL)
76     {
77       STRUCT_UTMP *p;
78       ++n_read;
79       p = (STRUCT_UTMP *) realloc (utmp, n_read * sizeof (STRUCT_UTMP));
80       if (p == NULL)
81         {
82           free (utmp);
83           END_UTMP_ENT ();
84           return 1;
85         }
86       utmp = p;
87       utmp[n_read - 1] = *u;
88     }
89
90   END_UTMP_ENT ();
91
92   *n_entries = n_read;
93   *utmp_buf = utmp;
94
95   return 0;
96 }
97
98 #else
99
100 int
101 read_utmp (const char *filename, int *n_entries, STRUCT_UTMP **utmp_buf)
102 {
103   FILE *utmp;
104   struct stat file_stats;
105   size_t n_read;
106   size_t size;
107   STRUCT_UTMP *buf;
108
109   utmp = fopen (filename, "r");
110   if (utmp == NULL)
111     return 1;
112
113   if (fstat (fileno (utmp), &file_stats) != 0)
114     {
115       int e = errno;
116       fclose (utmp);
117       errno = e;
118       return 1;
119     }
120   size = file_stats.st_size;
121   buf = xmalloc (size);
122   n_read = fread (buf, sizeof *buf, size / sizeof *buf, utmp);
123   if (ferror (utmp))
124     {
125       int e = errno;
126       free (buf);
127       fclose (utmp);
128       errno = e;
129       return 1;
130     }
131   if (fclose (utmp) != 0)
132     {
133       int e = errno;
134       free (buf);
135       errno = e;
136       return 1;
137     }
138
139   *n_entries = n_read;
140   *utmp_buf = buf;
141
142   return 0;
143 }
144
145 #endif