Thu Apr 18 16:09:09 2024 UTC (22d)
Pull up following revision(s) (requested by riastradh in ticket #1833):

	usr.bin/who/utmpentry.c: revision 1.22

PR/56013: Kouichi Hashikawa: Move setutent/setutxent right before the loops.


(martin)
diff -r1.18 -r1.18.18.1 src/usr.bin/who/utmpentry.c

cvs diff -r1.18 -r1.18.18.1 src/usr.bin/who/utmpentry.c (expand / switch to unified diff)

--- src/usr.bin/who/utmpentry.c 2015/11/21 15:01:43 1.18
+++ src/usr.bin/who/utmpentry.c 2024/04/18 16:09:09 1.18.18.1
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: utmpentry.c,v 1.18 2015/11/21 15:01:43 christos Exp $ */ 1/* $NetBSD: utmpentry.c,v 1.18.18.1 2024/04/18 16:09:09 martin Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc. 4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas. 8 * by Christos Zoulas.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -21,27 +21,27 @@ @@ -21,27 +21,27 @@
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33#ifndef lint 33#ifndef lint
34__RCSID("$NetBSD: utmpentry.c,v 1.18 2015/11/21 15:01:43 christos Exp $"); 34__RCSID("$NetBSD: utmpentry.c,v 1.18.18.1 2024/04/18 16:09:09 martin Exp $");
35#endif 35#endif
36 36
37#include <sys/stat.h> 37#include <sys/stat.h>
38 38
39#include <time.h> 39#include <time.h>
40#include <string.h> 40#include <string.h>
41#include <err.h> 41#include <err.h>
42#include <stdlib.h> 42#include <stdlib.h>
43 43
44#ifdef SUPPORT_UTMP 44#ifdef SUPPORT_UTMP
45#include <utmp.h> 45#include <utmp.h>
46#endif 46#endif
47#ifdef SUPPORT_UTMPX 47#ifdef SUPPORT_UTMPX
@@ -85,34 +85,27 @@ adjust_size(struct utmpentry *e) @@ -85,34 +85,27 @@ adjust_size(struct utmpentry *e)
85 if ((max = strlen(e->line)) > maxline) 85 if ((max = strlen(e->line)) > maxline)
86 maxline = max; 86 maxline = max;
87 if ((max = strlen(e->host)) > maxhost) 87 if ((max = strlen(e->host)) > maxhost)
88 maxhost = max; 88 maxhost = max;
89} 89}
90 90
91static int 91static int
92setup(const char *fname) 92setup(const char *fname)
93{ 93{
94 int what = 3; 94 int what = 3;
95 struct stat st; 95 struct stat st;
96 const char *sfname; 96 const char *sfname;
97 97
98 if (fname == NULL) { 98 if (fname != NULL) {
99#ifdef SUPPORT_UTMPX 
100 setutxent(); 
101#endif 
102#ifdef SUPPORT_UTMP 
103 setutent(); 
104#endif 
105 } else { 
106 size_t len = strlen(fname); 99 size_t len = strlen(fname);
107 if (len == 0) 100 if (len == 0)
108 errx(1, "Filename cannot be 0 length."); 101 errx(1, "Filename cannot be 0 length.");
109 what = fname[len - 1] == 'x' ? 1 : 2; 102 what = fname[len - 1] == 'x' ? 1 : 2;
110 if (what == 1) { 103 if (what == 1) {
111#ifdef SUPPORT_UTMPX 104#ifdef SUPPORT_UTMPX
112 if (utmpxname(fname) == 0) 105 if (utmpxname(fname) == 0)
113 warnx("Cannot set utmpx file to `%s'", 106 warnx("Cannot set utmpx file to `%s'",
114 fname); 107 fname);
115#else 108#else
116 warnx("utmpx support not compiled in"); 109 warnx("utmpx support not compiled in");
117#endif 110#endif
118 } else { 111 } else {
@@ -123,29 +116,29 @@ setup(const char *fname) @@ -123,29 +116,29 @@ setup(const char *fname)
123#else 116#else
124 warnx("utmp support not compiled in"); 117 warnx("utmp support not compiled in");
125#endif 118#endif
126 } 119 }
127 } 120 }
128#ifdef SUPPORT_UTMPX 121#ifdef SUPPORT_UTMPX
129 if (what & 1) { 122 if (what & 1) {
130 sfname = fname ? fname : _PATH_UTMPX; 123 sfname = fname ? fname : _PATH_UTMPX;
131 if (stat(sfname, &st) == -1) { 124 if (stat(sfname, &st) == -1) {
132 warn("Cannot stat `%s'", sfname); 125 warn("Cannot stat `%s'", sfname);
133 what &= ~1; 126 what &= ~1;
134 } else { 127 } else {
135 if (timespeccmp(&st.st_mtimespec, &utmpxtime, >)) 128 if (timespeccmp(&st.st_mtimespec, &utmpxtime, >))
136 utmpxtime = st.st_mtimespec; 129 utmpxtime = st.st_mtimespec;
137 else 130 else
138 what &= ~1; 131 what &= ~1;
139 } 132 }
140 } 133 }
141#endif 134#endif
142#ifdef SUPPORT_UTMP 135#ifdef SUPPORT_UTMP
143 if (what & 2) { 136 if (what & 2) {
144 sfname = fname ? fname : _PATH_UTMP; 137 sfname = fname ? fname : _PATH_UTMP;
145 if (stat(sfname, &st) == -1) { 138 if (stat(sfname, &st) == -1) {
146 warn("Cannot stat `%s'", sfname); 139 warn("Cannot stat `%s'", sfname);
147 what &= ~2; 140 what &= ~2;
148 } else { 141 } else {
149 if (timespeccmp(&st.st_mtimespec, &utmptime, >)) 142 if (timespeccmp(&st.st_mtimespec, &utmptime, >))
150 utmptime = st.st_mtimespec; 143 utmptime = st.st_mtimespec;
151 else 144 else
@@ -194,40 +187,42 @@ getutentries(const char *fname, struct u @@ -194,40 +187,42 @@ getutentries(const char *fname, struct u
194 switch (what) { 187 switch (what) {
195 case 0: 188 case 0:
196 /* No updates */ 189 /* No updates */
197 *epp = ehead; 190 *epp = ehead;
198 return numutmp; 191 return numutmp;
199 default: 192 default:
200 /* Need to re-scan */ 193 /* Need to re-scan */
201 ehead = NULL; 194 ehead = NULL;
202 numutmp = 0; 195 numutmp = 0;
203 } 196 }
204#endif 197#endif
205 198
206#ifdef SUPPORT_UTMPX 199#ifdef SUPPORT_UTMPX
 200 setutxent();
207 while ((what & 1) && (utx = getutxent()) != NULL) { 201 while ((what & 1) && (utx = getutxent()) != NULL) {
208 if (fname == NULL && ((1 << utx->ut_type) & etype) == 0) 202 if (fname == NULL && ((1 << utx->ut_type) & etype) == 0)
209 continue; 203 continue;
210 if ((ep = calloc(1, sizeof(struct utmpentry))) == NULL) { 204 if ((ep = calloc(1, sizeof(*ep))) == NULL) {
211 warn(NULL); 205 warn(NULL);
212 return 0; 206 return 0;
213 } 207 }
214 getentryx(ep, utx); 208 getentryx(ep, utx);
215 *nextp = ep; 209 *nextp = ep;
216 nextp = &(ep->next); 210 nextp = &(ep->next);
217 } 211 }
218#endif 212#endif
219 213
220#ifdef SUPPORT_UTMP 214#ifdef SUPPORT_UTMP
 215 setutent();
221 if ((etype & (1 << USER_PROCESS)) != 0) { 216 if ((etype & (1 << USER_PROCESS)) != 0) {
222 while ((what & 2) && (ut = getutent()) != NULL) { 217 while ((what & 2) && (ut = getutent()) != NULL) {
223 if (fname == NULL && (*ut->ut_name == '\0' || 218 if (fname == NULL && (*ut->ut_name == '\0' ||
224 *ut->ut_line == '\0')) 219 *ut->ut_line == '\0'))
225 continue; 220 continue;
226 /* Don't process entries that we have utmpx for */ 221 /* Don't process entries that we have utmpx for */
227 for (ep = ehead; ep != NULL; ep = ep->next) { 222 for (ep = ehead; ep != NULL; ep = ep->next) {
228 if (strncmp(ep->line, ut->ut_line, 223 if (strncmp(ep->line, ut->ut_line,
229 sizeof(ut->ut_line)) == 0) 224 sizeof(ut->ut_line)) == 0)
230 break; 225 break;
231 } 226 }
232 if (ep != NULL) 227 if (ep != NULL)
233 continue; 228 continue;