Include unlocked-io.h.
[gnulib.git] / lib / readutmp.c
1 /* GNU's read utmp module.
2    Copyright (C) 1992-2001 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 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
27 # include <string.h>
28 #else
29 # include <strings.h>
30 #endif /* STDC_HEADERS || HAVE_STRING_H */
31
32 #include "readutmp.h"
33 #include "unlocked-io.h"
34
35 char *xmalloc ();
36 char *realloc ();
37
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.  */
40
41 char *
42 extract_trimmed_name (const STRUCT_UTMP *ut)
43 {
44   char *p, *trimmed_name;
45
46   trimmed_name = xmalloc (sizeof (UT_USER (ut)) + 1);
47   strncpy (trimmed_name, UT_USER (ut), sizeof (UT_USER (ut)));
48   /* Append a trailing space character.  Some systems pad names shorter than
49      the maximum with spaces, others pad with NULs.  Remove any spaces.  */
50   trimmed_name[sizeof (UT_USER (ut))] = ' ';
51   p = strchr (trimmed_name, ' ');
52   if (p != NULL)
53     *p = '\0';
54   return trimmed_name;
55 }
56
57 /* Read the utmp entries corresponding to file FILENAME into freshly-
58    malloc'd storage, set *UTMP_BUF to that pointer, set *N_ENTRIES to
59    the number of entries, and return zero.  If there is any error,
60    return non-zero and don't modify the parameters.  */
61
62 #ifdef UTMP_NAME_FUNCTION
63
64 int
65 read_utmp (const char *filename, int *n_entries, STRUCT_UTMP **utmp_buf)
66 {
67   int n_read;
68   STRUCT_UTMP *u;
69   STRUCT_UTMP *utmp = NULL;
70
71   /* Ignore the return value for now.
72      Solaris' utmpname returns 1 upon success -- which is contrary
73      to what the GNU libc version does.  In addition, older GNU libc
74      versions are actually void.   */
75   UTMP_NAME_FUNCTION (filename);
76
77   SET_UTMP_ENT ();
78
79   n_read = 0;
80   while ((u = GET_UTMP_ENT ()) != NULL)
81     {
82       ++n_read;
83       utmp = (STRUCT_UTMP *) realloc (utmp, n_read * sizeof (STRUCT_UTMP));
84       if (utmp == NULL)
85         return 1;
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, int *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   fstat (fileno (utmp), &file_stats);
113   size = file_stats.st_size;
114   if (size > 0)
115     buf = (STRUCT_UTMP *) xmalloc (size);
116   else
117     {
118       fclose (utmp);
119       return 1;
120     }
121
122   /* Use < instead of != in case the utmp just grew.  */
123   n_read = fread (buf, 1, size, utmp);
124   if (ferror (utmp) || fclose (utmp) == EOF
125       || n_read < size)
126     return 1;
127
128   *n_entries = size / sizeof (STRUCT_UTMP);
129   *utmp_buf = buf;
130
131   return 0;
132 }
133
134 #endif