2 Copyright (C) 1992-1997, 1999-2006, 2008-2010 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"
29 #include "hard-locale.h"
32 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "pinky"
36 proper_name ("Joseph Arceneaux"), \
37 proper_name ("David MacKenzie"), \
38 proper_name ("Kaveh Ghazi")
40 #ifndef MAXHOSTNAMELEN
41 # define MAXHOSTNAMELEN 64
46 /* If true, display the hours:minutes since each user has touched
47 the keyboard, or blank if within the last minute, or days followed
48 by a 'd' if not within the last day. */
49 static bool include_idle
= true;
51 /* If true, display a line at the top describing each field. */
52 static bool include_heading
= true;
54 /* if true, display the user's full name from pw_gecos. */
55 static bool include_fullname
= true;
57 /* if true, display the user's ~/.project file when doing long format. */
58 static bool include_project
= true;
60 /* if true, display the user's ~/.plan file when doing long format. */
61 static bool include_plan
= true;
63 /* if true, display the user's home directory and shell
64 when doing long format. */
65 static bool include_home_and_shell
= true;
67 /* if true, use the "short" output format. */
68 static bool do_short_format
= true;
70 /* if true, display the ut_host field. */
72 static bool include_where
= true;
75 /* The strftime format to use for login times, and its expected
77 static char const *time_format
;
78 static int time_format_width
;
80 static struct option
const longopts
[] =
82 {GETOPT_HELP_OPTION_DECL
},
83 {GETOPT_VERSION_OPTION_DECL
},
87 /* Count and return the number of ampersands in STR. */
90 count_ampersands (const char *str
)
101 /* Create a string (via xmalloc) which contains a full name by substituting
102 for each ampersand in GECOS_NAME the USER_NAME string with its first
103 character capitalized. The caller must ensure that GECOS_NAME contains
104 no `,'s. The caller also is responsible for free'ing the return value of
108 create_fullname (const char *gecos_name
, const char *user_name
)
110 size_t rsize
= strlen (gecos_name
) + 1;
113 size_t ampersands
= count_ampersands (gecos_name
);
117 size_t ulen
= strlen (user_name
);
118 size_t product
= ampersands
* ulen
;
119 rsize
+= product
- ampersands
;
120 if (xalloc_oversized (ulen
, ampersands
) || rsize
< product
)
124 r
= result
= xmalloc (rsize
);
128 if (*gecos_name
== '&')
130 const char *uname
= user_name
;
131 if (islower (to_uchar (*uname
)))
132 *r
++ = toupper (to_uchar (*uname
++));
148 /* Return a string representing the time between WHEN and the time
149 that this function is first run. */
152 idle_string (time_t when
)
154 static time_t now
= 0;
155 static char buf
[INT_STRLEN_BOUND (long int) + 2];
161 seconds_idle
= now
- when
;
162 if (seconds_idle
< 60) /* One minute. */
164 if (seconds_idle
< (24 * 60 * 60)) /* One day. */
166 int hours
= seconds_idle
/ (60 * 60);
167 int minutes
= (seconds_idle
% (60 * 60)) / 60;
168 sprintf (buf
, "%02d:%02d", hours
, minutes
);
172 unsigned long int days
= seconds_idle
/ (24 * 60 * 60);
173 sprintf (buf
, "%lud", days
);
178 /* Return a time string. */
180 time_string (const STRUCT_UTMP
*utmp_ent
)
182 static char buf
[INT_STRLEN_BOUND (intmax_t) + sizeof "-%m-%d %H:%M"];
184 /* Don't take the address of UT_TIME_MEMBER directly.
185 Ulrich Drepper wrote:
186 ``... GNU libc (and perhaps other libcs as well) have extended
187 utmp file formats which do not use a simple time_t ut_time field.
188 In glibc, ut_time is a macro which selects for backward compatibility
189 the tv_sec member of a struct timeval value.'' */
190 time_t t
= UT_TIME_MEMBER (utmp_ent
);
191 struct tm
*tmp
= localtime (&t
);
195 strftime (buf
, sizeof buf
, time_format
, tmp
);
199 return timetostr (t
, buf
);
202 /* Display a line of information about UTMP_ENT. */
205 print_entry (const STRUCT_UTMP
*utmp_ent
)
211 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
212 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
214 char line
[sizeof (utmp_ent
->ut_line
) + DEV_DIR_LEN
+ 1];
216 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
217 already an absolute file name. Some system may put the full,
218 absolute file name in ut_line. */
219 if (utmp_ent
->ut_line
[0] == '/')
221 strncpy (line
, utmp_ent
->ut_line
, sizeof (utmp_ent
->ut_line
));
222 line
[sizeof (utmp_ent
->ut_line
)] = '\0';
226 strcpy (line
, DEV_DIR_WITH_TRAILING_SLASH
);
227 strncpy (line
+ DEV_DIR_LEN
, utmp_ent
->ut_line
, sizeof (utmp_ent
->ut_line
));
228 line
[DEV_DIR_LEN
+ sizeof (utmp_ent
->ut_line
)] = '\0';
231 if (stat (line
, &stats
) == 0)
233 mesg
= (stats
.st_mode
& S_IWGRP
) ? ' ' : '*';
234 last_change
= stats
.st_atime
;
242 printf ("%-8.*s", UT_USER_SIZE
, UT_USER (utmp_ent
));
244 if (include_fullname
)
247 char name
[UT_USER_SIZE
+ 1];
249 strncpy (name
, UT_USER (utmp_ent
), UT_USER_SIZE
);
250 name
[UT_USER_SIZE
] = '\0';
251 pw
= getpwnam (name
);
253 /* TRANSLATORS: Real name is unknown; at most 19 characters. */
254 printf (" %19s", _(" ???"));
257 char *const comma
= strchr (pw
->pw_gecos
, ',');
263 result
= create_fullname (pw
->pw_gecos
, pw
->pw_name
);
264 printf (" %-19.19s", result
);
270 mesg
, (int) sizeof (utmp_ent
->ut_line
), utmp_ent
->ut_line
);
275 printf (" %-6s", idle_string (last_change
));
277 /* TRANSLATORS: Idle time is unknown; at most 5 characters. */
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 /* TRANSLATORS: Real name is unknown; no hard limit. */
334 printf (" %s", _("???\n"));
339 char *const comma
= strchr (pw
->pw_gecos
, ',');
345 result
= create_fullname (pw
->pw_gecos
, pw
->pw_name
);
346 printf (" %s", result
);
352 if (include_home_and_shell
)
354 printf (_("Directory: "));
355 printf ("%-29s", pw
->pw_dir
);
356 printf (_("Shell: "));
357 printf (" %s", pw
->pw_shell
);
365 const char *const baseproject
= "/.project";
366 char *const project
=
367 xmalloc (strlen (pw
->pw_dir
) + strlen (baseproject
) + 1);
368 stpcpy (stpcpy (project
, pw
->pw_dir
), baseproject
);
370 stream
= fopen (project
, "r");
375 printf (_("Project: "));
377 while ((bytes
= fread (buf
, 1, sizeof (buf
), stream
)) > 0)
378 fwrite (buf
, 1, bytes
, stdout
);
389 const char *const baseplan
= "/.plan";
391 xmalloc (strlen (pw
->pw_dir
) + strlen (baseplan
) + 1);
392 stpcpy (stpcpy (plan
, pw
->pw_dir
), baseplan
);
394 stream
= fopen (plan
, "r");
399 printf (_("Plan:\n"));
401 while ((bytes
= fread (buf
, 1, sizeof (buf
), stream
)) > 0)
402 fwrite (buf
, 1, bytes
, stdout
);
412 /* Print the username of each valid entry and the number of valid entries
413 in UTMP_BUF, which should have N elements. */
418 printf ("%-8s", _("Login"));
419 if (include_fullname
)
420 printf (" %-19s", _("Name"));
421 printf (" %-9s", _(" TTY"));
423 printf (" %-6s", _("Idle"));
424 printf (" %-*s", time_format_width
, _("When"));
427 printf (" %s", _("Where"));
432 /* Display UTMP_BUF, which should have N entries. */
435 scan_entries (size_t n
, const STRUCT_UTMP
*utmp_buf
,
436 const int argc_names
, char *const argv_names
[])
438 if (hard_locale (LC_TIME
))
440 time_format
= "%Y-%m-%d %H:%M";
441 time_format_width
= 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2;
445 time_format
= "%b %e %H:%M";
446 time_format_width
= 3 + 1 + 2 + 1 + 2 + 1 + 2;
454 if (IS_USER_PROCESS (utmp_buf
))
460 for (i
= 0; i
< argc_names
; i
++)
461 if (strncmp (UT_USER (utmp_buf
), argv_names
[i
], UT_USER_SIZE
)
464 print_entry (utmp_buf
);
469 print_entry (utmp_buf
);
475 /* Display a list of who is on the system, according to utmp file FILENAME. */
478 short_pinky (const char *filename
,
479 const int argc_names
, char *const argv_names
[])
482 STRUCT_UTMP
*utmp_buf
;
484 if (read_utmp (filename
, &n_users
, &utmp_buf
, 0) != 0)
485 error (EXIT_FAILURE
, errno
, "%s", filename
);
487 scan_entries (n_users
, utmp_buf
, argc_names
, argv_names
);
491 long_pinky (const int argc_names
, char *const argv_names
[])
495 for (i
= 0; i
< argc_names
; i
++)
496 print_long_entry (argv_names
[i
]);
502 if (status
!= EXIT_SUCCESS
)
503 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
507 printf (_("Usage: %s [OPTION]... [USER]...\n"), program_name
);
510 -l produce long format output for the specified USERs\n\
511 -b omit the user's home directory and shell in long format\n\
512 -h omit the user's project file in long format\n\
513 -p omit the user's plan file in long format\n\
514 -s do short format output, this is the default\n\
517 -f omit the line of column headings in short format\n\
518 -w omit the user's full name in short format\n\
519 -i omit the user's full name and remote host in short format\n\
520 -q omit the user's full name, remote host and idle time\n\
523 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
524 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
527 A lightweight `finger' program; print user information.\n\
528 The utmp file will be %s.\n\
530 emit_ancillary_info ();
536 main (int argc
, char **argv
)
541 initialize_main (&argc
, &argv
);
542 set_program_name (argv
[0]);
543 setlocale (LC_ALL
, "");
544 bindtextdomain (PACKAGE
, LOCALEDIR
);
545 textdomain (PACKAGE
);
547 atexit (close_stdout
);
549 while ((optc
= getopt_long (argc
, argv
, "sfwiqbhlp", longopts
, NULL
)) != -1)
554 do_short_format
= true;
558 do_short_format
= false;
562 include_heading
= false;
566 include_fullname
= false;
570 include_fullname
= false;
572 include_where
= false;
577 include_fullname
= false;
579 include_where
= false;
581 include_idle
= false;
585 include_project
= false;
589 include_plan
= false;
593 include_home_and_shell
= false;
596 case_GETOPT_HELP_CHAR
;
598 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
601 usage (EXIT_FAILURE
);
605 n_users
= argc
- optind
;
607 if (!do_short_format
&& n_users
== 0)
609 error (0, 0, _("no username specified; at least one must be\
610 specified when using -l"));
611 usage (EXIT_FAILURE
);
615 short_pinky (UTMP_FILE
, n_users
, argv
+ optind
);
617 long_pinky (n_users
, argv
+ optind
);