.
[coreutils.git] / src / users.c
bloba9691bc38ff5390914e918492d405280d5b8620b
1 /* GNU's users.
2 Copyright (C) 92, 93, 94, 95, 96, 1997 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)
7 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, 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 */
20 #include <config.h>
21 #include <getopt.h>
23 #include "system.h"
24 #include "error.h"
25 #include "readutmp.h"
27 /* The name this program was run with. */
28 char *program_name;
30 /* If nonzero, display usage information and exit. */
31 static int show_help;
33 /* If nonzero, print the version on standard output and exit. */
34 static int show_version;
36 static struct option const longopts[] =
38 {"help", no_argument, &show_help, 1},
39 {"version", no_argument, &show_version, 1},
40 {NULL, 0, NULL, 0}
43 static int
44 userid_compare (const void *v_a, const void *v_b)
46 char **a = (char **) v_a;
47 char **b = (char **) v_b;
48 return strcmp (*a, *b);
51 static void
52 list_entries_users (int n, const STRUCT_UTMP *this)
54 char **u;
55 int i;
56 int n_entries;
58 n_entries = 0;
59 u = (char **) xmalloc (n * sizeof (u[0]));
60 for (i = 0; i < n; i++)
62 if (this->ut_name[0]
63 #ifdef USER_PROCESS
64 && this->ut_type == USER_PROCESS
65 #endif
68 char *trimmed_name;
70 trimmed_name = extract_trimmed_name (this);
72 u[n_entries] = trimmed_name;
73 ++n_entries;
75 this++;
78 qsort (u, n_entries, sizeof (u[0]), userid_compare);
80 for (i = 0; i < n_entries; i++)
82 int c;
83 fputs (u[i], stdout);
84 c = (i < n_entries - 1 ? ' ' : '\n');
85 putchar (c);
88 for (i = 0; i < n_entries; i++)
89 free (u[i]);
90 free (u);
93 /* Display a list of users on the system, according to utmp file FILENAME. */
95 static void
96 users (const char *filename)
98 int n_users;
99 STRUCT_UTMP *utmp_buf;
100 int fail = read_utmp (filename, &n_users, &utmp_buf);
102 if (fail)
103 error (1, errno, "%s", filename);
105 list_entries_users (n_users, utmp_buf);
108 static void
109 usage (int status)
111 if (status != 0)
112 fprintf (stderr, _("Try `%s --help' for more information.\n"),
113 program_name);
114 else
116 printf (_("Usage: %s [OPTION]... [ FILE ]\n"), program_name);
117 printf (_("\
118 Output who is currently logged in according to FILE.\n\
119 If FILE is not specified, use %s. %s as FILE is common.\n\
121 --help display this help and exit\n\
122 --version output version information and exit\n"),
123 UTMP_FILE, WTMP_FILE);
124 puts (_("\nReport bugs to <sh-utils-bugs@gnu.org>."));
126 exit (status);
130 main (int argc, char **argv)
132 int optc, longind;
133 program_name = argv[0];
134 setlocale (LC_ALL, "");
135 bindtextdomain (PACKAGE, LOCALEDIR);
136 textdomain (PACKAGE);
138 while ((optc = getopt_long (argc, argv, "", longopts, &longind)) != -1)
140 switch (optc)
142 case 0:
143 break;
145 default:
146 usage (1);
150 if (show_version)
152 printf ("users (%s) %s\n", GNU_PACKAGE, VERSION);
153 exit (0);
156 if (show_help)
157 usage (0);
159 switch (argc - optind)
161 case 0: /* users */
162 users (UTMP_FILE);
163 break;
165 case 1: /* users <utmp file> */
166 users (argv[optind]);
167 break;
169 default: /* lose */
170 error (0, 0, _("too many arguments"));
171 usage (1);
174 exit (0);