2 Copyright (C) 1992-1997, 1999-2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Created by hacking who.c by Kaveh Ghazi ghazi@caip.rutgers.edu */
25 #include <sys/types.h>
28 #include "canon-host.h"
30 #include "hard-locale.h"
33 #include "stat-macros.h"
35 /* The official name of this program (e.g., no `g' prefix). */
36 #define PROGRAM_NAME "pinky"
38 #define AUTHORS "Joseph Arceneaux", "David MacKenzie", "Kaveh Ghazi"
40 #ifndef MAXHOSTNAMELEN
41 # define MAXHOSTNAMELEN 64
46 /* The name this program was run with. */
47 const char *program_name;
49 /* If true, display the hours:minutes since each user has touched
50 the keyboard, or blank if within the last minute, or days followed
51 by a 'd' if not within the last day. */
52 static bool include_idle = true;
54 /* If true, display a line at the top describing each field. */
55 static bool include_heading = true;
57 /* if true, display the user's full name from pw_gecos. */
58 static bool include_fullname = true;
60 /* if true, display the user's ~/.project file when doing long format. */
61 static bool include_project = true;
63 /* if true, display the user's ~/.plan file when doing long format. */
64 static bool include_plan = true;
66 /* if true, display the user's home directory and shell
67 when doing long format. */
68 static bool include_home_and_shell = true;
70 /* if true, use the "short" output format. */
71 static bool do_short_format = true;
73 /* if true, display the ut_host field. */
75 static bool include_where = true;
78 /* The strftime format to use for login times, and its expected
80 static char const *time_format;
81 static int time_format_width;
83 static struct option const longopts[] =
85 {GETOPT_HELP_OPTION_DECL},
86 {GETOPT_VERSION_OPTION_DECL},
90 /* Count and return the number of ampersands in STR. */
93 count_ampersands (const char *str)
104 /* Create a string (via xmalloc) which contains a full name by substituting
105 for each ampersand in GECOS_NAME the USER_NAME string with its first
106 character capitalized. The caller must ensure that GECOS_NAME contains
107 no `,'s. The caller also is responsible for free'ing the return value of
111 create_fullname (const char *gecos_name, const char *user_name)
113 size_t rsize = strlen (gecos_name) + 1;
116 size_t ampersands = count_ampersands (gecos_name);
120 size_t ulen = strlen (user_name);
121 size_t product = ampersands * ulen;
122 rsize += product - ampersands;
123 if (xalloc_oversized (ulen, ampersands) || rsize < product)
127 r = result = xmalloc (rsize);
131 if (*gecos_name == '&')
133 const char *uname = user_name;
134 if (islower (to_uchar (*uname)))
135 *r++ = toupper (to_uchar (*uname++));
151 /* Return a string representing the time between WHEN and the time
152 that this function is first run. */
155 idle_string (time_t when)
157 static time_t now = 0;
158 static char buf[INT_STRLEN_BOUND (long int) + 2];
164 seconds_idle = now - when;
165 if (seconds_idle < 60) /* One minute. */
167 if (seconds_idle < (24 * 60 * 60)) /* One day. */
169 int hours = seconds_idle / (60 * 60);
170 int minutes = (seconds_idle % (60 * 60)) / 60;
171 sprintf (buf, "%02d:%02d", hours, minutes);
175 unsigned long int days = seconds_idle / (24 * 60 * 60);
176 sprintf (buf, "%lud", days);
181 /* Return a time string. */
183 time_string (const STRUCT_UTMP *utmp_ent)
185 static char buf[INT_STRLEN_BOUND (intmax_t) + sizeof "-%m-%d %H:%M"];
187 /* Don't take the address of UT_TIME_MEMBER directly.
188 Ulrich Drepper wrote:
189 ``... GNU libc (and perhaps other libcs as well) have extended
190 utmp file formats which do not use a simple time_t ut_time field.
191 In glibc, ut_time is a macro which selects for backward compatibility
192 the tv_sec member of a struct timeval value.'' */
193 time_t t = UT_TIME_MEMBER (utmp_ent);
194 struct tm *tmp = localtime (&t);
198 strftime (buf, sizeof buf, time_format, tmp);
202 return TYPE_SIGNED (time_t) ? imaxtostr (t, buf) : umaxtostr (t, buf);
205 /* Display a line of information about UTMP_ENT. */
208 print_entry (const STRUCT_UTMP *utmp_ent)
214 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
215 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
217 char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
219 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
220 already an absolute file name. Some system may put the full,
221 absolute file name in ut_line. */
222 if (utmp_ent->ut_line[0] == '/')
224 strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
225 line[sizeof (utmp_ent->ut_line)] = '\0';
229 strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
230 strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
231 line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
234 if (stat (line, &stats) == 0)
236 mesg = (stats.st_mode & S_IWGRP) ? ' ' : '*';
237 last_change = stats.st_atime;
245 printf ("%-8.*s", UT_USER_SIZE, UT_USER (utmp_ent));
247 if (include_fullname)
250 char name[UT_USER_SIZE + 1];
252 strncpy (name, UT_USER (utmp_ent), UT_USER_SIZE);
253 name[UT_USER_SIZE] = '\0';
254 pw = getpwnam (name);
256 printf (" %19s", " ???");
259 char *const comma = strchr (pw->pw_gecos, ',');
265 result = create_fullname (pw->pw_gecos, pw->pw_name);
266 printf (" %-19.19s", result);
272 mesg, (int) sizeof (utmp_ent->ut_line), utmp_ent->ut_line);
277 printf (" %-6s", idle_string (last_change));
279 printf (" %-6s", "???");
282 printf (" %s", time_string (utmp_ent));
285 if (include_where && utmp_ent->ut_host[0])
287 char ut_host[sizeof (utmp_ent->ut_host) + 1];
289 char *display = NULL;
291 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
292 strncpy (ut_host, utmp_ent->ut_host, (int) sizeof (utmp_ent->ut_host));
293 ut_host[sizeof (utmp_ent->ut_host)] = '\0';
295 /* Look for an X display. */
296 display = strchr (ut_host, ':');
301 /* See if we can canonicalize it. */
302 host = canon_host (ut_host);
307 printf (" %s:%s", host, display);
309 printf (" %s", host);
319 /* Display a verbose line of information about UTMP_ENT. */
322 print_long_entry (const char name[])
326 pw = getpwnam (name);
328 printf (_("Login name: "));
329 printf ("%-28s", name);
331 printf (_("In real life: "));
334 printf (" %s", _("???\n"));
339 char *const comma = strchr (pw->pw_gecos, ',');
345 result = create_fullname (pw->pw_gecos, pw->pw_name);
346 printf (" %s", result);
352 if (include_home_and_shell)
354 printf (_("Directory: "));
355 printf ("%-29s", pw->pw_dir);
356 printf (_("Shell: "));
357 printf (" %s", pw->pw_shell);
365 const char *const baseproject = "/.project";
366 char *const project =
367 xmalloc (strlen (pw->pw_dir) + strlen (baseproject) + 1);
369 strcpy (project, pw->pw_dir);
370 strcat (project, baseproject);
372 stream = fopen (project, "r");
377 printf (_("Project: "));
379 while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
380 fwrite (buf, 1, bytes, stdout);
391 const char *const baseplan = "/.plan";
393 xmalloc (strlen (pw->pw_dir) + strlen (baseplan) + 1);
395 strcpy (plan, pw->pw_dir);
396 strcat (plan, baseplan);
398 stream = fopen (plan, "r");
403 printf (_("Plan:\n"));
405 while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
406 fwrite (buf, 1, bytes, stdout);
416 /* Print the username of each valid entry and the number of valid entries
417 in UTMP_BUF, which should have N elements. */
422 printf ("%-8s", _("Login"));
423 if (include_fullname)
424 printf (" %-19s", _("Name"));
425 printf (" %-9s", _(" TTY"));
427 printf (" %-6s", _("Idle"));
428 printf (" %-*s", time_format_width, _("When"));
431 printf (" %s", _("Where"));
436 /* Display UTMP_BUF, which should have N entries. */
439 scan_entries (size_t n, const STRUCT_UTMP *utmp_buf,
440 const int argc_names, char *const argv_names[])
442 if (hard_locale (LC_TIME))
444 time_format = "%Y-%m-%d %H:%M";
445 time_format_width = 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2;
449 time_format = "%b %e %H:%M";
450 time_format_width = 3 + 1 + 2 + 1 + 2 + 1 + 2;
458 if (IS_USER_PROCESS (utmp_buf))
464 for (i = 0; i < argc_names; i++)
465 if (strncmp (UT_USER (utmp_buf), argv_names[i], UT_USER_SIZE)
468 print_entry (utmp_buf);
473 print_entry (utmp_buf);
479 /* Display a list of who is on the system, according to utmp file FILENAME. */
482 short_pinky (const char *filename,
483 const int argc_names, char *const argv_names[])
486 STRUCT_UTMP *utmp_buf;
488 if (read_utmp (filename, &n_users, &utmp_buf, 0) != 0)
489 error (EXIT_FAILURE, errno, "%s", filename);
491 scan_entries (n_users, utmp_buf, argc_names, argv_names);
495 long_pinky (const int argc_names, char *const argv_names[])
499 for (i = 0; i < argc_names; i++)
500 print_long_entry (argv_names[i]);
506 if (status != EXIT_SUCCESS)
507 fprintf (stderr, _("Try `%s --help' for more information.\n"),
511 printf (_("Usage: %s [OPTION]... [USER]...\n"), program_name);
514 -l produce long format output for the specified USERs\n\
515 -b omit the user's home directory and shell in long format\n\
516 -h omit the user's project file in long format\n\
517 -p omit the user's plan file in long format\n\
518 -s do short format output, this is the default\n\
521 -f omit the line of column headings in short format\n\
522 -w omit the user's full name in short format\n\
523 -i omit the user's full name and remote host in short format\n\
524 -q omit the user's full name, remote host and idle time\n\
527 fputs (HELP_OPTION_DESCRIPTION, stdout);
528 fputs (VERSION_OPTION_DESCRIPTION, stdout);
531 A lightweight `finger' program; print user information.\n\
532 The utmp file will be %s.\n\
534 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
540 main (int argc, char **argv)
545 initialize_main (&argc, &argv);
546 program_name = argv[0];
547 setlocale (LC_ALL, "");
548 bindtextdomain (PACKAGE, LOCALEDIR);
549 textdomain (PACKAGE);
551 atexit (close_stdout);
553 while ((optc = getopt_long (argc, argv, "sfwiqbhlp", longopts, NULL)) != -1)
558 do_short_format = true;
562 do_short_format = false;
566 include_heading = false;
570 include_fullname = false;
574 include_fullname = false;
576 include_where = false;
581 include_fullname = false;
583 include_where = false;
585 include_idle = false;
589 include_project = false;
593 include_plan = false;
597 include_home_and_shell = false;
600 case_GETOPT_HELP_CHAR;
602 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
605 usage (EXIT_FAILURE);
609 n_users = argc - optind;
611 if (!do_short_format && n_users == 0)
613 error (0, 0, _("no username specified; at least one must be\
614 specified when using -l"));
615 usage (EXIT_FAILURE);
619 short_pinky (UTMP_FILE, n_users, argv + optind);
621 long_pinky (n_users, argv + optind);