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, attempt to canonicalize hostnames via a DNS lookup. */
58 /* If nonzero, display only a list of usernames and count of
60 Ignored for `who am i'. */
61 static int short_list;
63 /* If nonzero, display the hours:minutes since each user has touched
64 the keyboard, or "." if within the last minute, or "old" if
65 not within the last day. */
66 static int include_idle;
68 /* If nonzero, display a line at the top describing each field. */
69 static int include_heading;
71 /* If nonzero, display a `+' for each user if mesg y, a `-' if mesg n,
72 or a `?' if their tty cannot be statted. */
73 static int include_mesg;
75 static struct option const longopts[] =
77 {"count", no_argument, NULL, 'q'},
78 {"idle", no_argument, NULL, 'u'},
79 {"heading", no_argument, NULL, 'H'},
80 {"lookup", no_argument, NULL, 'l'},
81 {"message", no_argument, NULL, 'T'},
82 {"mesg", no_argument, NULL, 'T'},
83 {"writable", no_argument, NULL, 'T'},
84 {"help", no_argument, &show_help, 1},
85 {"version", no_argument, &show_version, 1},
89 /* Return a string representing the time between WHEN and the time
90 that this function is first run. */
93 idle_string (time_t when)
95 static time_t now = 0;
96 static char idle_hhmm[10];
102 seconds_idle = now - when;
103 if (seconds_idle < 60) /* One minute. */
105 if (seconds_idle < (24 * 60 * 60)) /* One day. */
107 sprintf (idle_hhmm, "%02d:%02d",
108 (int) (seconds_idle / (60 * 60)),
109 (int) ((seconds_idle % (60 * 60)) / 60));
110 return (const char *) idle_hhmm;
115 /* Display a line of information about UTMP_ENT. */
118 print_entry (const STRUCT_UTMP *utmp_ent)
124 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
125 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
127 char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
130 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
131 already an absolute pathname. Some system may put the full,
132 absolute pathname in ut_line. */
133 if (utmp_ent->ut_line[0] == '/')
135 strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
136 line[sizeof (utmp_ent->ut_line)] = '\0';
140 strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
141 strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
142 line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
145 if (stat (line, &stats) == 0)
147 mesg = (stats.st_mode & S_IWGRP) ? '+' : '-';
148 last_change = stats.st_atime;
156 printf ("%-8.*s", (int) sizeof (utmp_ent->ut_name), utmp_ent->ut_name);
158 printf (" %c ", mesg);
159 printf (" %-8.*s", (int) sizeof (utmp_ent->ut_line), utmp_ent->ut_line);
161 /* Don't take the address of UT_TIME_MEMBER directly.
162 Ulrich Drepper wrote:
163 ``... GNU libc (and perhaps other libcs as well) have extended
164 utmp file formats which do not use a simple time_t ut_time field.
165 In glibc, ut_time is a macro which selects for backward compatibility
166 the tv_sec member of a struct timeval value.'' */
167 tm = UT_TIME_MEMBER (utmp_ent);
168 printf (" %-12.12s", ctime (&tm) + 4);
173 printf (" %s", idle_string (last_change));
178 if (utmp_ent->ut_host[0] && do_lookup)
180 extern char *canon_host ();
181 char ut_host[sizeof (utmp_ent->ut_host) + 1];
182 char *host = 0, *display = 0;
184 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
185 strncpy (ut_host, utmp_ent->ut_host, (int) sizeof (utmp_ent->ut_host));
186 ut_host[sizeof (utmp_ent->ut_host)] = '\0';
188 /* Look for an X display. */
189 display = strrchr (ut_host, ':');
194 /* See if we can canonicalize it. */
195 host = canon_host (ut_host);
200 printf (" (%s:%s)", host, display);
202 printf (" (%s)", host);
209 /* Print the username of each valid entry and the number of valid entries
210 in UTMP_BUF, which should have N elements. */
213 list_entries_who (int n, const STRUCT_UTMP *utmp_buf)
220 if (utmp_buf->ut_name[0]
222 && utmp_buf->ut_type == USER_PROCESS
228 trimmed_name = extract_trimmed_name (utmp_buf);
230 printf ("%s ", trimmed_name);
236 printf (_("\n# users=%u\n"), entries);
242 printf ("%-8s ", _("USER"));
245 printf ("%-8s ", _("LINE"));
246 printf (_("LOGIN-TIME "));
249 printf (_("FROM\n"));
252 /* Display UTMP_BUF, which should have N entries. */
255 scan_entries (int n, const STRUCT_UTMP *utmp_buf)
262 if (utmp_buf->ut_name[0]
264 && utmp_buf->ut_type == USER_PROCESS
267 print_entry (utmp_buf);
272 /* Display a list of who is on the system, according to utmp file FILENAME. */
275 who (const char *filename)
278 STRUCT_UTMP *utmp_buf;
279 int fail = read_utmp (filename, &n_users, &utmp_buf);
282 error (1, errno, "%s", filename);
285 list_entries_who (n_users, utmp_buf);
287 scan_entries (n_users, utmp_buf);
290 /* Search UTMP_CONTENTS, which should have N entries, for
291 an entry with a `ut_line' field identical to LINE.
292 Return the first matching entry found, or NULL if there
293 is no matching entry. */
295 static const STRUCT_UTMP *
296 search_entries (int n, const STRUCT_UTMP *utmp_buf, const char *line)
300 if (utmp_buf->ut_name[0]
302 && utmp_buf->ut_type == USER_PROCESS
304 && !strncmp (line, utmp_buf->ut_line, sizeof (utmp_buf->ut_line)))
311 /* Display the entry in utmp file FILENAME for this tty on standard input,
312 or nothing if there is no entry for it. */
315 who_am_i (const char *filename)
317 const STRUCT_UTMP *utmp_entry;
318 STRUCT_UTMP *utmp_buf;
319 char hostname[MAXHOSTNAMELEN + 1];
324 if (gethostname (hostname, MAXHOSTNAMELEN + 1))
329 printf ("%*s ", (int) strlen (hostname), " ");
336 tty += 5; /* Remove "/dev/". */
338 fail = read_utmp (filename, &n_users, &utmp_buf);
341 error (1, errno, "%s", filename);
343 utmp_entry = search_entries (n_users, utmp_buf, tty);
344 if (utmp_entry == NULL)
347 printf ("%s!", hostname);
348 print_entry (utmp_entry);
355 fprintf (stderr, _("Try `%s --help' for more information.\n"),
359 printf (_("Usage: %s [OPTION]... [ FILE | ARG1 ARG2 ]\n"), program_name);
362 -H, --heading print line of column headings\n\
363 -i, -u, --idle add user idle time as HOURS:MINUTES, . or old\n\
364 -l, --lookup attempt to canonicalize hostnames via DNS\n\
365 -m only hostname and user associated with stdin\n\
366 -q, --count all login names and number of users logged on\n\
368 -T, -w, --mesg add user's message status as +, - or ?\n\
369 --message same as -T\n\
370 --writable same as -T\n\
371 --help display this help and exit\n\
372 --version output version information and exit\n\
374 If FILE is not specified, use %s. %s as FILE is common.\n\
375 If ARG1 ARG2 given, -m presumed: `am i' or `mom likes' are usual.\n\
376 "), UTMP_FILE, WTMP_FILE);
377 puts (_("\nReport bugs to <sh-utils-bugs@gnu.org>."));
383 main (int argc, char **argv)
386 int my_line_only = 0;
388 program_name = argv[0];
389 setlocale (LC_ALL, "");
390 bindtextdomain (PACKAGE, LOCALEDIR);
391 textdomain (PACKAGE);
393 while ((optc = getopt_long (argc, argv, "ilmqsuwHT", longopts, &longind)) != -1)
436 printf ("who (%s) %s\n", GNU_PACKAGE, VERSION);
443 switch (argc - optind)
447 who_am_i (UTMP_FILE);
452 case 1: /* who <utmp file> */
454 who_am_i (argv[optind]);
459 case 2: /* who <blurf> <glop> */
460 who_am_i (UTMP_FILE);
464 error (0, 0, _("too many arguments"));