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"
33 /* The official name of this program (e.g., no `g' prefix). */
34 #define PROGRAM_NAME "pinky"
37 proper_name ("Joseph Arceneaux"), \
38 proper_name ("David MacKenzie"), \
39 proper_name ("Kaveh Ghazi")
41 #ifndef MAXHOSTNAMELEN
42 # define MAXHOSTNAMELEN 64
47 /* The name this program was run with. */
48 const char *program_name;
50 /* If true, display the hours:minutes since each user has touched
51 the keyboard, or blank if within the last minute, or days followed
52 by a 'd' if not within the last day. */
53 static bool include_idle = true;
55 /* If true, display a line at the top describing each field. */
56 static bool include_heading = true;
58 /* if true, display the user's full name from pw_gecos. */
59 static bool include_fullname = true;
61 /* if true, display the user's ~/.project file when doing long format. */
62 static bool include_project = true;
64 /* if true, display the user's ~/.plan file when doing long format. */
65 static bool include_plan = true;
67 /* if true, display the user's home directory and shell
68 when doing long format. */
69 static bool include_home_and_shell = true;
71 /* if true, use the "short" output format. */
72 static bool do_short_format = true;
74 /* if true, display the ut_host field. */
76 static bool include_where = true;
79 /* The strftime format to use for login times, and its expected
81 static char const *time_format;
82 static int time_format_width;
84 static struct option const longopts[] =
86 {GETOPT_HELP_OPTION_DECL},
87 {GETOPT_VERSION_OPTION_DECL},
91 /* Count and return the number of ampersands in STR. */
94 count_ampersands (const char *str)
105 /* Create a string (via xmalloc) which contains a full name by substituting
106 for each ampersand in GECOS_NAME the USER_NAME string with its first
107 character capitalized. The caller must ensure that GECOS_NAME contains
108 no `,'s. The caller also is responsible for free'ing the return value of
112 create_fullname (const char *gecos_name, const char *user_name)
114 size_t rsize = strlen (gecos_name) + 1;
117 size_t ampersands = count_ampersands (gecos_name);
121 size_t ulen = strlen (user_name);
122 size_t product = ampersands * ulen;
123 rsize += product - ampersands;
124 if (xalloc_oversized (ulen, ampersands) || rsize < product)
128 r = result = xmalloc (rsize);
132 if (*gecos_name == '&')
134 const char *uname = user_name;
135 if (islower (to_uchar (*uname)))
136 *r++ = toupper (to_uchar (*uname++));
152 /* Return a string representing the time between WHEN and the time
153 that this function is first run. */
156 idle_string (time_t when)
158 static time_t now = 0;
159 static char buf[INT_STRLEN_BOUND (long int) + 2];
165 seconds_idle = now - when;
166 if (seconds_idle < 60) /* One minute. */
168 if (seconds_idle < (24 * 60 * 60)) /* One day. */
170 int hours = seconds_idle / (60 * 60);
171 int minutes = (seconds_idle % (60 * 60)) / 60;
172 sprintf (buf, "%02d:%02d", hours, minutes);
176 unsigned long int days = seconds_idle / (24 * 60 * 60);
177 sprintf (buf, "%lud", days);
182 /* Return a time string. */
184 time_string (const STRUCT_UTMP *utmp_ent)
186 static char buf[INT_STRLEN_BOUND (intmax_t) + sizeof "-%m-%d %H:%M"];
188 /* Don't take the address of UT_TIME_MEMBER directly.
189 Ulrich Drepper wrote:
190 ``... GNU libc (and perhaps other libcs as well) have extended
191 utmp file formats which do not use a simple time_t ut_time field.
192 In glibc, ut_time is a macro which selects for backward compatibility
193 the tv_sec member of a struct timeval value.'' */
194 time_t t = UT_TIME_MEMBER (utmp_ent);
195 struct tm *tmp = localtime (&t);
199 strftime (buf, sizeof buf, time_format, tmp);
203 return TYPE_SIGNED (time_t) ? imaxtostr (t, buf) : umaxtostr (t, buf);
206 /* Display a line of information about UTMP_ENT. */
209 print_entry (const STRUCT_UTMP *utmp_ent)
215 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
216 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
218 char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
220 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
221 already an absolute file name. Some system may put the full,
222 absolute file name in ut_line. */
223 if (utmp_ent->ut_line[0] == '/')
225 strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
226 line[sizeof (utmp_ent->ut_line)] = '\0';
230 strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
231 strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
232 line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
235 if (stat (line, &stats) == 0)
237 mesg = (stats.st_mode & S_IWGRP) ? ' ' : '*';
238 last_change = stats.st_atime;
246 printf ("%-8.*s", UT_USER_SIZE, UT_USER (utmp_ent));
248 if (include_fullname)
251 char name[UT_USER_SIZE + 1];
253 strncpy (name, UT_USER (utmp_ent), UT_USER_SIZE);
254 name[UT_USER_SIZE] = '\0';
255 pw = getpwnam (name);
257 printf (" %19s", " ???");
260 char *const comma = strchr (pw->pw_gecos, ',');
266 result = create_fullname (pw->pw_gecos, pw->pw_name);
267 printf (" %-19.19s", result);
273 mesg, (int) sizeof (utmp_ent->ut_line), utmp_ent->ut_line);
278 printf (" %-6s", idle_string (last_change));
280 printf (" %-6s", "???");
283 printf (" %s", time_string (utmp_ent));
286 if (include_where && utmp_ent->ut_host[0])
288 char ut_host[sizeof (utmp_ent->ut_host) + 1];
290 char *display = NULL;
292 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
293 strncpy (ut_host, utmp_ent->ut_host, (int) sizeof (utmp_ent->ut_host));
294 ut_host[sizeof (utmp_ent->ut_host)] = '\0';
296 /* Look for an X display. */
297 display = strchr (ut_host, ':');
302 /* See if we can canonicalize it. */
303 host = canon_host (ut_host);
308 printf (" %s:%s", host, display);
310 printf (" %s", host);
320 /* Display a verbose line of information about UTMP_ENT. */
323 print_long_entry (const char name[])
327 pw = getpwnam (name);
329 printf (_("Login name: "));
330 printf ("%-28s", name);
332 printf (_("In real life: "));
335 printf (" %s", _("???\n"));
340 char *const comma = strchr (pw->pw_gecos, ',');
346 result = create_fullname (pw->pw_gecos, pw->pw_name);
347 printf (" %s", result);
353 if (include_home_and_shell)
355 printf (_("Directory: "));
356 printf ("%-29s", pw->pw_dir);
357 printf (_("Shell: "));
358 printf (" %s", pw->pw_shell);
366 const char *const baseproject = "/.project";
367 char *const project =
368 xmalloc (strlen (pw->pw_dir) + strlen (baseproject) + 1);
370 strcpy (project, pw->pw_dir);
371 strcat (project, baseproject);
373 stream = fopen (project, "r");
378 printf (_("Project: "));
380 while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
381 fwrite (buf, 1, bytes, stdout);
392 const char *const baseplan = "/.plan";
394 xmalloc (strlen (pw->pw_dir) + strlen (baseplan) + 1);
396 strcpy (plan, pw->pw_dir);
397 strcat (plan, baseplan);
399 stream = fopen (plan, "r");
404 printf (_("Plan:\n"));
406 while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
407 fwrite (buf, 1, bytes, stdout);
417 /* Print the username of each valid entry and the number of valid entries
418 in UTMP_BUF, which should have N elements. */
423 printf ("%-8s", _("Login"));
424 if (include_fullname)
425 printf (" %-19s", _("Name"));
426 printf (" %-9s", _(" TTY"));
428 printf (" %-6s", _("Idle"));
429 printf (" %-*s", time_format_width, _("When"));
432 printf (" %s", _("Where"));
437 /* Display UTMP_BUF, which should have N entries. */
440 scan_entries (size_t n, const STRUCT_UTMP *utmp_buf,
441 const int argc_names, char *const argv_names[])
443 if (hard_locale (LC_TIME))
445 time_format = "%Y-%m-%d %H:%M";
446 time_format_width = 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2;
450 time_format = "%b %e %H:%M";
451 time_format_width = 3 + 1 + 2 + 1 + 2 + 1 + 2;
459 if (IS_USER_PROCESS (utmp_buf))
465 for (i = 0; i < argc_names; i++)
466 if (strncmp (UT_USER (utmp_buf), argv_names[i], UT_USER_SIZE)
469 print_entry (utmp_buf);
474 print_entry (utmp_buf);
480 /* Display a list of who is on the system, according to utmp file FILENAME. */
483 short_pinky (const char *filename,
484 const int argc_names, char *const argv_names[])
487 STRUCT_UTMP *utmp_buf;
489 if (read_utmp (filename, &n_users, &utmp_buf, 0) != 0)
490 error (EXIT_FAILURE, errno, "%s", filename);
492 scan_entries (n_users, utmp_buf, argc_names, argv_names);
496 long_pinky (const int argc_names, char *const argv_names[])
500 for (i = 0; i < argc_names; i++)
501 print_long_entry (argv_names[i]);
507 if (status != EXIT_SUCCESS)
508 fprintf (stderr, _("Try `%s --help' for more information.\n"),
512 printf (_("Usage: %s [OPTION]... [USER]...\n"), program_name);
515 -l produce long format output for the specified USERs\n\
516 -b omit the user's home directory and shell in long format\n\
517 -h omit the user's project file in long format\n\
518 -p omit the user's plan file in long format\n\
519 -s do short format output, this is the default\n\
522 -f omit the line of column headings in short format\n\
523 -w omit the user's full name in short format\n\
524 -i omit the user's full name and remote host in short format\n\
525 -q omit the user's full name, remote host and idle time\n\
528 fputs (HELP_OPTION_DESCRIPTION, stdout);
529 fputs (VERSION_OPTION_DESCRIPTION, stdout);
532 A lightweight `finger' program; print user information.\n\
533 The utmp file will be %s.\n\
535 emit_bug_reporting_address ();
541 main (int argc, char **argv)
546 initialize_main (&argc, &argv);
547 program_name = argv[0];
548 setlocale (LC_ALL, "");
549 bindtextdomain (PACKAGE, LOCALEDIR);
550 textdomain (PACKAGE);
552 atexit (close_stdout);
554 while ((optc = getopt_long (argc, argv, "sfwiqbhlp", longopts, NULL)) != -1)
559 do_short_format = true;
563 do_short_format = false;
567 include_heading = false;
571 include_fullname = false;
575 include_fullname = false;
577 include_where = false;
582 include_fullname = false;
584 include_where = false;
586 include_idle = false;
590 include_project = false;
594 include_plan = false;
598 include_home_and_shell = false;
601 case_GETOPT_HELP_CHAR;
603 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
606 usage (EXIT_FAILURE);
610 n_users = argc - optind;
612 if (!do_short_format && n_users == 0)
614 error (0, 0, _("no username specified; at least one must be\
615 specified when using -l"));
616 usage (EXIT_FAILURE);
620 short_pinky (UTMP_FILE, n_users, argv + optind);
622 long_pinky (n_users, argv + optind);