xref: /netbsd-src/usr.bin/users/users.c (revision 39bbc68a3b8731308f39852383f5430560168a91)
1*39bbc68aSsevan /*	$NetBSD: users.c,v 1.17 2016/09/05 00:40:30 sevan Exp $	*/
23ed25c1fSjtc 
361f28255Scgd /*
43ed25c1fSjtc  * Copyright (c) 1980, 1987, 1993
53ed25c1fSjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
1589aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32d7344283Slukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3498e5374cSlukem __COPYRIGHT("@(#) Copyright (c) 1980, 1987, 1993\
3598e5374cSlukem  The Regents of the University of California.  All rights reserved.");
3661f28255Scgd #endif /* not lint */
3761f28255Scgd 
3861f28255Scgd #ifndef lint
393ed25c1fSjtc #if 0
403ed25c1fSjtc static char sccsid[] = "@(#)users.c	8.1 (Berkeley) 6/6/93";
413ed25c1fSjtc #endif
42*39bbc68aSsevan __RCSID("$NetBSD: users.c,v 1.17 2016/09/05 00:40:30 sevan Exp $");
4361f28255Scgd #endif /* not lint */
4461f28255Scgd 
4561f28255Scgd #include <sys/types.h>
46d15c52e6Sperry 
47d15c52e6Sperry #include <err.h>
4851fb2858Schristos #include <time.h>
4961f28255Scgd #include <stdio.h>
503b09eeb7Sjtc #include <stdlib.h>
51b9dc358fSjtc #include <string.h>
52d15c52e6Sperry #include <unistd.h>
5361f28255Scgd 
5451fb2858Schristos #include "utmpentry.h"
5561f28255Scgd 
563b09eeb7Sjtc int
main(int argc,char ** argv)57d34c2845Smatt main(int argc, char **argv)
5861f28255Scgd {
59d7344283Slukem 	int ncnt = 0;
60b9dc358fSjtc 	int ch;
6151fb2858Schristos 	struct utmpentry *from, *ehead, *save, **nextp;
6261f28255Scgd 
63d7344283Slukem 	while ((ch = getopt(argc, argv, "")) != -1)
6461f28255Scgd 		switch(ch) {
6561f28255Scgd 		case '?':
6661f28255Scgd 		default:
67b635f565Sjmmv 			(void)fprintf(stderr, "usage: %s\n", getprogname());
6861f28255Scgd 			exit(1);
6961f28255Scgd 		}
7061f28255Scgd 	argc -= optind;
7161f28255Scgd 	argv += optind;
7261f28255Scgd 
7351fb2858Schristos 	ncnt = getutentries(NULL, &ehead);
7451fb2858Schristos 
7551fb2858Schristos 	if (ncnt == 0)
7651fb2858Schristos 		return 0;
7751fb2858Schristos 
7816e3bec2Sdholland 	/*
7916e3bec2Sdholland 	 * XXX the utmp list belongs to getutentries and we shouldn't
8016e3bec2Sdholland 	 * sort it in place. Since we don't call endutentries and we
8116e3bec2Sdholland 	 * don't call getutentries again it doesn't matter, but it's
8216e3bec2Sdholland 	 * untidy. Maybe give getutentries a sort function? It has to
8316e3bec2Sdholland 	 * sort anyway if it's merging utmp and utmpx data.
8416e3bec2Sdholland 	 */
8516e3bec2Sdholland 
8651fb2858Schristos 	from = ehead;
8751fb2858Schristos 	ehead = NULL;
8851fb2858Schristos 	while (from != NULL) {
8951fb2858Schristos 		for (nextp = &ehead;
9051fb2858Schristos 		    (*nextp) && strcmp(from->name, (*nextp)->name) > 0;
9151fb2858Schristos 		    nextp = &(*nextp)->next)
9251fb2858Schristos 			continue;
9351fb2858Schristos 		save = from;
9451fb2858Schristos 		from = from->next;
9551fb2858Schristos 		save->next = *nextp;
9651fb2858Schristos 		*nextp = save;
9761f28255Scgd 	}
983b09eeb7Sjtc 
9951fb2858Schristos 	save = ehead;
10051fb2858Schristos 	(void)printf("%s", ehead->name);
1013b09eeb7Sjtc 
10251fb2858Schristos 	for (from = ehead->next; from; from = from->next)
10351fb2858Schristos 		if (strcmp(save->name, from->name) != 0) {
10451fb2858Schristos 			(void)printf(" %s", from->name);
10551fb2858Schristos 			save = from;
1063b09eeb7Sjtc 		}
1073b09eeb7Sjtc 
10861f28255Scgd 	(void)printf("\n");
10961f28255Scgd 	exit(0);
11061f28255Scgd }
111