2 Copyright (C) 1992-2003 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 N_ ("Joseph Arceneaux and David MacKenzie")
37 /* The name this program was run with. */
40 static struct option const longopts[] =
46 userid_compare (const void *v_a, const void *v_b)
48 char **a = (char **) v_a;
49 char **b = (char **) v_b;
50 return strcmp (*a, *b);
54 list_entries_users (int n, const STRUCT_UTMP *this)
61 u = xmalloc (n * sizeof (u[0]));
62 for (i = 0; i < n; i++)
64 if (UT_USER (this) [0]
66 && this->ut_type == USER_PROCESS
72 trimmed_name = extract_trimmed_name (this);
74 u[n_entries] = trimmed_name;
80 qsort (u, n_entries, sizeof (u[0]), userid_compare);
82 for (i = 0; i < n_entries; i++)
86 c = (i < n_entries - 1 ? ' ' : '\n');
90 for (i = 0; i < n_entries; i++)
95 /* Display a list of users on the system, according to utmp file FILENAME. */
98 users (const char *filename)
101 STRUCT_UTMP *utmp_buf;
102 int fail = read_utmp (filename, &n_users, &utmp_buf);
105 error (EXIT_FAILURE, errno, "%s", filename);
107 list_entries_users (n_users, utmp_buf);
114 fprintf (stderr, _("Try `%s --help' for more information.\n"),
118 printf (_("Usage: %s [OPTION]... [ FILE ]\n"), program_name);
120 Output who is currently logged in according to FILE.\n\
121 If FILE is not specified, use %s. %s as FILE is common.\n\
124 UTMP_FILE, WTMP_FILE);
125 fputs (HELP_OPTION_DESCRIPTION, stdout);
126 fputs (VERSION_OPTION_DESCRIPTION, stdout);
127 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
133 main (int argc, char **argv)
136 initialize_main (&argc, &argv);
137 program_name = argv[0];
138 setlocale (LC_ALL, "");
139 bindtextdomain (PACKAGE, LOCALEDIR);
140 textdomain (PACKAGE);
142 atexit (close_stdout);
144 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
147 while ((optc = getopt_long (argc, argv, "", longopts, &longind)) != -1)
155 usage (EXIT_FAILURE);
159 switch (argc - optind)
165 case 1: /* users <utmp file> */
166 users (argv[optind]);
170 error (0, 0, _("too many arguments"));
171 usage (EXIT_FAILURE);