2 Copyright (C) 1992-1997, 1999-2006, 2008 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 3 of the License, or
7 (at your option) 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, see <http://www.gnu.org/licenses/>. */
17 /* Created by hacking who.c by Kaveh Ghazi ghazi@caip.rutgers.edu */
24 #include <sys/types.h>
27 #include "canon-host.h"
29 #include "hard-locale.h"
32 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "pinky"
36 proper_name ("Joseph Arceneaux"), \
37 proper_name ("David MacKenzie"), \
38 proper_name ("Kaveh Ghazi")
40 #ifndef MAXHOSTNAMELEN
41 # define MAXHOSTNAMELEN 64
46 /* If true, display the hours:minutes since each user has touched
47 the keyboard, or blank if within the last minute, or days followed
48 by a 'd' if not within the last day. */
49 static bool include_idle = true;
51 /* If true, display a line at the top describing each field. */
52 static bool include_heading = true;
54 /* if true, display the user's full name from pw_gecos. */
55 static bool include_fullname = true;
57 /* if true, display the user's ~/.project file when doing long format. */
58 static bool include_project = true;
60 /* if true, display the user's ~/.plan file when doing long format. */
61 static bool include_plan = true;
63 /* if true, display the user's home directory and shell
64 when doing long format. */
65 static bool include_home_and_shell = true;
67 /* if true, use the "short" output format. */
68 static bool do_short_format = true;
70 /* if true, display the ut_host field. */
72 static bool include_where = true;
75 /* The strftime format to use for login times, and its expected
77 static char const *time_format;
78 static int time_format_width;
80 static struct option const longopts[] =
82 {GETOPT_HELP_OPTION_DECL},
83 {GETOPT_VERSION_OPTION_DECL},
87 /* Count and return the number of ampersands in STR. */
90 count_ampersands (const char *str)
101 /* Create a string (via xmalloc) which contains a full name by substituting
102 for each ampersand in GECOS_NAME the USER_NAME string with its first
103 character capitalized. The caller must ensure that GECOS_NAME contains
104 no `,'s. The caller also is responsible for free'ing the return value of
108 create_fullname (const char *gecos_name, const char *user_name)
110 size_t rsize = strlen (gecos_name) + 1;
113 size_t ampersands = count_ampersands (gecos_name);
117 size_t ulen = strlen (user_name);
118 size_t product = ampersands * ulen;
119 rsize += product - ampersands;
120 if (xalloc_oversized (ulen, ampersands) || rsize < product)
124 r = result = xmalloc (rsize);
128 if (*gecos_name == '&')
130 const char *uname = user_name;
131 if (islower (to_uchar (*uname)))
132 *r++ = toupper (to_uchar (*uname++));
148 /* Return a string representing the time between WHEN and the time
149 that this function is first run. */
152 idle_string (time_t when)
154 static time_t now = 0;
155 static char buf[INT_STRLEN_BOUND (long int) + 2];
161 seconds_idle = now - when;
162 if (seconds_idle < 60) /* One minute. */
164 if (seconds_idle < (24 * 60 * 60)) /* One day. */
166 int hours = seconds_idle / (60 * 60);
167 int minutes = (seconds_idle % (60 * 60)) / 60;
168 sprintf (buf, "%02d:%02d", hours, minutes);
172 unsigned long int days = seconds_idle / (24 * 60 * 60);
173 sprintf (buf, "%lud", days);
178 /* Return a time string. */
180 time_string (const STRUCT_UTMP *utmp_ent)
182 static char buf[INT_STRLEN_BOUND (intmax_t) + sizeof "-%m-%d %H:%M"];
184 /* Don't take the address of UT_TIME_MEMBER directly.
185 Ulrich Drepper wrote:
186 ``... GNU libc (and perhaps other libcs as well) have extended
187 utmp file formats which do not use a simple time_t ut_time field.
188 In glibc, ut_time is a macro which selects for backward compatibility
189 the tv_sec member of a struct timeval value.'' */
190 time_t t = UT_TIME_MEMBER (utmp_ent);
191 struct tm *tmp = localtime (&t);
195 strftime (buf, sizeof buf, time_format, tmp);
199 return timetostr (t, buf);
202 /* Display a line of information about UTMP_ENT. */
205 print_entry (const STRUCT_UTMP *utmp_ent)
211 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
212 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
214 char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
216 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
217 already an absolute file name. Some system may put the full,
218 absolute file name in ut_line. */
219 if (utmp_ent->ut_line[0] == '/')
221 strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
222 line[sizeof (utmp_ent->ut_line)] = '\0';
226 strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
227 strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
228 line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
231 if (stat (line, &stats) == 0)
233 mesg = (stats.st_mode & S_IWGRP) ? ' ' : '*';
234 last_change = stats.st_atime;
242 printf ("%-8.*s", UT_USER_SIZE, UT_USER (utmp_ent));
244 if (include_fullname)
247 char name[UT_USER_SIZE + 1];
249 strncpy (name, UT_USER (utmp_ent), UT_USER_SIZE);
250 name[UT_USER_SIZE] = '\0';
251 pw = getpwnam (name);
253 printf (" %19s", " ???");
256 char *const comma = strchr (pw->pw_gecos, ',');
262 result = create_fullname (pw->pw_gecos, pw->pw_name);
263 printf (" %-19.19s", result);
269 mesg, (int) sizeof (utmp_ent->ut_line), utmp_ent->ut_line);
274 printf (" %-6s", idle_string (last_change));
276 printf (" %-6s", "???");
279 printf (" %s", time_string (utmp_ent));
282 if (include_where && utmp_ent->ut_host[0])
284 char ut_host[sizeof (utmp_ent->ut_host) + 1];
286 char *display = NULL;
288 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
289 strncpy (ut_host, utmp_ent->ut_host, (int) sizeof (utmp_ent->ut_host));
290 ut_host[sizeof (utmp_ent->ut_host)] = '\0';
292 /* Look for an X display. */
293 display = strchr (ut_host, ':');
298 /* See if we can canonicalize it. */
299 host = canon_host (ut_host);
304 printf (" %s:%s", host, display);
306 printf (" %s", host);
316 /* Display a verbose line of information about UTMP_ENT. */
319 print_long_entry (const char name[])
323 pw = getpwnam (name);
325 printf (_("Login name: "));
326 printf ("%-28s", name);
328 printf (_("In real life: "));
331 printf (" %s", _("???\n"));
336 char *const comma = strchr (pw->pw_gecos, ',');
342 result = create_fullname (pw->pw_gecos, pw->pw_name);
343 printf (" %s", result);
349 if (include_home_and_shell)
351 printf (_("Directory: "));
352 printf ("%-29s", pw->pw_dir);
353 printf (_("Shell: "));
354 printf (" %s", pw->pw_shell);
362 const char *const baseproject = "/.project";
363 char *const project =
364 xmalloc (strlen (pw->pw_dir) + strlen (baseproject) + 1);
366 strcpy (project, pw->pw_dir);
367 strcat (project, baseproject);
369 stream = fopen (project, "r");
374 printf (_("Project: "));
376 while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
377 fwrite (buf, 1, bytes, stdout);
388 const char *const baseplan = "/.plan";
390 xmalloc (strlen (pw->pw_dir) + strlen (baseplan) + 1);
392 strcpy (plan, pw->pw_dir);
393 strcat (plan, baseplan);
395 stream = fopen (plan, "r");
400 printf (_("Plan:\n"));
402 while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
403 fwrite (buf, 1, bytes, stdout);
413 /* Print the username of each valid entry and the number of valid entries
414 in UTMP_BUF, which should have N elements. */
419 printf ("%-8s", _("Login"));
420 if (include_fullname)
421 printf (" %-19s", _("Name"));
422 printf (" %-9s", _(" TTY"));
424 printf (" %-6s", _("Idle"));
425 printf (" %-*s", time_format_width, _("When"));
428 printf (" %s", _("Where"));
433 /* Display UTMP_BUF, which should have N entries. */
436 scan_entries (size_t n, const STRUCT_UTMP *utmp_buf,
437 const int argc_names, char *const argv_names[])
439 if (hard_locale (LC_TIME))
441 time_format = "%Y-%m-%d %H:%M";
442 time_format_width = 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2;
446 time_format = "%b %e %H:%M";
447 time_format_width = 3 + 1 + 2 + 1 + 2 + 1 + 2;
455 if (IS_USER_PROCESS (utmp_buf))
461 for (i = 0; i < argc_names; i++)
462 if (strncmp (UT_USER (utmp_buf), argv_names[i], UT_USER_SIZE)
465 print_entry (utmp_buf);
470 print_entry (utmp_buf);
476 /* Display a list of who is on the system, according to utmp file FILENAME. */
479 short_pinky (const char *filename,
480 const int argc_names, char *const argv_names[])
483 STRUCT_UTMP *utmp_buf;
485 if (read_utmp (filename, &n_users, &utmp_buf, 0) != 0)
486 error (EXIT_FAILURE, errno, "%s", filename);
488 scan_entries (n_users, utmp_buf, argc_names, argv_names);
492 long_pinky (const int argc_names, char *const argv_names[])
496 for (i = 0; i < argc_names; i++)
497 print_long_entry (argv_names[i]);
503 if (status != EXIT_SUCCESS)
504 fprintf (stderr, _("Try `%s --help' for more information.\n"),
508 printf (_("Usage: %s [OPTION]... [USER]...\n"), program_name);
511 -l produce long format output for the specified USERs\n\
512 -b omit the user's home directory and shell in long format\n\
513 -h omit the user's project file in long format\n\
514 -p omit the user's plan file in long format\n\
515 -s do short format output, this is the default\n\
518 -f omit the line of column headings in short format\n\
519 -w omit the user's full name in short format\n\
520 -i omit the user's full name and remote host in short format\n\
521 -q omit the user's full name, remote host and idle time\n\
524 fputs (HELP_OPTION_DESCRIPTION, stdout);
525 fputs (VERSION_OPTION_DESCRIPTION, stdout);
528 A lightweight `finger' program; print user information.\n\
529 The utmp file will be %s.\n\
531 emit_bug_reporting_address ();
537 main (int argc, char **argv)
542 initialize_main (&argc, &argv);
543 set_program_name (argv[0]);
544 setlocale (LC_ALL, "");
545 bindtextdomain (PACKAGE, LOCALEDIR);
546 textdomain (PACKAGE);
548 atexit (close_stdout);
550 while ((optc = getopt_long (argc, argv, "sfwiqbhlp", longopts, NULL)) != -1)
555 do_short_format = true;
559 do_short_format = false;
563 include_heading = false;
567 include_fullname = false;
571 include_fullname = false;
573 include_where = false;
578 include_fullname = false;
580 include_where = false;
582 include_idle = false;
586 include_project = false;
590 include_plan = false;
594 include_home_and_shell = false;
597 case_GETOPT_HELP_CHAR;
599 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
602 usage (EXIT_FAILURE);
606 n_users = argc - optind;
608 if (!do_short_format && n_users == 0)
610 error (0, 0, _("no username specified; at least one must be\
611 specified when using -l"));
612 usage (EXIT_FAILURE);
616 short_pinky (UTMP_FILE, n_users, argv + optind);
618 long_pinky (n_users, argv + optind);