1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-04-20 10:51:48 +02:00
Files
coreutils/src/pinky.c

567 lines
13 KiB
C
Raw Normal View History

1997-08-03 01:54:34 +00:00
/* GNU's pinky.
Copyright (C) 1992-1997, 1999, 2000 Free Software Foundation, Inc.
1997-08-03 01:54:34 +00:00
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 2, 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, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* Created by hacking who.c by Kaveh Ghazi ghazi@caip.rutgers.edu */
#include <config.h>
#include <getopt.h>
#include <pwd.h>
1999-02-08 03:29:02 +00:00
#include <stdio.h>
1997-08-03 01:54:34 +00:00
#include "system.h"
#include "error.h"
#include "readutmp.h"
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "pinky"
#define AUTHORS "Joseph Arceneaux, David MacKenzie, and Kaveh Ghazi"
1997-08-03 01:54:34 +00:00
#ifndef MAXHOSTNAMELEN
# define MAXHOSTNAMELEN 64
#endif
#ifndef S_IWGRP
# define S_IWGRP 020
#endif
int gethostname ();
char *ttyname ();
/* The name this program was run with. */
const char *program_name;
/* If nonzero, 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 int include_idle = 1;
/* If nonzero, display a line at the top describing each field. */
static int include_heading = 1;
/* if nonzero, display the user's full name from pw_gecos. */
static int include_fullname = 1;
/* if nonzero, display the user's ~/.project file when doing long format. */
static int include_project = 1;
/* if nonzero, display the user's ~/.plan file when doing long format. */
static int include_plan = 1;
/* if nonzero, display the user's home directory and shell
when doing long format. */
static int include_home_and_shell = 1;
/* if nonzero, use the "short" output format. */
static int do_short_format = 1;
/* if nonzero, display the ut_host field. */
#ifdef HAVE_UT_HOST
static int include_where = 1;
#endif
static struct option const longopts[] =
{
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
1997-08-03 01:54:34 +00:00
{NULL, 0, NULL, 0}
};
/* Count and return the number of ampersands in STR. */
static int
count_ampersands (const char *str)
{
int count = 0;
do
{
if (*str == '&')
count++;
} while (*str++);
return count;
}
/* 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 *
create_fullname (const char *gecos_name, const char *user_name)
{
const int result_len = strlen (gecos_name)
+ count_ampersands (gecos_name) * (strlen (user_name) - 1) + 1;
char *const result = xmalloc (result_len);
char *r = result;
while (*gecos_name)
{
if (*gecos_name == '&')
{
const char *uname = user_name;
if (ISLOWER (*uname))
*r++ = TOUPPER (*uname++);
while (*uname)
*r++ = *uname++;
}
else
{
*r++ = *gecos_name;
}
gecos_name++;
}
*r = 0;
return result;
}
1997-08-03 01:54:34 +00:00
/* Return a string representing the time between WHEN and the time
that this function is first run. */
static const char *
idle_string (time_t when)
{
static time_t now = 0;
static char idle_hhmm[10];
time_t seconds_idle;
if (now == 0)
time (&now);
seconds_idle = now - when;
if (seconds_idle < 60) /* One minute. */
return " ";
1997-08-03 02:04:28 +00:00
if (seconds_idle < (24 * 60 * 60)) /* One day. */
1997-08-03 01:54:34 +00:00
{
sprintf (idle_hhmm, "%02d:%02d",
(int) (seconds_idle / (60 * 60)),
(int) ((seconds_idle % (60 * 60)) / 60));
return (const char *) idle_hhmm;
}
1997-08-03 02:04:28 +00:00
sprintf (idle_hhmm, "%dd", (int) (seconds_idle / (24 * 60 * 60)));
1997-08-03 01:54:34 +00:00
return (const char *) idle_hhmm;
}
/* Display a line of information about UTMP_ENT. */
static void
print_entry (const STRUCT_UTMP *utmp_ent)
{
struct stat stats;
time_t last_change;
char mesg;
#define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
#define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
time_t tm;
/* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
already an absolute pathname. Some system may put the full,
absolute pathname in ut_line. */
if (utmp_ent->ut_line[0] == '/')
{
strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
line[sizeof (utmp_ent->ut_line)] = '\0';
}
else
{
strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
}
if (stat (line, &stats) == 0)
{
mesg = (stats.st_mode & S_IWGRP) ? ' ' : '*';
last_change = stats.st_atime;
}
else
{
mesg = '?';
last_change = 0;
}
printf ("%-8.*s", (int) sizeof (UT_USER (utmp_ent)), UT_USER (utmp_ent));
1997-08-03 01:54:34 +00:00
if (include_fullname)
{
1997-08-03 02:04:28 +00:00
struct passwd *pw;
char name[sizeof (UT_USER (utmp_ent)) + 1];
1997-08-03 02:04:28 +00:00
strncpy (name, UT_USER (utmp_ent), sizeof (UT_USER (utmp_ent)));
name[sizeof (UT_USER (utmp_ent))] = '\0';
1997-08-03 02:04:28 +00:00
pw = getpwnam (name);
1997-08-03 01:54:34 +00:00
if (pw == NULL)
1997-08-03 02:04:28 +00:00
printf (" %19s", " ???");
1997-08-03 01:54:34 +00:00
else
{
1997-08-03 02:04:28 +00:00
char *const comma = strchr (pw->pw_gecos, ',');
char *result;
1997-08-03 01:54:34 +00:00
if (comma)
*comma = '\0';
1997-08-03 02:04:28 +00:00
result = create_fullname (pw->pw_gecos, pw->pw_name);
printf (" %-19.19s", result);
free (result);
1997-08-03 01:54:34 +00:00
}
}
printf (" %c%-8.*s",
mesg, (int) sizeof (utmp_ent->ut_line), utmp_ent->ut_line);
if (include_idle)
{
if (last_change)
printf (" %-6s", idle_string (last_change));
else
printf (" %-6s", "???");
}
/* 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.'' */
tm = UT_TIME_MEMBER (utmp_ent);
printf (" %-12.12s", ctime (&tm) + 4);
#ifdef HAVE_UT_HOST
if (include_where && utmp_ent->ut_host[0])
{
extern char *canon_host ();
char ut_host[sizeof (utmp_ent->ut_host) + 1];
char *host = 0, *display = 0;
/* Copy the host name into UT_HOST, and ensure it's nul terminated. */
strncpy (ut_host, utmp_ent->ut_host, (int) sizeof (utmp_ent->ut_host));
ut_host[sizeof (utmp_ent->ut_host)] = '\0';
/* Look for an X display. */
display = strrchr (ut_host, ':');
if (display)
*display++ = '\0';
if (*ut_host)
/* See if we can canonicalize it. */
host = canon_host (ut_host);
1997-08-03 02:04:28 +00:00
if ( ! host)
1997-08-03 01:54:34 +00:00
host = ut_host;
if (display)
printf (" %s:%s", host, display);
else
printf (" %s", host);
}
#endif
putchar ('\n');
1997-08-03 01:54:34 +00:00
}
/* Display a verbose line of information about UTMP_ENT. */
static void
print_long_entry (const char name[])
{
1997-08-03 02:04:28 +00:00
struct passwd *pw;
1997-08-03 01:54:34 +00:00
1997-08-03 02:04:28 +00:00
pw = getpwnam (name);
1997-08-03 01:54:34 +00:00
1997-08-03 02:49:43 +00:00
printf (_("Login name: "));
1997-08-03 01:54:34 +00:00
printf ("%-28s", name);
1997-08-03 02:49:43 +00:00
printf (_("In real life: "));
1997-08-03 01:54:34 +00:00
if (pw == NULL)
{
1997-08-03 02:49:43 +00:00
printf (" %s", _("???\n"));
1997-08-03 01:54:34 +00:00
return;
}
else
{
1997-08-03 02:04:28 +00:00
char *const comma = strchr (pw->pw_gecos, ',');
char *result;
1997-08-03 01:54:34 +00:00
if (comma)
*comma = '\0';
1997-08-03 02:04:28 +00:00
result = create_fullname (pw->pw_gecos, pw->pw_name);
printf (" %s", result);
free (result);
1997-08-03 01:54:34 +00:00
}
putchar ('\n');
1997-08-03 02:04:28 +00:00
1997-08-03 01:54:34 +00:00
if (include_home_and_shell)
{
1997-08-03 02:49:43 +00:00
printf (_("Directory: "));
1997-08-03 02:04:28 +00:00
printf ("%-29s", pw->pw_dir);
1997-08-03 02:49:43 +00:00
printf (_("Shell: "));
1997-08-03 02:04:28 +00:00
printf (" %s", pw->pw_shell);
putchar ('\n');
1997-08-03 01:54:34 +00:00
}
if (include_project)
{
1997-08-03 02:04:28 +00:00
FILE *stream;
1997-08-03 01:54:34 +00:00
char buf[1024];
1997-08-03 02:04:28 +00:00
const char *const baseproject = "/.project";
char *const project =
xmalloc (strlen (pw->pw_dir) + strlen (baseproject) + 1);
1997-08-03 01:54:34 +00:00
strcpy (project, pw->pw_dir);
strcat (project, baseproject);
1997-08-03 02:04:28 +00:00
stream = fopen (project, "r");
1997-08-03 01:54:34 +00:00
if (stream)
{
int bytes;
1997-08-03 02:04:28 +00:00
1997-08-03 02:49:43 +00:00
printf (_("Project: "));
1997-08-03 02:04:28 +00:00
while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
fwrite (buf, 1, bytes, stdout);
fclose (stream);
1997-08-03 01:54:34 +00:00
}
1997-08-03 02:04:28 +00:00
1997-08-03 01:54:34 +00:00
free (project);
}
1997-08-03 02:04:28 +00:00
1997-08-03 01:54:34 +00:00
if (include_plan)
{
1997-08-03 02:04:28 +00:00
FILE *stream;
1997-08-03 01:54:34 +00:00
char buf[1024];
1997-08-03 02:04:28 +00:00
const char *const baseplan = "/.plan";
char *const plan =
xmalloc (strlen (pw->pw_dir) + strlen (baseplan) + 1);
1997-08-03 01:54:34 +00:00
strcpy (plan, pw->pw_dir);
strcat (plan, baseplan);
1997-08-03 02:04:28 +00:00
stream = fopen (plan, "r");
1997-08-03 01:54:34 +00:00
if (stream)
{
int bytes;
1997-08-03 02:04:28 +00:00
1997-08-03 02:49:43 +00:00
printf (_("Plan:\n"));
1997-08-03 02:04:28 +00:00
while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
fwrite (buf, 1, bytes, stdout);
fclose (stream);
1997-08-03 01:54:34 +00:00
}
1997-08-03 02:04:28 +00:00
1997-08-03 01:54:34 +00:00
free (plan);
}
putchar ('\n');
1997-08-03 01:54:34 +00:00
}
/* Print the username of each valid entry and the number of valid entries
in UTMP_BUF, which should have N elements. */
static void
print_heading (void)
{
1997-08-03 02:49:43 +00:00
printf ("%-8s", _("Login"));
1997-08-03 01:54:34 +00:00
if (include_fullname)
1997-08-03 02:49:43 +00:00
printf (" %-19s", _(" Name"));
printf (" %-9s", _("TTY"));
1997-08-03 01:54:34 +00:00
if (include_idle)
1997-08-03 02:49:43 +00:00
printf (" %-6s", _("Idle"));
printf (" %-12s", _("When"));
1997-08-03 01:54:34 +00:00
#ifdef HAVE_UT_HOST
if (include_where)
1997-08-03 02:49:43 +00:00
printf (" %s", _("Where"));
1997-08-03 01:54:34 +00:00
#endif
putchar ('\n');
1997-08-03 01:54:34 +00:00
}
/* Display UTMP_BUF, which should have N entries. */
static void
scan_entries (int n, const STRUCT_UTMP *utmp_buf,
1997-08-03 02:04:28 +00:00
const int argc_names, char *const argv_names[])
1997-08-03 01:54:34 +00:00
{
if (include_heading)
print_heading ();
while (n--)
{
if (UT_USER (utmp_buf)[0]
1997-08-03 01:54:34 +00:00
#ifdef USER_PROCESS
&& utmp_buf->ut_type == USER_PROCESS
#endif
1997-08-03 02:04:28 +00:00
)
1997-08-03 01:54:34 +00:00
{
if (argc_names)
{
int i;
1997-08-03 02:04:28 +00:00
1997-08-03 01:54:34 +00:00
for (i = 0; i < argc_names; i++)
if (strncmp (UT_USER (utmp_buf), argv_names[i],
sizeof (UT_USER (utmp_buf))) == 0)
1997-08-03 01:54:34 +00:00
{
print_entry (utmp_buf);
break;
}
}
else
print_entry (utmp_buf);
}
utmp_buf++;
}
}
/* Display a list of who is on the system, according to utmp file FILENAME. */
static void
short_pinky (const char *filename,
1997-08-03 02:04:28 +00:00
const int argc_names, char *const argv_names[])
1997-08-03 01:54:34 +00:00
{
int n_users;
STRUCT_UTMP *utmp_buf;
int fail = read_utmp (filename, &n_users, &utmp_buf);
if (fail)
error (1, errno, "%s", filename);
scan_entries (n_users, utmp_buf, argc_names, argv_names);
}
static void
1997-08-03 02:04:28 +00:00
long_pinky (const int argc_names, char *const argv_names[])
1997-08-03 01:54:34 +00:00
{
int i;
1997-08-03 02:04:28 +00:00
for (i = 0; i < argc_names; i++)
1997-08-03 01:54:34 +00:00
print_long_entry (argv_names[i]);
}
1999-01-25 14:33:38 +00:00
void
1997-08-03 01:54:34 +00:00
usage (int status)
{
if (status != 0)
1997-08-03 02:49:43 +00:00
fprintf (stderr, _("Try `%s --help' for more information.\n"),
1997-08-03 01:54:34 +00:00
program_name);
else
{
1997-08-03 02:49:43 +00:00
printf (_("Usage: %s [OPTION]... [USER]...\n"), program_name);
printf (_("\
1997-08-03 01:54:34 +00:00
\n\
-l do long format output\n\
-b omit the user's home directory and shell in long format\n\
-h omit the user's project file in long format\n\
-p omit the user's plan file in long format\n\
-s do short format output, this is the default\n\
-f omit the line of column headings in short format\n\
-w omit the user's full name in short format\n\
-i omit the user's full name and remote host in short format\n\
-q omit the user's full name, remote host and idle time\n\
in short format\n\
--help display this help and exit\n\
--version output version information and exit\n\
\n\
1999-02-16 04:22:00 +00:00
A lightweight `finger' program; print user information.\n\
1997-08-03 01:54:34 +00:00
The utmp file will be %s.\n\
"), UTMP_FILE);
1998-09-19 17:32:35 +00:00
puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
1997-08-03 01:54:34 +00:00
}
exit (status);
}
int
main (int argc, char **argv)
1997-08-03 01:54:34 +00:00
{
int optc, longind;
program_name = argv[0];
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
1999-02-16 04:15:46 +00:00
while ((optc = getopt_long (argc, argv, "sfwiqbhlp", longopts, &longind))
!= -1)
1997-08-03 01:54:34 +00:00
{
switch (optc)
{
case 0:
break;
case 's':
do_short_format = 1;
break;
case 'l':
do_short_format = 0;
break;
case 'f':
include_heading = 0;
break;
case 'w':
include_fullname = 0;
break;
case 'i':
include_fullname = 0;
#ifdef HAVE_UT_HOST
include_where = 0;
#endif
break;
case 'q':
include_fullname = 0;
#ifdef HAVE_UT_HOST
include_where = 0;
#endif
include_idle = 0;
break;
case 'h':
include_project = 0;
break;
1997-08-03 02:04:28 +00:00
1997-08-03 01:54:34 +00:00
case 'p':
include_plan = 0;
break;
1997-08-03 02:04:28 +00:00
1997-08-03 01:54:34 +00:00
case 'b':
include_home_and_shell = 0;
break;
1997-08-03 02:04:28 +00:00
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
1997-08-03 01:54:34 +00:00
default:
usage (1);
}
}
if (do_short_format)
1997-08-03 02:04:28 +00:00
short_pinky (UTMP_FILE, argc - optind, argv + optind);
1997-08-03 01:54:34 +00:00
else
1997-08-03 02:04:28 +00:00
long_pinky (argc - optind, argv + optind);
1997-08-03 01:54:34 +00:00
exit (0);
}