(main): Stat all non-`-' input file files (and fail if a
[coreutils.git] / src / who.c
blob59f14290c252931bb41a54c9b955fea556d8757d
1 /* GNU's who.
2 Copyright (C) 92, 93, 94, 95, 96, 1997, 1998 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>
30 #include "error.h"
31 #include "readutmp.h"
32 #include "system.h"
34 #ifndef MAXHOSTNAMELEN
35 # define MAXHOSTNAMELEN 64
36 #endif
38 #ifndef S_IWGRP
39 # define S_IWGRP 020
40 #endif
42 int gethostname ();
43 char *ttyname ();
45 /* The name this program was run with. */
46 char *program_name;
48 /* If nonzero, display usage information and exit. */
49 static int show_help;
51 /* If nonzero, print the version on standard output and exit. */
52 static int show_version;
54 /* If nonzero, attempt to canonicalize hostnames via a DNS lookup. */
55 static int do_lookup;
57 /* If nonzero, display only a list of usernames and count of
58 the users logged on.
59 Ignored for `who am i'. */
60 static int short_list;
62 /* If nonzero, display the hours:minutes since each user has touched
63 the keyboard, or "." if within the last minute, or "old" if
64 not within the last day. */
65 static int include_idle;
67 /* If nonzero, display a line at the top describing each field. */
68 static int include_heading;
70 /* If nonzero, display a `+' for each user if mesg y, a `-' if mesg n,
71 or a `?' if their tty cannot be statted. */
72 static int include_mesg;
74 static struct option const longopts[] =
76 {"count", no_argument, NULL, 'q'},
77 {"idle", no_argument, NULL, 'u'},
78 {"heading", no_argument, NULL, 'H'},
79 {"lookup", no_argument, NULL, 'l'},
80 {"message", no_argument, NULL, 'T'},
81 {"mesg", no_argument, NULL, 'T'},
82 {"writable", no_argument, NULL, 'T'},
83 {"help", no_argument, &show_help, 1},
84 {"version", no_argument, &show_version, 1},
85 {NULL, 0, NULL, 0}
88 /* Return a string representing the time between WHEN and the time
89 that this function is first run. */
91 static const char *
92 idle_string (time_t when)
94 static time_t now = 0;
95 static char idle_hhmm[10];
96 time_t seconds_idle;
98 if (now == 0)
99 time (&now);
101 seconds_idle = now - when;
102 if (seconds_idle < 60) /* One minute. */
103 return " . ";
104 if (seconds_idle < (24 * 60 * 60)) /* One day. */
106 sprintf (idle_hhmm, "%02d:%02d",
107 (int) (seconds_idle / (60 * 60)),
108 (int) ((seconds_idle % (60 * 60)) / 60));
109 return (const char *) idle_hhmm;
111 return _(" old ");
114 /* Display a line of information about UTMP_ENT. */
116 static void
117 print_entry (const STRUCT_UTMP *utmp_ent)
119 struct stat stats;
120 time_t last_change;
121 char mesg;
123 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
124 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
126 char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
127 time_t tm;
129 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
130 already an absolute pathname. Some system may put the full,
131 absolute pathname in ut_line. */
132 if (utmp_ent->ut_line[0] == '/')
134 strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
135 line[sizeof (utmp_ent->ut_line)] = '\0';
137 else
139 strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
140 strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
141 line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
144 if (stat (line, &stats) == 0)
146 mesg = (stats.st_mode & S_IWGRP) ? '+' : '-';
147 last_change = stats.st_atime;
149 else
151 mesg = '?';
152 last_change = 0;
155 printf ("%-8.*s", (int) sizeof (utmp_ent->ut_name), utmp_ent->ut_name);
156 if (include_mesg)
157 printf (" %c ", mesg);
158 printf (" %-8.*s", (int) sizeof (utmp_ent->ut_line), utmp_ent->ut_line);
160 /* Don't take the address of UT_TIME_MEMBER directly.
161 Ulrich Drepper wrote:
162 ``... GNU libc (and perhaps other libcs as well) have extended
163 utmp file formats which do not use a simple time_t ut_time field.
164 In glibc, ut_time is a macro which selects for backward compatibility
165 the tv_sec member of a struct timeval value.'' */
166 tm = UT_TIME_MEMBER (utmp_ent);
167 printf (" %-12.12s", ctime (&tm) + 4);
169 if (include_idle)
171 if (last_change)
172 printf (" %s", idle_string (last_change));
173 else
174 printf (" . ");
176 #ifdef HAVE_UT_HOST
177 if (utmp_ent->ut_host[0] && do_lookup)
179 extern char *canon_host ();
180 char ut_host[sizeof (utmp_ent->ut_host) + 1];
181 char *host = 0, *display = 0;
183 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
184 strncpy (ut_host, utmp_ent->ut_host, (int) sizeof (utmp_ent->ut_host));
185 ut_host[sizeof (utmp_ent->ut_host)] = '\0';
187 /* Look for an X display. */
188 display = strrchr (ut_host, ':');
189 if (display)
190 *display++ = '\0';
192 if (*ut_host)
193 /* See if we can canonicalize it. */
194 host = canon_host (ut_host);
195 if (! host)
196 host = ut_host;
198 if (display)
199 printf (" (%s:%s)", host, display);
200 else
201 printf (" (%s)", host);
203 #endif
205 putchar ('\n');
208 /* Print the username of each valid entry and the number of valid entries
209 in UTMP_BUF, which should have N elements. */
211 static void
212 list_entries_who (int n, const STRUCT_UTMP *utmp_buf)
214 int entries;
216 entries = 0;
217 while (n--)
219 if (utmp_buf->ut_name[0]
220 #ifdef USER_PROCESS
221 && utmp_buf->ut_type == USER_PROCESS
222 #endif
225 char *trimmed_name;
227 trimmed_name = extract_trimmed_name (utmp_buf);
229 printf ("%s ", trimmed_name);
230 free (trimmed_name);
231 entries++;
233 utmp_buf++;
235 printf (_("\n# users=%u\n"), entries);
238 static void
239 print_heading (void)
241 printf ("%-8s ", _("USER"));
242 if (include_mesg)
243 printf (_("MESG "));
244 printf ("%-8s ", _("LINE"));
245 printf (_("LOGIN-TIME "));
246 if (include_idle)
247 printf (_("IDLE "));
248 printf (_("FROM\n"));
251 /* Display UTMP_BUF, which should have N entries. */
253 static void
254 scan_entries (int n, const STRUCT_UTMP *utmp_buf)
256 if (include_heading)
257 print_heading ();
259 while (n--)
261 if (utmp_buf->ut_name[0]
262 #ifdef USER_PROCESS
263 && utmp_buf->ut_type == USER_PROCESS
264 #endif
266 print_entry (utmp_buf);
267 utmp_buf++;
271 /* Display a list of who is on the system, according to utmp file FILENAME. */
273 static void
274 who (const char *filename)
276 int n_users;
277 STRUCT_UTMP *utmp_buf;
278 int fail = read_utmp (filename, &n_users, &utmp_buf);
280 if (fail)
281 error (1, errno, "%s", filename);
283 if (short_list)
284 list_entries_who (n_users, utmp_buf);
285 else
286 scan_entries (n_users, utmp_buf);
289 /* Search UTMP_CONTENTS, which should have N entries, for
290 an entry with a `ut_line' field identical to LINE.
291 Return the first matching entry found, or NULL if there
292 is no matching entry. */
294 static const STRUCT_UTMP *
295 search_entries (int n, const STRUCT_UTMP *utmp_buf, const char *line)
297 while (n--)
299 if (utmp_buf->ut_name[0]
300 #ifdef USER_PROCESS
301 && utmp_buf->ut_type == USER_PROCESS
302 #endif
303 && !strncmp (line, utmp_buf->ut_line, sizeof (utmp_buf->ut_line)))
304 return utmp_buf;
305 utmp_buf++;
307 return NULL;
310 /* Display the entry in utmp file FILENAME for this tty on standard input,
311 or nothing if there is no entry for it. */
313 static void
314 who_am_i (const char *filename)
316 const STRUCT_UTMP *utmp_entry;
317 STRUCT_UTMP *utmp_buf;
318 char hostname[MAXHOSTNAMELEN + 1];
319 char *tty;
320 int fail;
321 int n_users;
323 if (gethostname (hostname, MAXHOSTNAMELEN + 1))
324 *hostname = 0;
326 if (include_heading)
328 printf ("%*s ", (int) strlen (hostname), " ");
329 print_heading ();
332 tty = ttyname (0);
333 if (tty == NULL)
334 return;
335 tty += 5; /* Remove "/dev/". */
337 fail = read_utmp (filename, &n_users, &utmp_buf);
339 if (fail)
340 error (1, errno, "%s", filename);
342 utmp_entry = search_entries (n_users, utmp_buf, tty);
343 if (utmp_entry == NULL)
344 return;
346 printf ("%s!", hostname);
347 print_entry (utmp_entry);
350 static void
351 usage (int status)
353 if (status != 0)
354 fprintf (stderr, _("Try `%s --help' for more information.\n"),
355 program_name);
356 else
358 printf (_("Usage: %s [OPTION]... [ FILE | ARG1 ARG2 ]\n"), program_name);
359 printf (_("\
361 -H, --heading print line of column headings\n\
362 -i, -u, --idle add user idle time as HOURS:MINUTES, . or old\n\
363 -l, --lookup attempt to canonicalize hostnames via DNS\n\
364 -m only hostname and user associated with stdin\n\
365 -q, --count all login names and number of users logged on\n\
366 -s (ignored)\n\
367 -T, -w, --mesg add user's message status as +, - or ?\n\
368 --message same as -T\n\
369 --writable same as -T\n\
370 --help display this help and exit\n\
371 --version output version information and exit\n\
373 If FILE is not specified, use %s. %s as FILE is common.\n\
374 If ARG1 ARG2 given, -m presumed: `am i' or `mom likes' are usual.\n\
375 "), UTMP_FILE, WTMP_FILE);
376 puts (_("\nReport bugs to <sh-utils-bugs@gnu.org>."));
378 exit (status);
382 main (int argc, char **argv)
384 int optc, longind;
385 int my_line_only = 0;
387 program_name = argv[0];
388 setlocale (LC_ALL, "");
389 bindtextdomain (PACKAGE, LOCALEDIR);
390 textdomain (PACKAGE);
392 while ((optc = getopt_long (argc, argv, "ilmqsuwHT", longopts, &longind)) != -1)
394 switch (optc)
396 case 0:
397 break;
399 case 'm':
400 my_line_only = 1;
401 break;
403 case 'l':
404 do_lookup = 1;
405 break;
407 case 'q':
408 short_list = 1;
409 break;
411 case 's':
412 break;
414 case 'i':
415 case 'u':
416 include_idle = 1;
417 break;
419 case 'H':
420 include_heading = 1;
421 break;
423 case 'w':
424 case 'T':
425 include_mesg = 1;
426 break;
428 default:
429 usage (1);
433 if (show_version)
435 printf ("who (%s) %s\n", GNU_PACKAGE, VERSION);
436 exit (0);
439 if (show_help)
440 usage (0);
442 switch (argc - optind)
444 case 0: /* who */
445 if (my_line_only)
446 who_am_i (UTMP_FILE);
447 else
448 who (UTMP_FILE);
449 break;
451 case 1: /* who <utmp file> */
452 if (my_line_only)
453 who_am_i (argv[optind]);
454 else
455 who (argv[optind]);
456 break;
458 case 2: /* who <blurf> <glop> */
459 who_am_i (UTMP_FILE);
460 break;
462 default: /* lose */
463 error (0, 0, _("too many arguments"));
464 usage (1);
467 exit (0);