2 Copyright (C) 1992-1997, 1999 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 */
29 /* The official name of this program (e.g., no `g' prefix). */
30 #define PROGRAM_NAME "pinky"
32 #define AUTHORS "Joseph Arceneaux, David MacKenzie, and Kaveh Ghazi"
34 #ifndef MAXHOSTNAMELEN
35 # define MAXHOSTNAMELEN 64
45 /* The name this program was run with. */
46 const char *program_name
;
48 /* If nonzero, 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 int include_idle
= 1;
53 /* If nonzero, display a line at the top describing each field. */
54 static int include_heading
= 1;
56 /* if nonzero, display the user's full name from pw_gecos. */
57 static int include_fullname
= 1;
59 /* if nonzero, display the user's ~/.project file when doing long format. */
60 static int include_project
= 1;
62 /* if nonzero, display the user's ~/.plan file when doing long format. */
63 static int include_plan
= 1;
65 /* if nonzero, display the user's home directory and shell
66 when doing long format. */
67 static int include_home_and_shell
= 1;
69 /* if nonzero, use the "short" output format. */
70 static int do_short_format
= 1;
72 /* if nonzero, display the ut_host field. */
74 static int include_where
= 1;
77 static struct option
const longopts
[] =
79 {GETOPT_HELP_OPTION_DECL
},
80 {GETOPT_VERSION_OPTION_DECL
},
84 /* Count and return the number of ampersands in STR. */
87 count_ampersands (const char *str
)
98 /* Create a string (via xmalloc) which contains a full name by substituting
99 for each ampersand in GECOS_NAME the USER_NAME string with its first
100 character capitalized. The caller must ensure that GECOS_NAME contains
101 no `,'s. The caller also is responsible for free'ing the return value of
105 create_fullname (const char *gecos_name
, const char *user_name
)
107 const int result_len
= strlen (gecos_name
)
108 + count_ampersands (gecos_name
) * (strlen (user_name
) - 1) + 1;
109 char *const result
= xmalloc (result_len
);
114 if (*gecos_name
== '&')
116 const char *uname
= user_name
;
117 if (ISLOWER (*uname
))
118 *r
++ = TOUPPER (*uname
++);
134 /* Return a string representing the time between WHEN and the time
135 that this function is first run. */
138 idle_string (time_t when
)
140 static time_t now
= 0;
141 static char idle_hhmm
[10];
147 seconds_idle
= now
- when
;
148 if (seconds_idle
< 60) /* One minute. */
150 if (seconds_idle
< (24 * 60 * 60)) /* One day. */
152 sprintf (idle_hhmm
, "%02d:%02d",
153 (int) (seconds_idle
/ (60 * 60)),
154 (int) ((seconds_idle
% (60 * 60)) / 60));
155 return (const char *) idle_hhmm
;
157 sprintf (idle_hhmm
, "%dd", (int) (seconds_idle
/ (24 * 60 * 60)));
158 return (const char *) idle_hhmm
;
161 /* Display a line of information about UTMP_ENT. */
164 print_entry (const STRUCT_UTMP
*utmp_ent
)
170 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
171 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
173 char line
[sizeof (utmp_ent
->ut_line
) + DEV_DIR_LEN
+ 1];
176 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
177 already an absolute pathname. Some system may put the full,
178 absolute pathname in ut_line. */
179 if (utmp_ent
->ut_line
[0] == '/')
181 strncpy (line
, utmp_ent
->ut_line
, sizeof (utmp_ent
->ut_line
));
182 line
[sizeof (utmp_ent
->ut_line
)] = '\0';
186 strcpy (line
, DEV_DIR_WITH_TRAILING_SLASH
);
187 strncpy (line
+ DEV_DIR_LEN
, utmp_ent
->ut_line
, sizeof (utmp_ent
->ut_line
));
188 line
[DEV_DIR_LEN
+ sizeof (utmp_ent
->ut_line
)] = '\0';
191 if (stat (line
, &stats
) == 0)
193 mesg
= (stats
.st_mode
& S_IWGRP
) ? ' ' : '*';
194 last_change
= stats
.st_atime
;
202 printf ("%-8.*s", (int) sizeof (utmp_ent
->ut_name
), utmp_ent
->ut_name
);
204 if (include_fullname
)
207 char name
[sizeof (utmp_ent
->ut_name
) + 1];
209 strncpy (name
, utmp_ent
->ut_name
, sizeof (utmp_ent
->ut_name
));
210 name
[sizeof (utmp_ent
->ut_name
)] = '\0';
211 pw
= getpwnam (name
);
213 printf (" %19s", " ???");
216 char *const comma
= strchr (pw
->pw_gecos
, ',');
222 result
= create_fullname (pw
->pw_gecos
, pw
->pw_name
);
223 printf (" %-19.19s", result
);
229 mesg
, (int) sizeof (utmp_ent
->ut_line
), utmp_ent
->ut_line
);
234 printf (" %-6s", idle_string (last_change
));
236 printf (" %-6s", "???");
239 /* Don't take the address of UT_TIME_MEMBER directly.
240 Ulrich Drepper wrote:
241 ``... GNU libc (and perhaps other libcs as well) have extended
242 utmp file formats which do not use a simple time_t ut_time field.
243 In glibc, ut_time is a macro which selects for backward compatibility
244 the tv_sec member of a struct timeval value.'' */
245 tm
= UT_TIME_MEMBER (utmp_ent
);
246 printf (" %-12.12s", ctime (&tm
) + 4);
249 if (include_where
&& utmp_ent
->ut_host
[0])
251 extern char *canon_host ();
252 char ut_host
[sizeof (utmp_ent
->ut_host
) + 1];
253 char *host
= 0, *display
= 0;
255 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
256 strncpy (ut_host
, utmp_ent
->ut_host
, (int) sizeof (utmp_ent
->ut_host
));
257 ut_host
[sizeof (utmp_ent
->ut_host
)] = '\0';
259 /* Look for an X display. */
260 display
= strrchr (ut_host
, ':');
265 /* See if we can canonicalize it. */
266 host
= canon_host (ut_host
);
271 printf (" %s:%s", host
, display
);
273 printf (" %s", host
);
280 /* Display a verbose line of information about UTMP_ENT. */
283 print_long_entry (const char name
[])
287 pw
= getpwnam (name
);
289 printf (_("Login name: "));
290 printf ("%-28s", name
);
292 printf (_("In real life: "));
295 printf (" %s", _("???\n"));
300 char *const comma
= strchr (pw
->pw_gecos
, ',');
306 result
= create_fullname (pw
->pw_gecos
, pw
->pw_name
);
307 printf (" %s", result
);
313 if (include_home_and_shell
)
315 printf (_("Directory: "));
316 printf ("%-29s", pw
->pw_dir
);
317 printf (_("Shell: "));
318 printf (" %s", pw
->pw_shell
);
326 const char *const baseproject
= "/.project";
327 char *const project
=
328 xmalloc (strlen (pw
->pw_dir
) + strlen (baseproject
) + 1);
330 strcpy (project
, pw
->pw_dir
);
331 strcat (project
, baseproject
);
333 stream
= fopen (project
, "r");
338 printf (_("Project: "));
340 while ((bytes
= fread (buf
, 1, sizeof (buf
), stream
)) > 0)
341 fwrite (buf
, 1, bytes
, stdout
);
352 const char *const baseplan
= "/.plan";
354 xmalloc (strlen (pw
->pw_dir
) + strlen (baseplan
) + 1);
356 strcpy (plan
, pw
->pw_dir
);
357 strcat (plan
, baseplan
);
359 stream
= fopen (plan
, "r");
364 printf (_("Plan:\n"));
366 while ((bytes
= fread (buf
, 1, sizeof (buf
), stream
)) > 0)
367 fwrite (buf
, 1, bytes
, stdout
);
377 /* Print the username of each valid entry and the number of valid entries
378 in UTMP_BUF, which should have N elements. */
383 printf ("%-8s", _("Login"));
384 if (include_fullname
)
385 printf (" %-19s", _(" Name"));
386 printf (" %-9s", _("TTY"));
388 printf (" %-6s", _("Idle"));
389 printf (" %-12s", _("When"));
392 printf (" %s", _("Where"));
397 /* Display UTMP_BUF, which should have N entries. */
400 scan_entries (int n
, const STRUCT_UTMP
*utmp_buf
,
401 const int argc_names
, char *const argv_names
[])
408 if (utmp_buf
->ut_name
[0]
410 && utmp_buf
->ut_type
== USER_PROCESS
418 for (i
= 0; i
< argc_names
; i
++)
419 if (strncmp (utmp_buf
->ut_name
, argv_names
[i
],
420 sizeof (utmp_buf
->ut_name
)) == 0)
422 print_entry (utmp_buf
);
427 print_entry (utmp_buf
);
433 /* Display a list of who is on the system, according to utmp file FILENAME. */
436 short_pinky (const char *filename
,
437 const int argc_names
, char *const argv_names
[])
440 STRUCT_UTMP
*utmp_buf
;
441 int fail
= read_utmp (filename
, &n_users
, &utmp_buf
);
444 error (1, errno
, "%s", filename
);
446 scan_entries (n_users
, utmp_buf
, argc_names
, argv_names
);
450 long_pinky (const int argc_names
, char *const argv_names
[])
454 for (i
= 0; i
< argc_names
; i
++)
455 print_long_entry (argv_names
[i
]);
462 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
466 printf (_("Usage: %s [OPTION]... [USER]...\n"), program_name
);
469 -l do long format output\n\
470 -b omit the user's home directory and shell in long format\n\
471 -h omit the user's project file in long format\n\
472 -p omit the user's plan file in long format\n\
473 -s do short format output, this is the default\n\
474 -f omit the line of column headings in short format\n\
475 -w omit the user's full name in short format\n\
476 -i omit the user's full name and remote host in short format\n\
477 -q omit the user's full name, remote host and idle time\n\
479 --help display this help and exit\n\
480 --version output version information and exit\n\
482 A lightweight `finger' program; print user information.\n\
483 The utmp file will be %s.\n\
485 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
491 main (int argc
, char **argv
)
495 program_name
= argv
[0];
496 setlocale (LC_ALL
, "");
497 bindtextdomain (PACKAGE
, LOCALEDIR
);
498 textdomain (PACKAGE
);
500 while ((optc
= getopt_long (argc
, argv
, "sfwiqbhlp", longopts
, &longind
))
521 include_fullname
= 0;
525 include_fullname
= 0;
532 include_fullname
= 0;
548 include_home_and_shell
= 0;
551 case_GETOPT_HELP_CHAR
;
553 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
561 short_pinky (UTMP_FILE
, argc
- optind
, argv
+ optind
);
563 long_pinky (argc
- optind
, argv
+ optind
);