Merge from coreutils.
[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 NUL.  Some systems pad names shorter than the
44      maximum with spaces, others pad with NULs.  Remove any trailing
45      spaces.  */
46   trimmed_name[sizeof (UT_USER (ut))] = '\0';
47   for (p = trimmed_name + strlen (trimmed_name);
48        trimmed_name < p && p[-1] == ' ';
49        *--p = '\0')
50     continue;
51   return trimmed_name;
52 }
53
54 /* Read the utmp entries corresponding to file FILENAME into freshly-
55    malloc'd storage, set *UTMP_BUF to that pointer, set *N_ENTRIES to
56    the number of entries, and return zero.  If there is any error,
57    return -1, setting errno, and don't modify the parameters.  */
58
59 #ifdef UTMP_NAME_FUNCTION
60
61 int
62 read_utmp (const char *filename, size_t *n_entries, STRUCT_UTMP **utmp_buf)
63 {
64   size_t n_read;
65   size_t n_alloc = 4;
66   STRUCT_UTMP *utmp = xmalloc (n_alloc * sizeof *utmp);
67   STRUCT_UTMP *u;
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       if (n_read == n_alloc)
81         {
82           utmp = xnrealloc (utmp, n_alloc, 2 * sizeof *utmp);
83           n_alloc *= 2;
84         }
85       ++n_read;
86       utmp[n_read - 1] = *u;
87     }
88
89   END_UTMP_ENT ();
90
91   *n_entries = n_read;
92   *utmp_buf = utmp;
93
94   return 0;
95 }
96
97 #else
98
99 int
100 read_utmp (const char *filename, size_t *n_entries, STRUCT_UTMP **utmp_buf)
101 {
102   FILE *utmp;
103   struct stat file_stats;
104   size_t n_read;
105   size_t size;
106   STRUCT_UTMP *buf;
107
108   utmp = fopen (filename, "r");
109   if (utmp == NULL)
110     return -1;
111
112   if (fstat (fileno (utmp), &file_stats) != 0)
113     {
114       int e = errno;
115       fclose (utmp);
116       errno = e;
117       return -1;
118     }
119   size = file_stats.st_size;
120   buf = xmalloc (size);
121   n_read = fread (buf, sizeof *buf, size / sizeof *buf, utmp);
122   if (ferror (utmp))
123     {
124       int e = errno;
125       free (buf);
126       fclose (utmp);
127       errno = e;
128       return -1;
129     }
130   if (fclose (utmp) != 0)
131     {
132       int e = errno;
133       free (buf);
134       errno = e;
135       return -1;
136     }
137
138   *n_entries = n_read;
139   *utmp_buf = buf;
140
141   return 0;
142 }
143
144 #endif