2 Copyright (C) 92, 93, 94, 95, 96, 1997 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 */
28 #ifndef MAXHOSTNAMELEN
29 # define MAXHOSTNAMELEN 64
39 /* The name this program was run with. */
40 const char *program_name
;
42 /* If nonzero, display usage information and exit. */
45 /* If nonzero, print the version on standard output and exit. */
46 static int show_version
;
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 {"help", no_argument
, &show_help
, 1},
80 {"version", no_argument
, &show_version
, 1},
84 /* Return a string representing the time between WHEN and the time
85 that this function is first run. */
88 idle_string (time_t when
)
90 static time_t now
= 0;
91 static char idle_hhmm
[10];
97 seconds_idle
= now
- when
;
98 if (seconds_idle
< 60) /* One minute. */
100 if (seconds_idle
< (24 * 60 * 60)) /* One day. */
102 sprintf (idle_hhmm
, "%02d:%02d",
103 (int) (seconds_idle
/ (60 * 60)),
104 (int) ((seconds_idle
% (60 * 60)) / 60));
105 return (const char *) idle_hhmm
;
107 sprintf (idle_hhmm
, "%dd", (int) (seconds_idle
/ (24 * 60 * 60)));
108 return (const char *) idle_hhmm
;
111 /* Display a line of information about UTMP_ENT. */
114 print_entry (const STRUCT_UTMP
*utmp_ent
)
120 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
121 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
123 char line
[sizeof (utmp_ent
->ut_line
) + DEV_DIR_LEN
+ 1];
126 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
127 already an absolute pathname. Some system may put the full,
128 absolute pathname in ut_line. */
129 if (utmp_ent
->ut_line
[0] == '/')
131 strncpy (line
, utmp_ent
->ut_line
, sizeof (utmp_ent
->ut_line
));
132 line
[sizeof (utmp_ent
->ut_line
)] = '\0';
136 strcpy (line
, DEV_DIR_WITH_TRAILING_SLASH
);
137 strncpy (line
+ DEV_DIR_LEN
, utmp_ent
->ut_line
, sizeof (utmp_ent
->ut_line
));
138 line
[DEV_DIR_LEN
+ sizeof (utmp_ent
->ut_line
)] = '\0';
141 if (stat (line
, &stats
) == 0)
143 mesg
= (stats
.st_mode
& S_IWGRP
) ? ' ' : '*';
144 last_change
= stats
.st_atime
;
152 printf ("%-8.*s", (int) sizeof (utmp_ent
->ut_name
), utmp_ent
->ut_name
);
154 if (include_fullname
)
157 char name
[sizeof (utmp_ent
->ut_name
) + 1];
159 strncpy (name
, utmp_ent
->ut_name
, sizeof (utmp_ent
->ut_name
));
160 name
[sizeof (utmp_ent
->ut_name
)] = '\0';
161 pw
= getpwnam (name
);
163 printf (" %19s", " ???");
166 char *const comma
= strchr (pw
->pw_gecos
, ',');
170 /* FIXME: we don't yet convert '&' to username capitalized. */
171 printf (" %-19.19s", pw
->pw_gecos
);
176 mesg
, (int) sizeof (utmp_ent
->ut_line
), utmp_ent
->ut_line
);
181 printf (" %-6s", idle_string (last_change
));
183 printf (" %-6s", "???");
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 tm
= UT_TIME_MEMBER (utmp_ent
);
193 printf (" %-12.12s", ctime (&tm
) + 4);
196 if (include_where
&& utmp_ent
->ut_host
[0])
198 extern char *canon_host ();
199 char ut_host
[sizeof (utmp_ent
->ut_host
) + 1];
200 char *host
= 0, *display
= 0;
202 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
203 strncpy (ut_host
, utmp_ent
->ut_host
, (int) sizeof (utmp_ent
->ut_host
));
204 ut_host
[sizeof (utmp_ent
->ut_host
)] = '\0';
206 /* Look for an X display. */
207 display
= strrchr (ut_host
, ':');
212 /* See if we can canonicalize it. */
213 host
= canon_host (ut_host
);
218 printf (" %s:%s", host
, display
);
220 printf (" %s", host
);
227 /* Display a verbose line of information about UTMP_ENT. */
230 print_long_entry (const char name
[])
234 pw
= getpwnam (name
);
236 printf (_("Login name: "));
237 printf ("%-28s", name
);
239 printf (_("In real life: "));
242 printf (" %s", _("???\n"));
247 char *const comma
= strchr (pw
->pw_gecos
, ',');
251 /* FIXME: we don't yet convert '&' to username capitalized. */
252 printf (" %s", pw
->pw_gecos
);
257 if (include_home_and_shell
)
259 printf (_("Directory: "));
260 printf ("%-29s", pw
->pw_dir
);
261 printf (_("Shell: "));
262 printf (" %s", pw
->pw_shell
);
270 const char *const baseproject
= "/.project";
271 char *const project
=
272 xmalloc (strlen (pw
->pw_dir
) + strlen (baseproject
) + 1);
274 strcpy (project
, pw
->pw_dir
);
275 strcat (project
, baseproject
);
277 stream
= fopen (project
, "r");
282 printf (_("Project: "));
284 while ((bytes
= fread (buf
, 1, sizeof (buf
), stream
)) > 0)
285 fwrite (buf
, 1, bytes
, stdout
);
296 const char *const baseplan
= "/.plan";
298 xmalloc (strlen (pw
->pw_dir
) + strlen (baseplan
) + 1);
300 strcpy (plan
, pw
->pw_dir
);
301 strcat (plan
, baseplan
);
303 stream
= fopen (plan
, "r");
308 printf (_("Plan:\n"));
310 while ((bytes
= fread (buf
, 1, sizeof (buf
), stream
)) > 0)
311 fwrite (buf
, 1, bytes
, stdout
);
321 /* Print the username of each valid entry and the number of valid entries
322 in UTMP_BUF, which should have N elements. */
327 printf ("%-8s", _("Login"));
328 if (include_fullname
)
329 printf (" %-19s", _(" Name"));
330 printf (" %-9s", _("TTY"));
332 printf (" %-6s", _("Idle"));
333 printf (" %-12s", _("When"));
336 printf (" %s", _("Where"));
341 /* Display UTMP_BUF, which should have N entries. */
344 scan_entries (int n
, const STRUCT_UTMP
*utmp_buf
,
345 const int argc_names
, char *const argv_names
[])
352 if (utmp_buf
->ut_name
[0]
354 && utmp_buf
->ut_type
== USER_PROCESS
362 for (i
= 0; i
< argc_names
; i
++)
363 if (strncmp (utmp_buf
->ut_name
, argv_names
[i
],
364 sizeof (utmp_buf
->ut_name
)) == 0)
366 print_entry (utmp_buf
);
371 print_entry (utmp_buf
);
377 /* Display a list of who is on the system, according to utmp file FILENAME. */
380 short_pinky (const char *filename
,
381 const int argc_names
, char *const argv_names
[])
384 STRUCT_UTMP
*utmp_buf
;
385 int fail
= read_utmp (filename
, &n_users
, &utmp_buf
);
388 error (1, errno
, "%s", filename
);
390 scan_entries (n_users
, utmp_buf
, argc_names
, argv_names
);
394 long_pinky (const int argc_names
, char *const argv_names
[])
398 for (i
= 0; i
< argc_names
; i
++)
399 print_long_entry (argv_names
[i
]);
406 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
410 printf (_("Usage: %s [OPTION]... [USER]...\n"), program_name
);
413 -l do long format output\n\
414 -b omit the user's home directory and shell in long format\n\
415 -h omit the user's project file in long format\n\
416 -p omit the user's plan file in long format\n\
417 -s do short format output, this is the default\n\
418 -f omit the line of column headings in short format\n\
419 -w omit the user's full name in short format\n\
420 -i omit the user's full name and remote host in short format\n\
421 -q omit the user's full name, remote host and idle time\n\
423 --help display this help and exit\n\
424 --version output version information and exit\n\
426 The utmp file will be %s.\n\
428 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
434 main (int argc
, char *const argv
[])
438 program_name
= argv
[0];
439 setlocale (LC_ALL
, "");
440 bindtextdomain (PACKAGE
, LOCALEDIR
);
441 textdomain (PACKAGE
);
443 while ((optc
= getopt_long (argc
, argv
, "sfwiqbhlp", longopts
, &longind
)) != -1)
463 include_fullname
= 0;
467 include_fullname
= 0;
474 include_fullname
= 0;
490 include_home_and_shell
= 0;
500 printf ("pinky (%s) %s\n", GNU_PACKAGE
, VERSION
);
508 short_pinky (UTMP_FILE
, argc
- optind
, argv
+ optind
);
510 long_pinky (argc
- optind
, argv
+ optind
);