*** empty log message ***
[coreutils.git] / src / users.c
blob2c1b5199551ac46e1aae95dd2aa356045352a28e
1 /* GNU's users.
2 Copyright (C) 1992-2000 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"
30 #include "closeout.h"
32 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "users"
35 #define AUTHORS "Joseph Arceneaux and David MacKenzie"
37 /* The name this program was run with. */
38 char *program_name;
40 static struct option const longopts[] =
42 {NULL, 0, NULL, 0}
45 static int
46 userid_compare (const void *v_a, const void *v_b)
48 char **a = (char **) v_a;
49 char **b = (char **) v_b;
50 return strcmp (*a, *b);
53 static void
54 list_entries_users (int n, const STRUCT_UTMP *this)
56 char **u;
57 int i;
58 int n_entries;
60 n_entries = 0;
61 u = (char **) xmalloc (n * sizeof (u[0]));
62 for (i = 0; i < n; i++)
64 if (UT_USER (this) [0]
65 #ifdef USER_PROCESS
66 && this->ut_type == USER_PROCESS
67 #endif
70 char *trimmed_name;
72 trimmed_name = extract_trimmed_name (this);
74 u[n_entries] = trimmed_name;
75 ++n_entries;
77 this++;
80 qsort (u, n_entries, sizeof (u[0]), userid_compare);
82 for (i = 0; i < n_entries; i++)
84 int c;
85 fputs (u[i], stdout);
86 c = (i < n_entries - 1 ? ' ' : '\n');
87 putchar (c);
90 for (i = 0; i < n_entries; i++)
91 free (u[i]);
92 free (u);
95 /* Display a list of users on the system, according to utmp file FILENAME. */
97 static void
98 users (const char *filename)
100 int n_users;
101 STRUCT_UTMP *utmp_buf;
102 int fail = read_utmp (filename, &n_users, &utmp_buf);
104 if (fail)
105 error (1, errno, "%s", filename);
107 list_entries_users (n_users, utmp_buf);
110 void
111 usage (int status)
113 if (status != 0)
114 fprintf (stderr, _("Try `%s --help' for more information.\n"),
115 program_name);
116 else
118 printf (_("Usage: %s [OPTION]... [ FILE ]\n"), program_name);
119 printf (_("\
120 Output who is currently logged in according to FILE.\n\
121 If FILE is not specified, use %s. %s as FILE is common.\n\
123 --help display this help and exit\n\
124 --version output version information and exit\n"),
125 UTMP_FILE, WTMP_FILE);
126 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
128 exit (status);
132 main (int argc, char **argv)
134 int optc, longind;
135 program_name = argv[0];
136 setlocale (LC_ALL, "");
137 bindtextdomain (PACKAGE, LOCALEDIR);
138 textdomain (PACKAGE);
140 atexit (close_stdout);
142 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
143 AUTHORS, usage);
145 while ((optc = getopt_long (argc, argv, "", longopts, &longind)) != -1)
147 switch (optc)
149 case 0:
150 break;
152 default:
153 usage (1);
157 switch (argc - optind)
159 case 0: /* users */
160 users (UTMP_FILE);
161 break;
163 case 1: /* users <utmp file> */
164 users (argv[optind]);
165 break;
167 default: /* lose */
168 error (0, 0, _("too many arguments"));
169 usage (1);
172 exit (0);