2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2004 Dag-Erling Coïdan Smørgrav
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
66 static int everything = 1;
69 static const char *g_args;
70 static const char *l_args;
80 member(const char *elem, const char *list)
85 p = strstr(list, elem);
89 (p == list || p[-1] == ',') &&
90 (p[len] == '\0' || p[len] == ','));
98 if ((newptr = malloc(size)) == NULL)
104 xrealloc(void *ptr, size_t size)
108 if ((newptr = realloc(ptr, size)) == NULL)
114 xstrdup(const char *str)
118 if ((dupstr = strdup(str)) == NULL)
123 static struct xgroup *grps;
136 if (ngrps == grpsz) {
137 grpsz += grpsz ? grpsz : 128;
138 grps = xrealloc(grps, grpsz * sizeof *grps);
140 if ((grp = getgrent()) == NULL)
142 grps[ngrps].gr_name = xstrdup(grp->gr_name);
143 grps[ngrps].gr_passwd = xstrdup(grp->gr_passwd);
144 grps[ngrps].gr_gid = grp->gr_gid;
145 grps[ngrps].gr_mem = xstrdup("");
146 for (i = 0, len = 1; grp->gr_mem[i] != NULL; ++i)
147 len += strlen(grp->gr_mem[i]) + 1;
148 grps[ngrps].gr_mem = xmalloc(len);
149 for (i = 0, len = 0; grp->gr_mem[i] != NULL; ++i)
150 len += sprintf(grps[ngrps].gr_mem + len,
151 i ? ",%s" : "%s", grp->gr_mem[i]);
152 grps[ngrps].gr_mem[len] = '\0';
158 static struct xgroup *
159 find_group_bygid(gid_t gid)
163 for (i = 0; i < ngrps; ++i)
164 if (grps[i].gr_gid == gid)
170 static struct xgroup *
171 find_group_byname(const char *name)
175 for (i = 0; i < ngrps; ++i)
176 if (strcmp(grps[i].gr_name, name) == 0)
182 static struct xpasswd *pwds;
187 pwd_cmp_byname(const void *ap, const void *bp)
189 const struct passwd *a = ap;
190 const struct passwd *b = bp;
192 return (strcmp(a->pw_name, b->pw_name));
196 pwd_cmp_byuid(const void *ap, const void *bp)
198 const struct passwd *a = ap;
199 const struct passwd *b = bp;
201 return (a->pw_uid - b->pw_uid);
211 if (npwds == pwdsz) {
212 pwdsz += pwdsz ? pwdsz : 128;
213 pwds = xrealloc(pwds, pwdsz * sizeof *pwds);
215 if ((pwd = getpwent()) == NULL)
217 pwds[npwds].pw_name = xstrdup(pwd->pw_name);
218 pwds[npwds].pw_passwd = xstrdup(pwd->pw_passwd);
219 pwds[npwds].pw_uid = pwd->pw_uid;
220 pwds[npwds].pw_gid = pwd->pw_gid;
221 pwds[npwds].pw_change = pwd->pw_change;
222 pwds[npwds].pw_class = xstrdup(pwd->pw_class);
223 pwds[npwds].pw_gecos = xstrdup(pwd->pw_gecos);
224 pwds[npwds].pw_dir = xstrdup(pwd->pw_dir);
225 pwds[npwds].pw_shell = xstrdup(pwd->pw_shell);
226 pwds[npwds].pw_expire = pwd->pw_expire;
227 pwds[npwds].pw_selected = 0;
240 for (i = 0, pwd = pwds; i < npwds; ++i, ++pwd) {
242 pwd->pw_selected = 1;
246 if ((i > 0 && pwd->pw_uid == pwd[-1].pw_uid) ||
247 (i < npwds - 1 && pwd->pw_uid == pwd[1].pw_uid)) {
248 pwd->pw_selected = 1;
252 for (j = 0, grp = grps; j < ngrps; ++j, ++grp) {
253 if (member(grp->gr_name, g_args) &&
254 member(pwd->pw_name, grp->gr_mem)) {
255 pwd->pw_selected = 1;
259 if (pwd->pw_selected)
263 if (member(pwd->pw_name, l_args)) {
264 pwd->pw_selected = 1;
268 if (pwd->pw_passwd[0] == '\0') {
269 pwd->pw_selected = 1;
273 if (pwd->pw_uid < 1000 || pwd->pw_uid == 65534) {
274 pwd->pw_selected = 1;
278 if (pwd->pw_uid >= 1000 && pwd->pw_uid != 65534) {
279 pwd->pw_selected = 1;
289 mergesort(pwds, npwds, sizeof *pwds, pwd_cmp_byname);
291 mergesort(pwds, npwds, sizeof *pwds, pwd_cmp_byuid);
295 display_user(struct xpasswd *pwd)
299 char cbuf[16], ebuf[16];
302 grp = find_group_bygid(pwd->pw_gid);
303 printf(o_flag ? "%s:%ld:%s:%ld:%s" : "%-15s %-7ld %-15s %-7ld %s\n",
304 pwd->pw_name, (long)pwd->pw_uid, grp ? grp->gr_name : "",
305 (long)pwd->pw_gid, pwd->pw_gecos);
307 for (i = 0, grp = grps; i < ngrps; ++i, ++grp) {
308 if (grp->gr_gid == pwd->pw_gid ||
309 !member(pwd->pw_name, grp->gr_mem))
311 printf(o_flag ? "%s:%s:%ld" : "%24s%-15s %-7ld\n",
312 "", grp->gr_name, (long)grp->gr_gid);
316 printf(o_flag ? "%s:%s" : "%24s%s\n", "", pwd->pw_dir);
317 printf(o_flag ? "%s:%s" : "%24s%s\n", "", pwd->pw_shell);
320 tm = gmtime(&pwd->pw_change);
321 strftime(cbuf, sizeof(cbuf), pwd->pw_change ? "%F" : "0", tm);
322 tm = gmtime(&pwd->pw_expire);
323 strftime(ebuf, sizeof(ebuf), pwd->pw_expire ? "%F" : "0", tm);
324 printf(o_flag ? "%s:%s:%s" : "%24s%s %s\n", "", cbuf, ebuf);
336 for (i = 0, pwd = pwds; i < npwds; ++i, ++pwd)
337 if (pwd->pw_selected)
344 fprintf(stderr, "usage: logins [-admopstux] [-g group] [-l login]\n");
349 main(int argc, char * const argv[])
353 while ((o = getopt(argc, argv, "adg:l:mopstux")) != -1)