summaryrefslogtreecommitdiff
path: root/src/pinky.c
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@lorry>2016-01-20 10:55:18 +0000
committerLorry Tar Creator <lorry-tar-importer@lorry>2016-01-20 10:55:18 +0000
commit70e9163c9c18e995515598085cb824e554eb7ae7 (patch)
treea42dc8b2a6c031354bf31472de888bfc8a060132 /src/pinky.c
parentcbf5993c43f49281173f185863577d86bfac6eae (diff)
downloadcoreutils-tarball-master.tar.gz
Diffstat (limited to 'src/pinky.c')
-rw-r--r--src/pinky.c294
1 files changed, 139 insertions, 155 deletions
diff --git a/src/pinky.c b/src/pinky.c
index 885012b..68566fd 100644
--- a/src/pinky.c
+++ b/src/pinky.c
@@ -1,10 +1,10 @@
/* GNU's pinky.
- Copyright (C) 1992-1997, 1999-2006 Free Software Foundation, Inc.
+ Copyright (C) 1992-2016 Free Software Foundation, Inc.
- This program is free software; you can redistribute it and/or modify
+ 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.
+ 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
@@ -12,8 +12,7 @@
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* Created by hacking who.c by Kaveh Ghazi ghazi@caip.rutgers.edu */
@@ -28,22 +27,17 @@
#include "canon-host.h"
#include "error.h"
#include "hard-locale.h"
-#include "inttostr.h"
#include "readutmp.h"
-/* The official name of this program (e.g., no `g' prefix). */
+/* The official name of this program (e.g., no 'g' prefix). */
#define PROGRAM_NAME "pinky"
-#define AUTHORS "Joseph Arceneaux", "David MacKenzie", "Kaveh Ghazi"
+#define AUTHORS \
+ proper_name ("Joseph Arceneaux"), \
+ proper_name ("David MacKenzie"), \
+ proper_name ("Kaveh Ghazi")
-#ifndef MAXHOSTNAMELEN
-# define MAXHOSTNAMELEN 64
-#endif
-
-char *ttyname ();
-
-/* The name this program was run with. */
-const char *program_name;
+char *ttyname (int);
/* If true, display the hours:minutes since each user has touched
the keyboard, or blank if within the last minute, or days followed
@@ -88,14 +82,14 @@ static struct option const longopts[] =
/* Count and return the number of ampersands in STR. */
-static size_t
+static size_t _GL_ATTRIBUTE_PURE
count_ampersands (const char *str)
{
size_t count = 0;
do
{
if (*str == '&')
- count++;
+ count++;
} while (*str++);
return count;
}
@@ -103,7 +97,7 @@ count_ampersands (const char *str)
/* 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
+ no ','s. The caller also is responsible for free'ing the return value of
this function. */
static char *
@@ -120,7 +114,7 @@ create_fullname (const char *gecos_name, const char *user_name)
size_t product = ampersands * ulen;
rsize += product - ampersands;
if (xalloc_oversized (ulen, ampersands) || rsize < product)
- xalloc_die ();
+ xalloc_die ();
}
r = result = xmalloc (rsize);
@@ -128,17 +122,17 @@ create_fullname (const char *gecos_name, const char *user_name)
while (*gecos_name)
{
if (*gecos_name == '&')
- {
- const char *uname = user_name;
- if (islower (to_uchar (*uname)))
- *r++ = toupper (to_uchar (*uname++));
- while (*uname)
- *r++ = *uname++;
- }
+ {
+ const char *uname = user_name;
+ if (islower (to_uchar (*uname)))
+ *r++ = toupper (to_uchar (*uname++));
+ while (*uname)
+ *r++ = *uname++;
+ }
else
- {
- *r++ = *gecos_name;
- }
+ {
+ *r++ = *gecos_name;
+ }
gecos_name++;
}
@@ -185,10 +179,10 @@ time_string (const STRUCT_UTMP *utmp_ent)
/* Don't take the address of UT_TIME_MEMBER directly.
Ulrich Drepper wrote:
- ``... GNU libc (and perhaps other libcs as well) have extended
+ "... 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.'' */
+ the tv_sec member of a struct timeval value." */
time_t t = UT_TIME_MEMBER (utmp_ent);
struct tm *tmp = localtime (&t);
@@ -198,7 +192,7 @@ time_string (const STRUCT_UTMP *utmp_ent)
return buf;
}
else
- return TYPE_SIGNED (time_t) ? imaxtostr (t, buf) : umaxtostr (t, buf);
+ return timetostr (t, buf);
}
/* Display a line of information about UTMP_ENT. */
@@ -214,21 +208,14 @@ print_entry (const STRUCT_UTMP *utmp_ent)
#define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
+ char *p = line;
- /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
+ /* 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 (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 ( ! IS_ABSOLUTE_FILE_NAME (utmp_ent->ut_line))
+ p = stpcpy (p, DEV_DIR_WITH_TRAILING_SLASH);
+ stzncpy (p, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
if (stat (line, &stats) == 0)
{
@@ -248,34 +235,35 @@ print_entry (const STRUCT_UTMP *utmp_ent)
struct passwd *pw;
char name[UT_USER_SIZE + 1];
- strncpy (name, UT_USER (utmp_ent), UT_USER_SIZE);
- name[UT_USER_SIZE] = '\0';
+ stzncpy (name, UT_USER (utmp_ent), UT_USER_SIZE);
pw = getpwnam (name);
if (pw == NULL)
- printf (" %19s", " ???");
+ /* TRANSLATORS: Real name is unknown; at most 19 characters. */
+ printf (" %19s", _(" ???"));
else
- {
- char *const comma = strchr (pw->pw_gecos, ',');
- char *result;
+ {
+ char *const comma = strchr (pw->pw_gecos, ',');
+ char *result;
- if (comma)
- *comma = '\0';
+ if (comma)
+ *comma = '\0';
- result = create_fullname (pw->pw_gecos, pw->pw_name);
- printf (" %-19.19s", result);
- free (result);
- }
+ result = create_fullname (pw->pw_gecos, pw->pw_name);
+ printf (" %-19.19s", result);
+ free (result);
+ }
}
printf (" %c%-8.*s",
- mesg, (int) sizeof (utmp_ent->ut_line), utmp_ent->ut_line);
+ mesg, (int) sizeof (utmp_ent->ut_line), utmp_ent->ut_line);
if (include_idle)
{
if (last_change)
- printf (" %-6s", idle_string (last_change));
+ printf (" %-6s", idle_string (last_change));
else
- printf (" %-6s", "???");
+ /* TRANSLATORS: Idle time is unknown; at most 5 characters. */
+ printf (" %-6s", _("?????"));
}
printf (" %s", time_string (utmp_ent));
@@ -288,27 +276,26 @@ print_entry (const STRUCT_UTMP *utmp_ent)
char *display = NULL;
/* 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';
+ stzncpy (ut_host, utmp_ent->ut_host, sizeof (utmp_ent->ut_host));
/* Look for an X display. */
display = strchr (ut_host, ':');
if (display)
- *display++ = '\0';
+ *display++ = '\0';
if (*ut_host)
- /* See if we can canonicalize it. */
- host = canon_host (ut_host);
+ /* See if we can canonicalize it. */
+ host = canon_host (ut_host);
if ( ! host)
- host = ut_host;
+ host = ut_host;
if (display)
- printf (" %s:%s", host, display);
+ printf (" %s:%s", host, display);
else
- printf (" %s", host);
+ printf (" %s", host);
if (host != ut_host)
- free (host);
+ free (host);
}
#endif
@@ -330,6 +317,7 @@ print_long_entry (const char name[])
printf (_("In real life: "));
if (pw == NULL)
{
+ /* TRANSLATORS: Real name is unknown; no hard limit. */
printf (" %s", _("???\n"));
return;
}
@@ -339,7 +327,7 @@ print_long_entry (const char name[])
char *result;
if (comma)
- *comma = '\0';
+ *comma = '\0';
result = create_fullname (pw->pw_gecos, pw->pw_name);
printf (" %s", result);
@@ -363,22 +351,20 @@ print_long_entry (const char name[])
char buf[1024];
const char *const baseproject = "/.project";
char *const project =
- xmalloc (strlen (pw->pw_dir) + strlen (baseproject) + 1);
-
- strcpy (project, pw->pw_dir);
- strcat (project, baseproject);
+ xmalloc (strlen (pw->pw_dir) + strlen (baseproject) + 1);
+ stpcpy (stpcpy (project, pw->pw_dir), baseproject);
stream = fopen (project, "r");
if (stream)
- {
- size_t bytes;
+ {
+ size_t bytes;
- printf (_("Project: "));
+ printf (_("Project: "));
- while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
- fwrite (buf, 1, bytes, stdout);
- fclose (stream);
- }
+ while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
+ fwrite (buf, 1, bytes, stdout);
+ fclose (stream);
+ }
free (project);
}
@@ -389,22 +375,20 @@ print_long_entry (const char name[])
char buf[1024];
const char *const baseplan = "/.plan";
char *const plan =
- xmalloc (strlen (pw->pw_dir) + strlen (baseplan) + 1);
-
- strcpy (plan, pw->pw_dir);
- strcat (plan, baseplan);
+ xmalloc (strlen (pw->pw_dir) + strlen (baseplan) + 1);
+ stpcpy (stpcpy (plan, pw->pw_dir), baseplan);
stream = fopen (plan, "r");
if (stream)
- {
- size_t bytes;
+ {
+ size_t bytes;
- printf (_("Plan:\n"));
+ printf (_("Plan:\n"));
- while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
- fwrite (buf, 1, bytes, stdout);
- fclose (stream);
- }
+ while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
+ fwrite (buf, 1, bytes, stdout);
+ fclose (stream);
+ }
free (plan);
}
@@ -436,7 +420,7 @@ print_heading (void)
static void
scan_entries (size_t n, const STRUCT_UTMP *utmp_buf,
- const int argc_names, char *const argv_names[])
+ const int argc_names, char *const argv_names[])
{
if (hard_locale (LC_TIME))
{
@@ -455,22 +439,21 @@ scan_entries (size_t n, const STRUCT_UTMP *utmp_buf,
while (n--)
{
if (IS_USER_PROCESS (utmp_buf))
- {
- if (argc_names)
- {
- int i;
-
- for (i = 0; i < argc_names; i++)
- if (strncmp (UT_USER (utmp_buf), argv_names[i], UT_USER_SIZE)
- == 0)
- {
- print_entry (utmp_buf);
- break;
- }
- }
- else
- print_entry (utmp_buf);
- }
+ {
+ if (argc_names)
+ {
+ int i;
+
+ for (i = 0; i < argc_names; i++)
+ if (STREQ_LEN (UT_USER (utmp_buf), argv_names[i], UT_USER_SIZE))
+ {
+ print_entry (utmp_buf);
+ break;
+ }
+ }
+ else
+ print_entry (utmp_buf);
+ }
utmp_buf++;
}
}
@@ -479,15 +462,17 @@ scan_entries (size_t n, const STRUCT_UTMP *utmp_buf,
static void
short_pinky (const char *filename,
- const int argc_names, char *const argv_names[])
+ const int argc_names, char *const argv_names[])
{
size_t n_users;
- STRUCT_UTMP *utmp_buf;
+ STRUCT_UTMP *utmp_buf = NULL;
if (read_utmp (filename, &n_users, &utmp_buf, 0) != 0)
- error (EXIT_FAILURE, errno, "%s", filename);
+ error (EXIT_FAILURE, errno, "%s", quotef (filename));
scan_entries (n_users, utmp_buf, argc_names, argv_names);
+
+ IF_LINT (free (utmp_buf));
}
static void
@@ -503,8 +488,7 @@ void
usage (int status)
{
if (status != EXIT_SUCCESS)
- fprintf (stderr, _("Try `%s --help' for more information.\n"),
- program_name);
+ emit_try_help ();
else
{
printf (_("Usage: %s [OPTION]... [USER]...\n"), program_name);
@@ -527,10 +511,10 @@ usage (int status)
fputs (VERSION_OPTION_DESCRIPTION, stdout);
printf (_("\
\n\
-A lightweight `finger' program; print user information.\n\
+A lightweight 'finger' program; print user information.\n\
The utmp file will be %s.\n\
"), UTMP_FILE);
- printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
+ emit_ancillary_info (PROGRAM_NAME);
}
exit (status);
}
@@ -542,7 +526,7 @@ main (int argc, char **argv)
int n_users;
initialize_main (&argc, &argv);
- program_name = argv[0];
+ set_program_name (argv[0]);
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
@@ -552,57 +536,57 @@ main (int argc, char **argv)
while ((optc = getopt_long (argc, argv, "sfwiqbhlp", longopts, NULL)) != -1)
{
switch (optc)
- {
- case 's':
- do_short_format = true;
- break;
+ {
+ case 's':
+ do_short_format = true;
+ break;
- case 'l':
- do_short_format = false;
- break;
+ case 'l':
+ do_short_format = false;
+ break;
- case 'f':
- include_heading = false;
- break;
+ case 'f':
+ include_heading = false;
+ break;
- case 'w':
- include_fullname = false;
- break;
+ case 'w':
+ include_fullname = false;
+ break;
- case 'i':
- include_fullname = false;
+ case 'i':
+ include_fullname = false;
#ifdef HAVE_UT_HOST
- include_where = false;
+ include_where = false;
#endif
- break;
+ break;
- case 'q':
- include_fullname = false;
+ case 'q':
+ include_fullname = false;
#ifdef HAVE_UT_HOST
- include_where = false;
+ include_where = false;
#endif
- include_idle = false;
- break;
+ include_idle = false;
+ break;
- case 'h':
- include_project = false;
- break;
+ case 'h':
+ include_project = false;
+ break;
- case 'p':
- include_plan = false;
- break;
+ case 'p':
+ include_plan = false;
+ break;
- case 'b':
- include_home_and_shell = false;
- break;
+ case 'b':
+ include_home_and_shell = false;
+ break;
- case_GETOPT_HELP_CHAR;
+ case_GETOPT_HELP_CHAR;
- case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
+ case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
- default:
- usage (EXIT_FAILURE);
- }
+ default:
+ usage (EXIT_FAILURE);
+ }
}
n_users = argc - optind;
@@ -619,5 +603,5 @@ main (int argc, char **argv)
else
long_pinky (n_users, argv + optind);
- exit (EXIT_SUCCESS);
+ return EXIT_SUCCESS;
}