Include stdio.h.
[pspp] / lib / readutmp.c
1 /* GNU's read utmp module.
2    Copyright (C) 1992-1999 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/stat.h>
25 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
26 # include <string.h>
27 #else
28 # include <strings.h>
29 #endif /* STDC_HEADERS || HAVE_STRING_H */
30
31 #include "readutmp.h"
32
33 char *xmalloc ();
34
35 /* Copy UT->ut_name into storage obtained from malloc.  Then remove any
36    trailing spaces from the copy, NUL terminate it, and return the copy.  */
37
38 char *
39 extract_trimmed_name (const STRUCT_UTMP *ut)
40 {
41   char *p, *trimmed_name;
42
43   trimmed_name = xmalloc (sizeof (ut->ut_name) + 1);
44   strncpy (trimmed_name, ut->ut_name, sizeof (ut->ut_name));
45   /* Append a trailing space character.  Some systems pad names shorter than
46      the maximum with spaces, others pad with NULs.  Remove any spaces.  */
47   trimmed_name[sizeof (ut->ut_name)] = ' ';
48   p = strchr (trimmed_name, ' ');
49   if (p != NULL)
50     *p = '\0';
51   return trimmed_name;
52 }
53
54 /* Read the utmp file FILENAME into *UTMP_BUF, set *N_ENTRIES to the
55    number of entries read, and return zero.  If there is any error,
56    return non-zero and don't modify the parameters.  */
57
58 /* FIXME: use `#if HAVE_UTMPNAME' instead...  */
59 #if 0
60
61 int
62 read_utmp (const char *filename, int *n_entries, STRUCT_UTMP **utmp_buf)
63 {
64   int count_utmp = 0;
65   int n_read;
66   STRUCT_UTMP *u;
67   STRUCT_UTMP *uptr;
68   STRUCT_UTMP *utmp_contents;
69
70   if (utmpname (filename))
71     {
72       return 1;
73     }
74
75   /* FIXME: going through the list twice is wasteful. */
76
77   /* count the entries in utmp */
78   setutent ();
79   while ((u = getutent ()) != NULL)
80     ++count_utmp;
81
82   if (count_utmp == 0)
83     return 0;
84
85   utmp_contents = (STRUCT_UTMP *) xmalloc (count_utmp * sizeof (STRUCT_UTMP));
86
87   /* read the entries in utmp */
88
89   /* FIXME: can this fail? */
90   setutent ();
91
92   n_read = 0;
93   uptr = utmp_contents;
94   while ((u = getutent ()) != NULL)
95     {
96       ++n_read;
97       if (n_read > count_utmp)
98         {
99           STRUCT_UTMP *old_utmp_contents = utmp_contents;
100           ++count_utmp;
101           utmp_contents = (STRUCT_UTMP *) xrealloc (utmp_contents,
102                                                     (count_utmp
103                                                      * sizeof (STRUCT_UTMP)));
104           uptr = utmp_contents + (uptr - old_utmp_contents);
105         }
106       *uptr = *u;
107       ++uptr;
108     }
109
110   if (n_read != count_utmp)
111     utmp_contents = (STRUCT_UTMP *) xrealloc (utmp_contents,
112                                               n_read * sizeof (STRUCT_UTMP));
113
114   /* FIXME: can this fail? */
115   endutent ();
116
117   *n_entries = n_read;
118   *utmp_buf = utmp_contents;
119
120   return 0;
121 }
122
123 #else
124
125 int
126 read_utmp (const char *filename, int *n_entries, STRUCT_UTMP **utmp_buf)
127 {
128   FILE *utmp;
129   struct stat file_stats;
130   size_t n_read;
131   size_t size;
132   STRUCT_UTMP *buf;
133
134   utmp = fopen (filename, "r");
135   if (utmp == NULL)
136     return 1;
137
138   fstat (fileno (utmp), &file_stats);
139   size = file_stats.st_size;
140   if (size > 0)
141     buf = (STRUCT_UTMP *) xmalloc (size);
142   else
143     {
144       fclose (utmp);
145       return 1;
146     }
147
148   /* Use < instead of != in case the utmp just grew.  */
149   n_read = fread (buf, 1, size, utmp);
150   if (ferror (utmp) || fclose (utmp) == EOF
151       || n_read < size)
152     return 1;
153
154   *n_entries = size / sizeof (STRUCT_UTMP);
155   *utmp_buf = buf;
156
157   return 0;
158 }
159
160 #endif /* HAVE_UTMPNAME */