* NEWS: Add a line for 6.5-cvs.
[coreutils.git] / src / pinky.c
blob885012b3dd447f067febaf53d5f5f6753ec8a5e6
1 /* GNU's pinky.
2 Copyright (C) 1992-1997, 1999-2006 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 2, or (at your option)
7 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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Created by hacking who.c by Kaveh Ghazi ghazi@caip.rutgers.edu */
20 #include <config.h>
21 #include <getopt.h>
22 #include <pwd.h>
23 #include <stdio.h>
25 #include <sys/types.h>
26 #include "system.h"
28 #include "canon-host.h"
29 #include "error.h"
30 #include "hard-locale.h"
31 #include "inttostr.h"
32 #include "readutmp.h"
34 /* The official name of this program (e.g., no `g' prefix). */
35 #define PROGRAM_NAME "pinky"
37 #define AUTHORS "Joseph Arceneaux", "David MacKenzie", "Kaveh Ghazi"
39 #ifndef MAXHOSTNAMELEN
40 # define MAXHOSTNAMELEN 64
41 #endif
43 char *ttyname ();
45 /* The name this program was run with. */
46 const char *program_name;
48 /* If true, display the hours:minutes since each user has touched
49 the keyboard, or blank if within the last minute, or days followed
50 by a 'd' if not within the last day. */
51 static bool include_idle = true;
53 /* If true, display a line at the top describing each field. */
54 static bool include_heading = true;
56 /* if true, display the user's full name from pw_gecos. */
57 static bool include_fullname = true;
59 /* if true, display the user's ~/.project file when doing long format. */
60 static bool include_project = true;
62 /* if true, display the user's ~/.plan file when doing long format. */
63 static bool include_plan = true;
65 /* if true, display the user's home directory and shell
66 when doing long format. */
67 static bool include_home_and_shell = true;
69 /* if true, use the "short" output format. */
70 static bool do_short_format = true;
72 /* if true, display the ut_host field. */
73 #ifdef HAVE_UT_HOST
74 static bool include_where = true;
75 #endif
77 /* The strftime format to use for login times, and its expected
78 output width. */
79 static char const *time_format;
80 static int time_format_width;
82 static struct option const longopts[] =
84 {GETOPT_HELP_OPTION_DECL},
85 {GETOPT_VERSION_OPTION_DECL},
86 {NULL, 0, NULL, 0}
89 /* Count and return the number of ampersands in STR. */
91 static size_t
92 count_ampersands (const char *str)
94 size_t count = 0;
97 if (*str == '&')
98 count++;
99 } while (*str++);
100 return count;
103 /* Create a string (via xmalloc) which contains a full name by substituting
104 for each ampersand in GECOS_NAME the USER_NAME string with its first
105 character capitalized. The caller must ensure that GECOS_NAME contains
106 no `,'s. The caller also is responsible for free'ing the return value of
107 this function. */
109 static char *
110 create_fullname (const char *gecos_name, const char *user_name)
112 size_t rsize = strlen (gecos_name) + 1;
113 char *result;
114 char *r;
115 size_t ampersands = count_ampersands (gecos_name);
117 if (ampersands != 0)
119 size_t ulen = strlen (user_name);
120 size_t product = ampersands * ulen;
121 rsize += product - ampersands;
122 if (xalloc_oversized (ulen, ampersands) || rsize < product)
123 xalloc_die ();
126 r = result = xmalloc (rsize);
128 while (*gecos_name)
130 if (*gecos_name == '&')
132 const char *uname = user_name;
133 if (islower (to_uchar (*uname)))
134 *r++ = toupper (to_uchar (*uname++));
135 while (*uname)
136 *r++ = *uname++;
138 else
140 *r++ = *gecos_name;
143 gecos_name++;
145 *r = 0;
147 return result;
150 /* Return a string representing the time between WHEN and the time
151 that this function is first run. */
153 static const char *
154 idle_string (time_t when)
156 static time_t now = 0;
157 static char buf[INT_STRLEN_BOUND (long int) + 2];
158 time_t seconds_idle;
160 if (now == 0)
161 time (&now);
163 seconds_idle = now - when;
164 if (seconds_idle < 60) /* One minute. */
165 return " ";
166 if (seconds_idle < (24 * 60 * 60)) /* One day. */
168 int hours = seconds_idle / (60 * 60);
169 int minutes = (seconds_idle % (60 * 60)) / 60;
170 sprintf (buf, "%02d:%02d", hours, minutes);
172 else
174 unsigned long int days = seconds_idle / (24 * 60 * 60);
175 sprintf (buf, "%lud", days);
177 return buf;
180 /* Return a time string. */
181 static const char *
182 time_string (const STRUCT_UTMP *utmp_ent)
184 static char buf[INT_STRLEN_BOUND (intmax_t) + sizeof "-%m-%d %H:%M"];
186 /* Don't take the address of UT_TIME_MEMBER directly.
187 Ulrich Drepper wrote:
188 ``... GNU libc (and perhaps other libcs as well) have extended
189 utmp file formats which do not use a simple time_t ut_time field.
190 In glibc, ut_time is a macro which selects for backward compatibility
191 the tv_sec member of a struct timeval value.'' */
192 time_t t = UT_TIME_MEMBER (utmp_ent);
193 struct tm *tmp = localtime (&t);
195 if (tmp)
197 strftime (buf, sizeof buf, time_format, tmp);
198 return buf;
200 else
201 return TYPE_SIGNED (time_t) ? imaxtostr (t, buf) : umaxtostr (t, buf);
204 /* Display a line of information about UTMP_ENT. */
206 static void
207 print_entry (const STRUCT_UTMP *utmp_ent)
209 struct stat stats;
210 time_t last_change;
211 char mesg;
213 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
214 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
216 char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
218 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
219 already an absolute file name. Some system may put the full,
220 absolute file name in ut_line. */
221 if (utmp_ent->ut_line[0] == '/')
223 strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
224 line[sizeof (utmp_ent->ut_line)] = '\0';
226 else
228 strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
229 strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
230 line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
233 if (stat (line, &stats) == 0)
235 mesg = (stats.st_mode & S_IWGRP) ? ' ' : '*';
236 last_change = stats.st_atime;
238 else
240 mesg = '?';
241 last_change = 0;
244 printf ("%-8.*s", UT_USER_SIZE, UT_USER (utmp_ent));
246 if (include_fullname)
248 struct passwd *pw;
249 char name[UT_USER_SIZE + 1];
251 strncpy (name, UT_USER (utmp_ent), UT_USER_SIZE);
252 name[UT_USER_SIZE] = '\0';
253 pw = getpwnam (name);
254 if (pw == NULL)
255 printf (" %19s", " ???");
256 else
258 char *const comma = strchr (pw->pw_gecos, ',');
259 char *result;
261 if (comma)
262 *comma = '\0';
264 result = create_fullname (pw->pw_gecos, pw->pw_name);
265 printf (" %-19.19s", result);
266 free (result);
270 printf (" %c%-8.*s",
271 mesg, (int) sizeof (utmp_ent->ut_line), utmp_ent->ut_line);
273 if (include_idle)
275 if (last_change)
276 printf (" %-6s", idle_string (last_change));
277 else
278 printf (" %-6s", "???");
281 printf (" %s", time_string (utmp_ent));
283 #ifdef HAVE_UT_HOST
284 if (include_where && utmp_ent->ut_host[0])
286 char ut_host[sizeof (utmp_ent->ut_host) + 1];
287 char *host = NULL;
288 char *display = NULL;
290 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
291 strncpy (ut_host, utmp_ent->ut_host, (int) sizeof (utmp_ent->ut_host));
292 ut_host[sizeof (utmp_ent->ut_host)] = '\0';
294 /* Look for an X display. */
295 display = strchr (ut_host, ':');
296 if (display)
297 *display++ = '\0';
299 if (*ut_host)
300 /* See if we can canonicalize it. */
301 host = canon_host (ut_host);
302 if ( ! host)
303 host = ut_host;
305 if (display)
306 printf (" %s:%s", host, display);
307 else
308 printf (" %s", host);
310 if (host != ut_host)
311 free (host);
313 #endif
315 putchar ('\n');
318 /* Display a verbose line of information about UTMP_ENT. */
320 static void
321 print_long_entry (const char name[])
323 struct passwd *pw;
325 pw = getpwnam (name);
327 printf (_("Login name: "));
328 printf ("%-28s", name);
330 printf (_("In real life: "));
331 if (pw == NULL)
333 printf (" %s", _("???\n"));
334 return;
336 else
338 char *const comma = strchr (pw->pw_gecos, ',');
339 char *result;
341 if (comma)
342 *comma = '\0';
344 result = create_fullname (pw->pw_gecos, pw->pw_name);
345 printf (" %s", result);
346 free (result);
349 putchar ('\n');
351 if (include_home_and_shell)
353 printf (_("Directory: "));
354 printf ("%-29s", pw->pw_dir);
355 printf (_("Shell: "));
356 printf (" %s", pw->pw_shell);
357 putchar ('\n');
360 if (include_project)
362 FILE *stream;
363 char buf[1024];
364 const char *const baseproject = "/.project";
365 char *const project =
366 xmalloc (strlen (pw->pw_dir) + strlen (baseproject) + 1);
368 strcpy (project, pw->pw_dir);
369 strcat (project, baseproject);
371 stream = fopen (project, "r");
372 if (stream)
374 size_t bytes;
376 printf (_("Project: "));
378 while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
379 fwrite (buf, 1, bytes, stdout);
380 fclose (stream);
383 free (project);
386 if (include_plan)
388 FILE *stream;
389 char buf[1024];
390 const char *const baseplan = "/.plan";
391 char *const plan =
392 xmalloc (strlen (pw->pw_dir) + strlen (baseplan) + 1);
394 strcpy (plan, pw->pw_dir);
395 strcat (plan, baseplan);
397 stream = fopen (plan, "r");
398 if (stream)
400 size_t bytes;
402 printf (_("Plan:\n"));
404 while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
405 fwrite (buf, 1, bytes, stdout);
406 fclose (stream);
409 free (plan);
412 putchar ('\n');
415 /* Print the username of each valid entry and the number of valid entries
416 in UTMP_BUF, which should have N elements. */
418 static void
419 print_heading (void)
421 printf ("%-8s", _("Login"));
422 if (include_fullname)
423 printf (" %-19s", _("Name"));
424 printf (" %-9s", _(" TTY"));
425 if (include_idle)
426 printf (" %-6s", _("Idle"));
427 printf (" %-*s", time_format_width, _("When"));
428 #ifdef HAVE_UT_HOST
429 if (include_where)
430 printf (" %s", _("Where"));
431 #endif
432 putchar ('\n');
435 /* Display UTMP_BUF, which should have N entries. */
437 static void
438 scan_entries (size_t n, const STRUCT_UTMP *utmp_buf,
439 const int argc_names, char *const argv_names[])
441 if (hard_locale (LC_TIME))
443 time_format = "%Y-%m-%d %H:%M";
444 time_format_width = 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2;
446 else
448 time_format = "%b %e %H:%M";
449 time_format_width = 3 + 1 + 2 + 1 + 2 + 1 + 2;
452 if (include_heading)
453 print_heading ();
455 while (n--)
457 if (IS_USER_PROCESS (utmp_buf))
459 if (argc_names)
461 int i;
463 for (i = 0; i < argc_names; i++)
464 if (strncmp (UT_USER (utmp_buf), argv_names[i], UT_USER_SIZE)
465 == 0)
467 print_entry (utmp_buf);
468 break;
471 else
472 print_entry (utmp_buf);
474 utmp_buf++;
478 /* Display a list of who is on the system, according to utmp file FILENAME. */
480 static void
481 short_pinky (const char *filename,
482 const int argc_names, char *const argv_names[])
484 size_t n_users;
485 STRUCT_UTMP *utmp_buf;
487 if (read_utmp (filename, &n_users, &utmp_buf, 0) != 0)
488 error (EXIT_FAILURE, errno, "%s", filename);
490 scan_entries (n_users, utmp_buf, argc_names, argv_names);
493 static void
494 long_pinky (const int argc_names, char *const argv_names[])
496 int i;
498 for (i = 0; i < argc_names; i++)
499 print_long_entry (argv_names[i]);
502 void
503 usage (int status)
505 if (status != EXIT_SUCCESS)
506 fprintf (stderr, _("Try `%s --help' for more information.\n"),
507 program_name);
508 else
510 printf (_("Usage: %s [OPTION]... [USER]...\n"), program_name);
511 fputs (_("\
513 -l produce long format output for the specified USERs\n\
514 -b omit the user's home directory and shell in long format\n\
515 -h omit the user's project file in long format\n\
516 -p omit the user's plan file in long format\n\
517 -s do short format output, this is the default\n\
518 "), stdout);
519 fputs (_("\
520 -f omit the line of column headings in short format\n\
521 -w omit the user's full name in short format\n\
522 -i omit the user's full name and remote host in short format\n\
523 -q omit the user's full name, remote host and idle time\n\
524 in short format\n\
525 "), stdout);
526 fputs (HELP_OPTION_DESCRIPTION, stdout);
527 fputs (VERSION_OPTION_DESCRIPTION, stdout);
528 printf (_("\
530 A lightweight `finger' program; print user information.\n\
531 The utmp file will be %s.\n\
532 "), UTMP_FILE);
533 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
535 exit (status);
539 main (int argc, char **argv)
541 int optc;
542 int n_users;
544 initialize_main (&argc, &argv);
545 program_name = argv[0];
546 setlocale (LC_ALL, "");
547 bindtextdomain (PACKAGE, LOCALEDIR);
548 textdomain (PACKAGE);
550 atexit (close_stdout);
552 while ((optc = getopt_long (argc, argv, "sfwiqbhlp", longopts, NULL)) != -1)
554 switch (optc)
556 case 's':
557 do_short_format = true;
558 break;
560 case 'l':
561 do_short_format = false;
562 break;
564 case 'f':
565 include_heading = false;
566 break;
568 case 'w':
569 include_fullname = false;
570 break;
572 case 'i':
573 include_fullname = false;
574 #ifdef HAVE_UT_HOST
575 include_where = false;
576 #endif
577 break;
579 case 'q':
580 include_fullname = false;
581 #ifdef HAVE_UT_HOST
582 include_where = false;
583 #endif
584 include_idle = false;
585 break;
587 case 'h':
588 include_project = false;
589 break;
591 case 'p':
592 include_plan = false;
593 break;
595 case 'b':
596 include_home_and_shell = false;
597 break;
599 case_GETOPT_HELP_CHAR;
601 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
603 default:
604 usage (EXIT_FAILURE);
608 n_users = argc - optind;
610 if (!do_short_format && n_users == 0)
612 error (0, 0, _("no username specified; at least one must be\
613 specified when using -l"));
614 usage (EXIT_FAILURE);
617 if (do_short_format)
618 short_pinky (UTMP_FILE, n_users, argv + optind);
619 else
620 long_pinky (n_users, argv + optind);
622 exit (EXIT_SUCCESS);