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)
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 */
25 #include <sys/types.h>
28 #include "canon-host.h"
30 #include "hard-locale.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
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. */
74 static bool include_where
= true;
77 /* The strftime format to use for login times, and its expected
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
},
89 /* Count and return the number of ampersands in STR. */
92 count_ampersands (const char *str
)
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
110 create_fullname (const char *gecos_name
, const char *user_name
)
112 size_t rsize
= strlen (gecos_name
) + 1;
115 size_t ampersands
= count_ampersands (gecos_name
);
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
)
126 r
= result
= xmalloc (rsize
);
130 if (*gecos_name
== '&')
132 const char *uname
= user_name
;
133 if (islower (to_uchar (*uname
)))
134 *r
++ = toupper (to_uchar (*uname
++));
150 /* Return a string representing the time between WHEN and the time
151 that this function is first run. */
154 idle_string (time_t when
)
156 static time_t now
= 0;
157 static char buf
[INT_STRLEN_BOUND (long int) + 2];
163 seconds_idle
= now
- when
;
164 if (seconds_idle
< 60) /* One minute. */
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
);
174 unsigned long int days
= seconds_idle
/ (24 * 60 * 60);
175 sprintf (buf
, "%lud", days
);
180 /* Return a time string. */
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
);
197 strftime (buf
, sizeof buf
, time_format
, tmp
);
201 return TYPE_SIGNED (time_t) ? imaxtostr (t
, buf
) : umaxtostr (t
, buf
);
204 /* Display a line of information about UTMP_ENT. */
207 print_entry (const STRUCT_UTMP
*utmp_ent
)
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';
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
;
244 printf ("%-8.*s", UT_USER_SIZE
, UT_USER (utmp_ent
));
246 if (include_fullname
)
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
);
255 printf (" %19s", " ???");
258 char *const comma
= strchr (pw
->pw_gecos
, ',');
264 result
= create_fullname (pw
->pw_gecos
, pw
->pw_name
);
265 printf (" %-19.19s", result
);
271 mesg
, (int) sizeof (utmp_ent
->ut_line
), utmp_ent
->ut_line
);
276 printf (" %-6s", idle_string (last_change
));
278 printf (" %-6s", "???");
281 printf (" %s", time_string (utmp_ent
));
284 if (include_where
&& utmp_ent
->ut_host
[0])
286 char ut_host
[sizeof (utmp_ent
->ut_host
) + 1];
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
, ':');
300 /* See if we can canonicalize it. */
301 host
= canon_host (ut_host
);
306 printf (" %s:%s", host
, display
);
308 printf (" %s", host
);
318 /* Display a verbose line of information about UTMP_ENT. */
321 print_long_entry (const char name
[])
325 pw
= getpwnam (name
);
327 printf (_("Login name: "));
328 printf ("%-28s", name
);
330 printf (_("In real life: "));
333 printf (" %s", _("???\n"));
338 char *const comma
= strchr (pw
->pw_gecos
, ',');
344 result
= create_fullname (pw
->pw_gecos
, pw
->pw_name
);
345 printf (" %s", result
);
351 if (include_home_and_shell
)
353 printf (_("Directory: "));
354 printf ("%-29s", pw
->pw_dir
);
355 printf (_("Shell: "));
356 printf (" %s", pw
->pw_shell
);
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");
376 printf (_("Project: "));
378 while ((bytes
= fread (buf
, 1, sizeof (buf
), stream
)) > 0)
379 fwrite (buf
, 1, bytes
, stdout
);
390 const char *const baseplan
= "/.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");
402 printf (_("Plan:\n"));
404 while ((bytes
= fread (buf
, 1, sizeof (buf
), stream
)) > 0)
405 fwrite (buf
, 1, bytes
, stdout
);
415 /* Print the username of each valid entry and the number of valid entries
416 in UTMP_BUF, which should have N elements. */
421 printf ("%-8s", _("Login"));
422 if (include_fullname
)
423 printf (" %-19s", _("Name"));
424 printf (" %-9s", _(" TTY"));
426 printf (" %-6s", _("Idle"));
427 printf (" %-*s", time_format_width
, _("When"));
430 printf (" %s", _("Where"));
435 /* Display UTMP_BUF, which should have N entries. */
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;
448 time_format
= "%b %e %H:%M";
449 time_format_width
= 3 + 1 + 2 + 1 + 2 + 1 + 2;
457 if (IS_USER_PROCESS (utmp_buf
))
463 for (i
= 0; i
< argc_names
; i
++)
464 if (strncmp (UT_USER (utmp_buf
), argv_names
[i
], UT_USER_SIZE
)
467 print_entry (utmp_buf
);
472 print_entry (utmp_buf
);
478 /* Display a list of who is on the system, according to utmp file FILENAME. */
481 short_pinky (const char *filename
,
482 const int argc_names
, char *const argv_names
[])
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
);
494 long_pinky (const int argc_names
, char *const argv_names
[])
498 for (i
= 0; i
< argc_names
; i
++)
499 print_long_entry (argv_names
[i
]);
505 if (status
!= EXIT_SUCCESS
)
506 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
510 printf (_("Usage: %s [OPTION]... [USER]...\n"), program_name
);
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\
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\
526 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
527 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
530 A lightweight `finger' program; print user information.\n\
531 The utmp file will be %s.\n\
533 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
539 main (int argc
, char **argv
)
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)
557 do_short_format
= true;
561 do_short_format
= false;
565 include_heading
= false;
569 include_fullname
= false;
573 include_fullname
= false;
575 include_where
= false;
580 include_fullname
= false;
582 include_where
= false;
584 include_idle
= false;
588 include_project
= false;
592 include_plan
= false;
596 include_home_and_shell
= false;
599 case_GETOPT_HELP_CHAR
;
601 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
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
);
618 short_pinky (UTMP_FILE
, n_users
, argv
+ optind
);
620 long_pinky (n_users
, argv
+ optind
);