2 Copyright (C) 1992-2024 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 <https://www.gnu.org/licenses/>. */
17 /* Created by hacking who.c by Kaveh Ghazi ghazi@caip.rutgers.edu */
25 #include <sys/types.h>
28 #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 /* If true, display the hours:minutes since each user has touched
41 the keyboard, or blank if within the last minute, or days followed
42 by a 'd' if not within the last day. */
43 static bool include_idle
= true;
45 /* If true, display a line at the top describing each field. */
46 static bool include_heading
= true;
48 /* if true, display the user's full name from pw_gecos. */
49 static bool include_fullname
= true;
51 /* if true, display the user's ~/.project file when doing long format. */
52 static bool include_project
= true;
54 /* if true, display the user's ~/.plan file when doing long format. */
55 static bool include_plan
= true;
57 /* if true, display the user's home directory and shell
58 when doing long format. */
59 static bool include_home_and_shell
= true;
61 /* if true, use the "short" output format. */
62 static bool do_short_format
= true;
64 /* If true, attempt to canonicalize hostnames via a DNS lookup. */
65 static bool do_lookup
;
67 /* if true, display the ut_host field. */
68 #if HAVE_STRUCT_XTMP_UT_HOST
69 static bool include_where
= true;
72 /* The strftime format to use for login times, and its expected
74 static char const *time_format
;
75 static int time_format_width
;
77 /* for long options with no corresponding short option, use enum */
80 LOOKUP_OPTION
= CHAR_MAX
+ 1
83 static struct option
const longopts
[] =
85 {"lookup", no_argument
, nullptr, LOOKUP_OPTION
},
86 {GETOPT_HELP_OPTION_DECL
},
87 {GETOPT_VERSION_OPTION_DECL
},
88 {nullptr, 0, nullptr, 0}
91 /* Count and return the number of ampersands in STR. */
95 count_ampersands (char const *str
)
103 /* Create a string (via xmalloc) which contains a full name by substituting
104 for each ampersand in GECOS_NAME the USER_NAME string with its first
105 character capitalized. The caller must ensure that GECOS_NAME contains
106 no ','s. The caller also is responsible for free'ing the return value of
110 create_fullname (char const *gecos_name
, char const *user_name
)
112 idx_t rsize
= strlen (gecos_name
) + 1;
115 idx_t ampersands
= count_ampersands (gecos_name
);
119 idx_t ulen
= strlen (user_name
);
121 if (ckd_mul (&product
, ulen
- 1, ampersands
)
122 || ckd_add (&rsize
, rsize
, product
))
126 r
= result
= xmalloc (rsize
);
130 if (*gecos_name
== '&')
132 char const *uname
= user_name
;
133 if (islower (to_uchar (*uname
)))
134 *r
++ = toupper (to_uchar (*uname
++));
150 /* Return a string representing the time between WHEN and the time
151 that this function is first run. */
154 idle_string (time_t when
)
156 static time_t now
= 0;
157 static char buf
[INT_STRLEN_BOUND (intmax_t) + sizeof "d"];
163 seconds_idle
= now
- when
;
164 if (seconds_idle
< 60) /* One minute. */
166 if (seconds_idle
< (24 * 60 * 60)) /* One day. */
168 int hours
= seconds_idle
/ (60 * 60);
169 int minutes
= (seconds_idle
% (60 * 60)) / 60;
170 sprintf (buf
, "%02d:%02d", hours
, minutes
);
174 intmax_t days
= seconds_idle
/ (24 * 60 * 60);
175 sprintf (buf
, "%jdd", days
);
180 /* Return a time string. */
182 time_string (struct gl_utmp
const *utmp_ent
)
184 static char buf
[INT_STRLEN_BOUND (intmax_t) + sizeof "-%m-%d %H:%M"];
185 struct tm
*tmp
= localtime (&utmp_ent
->ut_ts
.tv_sec
);
189 strftime (buf
, sizeof buf
, time_format
, tmp
);
193 return timetostr (utmp_ent
->ut_ts
.tv_sec
, buf
);
196 /* Display a line of information about UTMP_ENT. */
199 print_entry (struct gl_utmp
const *utmp_ent
)
205 /* If ut_line contains a space, the device name starts after the space. */
206 char *line
= utmp_ent
->ut_line
;
207 char *space
= strchr (line
, ' ');
208 line
= space
? space
+ 1 : line
;
211 if (IS_ABSOLUTE_FILE_NAME (line
))
215 static int dev_dirfd
;
218 dev_dirfd
= open ("/dev", O_PATHSEARCH
| O_DIRECTORY
);
220 dev_dirfd
= AT_FDCWD
- 1;
225 if (AT_FDCWD
<= dirfd
&& fstatat (dirfd
, line
, &stats
, 0) == 0)
227 mesg
= (stats
.st_mode
& S_IWGRP
) ? ' ' : '*';
228 last_change
= stats
.st_atime
;
236 char *ut_user
= utmp_ent
->ut_user
;
237 if (strnlen (ut_user
, 8) < 8)
238 printf ("%-8s", ut_user
);
240 fputs (ut_user
, stdout
);
242 if (include_fullname
)
244 struct passwd
*pw
= getpwnam (ut_user
);
246 /* TRANSLATORS: Real name is unknown; at most 19 characters. */
247 printf (" %19s", _(" ???"));
250 char *const comma
= strchr (pw
->pw_gecos
, ',');
256 result
= create_fullname (pw
->pw_gecos
, pw
->pw_name
);
257 printf (" %-19.19s", result
);
263 fputc (mesg
, stdout
);
264 if (strnlen (utmp_ent
->ut_line
, 8) < 8)
265 printf ("%-8s", utmp_ent
->ut_line
);
267 fputs (utmp_ent
->ut_line
, stdout
);
272 printf (" %-6s", idle_string (last_change
));
274 /* TRANSLATORS: Idle time is unknown; at most 5 characters. */
275 printf (" %-6s", _("?????"));
278 printf (" %s", time_string (utmp_ent
));
280 #ifdef HAVE_STRUCT_XTMP_UT_HOST
281 if (include_where
&& utmp_ent
->ut_host
[0])
283 char *host
= nullptr;
284 char *display
= nullptr;
285 char *ut_host
= utmp_ent
->ut_host
;
287 /* Look for an X display. */
288 display
= strchr (ut_host
, ':');
292 if (*ut_host
&& do_lookup
)
293 /* See if we can canonicalize it. */
294 host
= canon_host (ut_host
);
299 fputs (host
, stdout
);
303 fputs (display
, stdout
);
314 /* Display a verbose line of information about UTMP_ENT. */
317 print_long_entry (const char name
[])
321 pw
= getpwnam (name
);
323 printf (_("Login name: "));
324 printf ("%-28s", name
);
326 printf (_("In real life: "));
329 /* TRANSLATORS: Real name is unknown; no hard limit. */
330 printf (" %s", _("???\n"));
335 char *const comma
= strchr (pw
->pw_gecos
, ',');
341 result
= create_fullname (pw
->pw_gecos
, pw
->pw_name
);
342 printf (" %s", result
);
348 if (include_home_and_shell
)
350 printf (_("Directory: "));
351 printf ("%-29s", pw
->pw_dir
);
352 printf (_("Shell: "));
353 printf (" %s", pw
->pw_shell
);
361 char const *const baseproject
= "/.project";
362 char *const project
=
363 xmalloc (strlen (pw
->pw_dir
) + strlen (baseproject
) + 1);
364 stpcpy (stpcpy (project
, pw
->pw_dir
), baseproject
);
366 stream
= fopen (project
, "r");
371 printf (_("Project: "));
373 while ((bytes
= fread (buf
, 1, sizeof (buf
), stream
)) > 0)
374 fwrite (buf
, 1, bytes
, stdout
);
385 char const *const baseplan
= "/.plan";
387 xmalloc (strlen (pw
->pw_dir
) + strlen (baseplan
) + 1);
388 stpcpy (stpcpy (plan
, pw
->pw_dir
), baseplan
);
390 stream
= fopen (plan
, "r");
395 printf (_("Plan:\n"));
397 while ((bytes
= fread (buf
, 1, sizeof (buf
), stream
)) > 0)
398 fwrite (buf
, 1, bytes
, stdout
);
408 /* Print the username of each valid entry and the number of valid entries
409 in UTMP_BUF, which should have N elements. */
414 printf ("%-8s", _("Login"));
415 if (include_fullname
)
416 printf (" %-19s", _("Name"));
417 printf (" %-9s", _(" TTY"));
419 printf (" %-6s", _("Idle"));
420 printf (" %-*s", time_format_width
, _("When"));
421 #ifdef HAVE_STRUCT_XTMP_UT_HOST
423 printf (" %s", _("Where"));
428 /* Display UTMP_BUF, which should have N entries. */
431 scan_entries (idx_t n
, struct gl_utmp
const *utmp_buf
,
432 const int argc_names
, char *const argv_names
[])
434 if (hard_locale (LC_TIME
))
436 time_format
= "%Y-%m-%d %H:%M";
437 time_format_width
= 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2;
441 time_format
= "%b %e %H:%M";
442 time_format_width
= 3 + 1 + 2 + 1 + 2 + 1 + 2;
450 if (IS_USER_PROCESS (utmp_buf
))
454 for (int i
= 0; i
< argc_names
; i
++)
455 if (STREQ (utmp_buf
->ut_user
, argv_names
[i
]))
457 print_entry (utmp_buf
);
462 print_entry (utmp_buf
);
468 /* Display a list of who is on the system, according to utmp file FILENAME. */
471 short_pinky (char const *filename
,
472 const int argc_names
, char *const argv_names
[])
475 struct gl_utmp
*utmp_buf
;
476 if (read_utmp (filename
, &n_users
, &utmp_buf
, READ_UTMP_USER_PROCESS
) != 0)
477 error (EXIT_FAILURE
, errno
, "%s", quotef (filename
));
479 scan_entries (n_users
, utmp_buf
, argc_names
, argv_names
);
484 long_pinky (const int argc_names
, char *const argv_names
[])
486 for (int i
= 0; i
< argc_names
; i
++)
487 print_long_entry (argv_names
[i
]);
493 if (status
!= EXIT_SUCCESS
)
497 printf (_("Usage: %s [OPTION]... [USER]...\n"), program_name
);
500 -l produce long format output for the specified USERs\n\
501 -b omit the user's home directory and shell in long format\n\
502 -h omit the user's project file in long format\n\
503 -p omit the user's plan file in long format\n\
504 -s do short format output, this is the default\n\
507 -f omit the line of column headings in short format\n\
508 -w omit the user's full name in short format\n\
509 -i omit the user's full name and remote host in short format\n\
510 -q omit the user's full name, remote host and idle time\n\
514 --lookup attempt to canonicalize hostnames via DNS\n\
516 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
517 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
520 A lightweight 'finger' program; print user information.\n\
521 The utmp file will be %s.\n\
523 emit_ancillary_info (PROGRAM_NAME
);
529 main (int argc
, char **argv
)
534 initialize_main (&argc
, &argv
);
535 set_program_name (argv
[0]);
536 setlocale (LC_ALL
, "");
537 bindtextdomain (PACKAGE
, LOCALEDIR
);
538 textdomain (PACKAGE
);
540 atexit (close_stdout
);
542 while ((optc
= getopt_long (argc
, argv
, "sfwiqbhlp", longopts
, nullptr))
548 do_short_format
= true;
552 do_short_format
= false;
556 include_heading
= false;
560 include_fullname
= false;
564 include_fullname
= false;
565 #ifdef HAVE_STRUCT_XTMP_UT_HOST
566 include_where
= false;
571 include_fullname
= false;
572 #ifdef HAVE_STRUCT_XTMP_UT_HOST
573 include_where
= false;
575 include_idle
= false;
579 include_project
= false;
583 include_plan
= false;
587 include_home_and_shell
= false;
594 case_GETOPT_HELP_CHAR
;
596 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
599 usage (EXIT_FAILURE
);
603 n_users
= argc
- optind
;
605 if (!do_short_format
&& n_users
== 0)
607 error (0, 0, _("no username specified; at least one must be\
608 specified when using -l"));
609 usage (EXIT_FAILURE
);
613 short_pinky (UTMP_FILE
, n_users
, argv
+ optind
);
615 long_pinky (n_users
, argv
+ optind
);