du: --apparent counts only symlinks and regular
[coreutils.git] / src / users.c
blob86bd1b1f41ec554aeeb07e9536006c9f38ede9bd
1 /* GNU's users.
2 Copyright (C) 1992-2023 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 3 of the License, or
7 (at your option) 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, see <https://www.gnu.org/licenses/>. */
17 /* Written by jla; revised by djm */
19 #include <config.h>
20 #include <stdio.h>
22 #include <sys/types.h>
23 #include "system.h"
25 #include "die.h"
26 #include "error.h"
27 #include "long-options.h"
28 #include "quote.h"
29 #include "readutmp.h"
31 /* The official name of this program (e.g., no 'g' prefix). */
32 #define PROGRAM_NAME "users"
34 #define AUTHORS \
35 proper_name ("Joseph Arceneaux"), \
36 proper_name ("David MacKenzie")
38 static int
39 userid_compare (const void *v_a, const void *v_b)
41 char **a = (char **) v_a;
42 char **b = (char **) v_b;
43 return strcmp (*a, *b);
46 static void
47 list_entries_users (size_t n, const STRUCT_UTMP *this)
49 char **u = xnmalloc (n, sizeof *u);
50 size_t i;
51 size_t n_entries = 0;
53 while (n--)
55 if (IS_USER_PROCESS (this))
57 char *trimmed_name;
59 trimmed_name = extract_trimmed_name (this);
61 u[n_entries] = trimmed_name;
62 ++n_entries;
64 this++;
67 qsort (u, n_entries, sizeof (u[0]), userid_compare);
69 for (i = 0; i < n_entries; i++)
71 char c = (i < n_entries - 1 ? ' ' : '\n');
72 fputs (u[i], stdout);
73 putchar (c);
76 for (i = 0; i < n_entries; i++)
77 free (u[i]);
78 free (u);
81 /* Display a list of users on the system, according to utmp file FILENAME.
82 Use read_utmp OPTIONS to read FILENAME. */
84 static void
85 users (char const *filename, int options)
87 size_t n_users;
88 STRUCT_UTMP *utmp_buf;
90 if (read_utmp (filename, &n_users, &utmp_buf, options) != 0)
91 die (EXIT_FAILURE, errno, "%s", quotef (filename));
93 list_entries_users (n_users, utmp_buf);
95 free (utmp_buf);
98 void
99 usage (int status)
101 if (status != EXIT_SUCCESS)
102 emit_try_help ();
103 else
105 printf (_("Usage: %s [OPTION]... [FILE]\n"), program_name);
106 printf (_("\
107 Output who is currently logged in according to FILE.\n\
108 If FILE is not specified, use %s. %s as FILE is common.\n\
111 UTMP_FILE, WTMP_FILE);
112 fputs (HELP_OPTION_DESCRIPTION, stdout);
113 fputs (VERSION_OPTION_DESCRIPTION, stdout);
114 emit_ancillary_info (PROGRAM_NAME);
116 exit (status);
120 main (int argc, char **argv)
122 initialize_main (&argc, &argv);
123 set_program_name (argv[0]);
124 setlocale (LC_ALL, "");
125 bindtextdomain (PACKAGE, LOCALEDIR);
126 textdomain (PACKAGE);
128 atexit (close_stdout);
130 parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
131 Version, true, usage, AUTHORS,
132 (char const *) NULL);
134 switch (argc - optind)
136 case 0: /* users */
137 users (UTMP_FILE, READ_UTMP_CHECK_PIDS);
138 break;
140 case 1: /* users <utmp file> */
141 users (argv[optind], 0);
142 break;
144 default: /* lose */
145 error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
146 usage (EXIT_FAILURE);
149 return EXIT_SUCCESS;