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 */
23 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
27 #endif /* STDC_HEADERS || HAVE_STRING_H */
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. */
37 extract_trimmed_name (const STRUCT_UTMP *ut)
39 char *p, *trimmed_name;
41 trimmed_name = xmalloc (sizeof (ut->ut_name) + 1);
42 strncpy (trimmed_name, ut->ut_name, sizeof (ut->ut_name));
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->ut_name)] = ' ';
46 p = strchr (trimmed_name, ' ');
52 /* Read the utmp file FILENAME into *UTMP_BUF, set *N_ENTRIES to the
53 number of entries read, and return zero. If there is any error,
54 return non-zero and don't modify the parameters. */
56 /* FIXME: use `#if HAVE_UTMPNAME' instead... */
60 read_utmp (const char *filename, int *n_entries, STRUCT_UTMP **utmp_buf)
66 STRUCT_UTMP *utmp_contents;
68 if (utmpname (filename))
73 /* FIXME: going through the list twice is wasteful. */
75 /* count the entries in utmp */
77 while ((u = getutent ()) != NULL)
83 utmp_contents = (STRUCT_UTMP *) xmalloc (count_utmp * sizeof (STRUCT_UTMP));
85 /* read the entries in utmp */
87 /* FIXME: can this fail? */
92 while ((u = getutent ()) != NULL)
95 if (n_read > count_utmp)
97 STRUCT_UTMP *old_utmp_contents = utmp_contents;
99 utmp_contents = (STRUCT_UTMP *) xrealloc (utmp_contents,
101 * sizeof (STRUCT_UTMP)));
102 uptr = utmp_contents + (uptr - old_utmp_contents);
108 if (n_read != count_utmp)
109 utmp_contents = (STRUCT_UTMP *) xrealloc (utmp_contents,
110 n_read * sizeof (STRUCT_UTMP));
112 /* FIXME: can this fail? */
116 *utmp_buf = utmp_contents;
124 read_utmp (const char *filename, int *n_entries, STRUCT_UTMP **utmp_buf)
127 struct stat file_stats;
132 utmp = fopen (filename, "r");
136 fstat (fileno (utmp), &file_stats);
137 size = file_stats.st_size;
139 buf = (STRUCT_UTMP *) xmalloc (size);
146 /* Use < instead of != in case the utmp just grew. */
147 n_read = fread (buf, 1, size, utmp);
148 if (ferror (utmp) || fclose (utmp) == EOF
152 *n_entries = size / sizeof (STRUCT_UTMP);
158 #endif /* HAVE_UTMPNAME */