2 Copyright (C) 92, 93, 94, 95, 96, 1997 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)
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 */
21 name [state] line time [idle] host
23 name, line, time: not -q
34 #ifndef MAXHOSTNAMELEN
35 # define MAXHOSTNAMELEN 64
46 /* The name this program was run with. */
49 /* If nonzero, display usage information and exit. */
52 /* If nonzero, print the version on standard output and exit. */
53 static int show_version;
55 /* If nonzero, display only a list of usernames and count of
57 Ignored for `who am i'. */
58 static int short_list;
60 /* If nonzero, display the hours:minutes since each user has touched
61 the keyboard, or "." if within the last minute, or "old" if
62 not within the last day. */
63 static int include_idle;
65 /* If nonzero, display a line at the top describing each field. */
66 static int include_heading;
68 /* If nonzero, display a `+' for each user if mesg y, a `-' if mesg n,
69 or a `?' if their tty cannot be statted. */
70 static int include_mesg;
72 static struct option const longopts[] =
74 {"count", no_argument, NULL, 'q'},
75 {"idle", no_argument, NULL, 'u'},
76 {"heading", no_argument, NULL, 'H'},
77 {"message", no_argument, NULL, 'T'},
78 {"mesg", no_argument, NULL, 'T'},
79 {"writable", no_argument, NULL, 'T'},
80 {"help", no_argument, &show_help, 1},
81 {"version", no_argument, &show_version, 1},
85 /* Return a string representing the time between WHEN and the time
86 that this function is first run. */
89 idle_string (time_t when)
91 static time_t now = 0;
92 static char idle_hhmm[10];
98 seconds_idle = now - when;
99 if (seconds_idle < 60) /* One minute. */
101 if (seconds_idle < (24 * 60 * 60)) /* One day. */
103 sprintf (idle_hhmm, "%02d:%02d",
104 (int) (seconds_idle / (60 * 60)),
105 (int) ((seconds_idle % (60 * 60)) / 60));
106 return (const char *) idle_hhmm;
111 /* Display a line of information about UTMP_ENT. */
114 print_entry (const STRUCT_UTMP *utmp_ent)
120 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
121 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
123 char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
126 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
127 already an absolute pathname. Some system may put the full,
128 absolute pathname in ut_line. */
129 if (utmp_ent->ut_line[0] == '/')
131 strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
132 line[sizeof (utmp_ent->ut_line)] = '\0';
136 strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
137 strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
138 line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
141 if (stat (line, &stats) == 0)
143 mesg = (stats.st_mode & S_IWGRP) ? '+' : '-';
144 last_change = stats.st_atime;
152 printf ("%-8.*s", (int) sizeof (utmp_ent->ut_name), utmp_ent->ut_name);
154 printf (" %c ", mesg);
155 printf (" %-8.*s", (int) sizeof (utmp_ent->ut_line), utmp_ent->ut_line);
157 /* Don't take the address of UT_TIME_MEMBER directly.
158 Ulrich Drepper wrote:
159 ``... GNU libc (and perhaps other libcs as well) have extended
160 utmp file formats which do not use a simple time_t ut_time field.
161 In glibc, ut_time is a macro which selects for backward compatibility
162 the tv_sec member of a struct timeval value.'' */
163 tm = UT_TIME_MEMBER (utmp_ent);
164 printf (" %-12.12s", ctime (&tm) + 4);
169 printf (" %s", idle_string (last_change));
174 if (utmp_ent->ut_host[0])
176 extern char *canon_host ();
177 char ut_host[sizeof (utmp_ent->ut_host) + 1];
178 char *host = 0, *display = 0;
180 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
181 strncpy (ut_host, utmp_ent->ut_host, (int) sizeof (utmp_ent->ut_host));
182 ut_host[sizeof (utmp_ent->ut_host)] = '\0';
184 /* Look for an X display. */
185 display = strrchr (ut_host, ':');
190 /* See if we can canonicalize it. */
191 host = canon_host (ut_host);
196 printf (" (%s:%s)", host, display);
198 printf (" (%s)", host);
205 /* Print the username of each valid entry and the number of valid entries
206 in UTMP_BUF, which should have N elements. */
209 list_entries_who (int n, const STRUCT_UTMP *utmp_buf)
216 if (utmp_buf->ut_name[0]
218 && utmp_buf->ut_type == USER_PROCESS
224 trimmed_name = extract_trimmed_name (utmp_buf);
226 printf ("%s ", trimmed_name);
232 printf (_("\n# users=%u\n"), entries);
238 printf ("%-8s ", _("USER"));
241 printf ("%-8s ", _("LINE"));
242 printf (_("LOGIN-TIME "));
245 printf (_("FROM\n"));
248 /* Display UTMP_BUF, which should have N entries. */
251 scan_entries (int n, const STRUCT_UTMP *utmp_buf)
258 if (utmp_buf->ut_name[0]
260 && utmp_buf->ut_type == USER_PROCESS
263 print_entry (utmp_buf);
268 /* Display a list of who is on the system, according to utmp file FILENAME. */
271 who (const char *filename)
274 STRUCT_UTMP *utmp_buf;
275 int fail = read_utmp (filename, &n_users, &utmp_buf);
278 error (1, errno, "%s", filename);
281 list_entries_who (n_users, utmp_buf);
283 scan_entries (n_users, utmp_buf);
286 /* Search UTMP_CONTENTS, which should have N entries, for
287 an entry with a `ut_line' field identical to LINE.
288 Return the first matching entry found, or NULL if there
289 is no matching entry. */
291 static const STRUCT_UTMP *
292 search_entries (int n, const STRUCT_UTMP *utmp_buf, const char *line)
296 if (utmp_buf->ut_name[0]
298 && utmp_buf->ut_type == USER_PROCESS
300 && !strncmp (line, utmp_buf->ut_line, sizeof (utmp_buf->ut_line)))
307 /* Display the entry in utmp file FILENAME for this tty on standard input,
308 or nothing if there is no entry for it. */
311 who_am_i (const char *filename)
313 const STRUCT_UTMP *utmp_entry;
314 STRUCT_UTMP *utmp_buf;
315 char hostname[MAXHOSTNAMELEN + 1];
320 if (gethostname (hostname, MAXHOSTNAMELEN + 1))
325 printf ("%*s ", (int) strlen (hostname), " ");
332 tty += 5; /* Remove "/dev/". */
334 fail = read_utmp (filename, &n_users, &utmp_buf);
337 error (1, errno, "%s", filename);
339 utmp_entry = search_entries (n_users, utmp_buf, tty);
340 if (utmp_entry == NULL)
343 printf ("%s!", hostname);
344 print_entry (utmp_entry);
351 fprintf (stderr, _("Try `%s --help' for more information.\n"),
355 printf (_("Usage: %s [OPTION]... [ FILE | ARG1 ARG2 ]\n"), program_name);
358 -H, --heading print line of column headings\n\
359 -i, -u, --idle add user idle time as HOURS:MINUTES, . or old\n\
360 -m only hostname and user associated with stdin\n\
361 -q, --count all login names and number of users logged on\n\
363 -T, -w, --mesg add user's message status as +, - or ?\n\
364 --message same as -T\n\
365 --writable same as -T\n\
366 --help display this help and exit\n\
367 --version output version information and exit\n\
369 If FILE is not specified, use %s. %s as FILE is common.\n\
370 If ARG1 ARG2 given, -m presumed: `am i' or `mom likes' are usual.\n\
371 "), UTMP_FILE, WTMP_FILE);
372 puts (_("\nReport bugs to <sh-utils-bugs@gnu.ai.mit.edu>."));
378 main (int argc, char **argv)
381 int my_line_only = 0;
383 program_name = argv[0];
384 setlocale (LC_ALL, "");
385 bindtextdomain (PACKAGE, LOCALEDIR);
386 textdomain (PACKAGE);
388 while ((optc = getopt_long (argc, argv, "imqsuwHT", longopts, &longind)) != -1)
427 printf ("who (%s) %s\n", GNU_PACKAGE, VERSION);
434 switch (argc - optind)
438 who_am_i (UTMP_FILE);
443 case 1: /* who <utmp file> */
445 who_am_i (argv[optind]);
450 case 2: /* who <blurf> <glop> */
451 who_am_i (UTMP_FILE);
455 error (0, 0, _("too many arguments"));