(PROGRAM_NAME, AUTHORS): Define and use.
[coreutils.git] / src / who.c
blob88088f313104361fe4ce3c860c446097763da2eb
1 /* GNU's who.
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 /* Output format:
21 name [state] line time [idle] host
22 state: -T
23 name, line, time: not -q
24 idle: -u
27 #include <config.h>
28 #include <getopt.h>
29 #include <stdio.h>
31 #include "error.h"
32 #include "long-options.h"
33 #include "readutmp.h"
34 #include "system.h"
36 /* The official name of this program (e.g., no `g' prefix). */
37 #define PROGRAM_NAME "who"
39 #define AUTHORS "Joseph Arceneaux and David MacKenzie"
41 #ifndef MAXHOSTNAMELEN
42 # define MAXHOSTNAMELEN 64
43 #endif
45 #ifndef S_IWGRP
46 # define S_IWGRP 020
47 #endif
49 int gethostname ();
50 char *ttyname ();
52 /* The name this program was run with. */
53 char *program_name;
55 /* If nonzero, attempt to canonicalize hostnames via a DNS lookup. */
56 static int do_lookup;
58 /* If nonzero, display only a list of usernames and count of
59 the users logged on.
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 {NULL, 0, NULL, 0}
87 /* Return a string representing the time between WHEN and the time
88 that this function is first run. */
90 static const char *
91 idle_string (time_t when)
93 static time_t now = 0;
94 static char idle_hhmm[10];
95 time_t seconds_idle;
97 if (now == 0)
98 time (&now);
100 seconds_idle = now - when;
101 if (seconds_idle < 60) /* One minute. */
102 return " . ";
103 if (seconds_idle < (24 * 60 * 60)) /* One day. */
105 sprintf (idle_hhmm, "%02d:%02d",
106 (int) (seconds_idle / (60 * 60)),
107 (int) ((seconds_idle % (60 * 60)) / 60));
108 return (const char *) idle_hhmm;
110 return _(" old ");
113 /* Display a line of information about UTMP_ENT. */
115 static void
116 print_entry (const STRUCT_UTMP *utmp_ent)
118 struct stat stats;
119 time_t last_change;
120 char mesg;
122 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
123 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
125 char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
126 time_t tm;
128 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
129 already an absolute pathname. Some system may put the full,
130 absolute pathname in ut_line. */
131 if (utmp_ent->ut_line[0] == '/')
133 strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
134 line[sizeof (utmp_ent->ut_line)] = '\0';
136 else
138 strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
139 strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
140 line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
143 if (stat (line, &stats) == 0)
145 mesg = (stats.st_mode & S_IWGRP) ? '+' : '-';
146 last_change = stats.st_atime;
148 else
150 mesg = '?';
151 last_change = 0;
154 printf ("%-8.*s", (int) sizeof (utmp_ent->ut_name), utmp_ent->ut_name);
155 if (include_mesg)
156 printf (" %c ", mesg);
157 printf (" %-8.*s", (int) sizeof (utmp_ent->ut_line), utmp_ent->ut_line);
159 /* Don't take the address of UT_TIME_MEMBER directly.
160 Ulrich Drepper wrote:
161 ``... GNU libc (and perhaps other libcs as well) have extended
162 utmp file formats which do not use a simple time_t ut_time field.
163 In glibc, ut_time is a macro which selects for backward compatibility
164 the tv_sec member of a struct timeval value.'' */
165 tm = UT_TIME_MEMBER (utmp_ent);
166 printf (" %-12.12s", ctime (&tm) + 4);
168 if (include_idle)
170 if (last_change)
171 printf (" %s", idle_string (last_change));
172 else
173 printf (" . ");
175 #ifdef HAVE_UT_HOST
176 if (utmp_ent->ut_host[0] && do_lookup)
178 extern char *canon_host ();
179 char ut_host[sizeof (utmp_ent->ut_host) + 1];
180 char *host = 0, *display = 0;
182 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
183 strncpy (ut_host, utmp_ent->ut_host, (int) sizeof (utmp_ent->ut_host));
184 ut_host[sizeof (utmp_ent->ut_host)] = '\0';
186 /* Look for an X display. */
187 display = strrchr (ut_host, ':');
188 if (display)
189 *display++ = '\0';
191 if (*ut_host)
192 /* See if we can canonicalize it. */
193 host = canon_host (ut_host);
194 if (! host)
195 host = ut_host;
197 if (display)
198 printf (" (%s:%s)", host, display);
199 else
200 printf (" (%s)", host);
202 #endif
204 putchar ('\n');
207 /* Print the username of each valid entry and the number of valid entries
208 in UTMP_BUF, which should have N elements. */
210 static void
211 list_entries_who (int n, const STRUCT_UTMP *utmp_buf)
213 int entries;
215 entries = 0;
216 while (n--)
218 if (utmp_buf->ut_name[0]
219 #ifdef USER_PROCESS
220 && utmp_buf->ut_type == USER_PROCESS
221 #endif
224 char *trimmed_name;
226 trimmed_name = extract_trimmed_name (utmp_buf);
228 printf ("%s ", trimmed_name);
229 free (trimmed_name);
230 entries++;
232 utmp_buf++;
234 printf (_("\n# users=%u\n"), entries);
237 static void
238 print_heading (void)
240 printf ("%-8s ", _("USER"));
241 if (include_mesg)
242 printf (_("MESG "));
243 printf ("%-8s ", _("LINE"));
244 printf (_("LOGIN-TIME "));
245 if (include_idle)
246 printf (_("IDLE "));
247 printf (_("FROM\n"));
250 /* Display UTMP_BUF, which should have N entries. */
252 static void
253 scan_entries (int n, const STRUCT_UTMP *utmp_buf)
255 if (include_heading)
256 print_heading ();
258 while (n--)
260 if (utmp_buf->ut_name[0]
261 #ifdef USER_PROCESS
262 && utmp_buf->ut_type == USER_PROCESS
263 #endif
265 print_entry (utmp_buf);
266 utmp_buf++;
270 /* Display a list of who is on the system, according to utmp file FILENAME. */
272 static void
273 who (const char *filename)
275 int n_users;
276 STRUCT_UTMP *utmp_buf;
277 int fail = read_utmp (filename, &n_users, &utmp_buf);
279 if (fail)
280 error (1, errno, "%s", filename);
282 if (short_list)
283 list_entries_who (n_users, utmp_buf);
284 else
285 scan_entries (n_users, utmp_buf);
288 /* Search UTMP_CONTENTS, which should have N entries, for
289 an entry with a `ut_line' field identical to LINE.
290 Return the first matching entry found, or NULL if there
291 is no matching entry. */
293 static const STRUCT_UTMP *
294 search_entries (int n, const STRUCT_UTMP *utmp_buf, const char *line)
296 while (n--)
298 if (utmp_buf->ut_name[0]
299 #ifdef USER_PROCESS
300 && utmp_buf->ut_type == USER_PROCESS
301 #endif
302 && !strncmp (line, utmp_buf->ut_line, sizeof (utmp_buf->ut_line)))
303 return utmp_buf;
304 utmp_buf++;
306 return NULL;
309 /* Display the entry in utmp file FILENAME for this tty on standard input,
310 or nothing if there is no entry for it. */
312 static void
313 who_am_i (const char *filename)
315 const STRUCT_UTMP *utmp_entry;
316 STRUCT_UTMP *utmp_buf;
317 char hostname[MAXHOSTNAMELEN + 1];
318 char *tty;
319 int fail;
320 int n_users;
322 if (gethostname (hostname, MAXHOSTNAMELEN + 1))
323 *hostname = 0;
325 if (include_heading)
327 printf ("%*s ", (int) strlen (hostname), " ");
328 print_heading ();
331 tty = ttyname (0);
332 if (tty == NULL)
333 return;
334 tty += 5; /* Remove "/dev/". */
336 fail = read_utmp (filename, &n_users, &utmp_buf);
338 if (fail)
339 error (1, errno, "%s", filename);
341 utmp_entry = search_entries (n_users, utmp_buf, tty);
342 if (utmp_entry == NULL)
343 return;
345 printf ("%s!", hostname);
346 print_entry (utmp_entry);
349 void
350 usage (int status)
352 if (status != 0)
353 fprintf (stderr, _("Try `%s --help' for more information.\n"),
354 program_name);
355 else
357 printf (_("Usage: %s [OPTION]... [ FILE | ARG1 ARG2 ]\n"), program_name);
358 printf (_("\
360 -H, --heading print line of column headings\n\
361 -i, -u, --idle add user idle time as HOURS:MINUTES, . or old\n\
362 -l, --lookup attempt to canonicalize hostnames via DNS\n\
363 -m only hostname and user associated with stdin\n\
364 -q, --count all login names and number of users logged on\n\
365 -s (ignored)\n\
366 -T, -w, --mesg add user's message status as +, - or ?\n\
367 --message same as -T\n\
368 --writable same as -T\n\
369 --help display this help and exit\n\
370 --version output version information and exit\n\
372 If FILE is not specified, use %s. %s as FILE is common.\n\
373 If ARG1 ARG2 given, -m presumed: `am i' or `mom likes' are usual.\n\
374 "), UTMP_FILE, WTMP_FILE);
375 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
377 exit (status);
381 main (int argc, char **argv)
383 int optc, longind;
384 int my_line_only = 0;
386 program_name = argv[0];
387 setlocale (LC_ALL, "");
388 bindtextdomain (PACKAGE, LOCALEDIR);
389 textdomain (PACKAGE);
391 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
392 AUTHORS, usage);
394 while ((optc = getopt_long (argc, argv, "ilmqsuwHT", longopts, &longind))
395 != -1)
397 switch (optc)
399 case 0:
400 break;
402 case 'm':
403 my_line_only = 1;
404 break;
406 case 'l':
407 do_lookup = 1;
408 break;
410 case 'q':
411 short_list = 1;
412 break;
414 case 's':
415 break;
417 case 'i':
418 case 'u':
419 include_idle = 1;
420 break;
422 case 'H':
423 include_heading = 1;
424 break;
426 case 'w':
427 case 'T':
428 include_mesg = 1;
429 break;
431 default:
432 usage (1);
436 switch (argc - optind)
438 case 0: /* who */
439 if (my_line_only)
440 who_am_i (UTMP_FILE);
441 else
442 who (UTMP_FILE);
443 break;
445 case 1: /* who <utmp file> */
446 if (my_line_only)
447 who_am_i (argv[optind]);
448 else
449 who (argv[optind]);
450 break;
452 case 2: /* who <blurf> <glop> */
453 who_am_i (UTMP_FILE);
454 break;
456 default: /* lose */
457 error (0, 0, _("too many arguments"));
458 usage (1);
461 exit (0);