2 Copyright (C) 1992-1997, 1999-2006, 2008 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 */
24 #include <sys/types.h>
27 #include "canon-host.h"
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "pinky"
35 proper_name ("Joseph Arceneaux"), \
36 proper_name ("David MacKenzie"), \
37 proper_name ("Kaveh Ghazi")
39 #ifndef MAXHOSTNAMELEN
40 # define MAXHOSTNAMELEN 64
45 /* If true, display the hours:minutes since each user has touched
46 the keyboard, or blank if within the last minute, or days followed
47 by a 'd' if not within the last day. */
48 static bool include_idle
= true;
50 /* If true, display a line at the top describing each field. */
51 static bool include_heading
= true;
53 /* if true, display the user's full name from pw_gecos. */
54 static bool include_fullname
= true;
56 /* if true, display the user's ~/.project file when doing long format. */
57 static bool include_project
= true;
59 /* if true, display the user's ~/.plan file when doing long format. */
60 static bool include_plan
= true;
62 /* if true, display the user's home directory and shell
63 when doing long format. */
64 static bool include_home_and_shell
= true;
66 /* if true, use the "short" output format. */
67 static bool do_short_format
= true;
69 /* if true, display the ut_host field. */
71 static bool include_where
= true;
74 /* The strftime format to use for login times, and its expected
76 static char const *time_format
;
77 static int time_format_width
;
79 static struct option
const longopts
[] =
81 {GETOPT_HELP_OPTION_DECL
},
82 {GETOPT_VERSION_OPTION_DECL
},
86 /* Count and return the number of ampersands in STR. */
89 count_ampersands (const char *str
)
100 /* Create a string (via xmalloc) which contains a full name by substituting
101 for each ampersand in GECOS_NAME the USER_NAME string with its first
102 character capitalized. The caller must ensure that GECOS_NAME contains
103 no `,'s. The caller also is responsible for free'ing the return value of
107 create_fullname (const char *gecos_name
, const char *user_name
)
109 size_t rsize
= strlen (gecos_name
) + 1;
112 size_t ampersands
= count_ampersands (gecos_name
);
116 size_t ulen
= strlen (user_name
);
117 size_t product
= ampersands
* ulen
;
118 rsize
+= product
- ampersands
;
119 if (xalloc_oversized (ulen
, ampersands
) || rsize
< product
)
123 r
= result
= xmalloc (rsize
);
127 if (*gecos_name
== '&')
129 const char *uname
= user_name
;
130 if (islower (to_uchar (*uname
)))
131 *r
++ = toupper (to_uchar (*uname
++));
147 /* Return a string representing the time between WHEN and the time
148 that this function is first run. */
151 idle_string (time_t when
)
153 static time_t now
= 0;
154 static char buf
[INT_STRLEN_BOUND (long int) + 2];
160 seconds_idle
= now
- when
;
161 if (seconds_idle
< 60) /* One minute. */
163 if (seconds_idle
< (24 * 60 * 60)) /* One day. */
165 int hours
= seconds_idle
/ (60 * 60);
166 int minutes
= (seconds_idle
% (60 * 60)) / 60;
167 sprintf (buf
, "%02d:%02d", hours
, minutes
);
171 unsigned long int days
= seconds_idle
/ (24 * 60 * 60);
172 sprintf (buf
, "%lud", days
);
177 /* Return a time string. */
179 time_string (const STRUCT_UTMP
*utmp_ent
)
181 static char buf
[INT_STRLEN_BOUND (intmax_t) + sizeof "-%m-%d %H:%M"];
183 /* Don't take the address of UT_TIME_MEMBER directly.
184 Ulrich Drepper wrote:
185 ``... GNU libc (and perhaps other libcs as well) have extended
186 utmp file formats which do not use a simple time_t ut_time field.
187 In glibc, ut_time is a macro which selects for backward compatibility
188 the tv_sec member of a struct timeval value.'' */
189 time_t t
= UT_TIME_MEMBER (utmp_ent
);
190 struct tm
*tmp
= localtime (&t
);
194 strftime (buf
, sizeof buf
, time_format
, tmp
);
198 return timetostr (t
, buf
);
201 /* Display a line of information about UTMP_ENT. */
204 print_entry (const STRUCT_UTMP
*utmp_ent
)
210 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
211 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
213 char line
[sizeof (utmp_ent
->ut_line
) + DEV_DIR_LEN
+ 1];
215 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
216 already an absolute file name. Some system may put the full,
217 absolute file name in ut_line. */
218 if (utmp_ent
->ut_line
[0] == '/')
220 strncpy (line
, utmp_ent
->ut_line
, sizeof (utmp_ent
->ut_line
));
221 line
[sizeof (utmp_ent
->ut_line
)] = '\0';
225 strcpy (line
, DEV_DIR_WITH_TRAILING_SLASH
);
226 strncpy (line
+ DEV_DIR_LEN
, utmp_ent
->ut_line
, sizeof (utmp_ent
->ut_line
));
227 line
[DEV_DIR_LEN
+ sizeof (utmp_ent
->ut_line
)] = '\0';
230 if (stat (line
, &stats
) == 0)
232 mesg
= (stats
.st_mode
& S_IWGRP
) ? ' ' : '*';
233 last_change
= stats
.st_atime
;
241 printf ("%-8.*s", UT_USER_SIZE
, UT_USER (utmp_ent
));
243 if (include_fullname
)
246 char name
[UT_USER_SIZE
+ 1];
248 strncpy (name
, UT_USER (utmp_ent
), UT_USER_SIZE
);
249 name
[UT_USER_SIZE
] = '\0';
250 pw
= getpwnam (name
);
252 /* TRANSLATORS: Real name is unknown; at most 19 characters. */
253 printf (" %19s", _(" ???"));
256 char *const comma
= strchr (pw
->pw_gecos
, ',');
262 result
= create_fullname (pw
->pw_gecos
, pw
->pw_name
);
263 printf (" %-19.19s", result
);
269 mesg
, (int) sizeof (utmp_ent
->ut_line
), utmp_ent
->ut_line
);
274 printf (" %-6s", idle_string (last_change
));
276 /* TRANSLATORS: Idle time is unknown; at most 5 characters. */
277 printf (" %-6s", _("?????"));
280 printf (" %s", time_string (utmp_ent
));
283 if (include_where
&& utmp_ent
->ut_host
[0])
285 char ut_host
[sizeof (utmp_ent
->ut_host
) + 1];
287 char *display
= NULL
;
289 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
290 strncpy (ut_host
, utmp_ent
->ut_host
, (int) sizeof (utmp_ent
->ut_host
));
291 ut_host
[sizeof (utmp_ent
->ut_host
)] = '\0';
293 /* Look for an X display. */
294 display
= strchr (ut_host
, ':');
299 /* See if we can canonicalize it. */
300 host
= canon_host (ut_host
);
305 printf (" %s:%s", host
, display
);
307 printf (" %s", host
);
317 /* Display a verbose line of information about UTMP_ENT. */
320 print_long_entry (const char name
[])
324 pw
= getpwnam (name
);
326 printf (_("Login name: "));
327 printf ("%-28s", name
);
329 printf (_("In real life: "));
332 /* TRANSLATORS: Real name is unknown; no hard limit. */
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 emit_bug_reporting_address ();
539 main (int argc
, char **argv
)
544 initialize_main (&argc
, &argv
);
545 set_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
);