1268500Sed/*-
2268491Sgahr * Copyright (c) 2014 Pietro Cerutti <gahr@FreeBSD.org>
3268491Sgahr * All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 4. Neither the name of the University nor the names of its contributors
141590Srgrimes *    may be used to endorse or promote products derived from this software
151590Srgrimes *    without specific prior written permission.
161590Srgrimes *
171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271590Srgrimes * SUCH DAMAGE.
281590Srgrimes */
291590Srgrimes
30330464Seadler#include <sys/capsicum.h>
31268491Sgahr#include <sys/cdefs.h>
32268491Sgahr__FBSDID("$FreeBSD: stable/11/usr.bin/users/users.cc 330464 2018-03-05 08:27:29Z eadler $");
331590Srgrimes
34330464Seadler#include <err.h>
35330464Seadler#include <errno.h>
36202200Sed#include <utmpx.h>
371590Srgrimes
38268491Sgahr#include <algorithm>
39268491Sgahr#include <iostream>
40268491Sgahr#include <iterator>
41268500Sed#include <set>
42268491Sgahr#include <string>
43268491Sgahrusing namespace std;
441590Srgrimes
4528789Scharnierint
46268491Sgahrmain(int argc, char **)
471590Srgrimes{
48200154Sed	struct utmpx *ut;
49268498Sed	set<string> names;
501590Srgrimes
51268491Sgahr	if (argc > 1) {
52268491Sgahr		cerr << "usage: users" << endl;
53268491Sgahr		return (1);
54268491Sgahr	}
551590Srgrimes
56200154Sed	setutxent();
57330464Seadler
58330464Seadler	if (cap_enter() < 0 && errno != ENOSYS)
59330464Seadler		err(1, "Failed to enter capability mode.");
60330464Seadler
61268498Sed	while ((ut = getutxent()) != NULL)
62268498Sed		if (ut->ut_type == USER_PROCESS)
63268498Sed			names.insert(ut->ut_user);
64200154Sed	endutxent();
65268491Sgahr
66268498Sed	if (!names.empty()) {
67268500Sed		set<string>::iterator last = names.end();
68268498Sed		--last;
69268498Sed		copy(names.begin(), last, ostream_iterator<string>(cout, " "));
70268498Sed		cout << *last << endl;
711590Srgrimes	}
7228563Scharnier}
73