Remove dependencies on unlocked-io.
[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 <stdio.h>
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <string.h>
27 #include <stdlib.h>
28
29 #include "readutmp.h"
30 #include "xalloc.h"
31
32 #if USE_UNLOCKED_IO
33 # include "unlocked-io.h"
34 #endif
35
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.  */
38
39 char *
40 extract_trimmed_name (const STRUCT_UTMP *ut)
41 {
42   char *p, *trimmed_name;
43
44   trimmed_name = xmalloc (sizeof (UT_USER (ut)) + 1);
45   strncpy (trimmed_name, UT_USER (ut), sizeof (UT_USER (ut)));
46   /* Append a trailing NUL.  Some systems pad names shorter than the
47      maximum with spaces, others pad with NULs.  Remove any trailing
48      spaces.  */
49   trimmed_name[sizeof (UT_USER (ut))] = '\0';
50   for (p = trimmed_name + strlen (trimmed_name);
51        trimmed_name < p && p[-1] == ' ';
52        *--p = '\0')
53     continue;
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 -1, setting errno, and don't modify the parameters.  */
61
62 #ifdef UTMP_NAME_FUNCTION
63
64 int
65 read_utmp (const char *filename, size_t *n_entries, STRUCT_UTMP **utmp_buf)
66 {
67   size_t n_read;
68   size_t n_alloc = 4;
69   STRUCT_UTMP *utmp = xmalloc (n_alloc * sizeof *utmp);
70   STRUCT_UTMP *u;
71
72   /* Ignore the return value for now.
73      Solaris' utmpname returns 1 upon success -- which is contrary
74      to what the GNU libc version does.  In addition, older GNU libc
75      versions are actually void.   */
76   UTMP_NAME_FUNCTION (filename);
77
78   SET_UTMP_ENT ();
79
80   n_read = 0;
81   while ((u = GET_UTMP_ENT ()) != NULL)
82     {
83       if (n_read == n_alloc)
84         {
85           utmp = xnrealloc (utmp, n_alloc, 2 * sizeof *utmp);
86           n_alloc *= 2;
87         }
88       ++n_read;
89       utmp[n_read - 1] = *u;
90     }
91
92   END_UTMP_ENT ();
93
94   *n_entries = n_read;
95   *utmp_buf = utmp;
96
97   return 0;
98 }
99
100 #else
101
102 int
103 read_utmp (const char *filename, size_t *n_entries, STRUCT_UTMP **utmp_buf)
104 {
105   FILE *utmp;
106   struct stat file_stats;
107   size_t n_read;
108   size_t size;
109   STRUCT_UTMP *buf;
110
111   utmp = fopen (filename, "r");
112   if (utmp == NULL)
113     return -1;
114
115   if (fstat (fileno (utmp), &file_stats) != 0)
116     {
117       int e = errno;
118       fclose (utmp);
119       errno = e;
120       return -1;
121     }
122   size = file_stats.st_size;
123   buf = xmalloc (size);
124   n_read = fread (buf, sizeof *buf, size / sizeof *buf, utmp);
125   if (ferror (utmp))
126     {
127       int e = errno;
128       free (buf);
129       fclose (utmp);
130       errno = e;
131       return -1;
132     }
133   if (fclose (utmp) != 0)
134     {
135       int e = errno;
136       free (buf);
137       errno = e;
138       return -1;
139     }
140
141   *n_entries = n_read;
142   *utmp_buf = buf;
143
144   return 0;
145 }
146
147 #endif