2 Copyright (C) 1992-2004 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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by jla; revised by djm */
24 #include <sys/types.h>
28 #include "long-options.h"
32 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "users"
35 #define AUTHORS "Joseph Arceneaux", "David MacKenzie"
37 /* The name this program was run with. */
41 userid_compare (const void *v_a, const void *v_b)
43 char **a = (char **) v_a;
44 char **b = (char **) v_b;
45 return strcmp (*a, *b);
49 list_entries_users (size_t n, const STRUCT_UTMP *this)
51 char **u = xnmalloc (n, sizeof *u);
57 if (IS_USER_PROCESS (this))
61 trimmed_name = extract_trimmed_name (this);
63 u[n_entries] = trimmed_name;
69 qsort (u, n_entries, sizeof (u[0]), userid_compare);
71 for (i = 0; i < n_entries; i++)
73 char c = (i < n_entries - 1 ? ' ' : '\n');
78 for (i = 0; i < n_entries; i++)
83 /* Display a list of users on the system, according to utmp file FILENAME. */
86 users (const char *filename)
89 STRUCT_UTMP *utmp_buf;
91 if (read_utmp (filename, &n_users, &utmp_buf) != 0)
92 error (EXIT_FAILURE, errno, "%s", filename);
94 list_entries_users (n_users, utmp_buf);
102 if (status != EXIT_SUCCESS)
103 fprintf (stderr, _("Try `%s --help' for more information.\n"),
107 printf (_("Usage: %s [OPTION]... [ FILE ]\n"), program_name);
109 Output who is currently logged in according to FILE.\n\
110 If FILE is not specified, use %s. %s as FILE is common.\n\
113 UTMP_FILE, WTMP_FILE);
114 fputs (HELP_OPTION_DESCRIPTION, stdout);
115 fputs (VERSION_OPTION_DESCRIPTION, stdout);
116 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
122 main (int argc, char **argv)
124 initialize_main (&argc, &argv);
125 program_name = argv[0];
126 setlocale (LC_ALL, "");
127 bindtextdomain (PACKAGE, LOCALEDIR);
128 textdomain (PACKAGE);
130 atexit (close_stdout);
132 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
133 usage, AUTHORS, (char const *) NULL);
134 if (getopt_long (argc, argv, "", NULL, NULL) != -1)
135 usage (EXIT_FAILURE);
137 switch (argc - optind)
143 case 1: /* users <utmp file> */
144 users (argv[optind]);
148 error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
149 usage (EXIT_FAILURE);