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 entry THIS. */
114 print_entry (STRUCT_UTMP *this)
120 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
121 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
123 char line[sizeof (this->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 (this->ut_line[0] == '/')
131 strncpy (line, this->ut_line, sizeof (this->ut_line));
132 line[sizeof (this->ut_line)] = '\0';
136 strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
137 strncpy (line + DEV_DIR_LEN, this->ut_line, sizeof (this->ut_line));
138 line[DEV_DIR_LEN + sizeof (this->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 (this->ut_name), this->ut_name);
154 printf (" %c ", mesg);
155 printf (" %-8.*s", (int) sizeof (this->ut_line), this->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 (this);
164 printf (" %-12.12s", ctime (&tm) + 4);
169 printf (" %s", idle_string (last_change));
174 if (this->ut_host[0])
176 extern char *canon_host ();
177 char ut_host[sizeof (this->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, this->ut_host, (int) sizeof (this->ut_host));
182 ut_host[sizeof (this->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_contents', which should have N elements. */
209 list_entries_who (int n)
211 register STRUCT_UTMP *this = utmp_contents;
219 && this->ut_type == USER_PROCESS
225 trimmed_name = extract_trimmed_name (this);
227 printf ("%s ", trimmed_name);
233 printf (_("\n# users=%u\n"), entries);
239 printf ("%-8s ", _("USER"));
242 printf ("%-8s ", _("LINE"));
243 printf (_("LOGIN-TIME "));
246 printf (_("FROM\n"));
249 /* Display `utmp_contents', which should have N entries. */
254 register STRUCT_UTMP *this = utmp_contents;
263 && this->ut_type == USER_PROCESS
271 /* Display a list of who is on the system, according to utmp file FILENAME. */
274 who (const char *filename)
276 int users = read_utmp (filename);
278 list_entries_who (users);
280 scan_entries (users);
283 /* Search `utmp_contents', which should have N entries, for
284 an entry with a `ut_line' field identical to LINE.
285 Return the first matching entry found, or NULL if there
286 is no matching entry. */
289 search_entries (int n, char *line)
291 register STRUCT_UTMP *this = utmp_contents;
297 && this->ut_type == USER_PROCESS
299 && !strncmp (line, this->ut_line, sizeof (this->ut_line)))
306 /* Display the entry in utmp file FILENAME for this tty on standard input,
307 or nothing if there is no entry for it. */
310 who_am_i (char *filename)
312 register STRUCT_UTMP *utmp_entry;
313 char hostname[MAXHOSTNAMELEN + 1];
316 if (gethostname (hostname, MAXHOSTNAMELEN + 1))
321 printf ("%*s ", (int) strlen (hostname), " ");
328 tty += 5; /* Remove "/dev/". */
330 utmp_entry = search_entries (read_utmp (filename), tty);
331 if (utmp_entry == NULL)
334 printf ("%s!", hostname);
335 print_entry (utmp_entry);
342 fprintf (stderr, _("Try `%s --help' for more information.\n"),
346 printf (_("Usage: %s [OPTION]... [ FILE | ARG1 ARG2 ]\n"), program_name);
349 -H, --heading print line of column headings\n\
350 -i, -u, --idle add user idle time as HOURS:MINUTES, . or old\n\
351 -m only hostname and user associated with stdin\n\
352 -q, --count all login names and number of users logged on\n\
354 -T, -w, --mesg add user's message status as +, - or ?\n\
355 --message same as -T\n\
356 --writable same as -T\n\
357 --help display this help and exit\n\
358 --version output version information and exit\n\
360 If FILE is not specified, use %s. %s as FILE is common.\n\
361 If ARG1 ARG2 given, -m presumed: `am i' or `mom likes' are usual.\n\
362 "), UTMP_FILE, WTMP_FILE);
363 puts (_("\nReport bugs to <sh-utils-bugs@gnu.ai.mit.edu>."));
369 main (int argc, char **argv)
372 int my_line_only = 0;
374 program_name = argv[0];
375 setlocale (LC_ALL, "");
376 bindtextdomain (PACKAGE, LOCALEDIR);
377 textdomain (PACKAGE);
379 while ((optc = getopt_long (argc, argv, "imqsuwHT", longopts, &longind)) != -1)
418 printf ("who (%s) %s\n", GNU_PACKAGE, VERSION);
425 switch (argc - optind)
429 who_am_i (UTMP_FILE);
434 case 1: /* who <utmp file> */
436 who_am_i (argv[optind]);
441 case 2: /* who <blurf> <glop> */
442 who_am_i (UTMP_FILE);
446 error (0, 0, _("too many arguments"));