2 Copyright (C) 1992-1999 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 */
25 #include "long-options.h"
29 /* The official name of this program (e.g., no `g' prefix). */
30 #define PROGRAM_NAME "users"
32 #define AUTHORS "Joseph Arceneaux and David MacKenzie"
34 /* The name this program was run with. */
37 static struct option
const longopts
[] =
43 userid_compare (const void *v_a
, const void *v_b
)
45 char **a
= (char **) v_a
;
46 char **b
= (char **) v_b
;
47 return strcmp (*a
, *b
);
51 list_entries_users (int n
, const STRUCT_UTMP
*this)
58 u
= (char **) xmalloc (n
* sizeof (u
[0]));
59 for (i
= 0; i
< n
; i
++)
63 && this->ut_type
== USER_PROCESS
69 trimmed_name
= extract_trimmed_name (this);
71 u
[n_entries
] = trimmed_name
;
77 qsort (u
, n_entries
, sizeof (u
[0]), userid_compare
);
79 for (i
= 0; i
< n_entries
; i
++)
83 c
= (i
< n_entries
- 1 ? ' ' : '\n');
87 for (i
= 0; i
< n_entries
; i
++)
92 /* Display a list of users on the system, according to utmp file FILENAME. */
95 users (const char *filename
)
98 STRUCT_UTMP
*utmp_buf
;
99 int fail
= read_utmp (filename
, &n_users
, &utmp_buf
);
102 error (1, errno
, "%s", filename
);
104 list_entries_users (n_users
, utmp_buf
);
111 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
115 printf (_("Usage: %s [OPTION]... [ FILE ]\n"), program_name
);
117 Output who is currently logged in according to FILE.\n\
118 If FILE is not specified, use %s. %s as FILE is common.\n\
120 --help display this help and exit\n\
121 --version output version information and exit\n"),
122 UTMP_FILE
, WTMP_FILE
);
123 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
129 main (int argc
, char **argv
)
132 program_name
= argv
[0];
133 setlocale (LC_ALL
, "");
134 bindtextdomain (PACKAGE
, LOCALEDIR
);
135 textdomain (PACKAGE
);
137 parse_long_options (argc
, argv
, PROGRAM_NAME
, GNU_PACKAGE
, VERSION
,
140 while ((optc
= getopt_long (argc
, argv
, "", longopts
, &longind
)) != -1)
152 switch (argc
- optind
)
158 case 1: /* users <utmp file> */
159 users (argv
[optind
]);
163 error (0, 0, _("too many arguments"));