(du invocation): Describe new option: -0, --null.
[coreutils.git] / src / users.c
blob7fbc8c6047a33d0a22c6f809514db00ecf5d0ad0
1 /* GNU's users.
2 Copyright (C) 1992-2003 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>
22 #include <stdio.h>
24 #include <sys/types.h>
25 #include "system.h"
27 #include "error.h"
28 #include "long-options.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 "Joseph Arceneaux", "David MacKenzie"
36 /* The name this program was run with. */
37 char *program_name;
39 static struct option const longopts[] =
41 {NULL, 0, NULL, 0}
44 static int
45 userid_compare (const void *v_a, const void *v_b)
47 char **a = (char **) v_a;
48 char **b = (char **) v_b;
49 return strcmp (*a, *b);
52 static void
53 list_entries_users (int n, const STRUCT_UTMP *this)
55 char **u;
56 int i;
57 int n_entries;
59 n_entries = 0;
60 u = xmalloc (n * sizeof (u[0]));
61 for (i = 0; i < n; i++)
63 if (UT_USER (this) [0]
64 #ifdef USER_PROCESS
65 && this->ut_type == USER_PROCESS
66 #endif
69 char *trimmed_name;
71 trimmed_name = extract_trimmed_name (this);
73 u[n_entries] = trimmed_name;
74 ++n_entries;
76 this++;
79 qsort (u, n_entries, sizeof (u[0]), userid_compare);
81 for (i = 0; i < n_entries; i++)
83 int c;
84 fputs (u[i], stdout);
85 c = (i < n_entries - 1 ? ' ' : '\n');
86 putchar (c);
89 for (i = 0; i < n_entries; i++)
90 free (u[i]);
91 free (u);
94 /* Display a list of users on the system, according to utmp file FILENAME. */
96 static void
97 users (const char *filename)
99 int n_users;
100 STRUCT_UTMP *utmp_buf;
101 int fail = read_utmp (filename, &n_users, &utmp_buf);
103 if (fail)
104 error (EXIT_FAILURE, errno, "%s", filename);
106 list_entries_users (n_users, utmp_buf);
108 free (utmp_buf);
111 void
112 usage (int status)
114 if (status != 0)
115 fprintf (stderr, _("Try `%s --help' for more information.\n"),
116 program_name);
117 else
119 printf (_("Usage: %s [OPTION]... [ FILE ]\n"), program_name);
120 printf (_("\
121 Output who is currently logged in according to FILE.\n\
122 If FILE is not specified, use %s. %s as FILE is common.\n\
125 UTMP_FILE, WTMP_FILE);
126 fputs (HELP_OPTION_DESCRIPTION, stdout);
127 fputs (VERSION_OPTION_DESCRIPTION, stdout);
128 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
130 exit (status);
134 main (int argc, char **argv)
136 int optc, longind;
137 initialize_main (&argc, &argv);
138 program_name = argv[0];
139 setlocale (LC_ALL, "");
140 bindtextdomain (PACKAGE, LOCALEDIR);
141 textdomain (PACKAGE);
143 atexit (close_stdout);
145 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
146 usage, AUTHORS, (char const *) NULL);
148 while ((optc = getopt_long (argc, argv, "", longopts, &longind)) != -1)
150 switch (optc)
152 case 0:
153 break;
155 default:
156 usage (EXIT_FAILURE);
160 switch (argc - optind)
162 case 0: /* users */
163 users (UTMP_FILE);
164 break;
166 case 1: /* users <utmp file> */
167 users (argv[optind]);
168 break;
170 default: /* lose */
171 error (0, 0, _("too many arguments"));
172 usage (EXIT_FAILURE);
175 exit (EXIT_SUCCESS);