xref: /csrg-svn/usr.bin/users/users.c (revision 62345)
11151Sbill /*
2*62345Sbostic  * Copyright (c) 1980, 1987, 1993
3*62345Sbostic  *	The Regents of the University of California.  All rights reserved.
433162Sbostic  *
542780Sbostic  * %sccs.include.redist.c%
621584Sdist  */
721584Sdist 
821584Sdist #ifndef lint
9*62345Sbostic static char copyright[] =
10*62345Sbostic "@(#) Copyright (c) 1980, 1987, 1993\n\
11*62345Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1233162Sbostic #endif /* not lint */
1321584Sdist 
1421584Sdist #ifndef lint
15*62345Sbostic static char sccsid[] = "@(#)users.c	8.1 (Berkeley) 06/06/93";
1633162Sbostic #endif /* not lint */
1721584Sdist 
1830281Sbostic #include <sys/types.h>
1930281Sbostic #include <utmp.h>
201151Sbill #include <stdio.h>
211151Sbill 
2240757Sbostic #define	MAXUSERS	200
231151Sbill 
main(argc,argv)2445041Sbostic main(argc, argv)
2545041Sbostic 	int argc;
2645041Sbostic 	char **argv;
271151Sbill {
2845041Sbostic 	extern int optind;
2940757Sbostic 	register int cnt, ncnt;
3040757Sbostic 	struct utmp utmp;
3140757Sbostic 	char names[MAXUSERS][UT_NAMESIZE];
3245041Sbostic 	int ch, scmp();
331151Sbill 
3445041Sbostic 	while ((ch = getopt(argc, argv, "")) != EOF)
3545041Sbostic 		switch(ch) {
3645041Sbostic 		case '?':
3745041Sbostic 		default:
3845041Sbostic 			(void)fprintf(stderr, "usage: users\n");
3945041Sbostic 			exit(1);
4045041Sbostic 		}
4145041Sbostic 	argc -= optind;
4245041Sbostic 	argv += optind;
4345041Sbostic 
4440757Sbostic 	if (!freopen(_PATH_UTMP, "r", stdin)) {
4537911Sbostic 		(void)fprintf(stderr, "users: can't open %s.\n", _PATH_UTMP);
4633162Sbostic 		exit(1);
471151Sbill 	}
4840757Sbostic 	for (ncnt = 0;
4940757Sbostic 	    fread((char *)&utmp, sizeof(utmp), 1, stdin) == 1;)
5030281Sbostic 		if (*utmp.ut_name) {
5140757Sbostic 			if (ncnt == MAXUSERS) {
5240757Sbostic 				(void)fprintf(stderr,
5340757Sbostic 				    "users: too many users.\n");
5430281Sbostic 				break;
5530281Sbostic 			}
5640757Sbostic 			(void)strncpy(names[ncnt], utmp.ut_name, UT_NAMESIZE);
5740757Sbostic 			++ncnt;
5830281Sbostic 		}
591151Sbill 
6040757Sbostic 	if (ncnt) {
6140757Sbostic 		qsort(names, ncnt, UT_NAMESIZE, scmp);
6245460Sbostic 		(void)printf("%.*s", UT_NAMESIZE, names[0]);
6345040Sbostic 		for (cnt = 1; cnt < ncnt; ++cnt)
6445040Sbostic 			if (strncmp(names[cnt], names[cnt - 1], UT_NAMESIZE))
6545040Sbostic 				(void)printf(" %.*s", UT_NAMESIZE, names[cnt]);
6640757Sbostic 		(void)printf("\n");
6730281Sbostic 	}
6840757Sbostic 	exit(0);
691151Sbill }
701151Sbill 
scmp(p,q)7133162Sbostic scmp(p, q)
7240757Sbostic 	char *p, *q;
7330281Sbostic {
7445460Sbostic 	return(strncmp(p, q, UT_NAMESIZE));
7530281Sbostic }
76