1 /* GNU's read utmp module.
2 Copyright (C) 1992-1999 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 */
25 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
29 #endif /* STDC_HEADERS || HAVE_STRING_H */
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. */
40 extract_trimmed_name (const STRUCT_UTMP *ut)
42 char *p, *trimmed_name;
44 trimmed_name = xmalloc (sizeof (ut->ut_name) + 1);
45 strncpy (trimmed_name, ut->ut_name, sizeof (ut->ut_name));
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->ut_name)] = ' ';
49 p = strchr (trimmed_name, ' ');
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. */
63 read_utmp (const char *filename, int *n_entries, STRUCT_UTMP **utmp_buf)
67 STRUCT_UTMP *utmp = NULL;
69 if (utmpname (filename))
75 while ((u = getutent ()) != NULL)
78 utmp = (STRUCT_UTMP *) realloc (utmp, n_read * sizeof (STRUCT_UTMP));
81 utmp[n_read - 1] = *u;
95 read_utmp (const char *filename, int *n_entries, STRUCT_UTMP **utmp_buf)
98 struct stat file_stats;
103 utmp = fopen (filename, "r");
107 fstat (fileno (utmp), &file_stats);
108 size = file_stats.st_size;
110 buf = (STRUCT_UTMP *) xmalloc (size);
117 /* Use < instead of != in case the utmp just grew. */
118 n_read = fread (buf, 1, size, utmp);
119 if (ferror (utmp) || fclose (utmp) == EOF
123 *n_entries = size / sizeof (STRUCT_UTMP);
129 #endif /* HAVE_UTMPNAME */