2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2004 Dag-Erling 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.
31 #include <sys/cdefs.h>
62 static int everything = 1;
65 static const char *g_args;
66 static const char *l_args;
76 member(const char *elem, const char *list)
81 p = strstr(list, elem);
85 (p == list || p[-1] == ',') &&
86 (p[len] == '\0' || p[len] == ','));
94 if ((newptr = malloc(size)) == NULL)
100 xrealloc(void *ptr, size_t size)
104 if ((newptr = realloc(ptr, size)) == NULL)
110 xstrdup(const char *str)
114 if ((dupstr = strdup(str)) == NULL)
119 static struct xgroup *grps;
132 if (ngrps == grpsz) {
133 grpsz += grpsz ? grpsz : 128;
134 grps = xrealloc(grps, grpsz * sizeof *grps);
136 if ((grp = getgrent()) == NULL)
138 grps[ngrps].gr_name = xstrdup(grp->gr_name);
139 grps[ngrps].gr_passwd = xstrdup(grp->gr_passwd);
140 grps[ngrps].gr_gid = grp->gr_gid;
141 grps[ngrps].gr_mem = xstrdup("");
142 for (i = 0, len = 1; grp->gr_mem[i] != NULL; ++i)
143 len += strlen(grp->gr_mem[i]) + 1;
144 grps[ngrps].gr_mem = xmalloc(len);
145 for (i = 0, len = 0; grp->gr_mem[i] != NULL; ++i)
146 len += sprintf(grps[ngrps].gr_mem + len,
147 i ? ",%s" : "%s", grp->gr_mem[i]);
148 grps[ngrps].gr_mem[len] = '\0';
154 static struct xgroup *
155 find_group_bygid(gid_t gid)
159 for (i = 0; i < ngrps; ++i)
160 if (grps[i].gr_gid == gid)
166 static struct xgroup *
167 find_group_byname(const char *name)
171 for (i = 0; i < ngrps; ++i)
172 if (strcmp(grps[i].gr_name, name) == 0)
178 static struct xpasswd *pwds;
183 pwd_cmp_byname(const void *ap, const void *bp)
185 const struct passwd *a = ap;
186 const struct passwd *b = bp;
188 return (strcmp(a->pw_name, b->pw_name));
192 pwd_cmp_byuid(const void *ap, const void *bp)
194 const struct passwd *a = ap;
195 const struct passwd *b = bp;
197 return (a->pw_uid - b->pw_uid);
207 if (npwds == pwdsz) {
208 pwdsz += pwdsz ? pwdsz : 128;
209 pwds = xrealloc(pwds, pwdsz * sizeof *pwds);
211 if ((pwd = getpwent()) == NULL)
213 pwds[npwds].pw_name = xstrdup(pwd->pw_name);
214 pwds[npwds].pw_passwd = xstrdup(pwd->pw_passwd);
215 pwds[npwds].pw_uid = pwd->pw_uid;
216 pwds[npwds].pw_gid = pwd->pw_gid;
217 pwds[npwds].pw_change = pwd->pw_change;
218 pwds[npwds].pw_class = xstrdup(pwd->pw_class);
219 pwds[npwds].pw_gecos = xstrdup(pwd->pw_gecos);
220 pwds[npwds].pw_dir = xstrdup(pwd->pw_dir);
221 pwds[npwds].pw_shell = xstrdup(pwd->pw_shell);
222 pwds[npwds].pw_expire = pwd->pw_expire;
223 pwds[npwds].pw_selected = 0;
236 for (i = 0, pwd = pwds; i < npwds; ++i, ++pwd) {
238 pwd->pw_selected = 1;
242 if ((i > 0 && pwd->pw_uid == pwd[-1].pw_uid) ||
243 (i < npwds - 1 && pwd->pw_uid == pwd[1].pw_uid)) {
244 pwd->pw_selected = 1;
248 for (j = 0, grp = grps; j < ngrps; ++j, ++grp) {
249 if (member(grp->gr_name, g_args) &&
250 member(pwd->pw_name, grp->gr_mem)) {
251 pwd->pw_selected = 1;
255 if (pwd->pw_selected)
259 if (member(pwd->pw_name, l_args)) {
260 pwd->pw_selected = 1;
264 if (pwd->pw_passwd[0] == '\0') {
265 pwd->pw_selected = 1;
269 if (pwd->pw_uid < 1000 || pwd->pw_uid == 65534) {
270 pwd->pw_selected = 1;
274 if (pwd->pw_uid >= 1000 && pwd->pw_uid != 65534) {
275 pwd->pw_selected = 1;
285 mergesort(pwds, npwds, sizeof *pwds, pwd_cmp_byname);
287 mergesort(pwds, npwds, sizeof *pwds, pwd_cmp_byuid);
291 display_user(struct xpasswd *pwd)
295 char cbuf[16], ebuf[16];
298 grp = find_group_bygid(pwd->pw_gid);
299 printf(o_flag ? "%s:%ld:%s:%ld:%s" : "%-15s %-7ld %-15s %-7ld %s\n",
300 pwd->pw_name, (long)pwd->pw_uid, grp ? grp->gr_name : "",
301 (long)pwd->pw_gid, pwd->pw_gecos);
303 for (i = 0, grp = grps; i < ngrps; ++i, ++grp) {
304 if (grp->gr_gid == pwd->pw_gid ||
305 !member(pwd->pw_name, grp->gr_mem))
307 printf(o_flag ? "%s:%s:%ld" : "%24s%-15s %-7ld\n",
308 "", grp->gr_name, (long)grp->gr_gid);
312 printf(o_flag ? "%s:%s" : "%24s%s\n", "", pwd->pw_dir);
313 printf(o_flag ? "%s:%s" : "%24s%s\n", "", pwd->pw_shell);
316 tm = gmtime(&pwd->pw_change);
317 strftime(cbuf, sizeof(cbuf), pwd->pw_change ? "%F" : "0", tm);
318 tm = gmtime(&pwd->pw_expire);
319 strftime(ebuf, sizeof(ebuf), pwd->pw_expire ? "%F" : "0", tm);
320 printf(o_flag ? "%s:%s:%s" : "%24s%s %s\n", "", cbuf, ebuf);
332 for (i = 0, pwd = pwds; i < npwds; ++i, ++pwd)
333 if (pwd->pw_selected)
340 fprintf(stderr, "usage: logins [-admopstux] [-g group] [-l login]\n");
345 main(int argc, char * const argv[])
349 while ((o = getopt(argc, argv, "adg:l:mopstux")) != -1)