tests: add fold(1) test for --bytes option
[coreutils.git] / src / users.c
blob03c3dc5341bc7201842d76814b09b5c078ef0658
1 /* GNU's users.
2 Copyright (C) 1992-2024 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 "long-options.h"
26 #include "quote.h"
27 #include "readutmp.h"
29 /* The official name of this program (e.g., no 'g' prefix). */
30 #define PROGRAM_NAME "users"
32 #define AUTHORS \
33 proper_name ("Joseph Arceneaux"), \
34 proper_name ("David MacKenzie")
36 static int
37 userid_compare (const void *v_a, const void *v_b)
39 char **a = (char **) v_a;
40 char **b = (char **) v_b;
41 return strcmp (*a, *b);
44 static void
45 list_entries_users (idx_t n, struct gl_utmp const *this)
47 char **u = xinmalloc (n, sizeof *u);
48 idx_t i;
49 idx_t n_entries = 0;
51 while (n--)
53 if (IS_USER_PROCESS (this))
55 char *trimmed_name;
57 trimmed_name = extract_trimmed_name (this);
59 u[n_entries] = trimmed_name;
60 ++n_entries;
62 this++;
65 qsort (u, n_entries, sizeof (u[0]), userid_compare);
67 for (i = 0; i < n_entries; i++)
69 char c = (i < n_entries - 1 ? ' ' : '\n');
70 fputs (u[i], stdout);
71 putchar (c);
74 for (i = 0; i < n_entries; i++)
75 free (u[i]);
76 free (u);
79 /* Display a list of users on the system, according to utmp file FILENAME.
80 Use read_utmp OPTIONS to read FILENAME. */
82 static void
83 users (char const *filename, int options)
85 idx_t n_users;
86 struct gl_utmp *utmp_buf;
87 options |= READ_UTMP_USER_PROCESS;
88 if (read_utmp (filename, &n_users, &utmp_buf, options) != 0)
89 error (EXIT_FAILURE, errno, "%s", quotef (filename));
91 list_entries_users (n_users, utmp_buf);
93 free (utmp_buf);
96 void
97 usage (int status)
99 if (status != EXIT_SUCCESS)
100 emit_try_help ();
101 else
103 printf (_("Usage: %s [OPTION]... [FILE]\n"), program_name);
104 printf (_("\
105 Output who is currently logged in according to FILE.\n\
106 If FILE is not specified, use %s. %s as FILE is common.\n\
109 UTMP_FILE, WTMP_FILE);
110 fputs (HELP_OPTION_DESCRIPTION, stdout);
111 fputs (VERSION_OPTION_DESCRIPTION, stdout);
112 emit_ancillary_info (PROGRAM_NAME);
114 exit (status);
118 main (int argc, char **argv)
120 initialize_main (&argc, &argv);
121 set_program_name (argv[0]);
122 setlocale (LC_ALL, "");
123 bindtextdomain (PACKAGE, LOCALEDIR);
124 textdomain (PACKAGE);
126 atexit (close_stdout);
128 parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
129 Version, true, usage, AUTHORS,
130 (char const *) nullptr);
132 switch (argc - optind)
134 case 0: /* users */
135 users (UTMP_FILE, READ_UTMP_CHECK_PIDS);
136 break;
138 case 1: /* users <utmp file> */
139 users (argv[optind], 0);
140 break;
142 default: /* lose */
143 error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
144 usage (EXIT_FAILURE);
147 return EXIT_SUCCESS;