1 /* GNU's read utmp module.
2 Copyright (C) 1992-2001, 2003, 2004 Free Software Foundation, Inc.
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)
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.
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. */
18 /* Written by jla; revised by djm */
27 #include <sys/types.h>
35 # include "unlocked-io.h"
38 /* Copy UT->ut_name into storage obtained from malloc. Then remove any
39 trailing spaces from the copy, NUL terminate it, and return the copy. */
42 extract_trimmed_name (const STRUCT_UTMP *ut)
44 char *p, *trimmed_name;
46 trimmed_name = xmalloc (sizeof (UT_USER (ut)) + 1);
47 strncpy (trimmed_name, UT_USER (ut), sizeof (UT_USER (ut)));
48 /* Append a trailing NUL. Some systems pad names shorter than the
49 maximum with spaces, others pad with NULs. Remove any trailing
51 trimmed_name[sizeof (UT_USER (ut))] = '\0';
52 for (p = trimmed_name + strlen (trimmed_name);
53 trimmed_name < p && p[-1] == ' ';
59 /* Read the utmp entries corresponding to file FILENAME into freshly-
60 malloc'd storage, set *UTMP_BUF to that pointer, set *N_ENTRIES to
61 the number of entries, and return zero. If there is any error,
62 return -1, setting errno, and don't modify the parameters. */
64 #ifdef UTMP_NAME_FUNCTION
67 read_utmp (const char *filename, size_t *n_entries, STRUCT_UTMP **utmp_buf)
71 STRUCT_UTMP *utmp = xmalloc (n_alloc * sizeof *utmp);
74 /* Ignore the return value for now.
75 Solaris' utmpname returns 1 upon success -- which is contrary
76 to what the GNU libc version does. In addition, older GNU libc
77 versions are actually void. */
78 UTMP_NAME_FUNCTION (filename);
83 while ((u = GET_UTMP_ENT ()) != NULL)
85 if (n_read == n_alloc)
87 utmp = xnrealloc (utmp, n_alloc, 2 * sizeof *utmp);
91 utmp[n_read - 1] = *u;
105 read_utmp (const char *filename, size_t *n_entries, STRUCT_UTMP **utmp_buf)
108 struct stat file_stats;
113 utmp = fopen (filename, "r");
117 if (fstat (fileno (utmp), &file_stats) != 0)
124 size = file_stats.st_size;
125 buf = xmalloc (size);
126 n_read = fread (buf, sizeof *buf, size / sizeof *buf, utmp);
135 if (fclose (utmp) != 0)