Merge from vendor branch BIND:
[dragonfly.git] / contrib / tar / lib / readutmp.c
1 /* GNU's read utmp module.
2    Copyright (C) 1992-2000 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
34 char *xmalloc ();
35 char *realloc ();
36
37 /* Copy UT->ut_name into storage obtained from malloc.  Then remove any
38    trailing spaces from the copy, NUL terminate it, and return the copy.  */
39
40 char *
41 extract_trimmed_name (const STRUCT_UTMP *ut)
42 {
43   char *p, *trimmed_name;
44
45   trimmed_name = xmalloc (sizeof (UT_USER (ut)) + 1);
46   strncpy (trimmed_name, UT_USER (ut), sizeof (UT_USER (ut)));
47   /* Append a trailing space character.  Some systems pad names shorter than
48      the maximum with spaces, others pad with NULs.  Remove any spaces.  */
49   trimmed_name[sizeof (UT_USER (ut))] = ' ';
50   p = strchr (trimmed_name, ' ');
51   if (p != NULL)
52     *p = '\0';
53   return trimmed_name;
54 }
55
56 /* Read the utmp entries corresponding to file FILENAME into freshly-
57    malloc'd storage, set *UTMP_BUF to that pointer, set *N_ENTRIES to
58    the number of entries, and return zero.  If there is any error,
59    return non-zero and don't modify the parameters.  */
60
61 #ifdef UTMP_NAME_FUNCTION
62
63 int
64 read_utmp (const char *filename, int *n_entries, STRUCT_UTMP **utmp_buf)
65 {
66   int n_read;
67   STRUCT_UTMP *u;
68   STRUCT_UTMP *utmp = NULL;
69
70   /* Ignore the return value for now.
71      Solaris' utmpname returns 1 upon success -- which is contrary
72      to what the GNU libc version does.  In addition, older GNU libc
73      versions are actually void.   */
74   UTMP_NAME_FUNCTION (filename);
75
76   SET_UTMP_ENT ();
77
78   n_read = 0;
79   while ((u = GET_UTMP_ENT ()) != NULL)
80     {
81       ++n_read;
82       utmp = (STRUCT_UTMP *) realloc (utmp, n_read * sizeof (STRUCT_UTMP));
83       if (utmp == NULL)
84         return 1;
85       utmp[n_read - 1] = *u;
86     }
87
88   END_UTMP_ENT ();
89
90   *n_entries = n_read;
91   *utmp_buf = utmp;
92
93   return 0;
94 }
95
96 #else
97
98 int
99 read_utmp (const char *filename, int *n_entries, STRUCT_UTMP **utmp_buf)
100 {
101   FILE *utmp;
102   struct stat file_stats;
103   size_t n_read;
104   size_t size;
105   STRUCT_UTMP *buf;
106
107   utmp = fopen (filename, "r");
108   if (utmp == NULL)
109     return 1;
110
111   fstat (fileno (utmp), &file_stats);
112   size = file_stats.st_size;
113   if (size > 0)
114     buf = (STRUCT_UTMP *) xmalloc (size);
115   else
116     {
117       fclose (utmp);
118       return 1;
119     }
120
121   /* Use < instead of != in case the utmp just grew.  */
122   n_read = fread (buf, 1, size, utmp);
123   if (ferror (utmp) || fclose (utmp) == EOF
124       || n_read < size)
125     return 1;
126
127   *n_entries = size / sizeof (STRUCT_UTMP);
128   *utmp_buf = buf;
129
130   return 0;
131 }
132
133 #endif