source: trunk/coreutils/src/users.c@ 2603

Last change on this file since 2603 was 2554, checked in by bird, 20 years ago

coretuils-5.94

File size: 3.6 KB
Line 
1/* GNU's users.
2 Copyright (C) 1992-2005 Free Software Foundation, Inc.
3
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.
8
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.
13
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18/* Written by jla; revised by djm */
19
20#include <config.h>
21#include <getopt.h>
22#include <stdio.h>
23
24#include <sys/types.h>
25#include "system.h"
26
27#include "error.h"
28#include "long-options.h"
29#include "quote.h"
30#include "readutmp.h"
31
32/* The official name of this program (e.g., no `g' prefix). */
33#define PROGRAM_NAME "users"
34
35#define AUTHORS "Joseph Arceneaux", "David MacKenzie"
36
37/* The name this program was run with. */
38char *program_name;
39
40static int
41userid_compare (const void *v_a, const void *v_b)
42{
43 char **a = (char **) v_a;
44 char **b = (char **) v_b;
45 return strcmp (*a, *b);
46}
47
48static void
49list_entries_users (size_t n, const STRUCT_UTMP *this)
50{
51 char **u = xnmalloc (n, sizeof *u);
52 size_t i;
53 size_t n_entries = 0;
54
55 while (n--)
56 {
57 if (IS_USER_PROCESS (this))
58 {
59 char *trimmed_name;
60
61 trimmed_name = extract_trimmed_name (this);
62
63 u[n_entries] = trimmed_name;
64 ++n_entries;
65 }
66 this++;
67 }
68
69 qsort (u, n_entries, sizeof (u[0]), userid_compare);
70
71 for (i = 0; i < n_entries; i++)
72 {
73 char c = (i < n_entries - 1 ? ' ' : '\n');
74 fputs (u[i], stdout);
75 putchar (c);
76 }
77
78 for (i = 0; i < n_entries; i++)
79 free (u[i]);
80 free (u);
81}
82
83/* Display a list of users on the system, according to utmp file FILENAME.
84 Use read_utmp OPTIONS to read FILENAME. */
85
86static void
87users (const char *filename, int options)
88{
89 size_t n_users;
90 STRUCT_UTMP *utmp_buf;
91
92 if (read_utmp (filename, &n_users, &utmp_buf, options) != 0)
93 error (EXIT_FAILURE, errno, "%s", filename);
94
95 list_entries_users (n_users, utmp_buf);
96
97 free (utmp_buf);
98}
99
100void
101usage (int status)
102{
103 if (status != EXIT_SUCCESS)
104 fprintf (stderr, _("Try `%s --help' for more information.\n"),
105 program_name);
106 else
107 {
108 printf (_("Usage: %s [OPTION]... [ FILE ]\n"), program_name);
109 printf (_("\
110Output who is currently logged in according to FILE.\n\
111If FILE is not specified, use %s. %s as FILE is common.\n\
112\n\
113"),
114 UTMP_FILE, WTMP_FILE);
115 fputs (HELP_OPTION_DESCRIPTION, stdout);
116 fputs (VERSION_OPTION_DESCRIPTION, stdout);
117 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
118 }
119 exit (status);
120}
121
122int
123main (int argc, char **argv)
124{
125 initialize_main (&argc, &argv);
126 program_name = argv[0];
127 setlocale (LC_ALL, "");
128 bindtextdomain (PACKAGE, LOCALEDIR);
129 textdomain (PACKAGE);
130
131 atexit (close_stdout);
132
133 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
134 usage, AUTHORS, (char const *) NULL);
135 if (getopt_long (argc, argv, "", NULL, NULL) != -1)
136 usage (EXIT_FAILURE);
137
138 switch (argc - optind)
139 {
140 case 0: /* users */
141 users (UTMP_FILE, READ_UTMP_CHECK_PIDS);
142 break;
143
144 case 1: /* users <utmp file> */
145 users (argv[optind], 0);
146 break;
147
148 default: /* lose */
149 error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
150 usage (EXIT_FAILURE);
151 }
152
153 exit (EXIT_SUCCESS);
154}
Note: See TracBrowser for help on using the repository browser.