bitset: properly use false/true instead of 0/1 for Booleans
[gnulib.git] / lib / readutmp.c
blob1454faab2fd86630073c61a1a0e5957cfb6c4e6a
1 /* GNU's read utmp module.
3 Copyright (C) 1992-2001, 2003-2006, 2009-2018 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by jla; revised by djm */
20 #include <config.h>
22 #include "readutmp.h"
24 #include <errno.h>
25 #include <stdio.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <signal.h>
30 #include <stdbool.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <stdint.h>
35 #include "xalloc.h"
37 #if USE_UNLOCKED_IO
38 # include "unlocked-io.h"
39 #endif
41 #if 8 <= __GNUC__
42 # pragma GCC diagnostic ignored "-Wsizeof-pointer-memaccess"
43 #endif
45 /* Copy UT->ut_name into storage obtained from malloc. Then remove any
46 trailing spaces from the copy, NUL terminate it, and return the copy. */
48 char *
49 extract_trimmed_name (const STRUCT_UTMP *ut)
51 char *p, *trimmed_name;
53 trimmed_name = xmalloc (sizeof (UT_USER (ut)) + 1);
54 strncpy (trimmed_name, UT_USER (ut), sizeof (UT_USER (ut)));
55 /* Append a trailing NUL. Some systems pad names shorter than the
56 maximum with spaces, others pad with NULs. Remove any trailing
57 spaces. */
58 trimmed_name[sizeof (UT_USER (ut))] = '\0';
59 for (p = trimmed_name + strlen (trimmed_name);
60 trimmed_name < p && p[-1] == ' ';
61 *--p = '\0')
62 continue;
63 return trimmed_name;
66 /* Is the utmp entry U desired by the user who asked for OPTIONS? */
68 static bool
69 desirable_utmp_entry (STRUCT_UTMP const *u, int options)
71 bool user_proc = IS_USER_PROCESS (u);
72 if ((options & READ_UTMP_USER_PROCESS) && !user_proc)
73 return false;
74 if ((options & READ_UTMP_CHECK_PIDS)
75 && user_proc
76 && 0 < UT_PID (u)
77 && (kill (UT_PID (u), 0) < 0 && errno == ESRCH))
78 return false;
79 return true;
82 /* Read the utmp entries corresponding to file FILE into freshly-
83 malloc'd storage, set *UTMP_BUF to that pointer, set *N_ENTRIES to
84 the number of entries, and return zero. If there is any error,
85 return -1, setting errno, and don't modify the parameters.
86 If OPTIONS & READ_UTMP_CHECK_PIDS is nonzero, omit entries whose
87 process-IDs do not currently exist. */
89 #ifdef UTMP_NAME_FUNCTION
91 int
92 read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,
93 int options)
95 size_t n_read = 0;
96 size_t n_alloc = 0;
97 STRUCT_UTMP *utmp = NULL;
98 STRUCT_UTMP *u;
100 /* Ignore the return value for now.
101 Solaris' utmpname returns 1 upon success -- which is contrary
102 to what the GNU libc version does. In addition, older GNU libc
103 versions are actually void. */
104 UTMP_NAME_FUNCTION ((char *) file);
106 SET_UTMP_ENT ();
108 while ((u = GET_UTMP_ENT ()) != NULL)
109 if (desirable_utmp_entry (u, options))
111 if (n_read == n_alloc)
112 utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp);
114 utmp[n_read++] = *u;
117 END_UTMP_ENT ();
119 *n_entries = n_read;
120 *utmp_buf = utmp;
122 return 0;
125 #else
128 read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,
129 int options)
131 size_t n_read = 0;
132 size_t n_alloc = 0;
133 STRUCT_UTMP *utmp = NULL;
134 int saved_errno;
135 FILE *f = fopen (file, "r");
137 if (! f)
138 return -1;
140 for (;;)
142 if (n_read == n_alloc)
143 utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp);
144 if (fread (&utmp[n_read], sizeof utmp[n_read], 1, f) == 0)
145 break;
146 n_read += desirable_utmp_entry (&utmp[n_read], options);
149 saved_errno = ferror (f) ? errno : 0;
150 if (fclose (f) != 0)
151 saved_errno = errno;
152 if (saved_errno != 0)
154 free (utmp);
155 errno = saved_errno;
156 return -1;
159 *n_entries = n_read;
160 *utmp_buf = utmp;
162 return 0;
165 #endif