Include readutmp.h first.
[gnulib.git] / lib / readutmp.c
1 /* GNU's read utmp module.
2    Copyright (C) 1992-2001, 2003, 2004 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 "readutmp.h"
23
24 #include <errno.h>
25 #include <stdio.h>
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <string.h>
30 #include <stdlib.h>
31
32 #include "xalloc.h"
33
34 #if USE_UNLOCKED_IO
35 # include "unlocked-io.h"
36 #endif
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 NUL.  Some systems pad names shorter than the
49      maximum with spaces, others pad with NULs.  Remove any trailing
50      spaces.  */
51   trimmed_name[sizeof (UT_USER (ut))] = '\0';
52   for (p = trimmed_name + strlen (trimmed_name);
53        trimmed_name < p && p[-1] == ' ';
54        *--p = '\0')
55     continue;
56   return trimmed_name;
57 }
58
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.  */
63
64 #ifdef UTMP_NAME_FUNCTION
65
66 int
67 read_utmp (const char *filename, size_t *n_entries, STRUCT_UTMP **utmp_buf)
68 {
69   size_t n_read;
70   size_t n_alloc = 4;
71   STRUCT_UTMP *utmp = xmalloc (n_alloc * sizeof *utmp);
72   STRUCT_UTMP *u;
73
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);
79
80   SET_UTMP_ENT ();
81
82   n_read = 0;
83   while ((u = GET_UTMP_ENT ()) != NULL)
84     {
85       if (n_read == n_alloc)
86         {
87           utmp = xnrealloc (utmp, n_alloc, 2 * sizeof *utmp);
88           n_alloc *= 2;
89         }
90       ++n_read;
91       utmp[n_read - 1] = *u;
92     }
93
94   END_UTMP_ENT ();
95
96   *n_entries = n_read;
97   *utmp_buf = utmp;
98
99   return 0;
100 }
101
102 #else
103
104 int
105 read_utmp (const char *filename, size_t *n_entries, STRUCT_UTMP **utmp_buf)
106 {
107   FILE *utmp;
108   struct stat file_stats;
109   size_t n_read;
110   size_t size;
111   STRUCT_UTMP *buf;
112
113   utmp = fopen (filename, "r");
114   if (utmp == NULL)
115     return -1;
116
117   if (fstat (fileno (utmp), &file_stats) != 0)
118     {
119       int e = errno;
120       fclose (utmp);
121       errno = e;
122       return -1;
123     }
124   size = file_stats.st_size;
125   buf = xmalloc (size);
126   n_read = fread (buf, sizeof *buf, size / sizeof *buf, utmp);
127   if (ferror (utmp))
128     {
129       int e = errno;
130       free (buf);
131       fclose (utmp);
132       errno = e;
133       return -1;
134     }
135   if (fclose (utmp) != 0)
136     {
137       int e = errno;
138       free (buf);
139       errno = e;
140       return -1;
141     }
142
143   *n_entries = n_read;
144   *utmp_buf = buf;
145
146   return 0;
147 }
148
149 #endif