/* GNU's pinky. This is the pinky utility
Copyright (C) 1992-2018 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */ The GNUv3 license
/* Created by hacking who.c by Kaveh Ghazi ghazi@caip.rutgers.edu */
#include <config.h> Provides system specific information
#include <getopt.h> ...!includes auto-comment...
#include <pwd.h> ...!includes auto-comment...
#include <stdio.h> Provides standard I/O capability
#include <sys/types.h> Provides system data types
#include "system.h" ...!includes auto-comment...
#include "canon-host.h" ...!includes auto-comment...
#include "die.h" ...!includes auto-comment...
#include "error.h" ...!includes auto-comment...
#include "hard-locale.h" ...!includes auto-comment...
#include "readutmp.h" ...!includes auto-comment...
/* The official name of this program (e.g., no 'g' prefix). */
#define PROGRAM_NAME "pinky" Line 34
#define AUTHORS \ Line 36
proper_name ("Joseph Arceneaux"), \ Line 37
proper_name ("David MacKenzie"), \ Line 38
proper_name ("Kaveh Ghazi") Line 39
/* If true, display the hours:minutes since each user has touched
the keyboard, or blank if within the last minute, or days followed
by a 'd' if not within the last day. */
static bool include_idle = true; Line 44
/* If true, display a line at the top describing each field. */
static bool include_heading = true; Line 47
/* if true, display the user's full name from pw_gecos. */
static bool include_fullname = true; Line 50
/* if true, display the user's ~/.project file when doing long format. */
static bool include_project = true; Line 53
/* if true, display the user's ~/.plan file when doing long format. */
static bool include_plan = true; Line 56
/* if true, display the user's home directory and shell
when doing long format. */
static bool include_home_and_shell = true; Line 60
/* if true, use the "short" output format. */
static bool do_short_format = true; Line 63
/* if true, display the ut_host field. */
#ifdef HAVE_UT_HOST Line 66
static bool include_where = true; Line 67
#endif Line 68
/* The strftime format to use for login times, and its expected
output width. */
static char const *time_format; Line 72
static int time_format_width; Line 73
static struct option const longopts[] = Line 75
{
{GETOPT_HELP_OPTION_DECL}, Line 77
{GETOPT_VERSION_OPTION_DECL}, Line 78
{NULL, 0, NULL, 0} Line 79
}; Block 1
/* Count and return the number of ampersands in STR. */
static size_t _GL_ATTRIBUTE_PURE Line 84
count_ampersands (const char *str) Line 85
{
size_t count = 0; Line 87
do
{
if (*str == '&') Line 90
count++; Line 91
} while (*str++); Line 92
return count; Line 93
} Block 2
/* Create a string (via xmalloc) which contains a full name by substituting
for each ampersand in GECOS_NAME the USER_NAME string with its first
character capitalized. The caller must ensure that GECOS_NAME contains
no ','s. The caller also is responsible for free'ing the return value of
this function. */
static char * Line 102
create_fullname (const char *gecos_name, const char *user_name) Line 103
{
size_t rsize = strlen (gecos_name) + 1; Line 105
char *result; Line 106
char *r; Line 107
size_t ampersands = count_ampersands (gecos_name); Line 108
if (ampersands != 0) Line 110
{
size_t ulen = strlen (user_name); Line 112
size_t product = ampersands * ulen; Line 113
rsize += product - ampersands; Line 114
if (xalloc_oversized (ulen, ampersands) || rsize < product) Line 115
xalloc_die (); ...!common auto-comment...
}
r = result = xmalloc (rsize); Line 119
while (*gecos_name) Line 121
{
if (*gecos_name == '&') Line 123
{
const char *uname = user_name; Line 125
if (islower (to_uchar (*uname))) Line 126
*r++ = toupper (to_uchar (*uname++)); Line 127
while (*uname) Line 128
*r++ = *uname++; Line 129
}
else Line 131
{
*r++ = *gecos_name; Line 133
}
gecos_name++; Line 136
}
*r = 0; Line 138
return result; Line 140
} Block 3
/* Return a string representing the time between WHEN and the time
that this function is first run. */
static const char * Line 146
idle_string (time_t when) Line 147
{
static time_t now = 0; Line 149
static char buf[INT_STRLEN_BOUND (long int) + 2]; Line 150
time_t seconds_idle; Line 151
if (now == 0) Line 153
time (&now); Line 154
seconds_idle = now - when; Line 156
if (seconds_idle < 60) /* One minute. */ Line 157
return " "; Line 158
if (seconds_idle < (24 * 60 * 60)) /* One day. */ Line 159
{
int hours = seconds_idle / (60 * 60); Line 161
int minutes = (seconds_idle % (60 * 60)) / 60; Line 162
sprintf (buf, "%02d:%02d", hours, minutes); Line 163
}
else Line 165
{
unsigned long int days = seconds_idle / (24 * 60 * 60); Line 167
sprintf (buf, "%lud", days); Line 168
}
return buf; Line 170
} Block 4
/* Return a time string. */
static const char * Line 174
time_string (const STRUCT_UTMP *utmp_ent) Line 175
{
static char buf[INT_STRLEN_BOUND (intmax_t) + sizeof "-%m-%d %H:%M"]; Line 177
/* Don't take the address of UT_TIME_MEMBER directly.
Ulrich Drepper wrote:
"... GNU libc (and perhaps other libcs as well) have extended
utmp file formats which do not use a simple time_t ut_time field.
In glibc, ut_time is a macro which selects for backward compatibility
the tv_sec member of a struct timeval value." */
time_t t = UT_TIME_MEMBER (utmp_ent); Line 185
struct tm *tmp = localtime (&t); Line 186
if (tmp) Line 188
{
strftime (buf, sizeof buf, time_format, tmp); Line 190
return buf; Line 191
}
else Line 193
return timetostr (t, buf); Line 194
} Block 5
/* Display a line of information about UTMP_ENT. */
static void Line 199
print_entry (const STRUCT_UTMP *utmp_ent) Line 200
{
struct stat stats; Line 202
time_t last_change; Line 203
char mesg; Line 204
#define DEV_DIR_WITH_TRAILING_SLASH "/dev/" Line 206
#define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1) Line 207
char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1]; Line 209
char *p = line; Line 210
/* Copy ut_line into LINE, prepending '/dev/' if ut_line is not
already an absolute file name. Some system may put the full,
absolute file name in ut_line. */
if ( ! IS_ABSOLUTE_FILE_NAME (utmp_ent->ut_line)) Line 215
p = stpcpy (p, DEV_DIR_WITH_TRAILING_SLASH); Line 216
stzncpy (p, utmp_ent->ut_line, sizeof (utmp_ent->ut_line)); Line 217
if (stat (line, &stats) == 0) Line 219...!syscalls auto-comment...
{
mesg = (stats.st_mode & S_IWGRP) ? ' ' : '*'; Line 221
last_change = stats.st_atime; Line 222
}
else Line 224
{
mesg = '?'; Line 226
last_change = 0; Line 227
}
printf ("%-8.*s", UT_USER_SIZE, UT_USER (utmp_ent)); Line 230
if (include_fullname) Line 232
{
struct passwd *pw; Line 234
char name[UT_USER_SIZE + 1]; Line 235
stzncpy (name, UT_USER (utmp_ent), UT_USER_SIZE); Line 237
pw = getpwnam (name); Line 238
if (pw == NULL) Line 239
/* TRANSLATORS: Real name is unknown; at most 19 characters. */
printf (" %19s", _(" ???")); Line 241
else Line 242
{
char *const comma = strchr (pw->pw_gecos, ','); Line 244
char *result; Line 245
if (comma) Line 247
*comma = '\0'; Line 248
result = create_fullname (pw->pw_gecos, pw->pw_name); Line 250
printf (" %-19.19s", result); Line 251
free (result); Line 252
}
}
printf (" %c%-8.*s", Line 256
mesg, (int) sizeof (utmp_ent->ut_line), utmp_ent->ut_line); Line 257
if (include_idle) Line 259
{
if (last_change) Line 261
printf (" %-6s", idle_string (last_change)); Line 262
else Line 263
/* TRANSLATORS: Idle time is unknown; at most 5 characters. */
printf (" %-6s", _("?????")); Line 265
}
printf (" %s", time_string (utmp_ent)); Line 268
#ifdef HAVE_UT_HOST Line 270
if (include_where && utmp_ent->ut_host[0]) Line 271
{
char ut_host[sizeof (utmp_ent->ut_host) + 1]; Line 273
char *host = NULL; Line 274
char *display = NULL; Line 275
/* Copy the host name into UT_HOST, and ensure it's nul terminated. */
stzncpy (ut_host, utmp_ent->ut_host, sizeof (utmp_ent->ut_host)); Line 278
/* Look for an X display. */
display = strchr (ut_host, ':'); Line 281
if (display) Line 282
*display++ = '\0'; Line 283
if (*ut_host) Line 285
/* See if we can canonicalize it. */
host = canon_host (ut_host); Line 287
if ( ! host) Line 288
host = ut_host; Line 289
if (display) Line 291
printf (" %s:%s", host, display); Line 292
else Line 293
printf (" %s", host); Line 294
if (host != ut_host) Line 296
free (host); Line 297
}
#endif Line 299
putchar ('\n'); Line 301
} Block 6
/* Display a verbose line of information about UTMP_ENT. */
static void Line 306
print_long_entry (const char name[]) Line 307
{
struct passwd *pw; Line 309
pw = getpwnam (name); Line 311
printf (_("Login name: ")); Line 313
printf ("%-28s", name); Line 314
printf (_("In real life: ")); Line 316
if (pw == NULL) Line 317
{
/* TRANSLATORS: Real name is unknown; no hard limit. */
printf (" %s", _("???\n")); Line 320
return; Line 321
}
else Line 323
{
char *const comma = strchr (pw->pw_gecos, ','); Line 325
char *result; Line 326
if (comma) Line 328
*comma = '\0'; Line 329
result = create_fullname (pw->pw_gecos, pw->pw_name); Line 331
printf (" %s", result); Line 332
free (result); Line 333
}
putchar ('\n'); Line 336
if (include_home_and_shell) Line 338
{
printf (_("Directory: ")); Line 340
printf ("%-29s", pw->pw_dir); Line 341
printf (_("Shell: ")); Line 342
printf (" %s", pw->pw_shell); Line 343
putchar ('\n'); Line 344
}
if (include_project) Line 347
{
FILE *stream; Line 349
char buf[1024]; Line 350
const char *const baseproject = "/.project"; Line 351
char *const project = Line 352
xmalloc (strlen (pw->pw_dir) + strlen (baseproject) + 1); Line 353
stpcpy (stpcpy (project, pw->pw_dir), baseproject); Line 354
stream = fopen (project, "r"); Line 356...!syscalls auto-comment...
if (stream) Line 357
{
size_t bytes; Line 359
printf (_("Project: ")); Line 361
while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0) Line 363...!syscalls auto-comment...
fwrite (buf, 1, bytes, stdout); Line 364...!syscalls auto-comment...
fclose (stream); Line 365...!syscalls auto-comment...
}
free (project); Line 368
}
if (include_plan) Line 371
{
FILE *stream; Line 373
char buf[1024]; Line 374
const char *const baseplan = "/.plan"; Line 375
char *const plan = Line 376
xmalloc (strlen (pw->pw_dir) + strlen (baseplan) + 1); Line 377
stpcpy (stpcpy (plan, pw->pw_dir), baseplan); Line 378
stream = fopen (plan, "r"); Line 380...!syscalls auto-comment...
if (stream) Line 381
{
size_t bytes; Line 383
printf (_("Plan:\n")); Line 385
while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0) Line 387...!syscalls auto-comment...
fwrite (buf, 1, bytes, stdout); Line 388...!syscalls auto-comment...
fclose (stream); Line 389...!syscalls auto-comment...
}
free (plan); Line 392
}
putchar ('\n'); Line 395
} Block 7
/* Print the username of each valid entry and the number of valid entries
in UTMP_BUF, which should have N elements. */
static void Line 401
print_heading (void) Line 402
{
printf ("%-8s", _("Login")); Line 404
if (include_fullname) Line 405
printf (" %-19s", _("Name")); Line 406
printf (" %-9s", _(" TTY")); Line 407
if (include_idle) Line 408
printf (" %-6s", _("Idle")); Line 409
printf (" %-*s", time_format_width, _("When")); Line 410
#ifdef HAVE_UT_HOST Line 411
if (include_where) Line 412
printf (" %s", _("Where")); Line 413
#endif Line 414
putchar ('\n'); Line 415
} Block 8
/* Display UTMP_BUF, which should have N entries. */
static void Line 420
scan_entries (size_t n, const STRUCT_UTMP *utmp_buf, Line 421
const int argc_names, char *const argv_names[]) Line 422
{
if (hard_locale (LC_TIME)) Line 424
{
time_format = "%Y-%m-%d %H:%M"; Line 426
time_format_width = 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2; Line 427
}
else Line 429
{
time_format = "%b %e %H:%M"; Line 431
time_format_width = 3 + 1 + 2 + 1 + 2 + 1 + 2; Line 432
}
if (include_heading) Line 435
print_heading (); Line 436
while (n--) Line 438
{
if (IS_USER_PROCESS (utmp_buf)) Line 440
{
if (argc_names) Line 442
{
for (int i = 0; i < argc_names; i++) Line 444
if (STREQ_LEN (UT_USER (utmp_buf), argv_names[i], UT_USER_SIZE))Line 445
{
print_entry (utmp_buf); Line 447
break; Line 448
}
}
else Line 451
print_entry (utmp_buf); Line 452
}
utmp_buf++; Line 454
}
} Block 9
/* Display a list of who is on the system, according to utmp file FILENAME. */
static void Line 460
short_pinky (const char *filename, Line 461
const int argc_names, char *const argv_names[]) Line 462
{
size_t n_users; Line 464
STRUCT_UTMP *utmp_buf = NULL; Line 465
if (read_utmp (filename, &n_users, &utmp_buf, 0) != 0) Line 467
die (EXIT_FAILURE, errno, "%s", quotef (filename)); Line 468
scan_entries (n_users, utmp_buf, argc_names, argv_names); Line 470
IF_LINT (free (utmp_buf)); Line 472
} Block 10
static void Line 475
long_pinky (const int argc_names, char *const argv_names[]) Line 476
{
for (int i = 0; i < argc_names; i++) Line 478
print_long_entry (argv_names[i]); Line 479
} Block 11
void Line 482
usage (int status) Line 483
{
if (status != EXIT_SUCCESS) Line 485
emit_try_help (); ...!common auto-comment...
else Line 487
{
printf (_("Usage: %s [OPTION]... [USER]...\n"), program_name); Line 489
fputs (_("\ Line 490
\n\
-l produce long format output for the specified USERs\n\ Line 492
-b omit the user's home directory and shell in long format\n\ Line 493
-h omit the user's project file in long format\n\ Line 494
-p omit the user's plan file in long format\n\ Line 495
-s do short format output, this is the default\n\ Line 496
"), stdout); Line 497
fputs (_("\ Line 498
-f omit the line of column headings in short format\n\ Line 499
-w omit the user's full name in short format\n\ Line 500
-i omit the user's full name and remote host in short format\n\ Line 501
-q omit the user's full name, remote host and idle time\n\ Line 502
in short format\n\ Line 503
"), stdout); Line 504
fputs (HELP_OPTION_DESCRIPTION, stdout); Line 505
fputs (VERSION_OPTION_DESCRIPTION, stdout); Line 506
printf (_("\ Line 507
\n\
A lightweight 'finger' program; print user information.\n\ Line 509
The utmp file will be %s.\n\ Line 510
"), UTMP_FILE); Line 511
emit_ancillary_info (PROGRAM_NAME); Line 512
}
exit (status); Line 514
} Block 12
int
main (int argc, char **argv) Line 518
{
int optc; Line 520
int n_users; Line 521
initialize_main (&argc, &argv); VMS-specific entry point handling wildcard expansion
set_program_name (argv[0]); Retains program name and discards path
setlocale (LC_ALL, ""); Sets up internationalization (i18n)
bindtextdomain (PACKAGE, LOCALEDIR); Assigns i18n directorySets text domain for _() [gettext()] function
textdomain (PACKAGE); Sets text domain for _() [gettext()] function
atexit (close_stdout); Close stdout on exit (see gnulib)
while ((optc = getopt_long (argc, argv, "sfwiqbhlp", longopts, NULL)) != -1) Line 531
{
switch (optc) Line 533
{
case 's': Line 535
do_short_format = true; Line 536
break; Line 537
case 'l': Line 539
do_short_format = false; Line 540
break; Line 541
case 'f': Line 543
include_heading = false; Line 544
break; Line 545
case 'w': Line 547
include_fullname = false; Line 548
break; Line 549
case 'i': Line 551
include_fullname = false; Line 552
#ifdef HAVE_UT_HOST Line 553
include_where = false; Line 554
#endif Line 555
break; Line 556
case 'q': Line 558
include_fullname = false; Line 559
#ifdef HAVE_UT_HOST Line 560
include_where = false; Line 561
#endif Line 562
include_idle = false; Line 563
break; Line 564
case 'h': Line 566
include_project = false; Line 567
break; Line 568
case 'p': Line 570
include_plan = false; Line 571
break; Line 572
case 'b': Line 574
include_home_and_shell = false; Line 575
break; Line 576
case_GETOPT_HELP_CHAR; Line 578
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); Line 580
default: Line 582
usage (EXIT_FAILURE); Line 583
}
}
n_users = argc - optind; Line 587
if (!do_short_format && n_users == 0) Line 589
{
error (0, 0, _("no username specified; at least one must be\ Line 591
specified when using -l")); Line 592
usage (EXIT_FAILURE); Line 593
}
if (do_short_format) Line 596
short_pinky (UTMP_FILE, n_users, argv + optind); Line 597
else Line 598
long_pinky (n_users, argv + optind); Line 599
return EXIT_SUCCESS; Line 601
} Block 13