all: update gnulib submodule to latest
[coreutils.git] / src / pinky.c
blob44dae9afddc1889ebb68f57a5d63d5872e7a4e4f
1 /* GNU's pinky.
2 Copyright (C) 1992-2017 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 <http://www.gnu.org/licenses/>. */
17 /* Created by hacking who.c by Kaveh Ghazi ghazi@caip.rutgers.edu */
19 #include <config.h>
20 #include <getopt.h>
21 #include <pwd.h>
22 #include <stdio.h>
24 #include <sys/types.h>
25 #include "system.h"
27 #include "canon-host.h"
28 #include "die.h"
29 #include "error.h"
30 #include "hard-locale.h"
31 #include "readutmp.h"
33 /* The official name of this program (e.g., no 'g' prefix). */
34 #define PROGRAM_NAME "pinky"
36 #define AUTHORS \
37 proper_name ("Joseph Arceneaux"), \
38 proper_name ("David MacKenzie"), \
39 proper_name ("Kaveh Ghazi")
41 /* If true, display the hours:minutes since each user has touched
42 the keyboard, or blank if within the last minute, or days followed
43 by a 'd' if not within the last day. */
44 static bool include_idle = true;
46 /* If true, display a line at the top describing each field. */
47 static bool include_heading = true;
49 /* if true, display the user's full name from pw_gecos. */
50 static bool include_fullname = true;
52 /* if true, display the user's ~/.project file when doing long format. */
53 static bool include_project = true;
55 /* if true, display the user's ~/.plan file when doing long format. */
56 static bool include_plan = true;
58 /* if true, display the user's home directory and shell
59 when doing long format. */
60 static bool include_home_and_shell = true;
62 /* if true, use the "short" output format. */
63 static bool do_short_format = true;
65 /* if true, display the ut_host field. */
66 #ifdef HAVE_UT_HOST
67 static bool include_where = true;
68 #endif
70 /* The strftime format to use for login times, and its expected
71 output width. */
72 static char const *time_format;
73 static int time_format_width;
75 static struct option const longopts[] =
77 {GETOPT_HELP_OPTION_DECL},
78 {GETOPT_VERSION_OPTION_DECL},
79 {NULL, 0, NULL, 0}
82 /* Count and return the number of ampersands in STR. */
84 static size_t _GL_ATTRIBUTE_PURE
85 count_ampersands (const char *str)
87 size_t count = 0;
90 if (*str == '&')
91 count++;
92 } while (*str++);
93 return count;
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
100 this function. */
102 static char *
103 create_fullname (const char *gecos_name, const char *user_name)
105 size_t rsize = strlen (gecos_name) + 1;
106 char *result;
107 char *r;
108 size_t ampersands = count_ampersands (gecos_name);
110 if (ampersands != 0)
112 size_t ulen = strlen (user_name);
113 size_t product = ampersands * ulen;
114 rsize += product - ampersands;
115 if (xalloc_oversized (ulen, ampersands) || rsize < product)
116 xalloc_die ();
119 r = result = xmalloc (rsize);
121 while (*gecos_name)
123 if (*gecos_name == '&')
125 const char *uname = user_name;
126 if (islower (to_uchar (*uname)))
127 *r++ = toupper (to_uchar (*uname++));
128 while (*uname)
129 *r++ = *uname++;
131 else
133 *r++ = *gecos_name;
136 gecos_name++;
138 *r = 0;
140 return result;
143 /* Return a string representing the time between WHEN and the time
144 that this function is first run. */
146 static const char *
147 idle_string (time_t when)
149 static time_t now = 0;
150 static char buf[INT_STRLEN_BOUND (long int) + 2];
151 time_t seconds_idle;
153 if (now == 0)
154 time (&now);
156 seconds_idle = now - when;
157 if (seconds_idle < 60) /* One minute. */
158 return " ";
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);
165 else
167 unsigned long int days = seconds_idle / (24 * 60 * 60);
168 sprintf (buf, "%lud", days);
170 return buf;
173 /* Return a time string. */
174 static const char *
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);
188 if (tmp)
190 strftime (buf, sizeof buf, time_format, tmp);
191 return buf;
193 else
194 return timetostr (t, buf);
197 /* Display a line of information about UTMP_ENT. */
199 static void
200 print_entry (const STRUCT_UTMP *utmp_ent)
202 struct stat stats;
203 time_t last_change;
204 char mesg;
206 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
207 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
209 char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
210 char *p = line;
212 /* Copy ut_line into LINE, prepending '/dev/' if ut_line is not
213 already an absolute file name. Some system may put the full,
214 absolute file name in ut_line. */
215 if ( ! IS_ABSOLUTE_FILE_NAME (utmp_ent->ut_line))
216 p = stpcpy (p, DEV_DIR_WITH_TRAILING_SLASH);
217 stzncpy (p, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
219 if (stat (line, &stats) == 0)
221 mesg = (stats.st_mode & S_IWGRP) ? ' ' : '*';
222 last_change = stats.st_atime;
224 else
226 mesg = '?';
227 last_change = 0;
230 printf ("%-8.*s", UT_USER_SIZE, UT_USER (utmp_ent));
232 if (include_fullname)
234 struct passwd *pw;
235 char name[UT_USER_SIZE + 1];
237 stzncpy (name, UT_USER (utmp_ent), UT_USER_SIZE);
238 pw = getpwnam (name);
239 if (pw == NULL)
240 /* TRANSLATORS: Real name is unknown; at most 19 characters. */
241 printf (" %19s", _(" ???"));
242 else
244 char *const comma = strchr (pw->pw_gecos, ',');
245 char *result;
247 if (comma)
248 *comma = '\0';
250 result = create_fullname (pw->pw_gecos, pw->pw_name);
251 printf (" %-19.19s", result);
252 free (result);
256 printf (" %c%-8.*s",
257 mesg, (int) sizeof (utmp_ent->ut_line), utmp_ent->ut_line);
259 if (include_idle)
261 if (last_change)
262 printf (" %-6s", idle_string (last_change));
263 else
264 /* TRANSLATORS: Idle time is unknown; at most 5 characters. */
265 printf (" %-6s", _("?????"));
268 printf (" %s", time_string (utmp_ent));
270 #ifdef HAVE_UT_HOST
271 if (include_where && utmp_ent->ut_host[0])
273 char ut_host[sizeof (utmp_ent->ut_host) + 1];
274 char *host = NULL;
275 char *display = NULL;
277 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
278 stzncpy (ut_host, utmp_ent->ut_host, sizeof (utmp_ent->ut_host));
280 /* Look for an X display. */
281 display = strchr (ut_host, ':');
282 if (display)
283 *display++ = '\0';
285 if (*ut_host)
286 /* See if we can canonicalize it. */
287 host = canon_host (ut_host);
288 if ( ! host)
289 host = ut_host;
291 if (display)
292 printf (" %s:%s", host, display);
293 else
294 printf (" %s", host);
296 if (host != ut_host)
297 free (host);
299 #endif
301 putchar ('\n');
304 /* Display a verbose line of information about UTMP_ENT. */
306 static void
307 print_long_entry (const char name[])
309 struct passwd *pw;
311 pw = getpwnam (name);
313 printf (_("Login name: "));
314 printf ("%-28s", name);
316 printf (_("In real life: "));
317 if (pw == NULL)
319 /* TRANSLATORS: Real name is unknown; no hard limit. */
320 printf (" %s", _("???\n"));
321 return;
323 else
325 char *const comma = strchr (pw->pw_gecos, ',');
326 char *result;
328 if (comma)
329 *comma = '\0';
331 result = create_fullname (pw->pw_gecos, pw->pw_name);
332 printf (" %s", result);
333 free (result);
336 putchar ('\n');
338 if (include_home_and_shell)
340 printf (_("Directory: "));
341 printf ("%-29s", pw->pw_dir);
342 printf (_("Shell: "));
343 printf (" %s", pw->pw_shell);
344 putchar ('\n');
347 if (include_project)
349 FILE *stream;
350 char buf[1024];
351 const char *const baseproject = "/.project";
352 char *const project =
353 xmalloc (strlen (pw->pw_dir) + strlen (baseproject) + 1);
354 stpcpy (stpcpy (project, pw->pw_dir), baseproject);
356 stream = fopen (project, "r");
357 if (stream)
359 size_t bytes;
361 printf (_("Project: "));
363 while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
364 fwrite (buf, 1, bytes, stdout);
365 fclose (stream);
368 free (project);
371 if (include_plan)
373 FILE *stream;
374 char buf[1024];
375 const char *const baseplan = "/.plan";
376 char *const plan =
377 xmalloc (strlen (pw->pw_dir) + strlen (baseplan) + 1);
378 stpcpy (stpcpy (plan, pw->pw_dir), baseplan);
380 stream = fopen (plan, "r");
381 if (stream)
383 size_t bytes;
385 printf (_("Plan:\n"));
387 while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
388 fwrite (buf, 1, bytes, stdout);
389 fclose (stream);
392 free (plan);
395 putchar ('\n');
398 /* Print the username of each valid entry and the number of valid entries
399 in UTMP_BUF, which should have N elements. */
401 static void
402 print_heading (void)
404 printf ("%-8s", _("Login"));
405 if (include_fullname)
406 printf (" %-19s", _("Name"));
407 printf (" %-9s", _(" TTY"));
408 if (include_idle)
409 printf (" %-6s", _("Idle"));
410 printf (" %-*s", time_format_width, _("When"));
411 #ifdef HAVE_UT_HOST
412 if (include_where)
413 printf (" %s", _("Where"));
414 #endif
415 putchar ('\n');
418 /* Display UTMP_BUF, which should have N entries. */
420 static void
421 scan_entries (size_t n, const STRUCT_UTMP *utmp_buf,
422 const int argc_names, char *const argv_names[])
424 if (hard_locale (LC_TIME))
426 time_format = "%Y-%m-%d %H:%M";
427 time_format_width = 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2;
429 else
431 time_format = "%b %e %H:%M";
432 time_format_width = 3 + 1 + 2 + 1 + 2 + 1 + 2;
435 if (include_heading)
436 print_heading ();
438 while (n--)
440 if (IS_USER_PROCESS (utmp_buf))
442 if (argc_names)
444 for (int i = 0; i < argc_names; i++)
445 if (STREQ_LEN (UT_USER (utmp_buf), argv_names[i], UT_USER_SIZE))
447 print_entry (utmp_buf);
448 break;
451 else
452 print_entry (utmp_buf);
454 utmp_buf++;
458 /* Display a list of who is on the system, according to utmp file FILENAME. */
460 static void
461 short_pinky (const char *filename,
462 const int argc_names, char *const argv_names[])
464 size_t n_users;
465 STRUCT_UTMP *utmp_buf = NULL;
467 if (read_utmp (filename, &n_users, &utmp_buf, 0) != 0)
468 die (EXIT_FAILURE, errno, "%s", quotef (filename));
470 scan_entries (n_users, utmp_buf, argc_names, argv_names);
472 IF_LINT (free (utmp_buf));
475 static void
476 long_pinky (const int argc_names, char *const argv_names[])
478 for (int i = 0; i < argc_names; i++)
479 print_long_entry (argv_names[i]);
482 void
483 usage (int status)
485 if (status != EXIT_SUCCESS)
486 emit_try_help ();
487 else
489 printf (_("Usage: %s [OPTION]... [USER]...\n"), program_name);
490 fputs (_("\
492 -l produce long format output for the specified USERs\n\
493 -b omit the user's home directory and shell in long format\n\
494 -h omit the user's project file in long format\n\
495 -p omit the user's plan file in long format\n\
496 -s do short format output, this is the default\n\
497 "), stdout);
498 fputs (_("\
499 -f omit the line of column headings in short format\n\
500 -w omit the user's full name in short format\n\
501 -i omit the user's full name and remote host in short format\n\
502 -q omit the user's full name, remote host and idle time\n\
503 in short format\n\
504 "), stdout);
505 fputs (HELP_OPTION_DESCRIPTION, stdout);
506 fputs (VERSION_OPTION_DESCRIPTION, stdout);
507 printf (_("\
509 A lightweight 'finger' program; print user information.\n\
510 The utmp file will be %s.\n\
511 "), UTMP_FILE);
512 emit_ancillary_info (PROGRAM_NAME);
514 exit (status);
518 main (int argc, char **argv)
520 int optc;
521 int n_users;
523 initialize_main (&argc, &argv);
524 set_program_name (argv[0]);
525 setlocale (LC_ALL, "");
526 bindtextdomain (PACKAGE, LOCALEDIR);
527 textdomain (PACKAGE);
529 atexit (close_stdout);
531 while ((optc = getopt_long (argc, argv, "sfwiqbhlp", longopts, NULL)) != -1)
533 switch (optc)
535 case 's':
536 do_short_format = true;
537 break;
539 case 'l':
540 do_short_format = false;
541 break;
543 case 'f':
544 include_heading = false;
545 break;
547 case 'w':
548 include_fullname = false;
549 break;
551 case 'i':
552 include_fullname = false;
553 #ifdef HAVE_UT_HOST
554 include_where = false;
555 #endif
556 break;
558 case 'q':
559 include_fullname = false;
560 #ifdef HAVE_UT_HOST
561 include_where = false;
562 #endif
563 include_idle = false;
564 break;
566 case 'h':
567 include_project = false;
568 break;
570 case 'p':
571 include_plan = false;
572 break;
574 case 'b':
575 include_home_and_shell = false;
576 break;
578 case_GETOPT_HELP_CHAR;
580 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
582 default:
583 usage (EXIT_FAILURE);
587 n_users = argc - optind;
589 if (!do_short_format && n_users == 0)
591 error (0, 0, _("no username specified; at least one must be\
592 specified when using -l"));
593 usage (EXIT_FAILURE);
596 if (do_short_format)
597 short_pinky (UTMP_FILE, n_users, argv + optind);
598 else
599 long_pinky (n_users, argv + optind);
601 return EXIT_SUCCESS;