12bad08bf9d09c245b9bae9f4a86a29dd0cdf3cb
[platform/upstream/coreutils.git] / src / users.c
1 /* GNU's users.
2    Copyright (C) 1992-2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 "readutmp.h"
30
31 /* The official name of this program (e.g., no `g' prefix).  */
32 #define PROGRAM_NAME "users"
33
34 #define WRITTEN_BY _("Written by Joseph Arceneaux and David MacKenzie.")
35
36 /* The name this program was run with. */
37 char *program_name;
38
39 static struct option const longopts[] =
40 {
41   {NULL, 0, NULL, 0}
42 };
43
44 static int
45 userid_compare (const void *v_a, const void *v_b)
46 {
47   char **a = (char **) v_a;
48   char **b = (char **) v_b;
49   return strcmp (*a, *b);
50 }
51
52 static void
53 list_entries_users (int n, const STRUCT_UTMP *this)
54 {
55   char **u;
56   int i;
57   int n_entries;
58
59   n_entries = 0;
60   u = xmalloc (n * sizeof (u[0]));
61   for (i = 0; i < n; i++)
62     {
63       if (UT_USER (this) [0]
64 #ifdef USER_PROCESS
65           && this->ut_type == USER_PROCESS
66 #endif
67           )
68         {
69           char *trimmed_name;
70
71           trimmed_name = extract_trimmed_name (this);
72
73           u[n_entries] = trimmed_name;
74           ++n_entries;
75         }
76       this++;
77     }
78
79   qsort (u, n_entries, sizeof (u[0]), userid_compare);
80
81   for (i = 0; i < n_entries; i++)
82     {
83       int c;
84       fputs (u[i], stdout);
85       c = (i < n_entries - 1 ? ' ' : '\n');
86       putchar (c);
87     }
88
89   for (i = 0; i < n_entries; i++)
90     free (u[i]);
91   free (u);
92 }
93
94 /* Display a list of users on the system, according to utmp file FILENAME. */
95
96 static void
97 users (const char *filename)
98 {
99   int n_users;
100   STRUCT_UTMP *utmp_buf;
101   int fail = read_utmp (filename, &n_users, &utmp_buf);
102
103   if (fail)
104     error (EXIT_FAILURE, errno, "%s", filename);
105
106   list_entries_users (n_users, utmp_buf);
107 }
108
109 void
110 usage (int status)
111 {
112   if (status != 0)
113     fprintf (stderr, _("Try `%s --help' for more information.\n"),
114              program_name);
115   else
116     {
117       printf (_("Usage: %s [OPTION]... [ FILE ]\n"), program_name);
118       printf (_("\
119 Output who is currently logged in according to FILE.\n\
120 If FILE is not specified, use %s.  %s as FILE is common.\n\
121 \n\
122 "),
123               UTMP_FILE, WTMP_FILE);
124       fputs (HELP_OPTION_DESCRIPTION, stdout);
125       fputs (VERSION_OPTION_DESCRIPTION, stdout);
126       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
127     }
128   exit (status);
129 }
130
131 int
132 main (int argc, char **argv)
133 {
134   int optc, longind;
135   initialize_main (&argc, &argv);
136   program_name = argv[0];
137   setlocale (LC_ALL, "");
138   bindtextdomain (PACKAGE, LOCALEDIR);
139   textdomain (PACKAGE);
140
141   atexit (close_stdout);
142
143   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
144                       WRITTEN_BY, usage);
145
146   while ((optc = getopt_long (argc, argv, "", longopts, &longind)) != -1)
147     {
148       switch (optc)
149         {
150         case 0:
151           break;
152
153         default:
154           usage (EXIT_FAILURE);
155         }
156     }
157
158   switch (argc - optind)
159     {
160     case 0:                     /* users */
161       users (UTMP_FILE);
162       break;
163
164     case 1:                     /* users <utmp file> */
165       users (argv[optind]);
166       break;
167
168     default:                    /* lose */
169       error (0, 0, _("too many arguments"));
170       usage (EXIT_FAILURE);
171     }
172
173   exit (EXIT_SUCCESS);
174 }