*** empty log message ***
[coreutils.git] / src / users.c
blobb0df3aaa1415d36cf13c2360e6b0e399468442ed
1 /* GNU's users.
2 Copyright (C) 1992-1999 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 "error.h"
25 #include "long-options.h"
26 #include "readutmp.h"
27 #include "system.h"
29 /* The official name of this program (e.g., no `g' prefix). */
30 #define PROGRAM_NAME "users"
32 #define AUTHORS "Joseph Arceneaux and David MacKenzie"
34 /* The name this program was run with. */
35 char *program_name;
37 static struct option const longopts[] =
39 {NULL, 0, NULL, 0}
42 static int
43 userid_compare (const void *v_a, const void *v_b)
45 char **a = (char **) v_a;
46 char **b = (char **) v_b;
47 return strcmp (*a, *b);
50 static void
51 list_entries_users (int n, const STRUCT_UTMP *this)
53 char **u;
54 int i;
55 int n_entries;
57 n_entries = 0;
58 u = (char **) xmalloc (n * sizeof (u[0]));
59 for (i = 0; i < n; i++)
61 if (this->ut_name[0]
62 #ifdef USER_PROCESS
63 && this->ut_type == USER_PROCESS
64 #endif
67 char *trimmed_name;
69 trimmed_name = extract_trimmed_name (this);
71 u[n_entries] = trimmed_name;
72 ++n_entries;
74 this++;
77 qsort (u, n_entries, sizeof (u[0]), userid_compare);
79 for (i = 0; i < n_entries; i++)
81 int c;
82 fputs (u[i], stdout);
83 c = (i < n_entries - 1 ? ' ' : '\n');
84 putchar (c);
87 for (i = 0; i < n_entries; i++)
88 free (u[i]);
89 free (u);
92 /* Display a list of users on the system, according to utmp file FILENAME. */
94 static void
95 users (const char *filename)
97 int n_users;
98 STRUCT_UTMP *utmp_buf;
99 int fail = read_utmp (filename, &n_users, &utmp_buf);
101 if (fail)
102 error (1, errno, "%s", filename);
104 list_entries_users (n_users, utmp_buf);
107 void
108 usage (int status)
110 if (status != 0)
111 fprintf (stderr, _("Try `%s --help' for more information.\n"),
112 program_name);
113 else
115 printf (_("Usage: %s [OPTION]... [ FILE ]\n"), program_name);
116 printf (_("\
117 Output who is currently logged in according to FILE.\n\
118 If FILE is not specified, use %s. %s as FILE is common.\n\
120 --help display this help and exit\n\
121 --version output version information and exit\n"),
122 UTMP_FILE, WTMP_FILE);
123 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
125 exit (status);
129 main (int argc, char **argv)
131 int optc, longind;
132 program_name = argv[0];
133 setlocale (LC_ALL, "");
134 bindtextdomain (PACKAGE, LOCALEDIR);
135 textdomain (PACKAGE);
137 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
138 AUTHORS, usage);
140 while ((optc = getopt_long (argc, argv, "", longopts, &longind)) != -1)
142 switch (optc)
144 case 0:
145 break;
147 default:
148 usage (1);
152 switch (argc - optind)
154 case 0: /* users */
155 users (UTMP_FILE);
156 break;
158 case 1: /* users <utmp file> */
159 users (argv[optind]);
160 break;
162 default: /* lose */
163 error (0, 0, _("too many arguments"));
164 usage (1);
167 exit (0);