2 Copyright (C) 1992-2005, 2007-2008 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by jla; revised by djm */
23 #include <sys/types.h>
27 #include "long-options.h"
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "users"
35 proper_name ("Joseph Arceneaux"), \
36 proper_name ("David MacKenzie")
38 /* The name this program was run with. */
39 char const *program_name;
42 userid_compare (const void *v_a, const void *v_b)
44 char **a = (char **) v_a;
45 char **b = (char **) v_b;
46 return strcmp (*a, *b);
50 list_entries_users (size_t n, const STRUCT_UTMP *this)
52 char **u = xnmalloc (n, sizeof *u);
58 if (IS_USER_PROCESS (this))
62 trimmed_name = extract_trimmed_name (this);
64 u[n_entries] = trimmed_name;
70 qsort (u, n_entries, sizeof (u[0]), userid_compare);
72 for (i = 0; i < n_entries; i++)
74 char c = (i < n_entries - 1 ? ' ' : '\n');
79 for (i = 0; i < n_entries; i++)
84 /* Display a list of users on the system, according to utmp file FILENAME.
85 Use read_utmp OPTIONS to read FILENAME. */
88 users (const char *filename, int options)
91 STRUCT_UTMP *utmp_buf;
93 if (read_utmp (filename, &n_users, &utmp_buf, options) != 0)
94 error (EXIT_FAILURE, errno, "%s", filename);
96 list_entries_users (n_users, utmp_buf);
104 if (status != EXIT_SUCCESS)
105 fprintf (stderr, _("Try `%s --help' for more information.\n"),
109 printf (_("Usage: %s [OPTION]... [ FILE ]\n"), program_name);
111 Output who is currently logged in according to FILE.\n\
112 If FILE is not specified, use %s. %s as FILE is common.\n\
115 UTMP_FILE, WTMP_FILE);
116 fputs (HELP_OPTION_DESCRIPTION, stdout);
117 fputs (VERSION_OPTION_DESCRIPTION, stdout);
118 emit_bug_reporting_address ();
124 main (int argc, char **argv)
126 initialize_main (&argc, &argv);
127 program_name = argv[0];
128 setlocale (LC_ALL, "");
129 bindtextdomain (PACKAGE, LOCALEDIR);
130 textdomain (PACKAGE);
132 atexit (close_stdout);
134 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, VERSION,
135 usage, AUTHORS, (char const *) NULL);
136 if (getopt_long (argc, argv, "", NULL, NULL) != -1)
137 usage (EXIT_FAILURE);
139 switch (argc - optind)
142 users (UTMP_FILE, READ_UTMP_CHECK_PIDS);
145 case 1: /* users <utmp file> */
146 users (argv[optind], 0);
150 error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
151 usage (EXIT_FAILURE);