2 Copyright (C) 1992-1997, 1999-2004 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)
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Created by hacking who.c by Kaveh Ghazi ghazi@caip.rutgers.edu */
25 #include <sys/types.h>
29 #include "hard-locale.h"
33 /* The official name of this program (e.g., no `g' prefix). */
34 #define PROGRAM_NAME "pinky"
36 #define AUTHORS "Joseph Arceneaux", "David MacKenzie", "Kaveh Ghazi"
38 #ifndef MAXHOSTNAMELEN
39 # define MAXHOSTNAMELEN 64
48 /* The name this program was run with. */
49 const char *program_name
;
51 /* If true, display the hours:minutes since each user has touched
52 the keyboard, or blank if within the last minute, or days followed
53 by a 'd' if not within the last day. */
54 static bool include_idle
= true;
56 /* If true, display a line at the top describing each field. */
57 static bool include_heading
= true;
59 /* if true, display the user's full name from pw_gecos. */
60 static bool include_fullname
= true;
62 /* if true, display the user's ~/.project file when doing long format. */
63 static bool include_project
= true;
65 /* if true, display the user's ~/.plan file when doing long format. */
66 static bool include_plan
= true;
68 /* if true, display the user's home directory and shell
69 when doing long format. */
70 static bool include_home_and_shell
= true;
72 /* if true, use the "short" output format. */
73 static bool do_short_format
= true;
75 /* if true, display the ut_host field. */
77 static bool include_where
= true;
80 /* The strftime format to use for login times, and its expected
82 static char const *time_format
;
83 static int time_format_width
;
85 static struct option
const longopts
[] =
87 {GETOPT_HELP_OPTION_DECL
},
88 {GETOPT_VERSION_OPTION_DECL
},
92 /* Count and return the number of ampersands in STR. */
95 count_ampersands (const char *str
)
106 /* Create a string (via xmalloc) which contains a full name by substituting
107 for each ampersand in GECOS_NAME the USER_NAME string with its first
108 character capitalized. The caller must ensure that GECOS_NAME contains
109 no `,'s. The caller also is responsible for free'ing the return value of
113 create_fullname (const char *gecos_name
, const char *user_name
)
115 size_t rsize
= strlen (gecos_name
) + 1;
118 size_t ampersands
= count_ampersands (gecos_name
);
122 size_t ulen
= strlen (user_name
);
123 size_t product
= ampersands
* ulen
;
124 rsize
+= product
- ampersands
;
125 if (xalloc_oversized (ulen
, ampersands
) || rsize
< product
)
129 r
= result
= xmalloc (rsize
);
133 if (*gecos_name
== '&')
135 const char *uname
= user_name
;
136 if (ISLOWER (*uname
))
137 *r
++ = TOUPPER (*uname
++);
153 /* Return a string representing the time between WHEN and the time
154 that this function is first run. */
157 idle_string (time_t when
)
159 static time_t now
= 0;
160 static char buf
[INT_STRLEN_BOUND (long int) + 2];
166 seconds_idle
= now
- when
;
167 if (seconds_idle
< 60) /* One minute. */
169 if (seconds_idle
< (24 * 60 * 60)) /* One day. */
171 int hours
= seconds_idle
/ (60 * 60);
172 int minutes
= (seconds_idle
% (60 * 60)) / 60;
173 sprintf (buf
, "%02d:%02d", hours
, minutes
);
177 unsigned long int days
= seconds_idle
/ (24 * 60 * 60);
178 sprintf (buf
, "%lud", days
);
183 /* Return a time string. */
185 time_string (const STRUCT_UTMP
*utmp_ent
)
187 static char buf
[INT_STRLEN_BOUND (intmax_t) + sizeof "-%m-%d %H:%M"];
189 /* Don't take the address of UT_TIME_MEMBER directly.
190 Ulrich Drepper wrote:
191 ``... GNU libc (and perhaps other libcs as well) have extended
192 utmp file formats which do not use a simple time_t ut_time field.
193 In glibc, ut_time is a macro which selects for backward compatibility
194 the tv_sec member of a struct timeval value.'' */
195 time_t t
= UT_TIME_MEMBER (utmp_ent
);
196 struct tm
*tmp
= localtime (&t
);
200 strftime (buf
, sizeof buf
, time_format
, tmp
);
204 return TYPE_SIGNED (time_t) ? imaxtostr (t
, buf
) : umaxtostr (t
, buf
);
207 /* Display a line of information about UTMP_ENT. */
210 print_entry (const STRUCT_UTMP
*utmp_ent
)
216 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
217 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
219 char line
[sizeof (utmp_ent
->ut_line
) + DEV_DIR_LEN
+ 1];
221 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
222 already an absolute pathname. Some system may put the full,
223 absolute pathname in ut_line. */
224 if (utmp_ent
->ut_line
[0] == '/')
226 strncpy (line
, utmp_ent
->ut_line
, sizeof (utmp_ent
->ut_line
));
227 line
[sizeof (utmp_ent
->ut_line
)] = '\0';
231 strcpy (line
, DEV_DIR_WITH_TRAILING_SLASH
);
232 strncpy (line
+ DEV_DIR_LEN
, utmp_ent
->ut_line
, sizeof (utmp_ent
->ut_line
));
233 line
[DEV_DIR_LEN
+ sizeof (utmp_ent
->ut_line
)] = '\0';
236 if (stat (line
, &stats
) == 0)
238 mesg
= (stats
.st_mode
& S_IWGRP
) ? ' ' : '*';
239 last_change
= stats
.st_atime
;
247 printf ("%-8.*s", UT_USER_SIZE
, UT_USER (utmp_ent
));
249 if (include_fullname
)
252 char name
[UT_USER_SIZE
+ 1];
254 strncpy (name
, UT_USER (utmp_ent
), UT_USER_SIZE
);
255 name
[UT_USER_SIZE
] = '\0';
256 pw
= getpwnam (name
);
258 printf (" %19s", " ???");
261 char *const comma
= strchr (pw
->pw_gecos
, ',');
267 result
= create_fullname (pw
->pw_gecos
, pw
->pw_name
);
268 printf (" %-19.19s", result
);
274 mesg
, (int) sizeof (utmp_ent
->ut_line
), utmp_ent
->ut_line
);
279 printf (" %-6s", idle_string (last_change
));
281 printf (" %-6s", "???");
284 printf (" %s", time_string (utmp_ent
));
287 if (include_where
&& utmp_ent
->ut_host
[0])
289 extern char *canon_host ();
290 char ut_host
[sizeof (utmp_ent
->ut_host
) + 1];
291 char *host
= 0, *display
= 0;
293 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
294 strncpy (ut_host
, utmp_ent
->ut_host
, (int) sizeof (utmp_ent
->ut_host
));
295 ut_host
[sizeof (utmp_ent
->ut_host
)] = '\0';
297 /* Look for an X display. */
298 display
= strchr (ut_host
, ':');
303 /* See if we can canonicalize it. */
304 host
= canon_host (ut_host
);
309 printf (" %s:%s", host
, display
);
311 printf (" %s", host
);
321 /* Display a verbose line of information about UTMP_ENT. */
324 print_long_entry (const char name
[])
328 pw
= getpwnam (name
);
330 printf (_("Login name: "));
331 printf ("%-28s", name
);
333 printf (_("In real life: "));
336 printf (" %s", _("???\n"));
341 char *const comma
= strchr (pw
->pw_gecos
, ',');
347 result
= create_fullname (pw
->pw_gecos
, pw
->pw_name
);
348 printf (" %s", result
);
354 if (include_home_and_shell
)
356 printf (_("Directory: "));
357 printf ("%-29s", pw
->pw_dir
);
358 printf (_("Shell: "));
359 printf (" %s", pw
->pw_shell
);
367 const char *const baseproject
= "/.project";
368 char *const project
=
369 xmalloc (strlen (pw
->pw_dir
) + strlen (baseproject
) + 1);
371 strcpy (project
, pw
->pw_dir
);
372 strcat (project
, baseproject
);
374 stream
= fopen (project
, "r");
379 printf (_("Project: "));
381 while ((bytes
= fread (buf
, 1, sizeof (buf
), stream
)) > 0)
382 fwrite (buf
, 1, bytes
, stdout
);
393 const char *const baseplan
= "/.plan";
395 xmalloc (strlen (pw
->pw_dir
) + strlen (baseplan
) + 1);
397 strcpy (plan
, pw
->pw_dir
);
398 strcat (plan
, baseplan
);
400 stream
= fopen (plan
, "r");
405 printf (_("Plan:\n"));
407 while ((bytes
= fread (buf
, 1, sizeof (buf
), stream
)) > 0)
408 fwrite (buf
, 1, bytes
, stdout
);
418 /* Print the username of each valid entry and the number of valid entries
419 in UTMP_BUF, which should have N elements. */
424 printf ("%-8s", _("Login"));
425 if (include_fullname
)
426 printf (" %-19s", _("Name"));
427 printf (" %-9s", _(" TTY"));
429 printf (" %-6s", _("Idle"));
430 printf (" %-*s", time_format_width
, _("When"));
433 printf (" %s", _("Where"));
438 /* Display UTMP_BUF, which should have N entries. */
441 scan_entries (size_t n
, const STRUCT_UTMP
*utmp_buf
,
442 const int argc_names
, char *const argv_names
[])
444 if (hard_locale (LC_TIME
))
446 time_format
= "%Y-%m-%d %H:%M";
447 time_format_width
= 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2;
451 time_format
= "%b %e %H:%M";
452 time_format_width
= 3 + 1 + 2 + 1 + 2 + 1 + 2;
460 if (IS_USER_PROCESS (utmp_buf
))
466 for (i
= 0; i
< argc_names
; i
++)
467 if (strncmp (UT_USER (utmp_buf
), argv_names
[i
], UT_USER_SIZE
)
470 print_entry (utmp_buf
);
475 print_entry (utmp_buf
);
481 /* Display a list of who is on the system, according to utmp file FILENAME. */
484 short_pinky (const char *filename
,
485 const int argc_names
, char *const argv_names
[])
488 STRUCT_UTMP
*utmp_buf
;
490 if (read_utmp (filename
, &n_users
, &utmp_buf
) != 0)
491 error (EXIT_FAILURE
, errno
, "%s", filename
);
493 scan_entries (n_users
, utmp_buf
, argc_names
, argv_names
);
497 long_pinky (const int argc_names
, char *const argv_names
[])
501 for (i
= 0; i
< argc_names
; i
++)
502 print_long_entry (argv_names
[i
]);
508 if (status
!= EXIT_SUCCESS
)
509 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
513 printf (_("Usage: %s [OPTION]... [USER]...\n"), program_name
);
516 -l produce long format output for the specified USERs\n\
517 -b omit the user's home directory and shell in long format\n\
518 -h omit the user's project file in long format\n\
519 -p omit the user's plan file in long format\n\
520 -s do short format output, this is the default\n\
523 -f omit the line of column headings in short format\n\
524 -w omit the user's full name in short format\n\
525 -i omit the user's full name and remote host in short format\n\
526 -q omit the user's full name, remote host and idle time\n\
529 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
530 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
533 A lightweight `finger' program; print user information.\n\
534 The utmp file will be %s.\n\
536 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
542 main (int argc
, char **argv
)
547 initialize_main (&argc
, &argv
);
548 program_name
= argv
[0];
549 setlocale (LC_ALL
, "");
550 bindtextdomain (PACKAGE
, LOCALEDIR
);
551 textdomain (PACKAGE
);
553 atexit (close_stdout
);
555 while ((optc
= getopt_long (argc
, argv
, "sfwiqbhlp", longopts
, NULL
)) != -1)
560 do_short_format
= true;
564 do_short_format
= false;
568 include_heading
= false;
572 include_fullname
= false;
576 include_fullname
= false;
578 include_where
= false;
583 include_fullname
= false;
585 include_where
= false;
587 include_idle
= false;
591 include_project
= false;
595 include_plan
= false;
599 include_home_and_shell
= false;
602 case_GETOPT_HELP_CHAR
;
604 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
607 usage (EXIT_FAILURE
);
611 n_users
= argc
- optind
;
613 if (!do_short_format
&& n_users
== 0)
615 error (0, 0, _("no username specified; at least one must be\
616 specified when using -l"));
617 usage (EXIT_FAILURE
);
621 short_pinky (UTMP_FILE
, n_users
, argv
+ optind
);
623 long_pinky (n_users
, argv
+ optind
);