2 Copyright (C) 1992-2023 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 */
22 #include <stdckdint.h>
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, display the ut_host field. */
65 #if HAVE_STRUCT_XTMP_UT_HOST
66 static bool include_where
= true;
69 /* The strftime format to use for login times, and its expected
71 static char const *time_format
;
72 static int time_format_width
;
74 static struct option
const longopts
[] =
76 {GETOPT_HELP_OPTION_DECL
},
77 {GETOPT_VERSION_OPTION_DECL
},
78 {nullptr, 0, nullptr, 0}
81 /* Count and return the number of ampersands in STR. */
85 count_ampersands (char const *str
)
96 /* Create a string (via xmalloc) which contains a full name by substituting
97 for each ampersand in GECOS_NAME the USER_NAME string with its first
98 character capitalized. The caller must ensure that GECOS_NAME contains
99 no ','s. The caller also is responsible for free'ing the return value of
103 create_fullname (char const *gecos_name
, char const *user_name
)
105 size_t rsize
= strlen (gecos_name
) + 1;
108 size_t ampersands
= count_ampersands (gecos_name
);
112 size_t ulen
= strlen (user_name
);
114 if (ckd_mul (&product
, ulen
, ampersands
- 1)
115 || ckd_add (&rsize
, rsize
, product
))
119 r
= result
= xmalloc (rsize
);
123 if (*gecos_name
== '&')
125 char const *uname
= user_name
;
126 if (islower (to_uchar (*uname
)))
127 *r
++ = toupper (to_uchar (*uname
++));
143 /* Return a string representing the time between WHEN and the time
144 that this function is first run. */
147 idle_string (time_t when
)
149 static time_t now
= 0;
150 static char buf
[INT_STRLEN_BOUND (intmax_t) + sizeof "d"];
156 seconds_idle
= now
- when
;
157 if (seconds_idle
< 60) /* One minute. */
159 if (seconds_idle
< (24 * 60 * 60)) /* One day. */
161 int hours
= seconds_idle
/ (60 * 60);
162 int minutes
= (seconds_idle
% (60 * 60)) / 60;
163 sprintf (buf
, "%02d:%02d", hours
, minutes
);
167 intmax_t days
= seconds_idle
/ (24 * 60 * 60);
168 sprintf (buf
, "%"PRIdMAX
"d", days
);
173 /* Return a time string. */
175 time_string (const STRUCT_UTMP
*utmp_ent
)
177 static char buf
[INT_STRLEN_BOUND (intmax_t) + sizeof "-%m-%d %H:%M"];
179 /* Don't take the address of UT_TIME_MEMBER directly.
180 Ulrich Drepper wrote:
181 "... GNU libc (and perhaps other libcs as well) have extended
182 utmp file formats which do not use a simple time_t ut_time field.
183 In glibc, ut_time is a macro which selects for backward compatibility
184 the tv_sec member of a struct timeval value." */
185 time_t t
= UT_TIME_MEMBER (utmp_ent
);
186 struct tm
*tmp
= localtime (&t
);
190 strftime (buf
, sizeof buf
, time_format
, tmp
);
194 return timetostr (t
, buf
);
197 /* Display a line of information about UTMP_ENT. */
200 print_entry (const STRUCT_UTMP
*utmp_ent
)
206 /* If ut_line contains a space, the device name starts after the space. */
207 char *line
= utmp_ent
->ut_line
;
208 char *space
= strchr (line
, ' ');
209 line
= space
? space
+ 1 : line
;
212 if (IS_ABSOLUTE_FILE_NAME (line
))
216 static int dev_dirfd
;
219 dev_dirfd
= open ("/dev", O_PATHSEARCH
| O_DIRECTORY
);
221 dev_dirfd
= AT_FDCWD
- 1;
226 if (AT_FDCWD
<= dirfd
&& fstatat (dirfd
, line
, &stats
, 0) == 0)
228 mesg
= (stats
.st_mode
& S_IWGRP
) ? ' ' : '*';
229 last_change
= stats
.st_atime
;
237 if (strnlen (UT_USER (utmp_ent
), 8) < 8)
238 printf ("%-8s", UT_USER (utmp_ent
));
240 fputs (UT_USER (utmp_ent
), stdout
);
242 if (include_fullname
)
244 char *name
= UT_USER (utmp_ent
);
245 struct passwd
*pw
= getpwnam (name
);
247 /* TRANSLATORS: Real name is unknown; at most 19 characters. */
248 printf (" %19s", _(" ???"));
251 char *const comma
= strchr (pw
->pw_gecos
, ',');
257 result
= create_fullname (pw
->pw_gecos
, pw
->pw_name
);
258 printf (" %-19.19s", result
);
264 fputc (mesg
, stdout
);
265 if (strnlen (utmp_ent
->ut_line
, 8) < 8)
266 printf ("%-8s", utmp_ent
->ut_line
);
268 fputs (utmp_ent
->ut_line
, stdout
);
273 printf (" %-6s", idle_string (last_change
));
275 /* TRANSLATORS: Idle time is unknown; at most 5 characters. */
276 printf (" %-6s", _("?????"));
279 printf (" %s", time_string (utmp_ent
));
281 #ifdef HAVE_STRUCT_XTMP_UT_HOST
282 if (include_where
&& utmp_ent
->ut_host
[0])
284 char *host
= nullptr;
285 char *display
= nullptr;
286 char *ut_host
= utmp_ent
->ut_host
;
288 /* Look for an X display. */
289 display
= strchr (ut_host
, ':');
294 /* See if we can canonicalize it. */
295 host
= canon_host (ut_host
);
300 fputs (host
, stdout
);
304 fputs (display
, stdout
);
315 /* Display a verbose line of information about UTMP_ENT. */
318 print_long_entry (const char name
[])
322 pw
= getpwnam (name
);
324 printf (_("Login name: "));
325 printf ("%-28s", name
);
327 printf (_("In real life: "));
330 /* TRANSLATORS: Real name is unknown; no hard limit. */
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 char const *const baseproject
= "/.project";
363 char *const project
=
364 xmalloc (strlen (pw
->pw_dir
) + strlen (baseproject
) + 1);
365 stpcpy (stpcpy (project
, pw
->pw_dir
), baseproject
);
367 stream
= fopen (project
, "r");
372 printf (_("Project: "));
374 while ((bytes
= fread (buf
, 1, sizeof (buf
), stream
)) > 0)
375 fwrite (buf
, 1, bytes
, stdout
);
386 char const *const baseplan
= "/.plan";
388 xmalloc (strlen (pw
->pw_dir
) + strlen (baseplan
) + 1);
389 stpcpy (stpcpy (plan
, pw
->pw_dir
), baseplan
);
391 stream
= fopen (plan
, "r");
396 printf (_("Plan:\n"));
398 while ((bytes
= fread (buf
, 1, sizeof (buf
), stream
)) > 0)
399 fwrite (buf
, 1, bytes
, stdout
);
409 /* Print the username of each valid entry and the number of valid entries
410 in UTMP_BUF, which should have N elements. */
415 printf ("%-8s", _("Login"));
416 if (include_fullname
)
417 printf (" %-19s", _("Name"));
418 printf (" %-9s", _(" TTY"));
420 printf (" %-6s", _("Idle"));
421 printf (" %-*s", time_format_width
, _("When"));
422 #ifdef HAVE_STRUCT_XTMP_UT_HOST
424 printf (" %s", _("Where"));
429 /* Display UTMP_BUF, which should have N entries. */
432 scan_entries (idx_t n
, const STRUCT_UTMP
*utmp_buf
,
433 const int argc_names
, char *const argv_names
[])
435 if (hard_locale (LC_TIME
))
437 time_format
= "%Y-%m-%d %H:%M";
438 time_format_width
= 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2;
442 time_format
= "%b %e %H:%M";
443 time_format_width
= 3 + 1 + 2 + 1 + 2 + 1 + 2;
451 if (IS_USER_PROCESS (utmp_buf
))
455 for (int i
= 0; i
< argc_names
; i
++)
456 if (STREQ (UT_USER (utmp_buf
), argv_names
[i
]))
458 print_entry (utmp_buf
);
463 print_entry (utmp_buf
);
469 /* Display a list of who is on the system, according to utmp file FILENAME. */
472 short_pinky (char const *filename
,
473 const int argc_names
, char *const argv_names
[])
476 STRUCT_UTMP
*utmp_buf
= nullptr;
478 if (read_utmp (filename
, &n_users
, &utmp_buf
, 0) != 0)
479 error (EXIT_FAILURE
, errno
, "%s", quotef (filename
));
481 scan_entries (n_users
, utmp_buf
, argc_names
, argv_names
);
486 long_pinky (const int argc_names
, char *const argv_names
[])
488 for (int i
= 0; i
< argc_names
; i
++)
489 print_long_entry (argv_names
[i
]);
495 if (status
!= EXIT_SUCCESS
)
499 printf (_("Usage: %s [OPTION]... [USER]...\n"), program_name
);
502 -l produce long format output for the specified USERs\n\
503 -b omit the user's home directory and shell in long format\n\
504 -h omit the user's project file in long format\n\
505 -p omit the user's plan file in long format\n\
506 -s do short format output, this is the default\n\
509 -f omit the line of column headings in short format\n\
510 -w omit the user's full name in short format\n\
511 -i omit the user's full name and remote host in short format\n\
512 -q omit the user's full name, remote host and idle time\n\
515 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
516 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
519 A lightweight 'finger' program; print user information.\n\
520 The utmp file will be %s.\n\
522 emit_ancillary_info (PROGRAM_NAME
);
528 main (int argc
, char **argv
)
533 initialize_main (&argc
, &argv
);
534 set_program_name (argv
[0]);
535 setlocale (LC_ALL
, "");
536 bindtextdomain (PACKAGE
, LOCALEDIR
);
537 textdomain (PACKAGE
);
539 atexit (close_stdout
);
541 while ((optc
= getopt_long (argc
, argv
, "sfwiqbhlp", longopts
, nullptr))
547 do_short_format
= true;
551 do_short_format
= false;
555 include_heading
= false;
559 include_fullname
= false;
563 include_fullname
= false;
564 #ifdef HAVE_STRUCT_XTMP_UT_HOST
565 include_where
= false;
570 include_fullname
= false;
571 #ifdef HAVE_STRUCT_XTMP_UT_HOST
572 include_where
= false;
574 include_idle
= false;
578 include_project
= false;
582 include_plan
= false;
586 include_home_and_shell
= false;
589 case_GETOPT_HELP_CHAR
;
591 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
594 usage (EXIT_FAILURE
);
598 n_users
= argc
- optind
;
600 if (!do_short_format
&& n_users
== 0)
602 error (0, 0, _("no username specified; at least one must be\
603 specified when using -l"));
604 usage (EXIT_FAILURE
);
608 short_pinky (UTMP_FILE
, n_users
, argv
+ optind
);
610 long_pinky (n_users
, argv
+ optind
);