*** empty log message ***
[coreutils.git] / src / pinky.c
blob65cec90a4db19ae386a3ae9e9806a4e9dd8948be
1 /* GNU's pinky.
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)
7 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, 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 */
20 #include <config.h>
21 #include <getopt.h>
22 #include <pwd.h>
23 #include <stdio.h>
25 #include "system.h"
26 #include "error.h"
27 #include "readutmp.h"
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
36 #endif
38 #ifndef S_IWGRP
39 # define S_IWGRP 020
40 #endif
42 int gethostname ();
43 char *ttyname ();
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. */
73 #ifdef HAVE_UT_HOST
74 static int include_where = 1;
75 #endif
77 static struct option const longopts[] =
79 {GETOPT_HELP_OPTION_DECL},
80 {GETOPT_VERSION_OPTION_DECL},
81 {NULL, 0, NULL, 0}
84 /* Count and return the number of ampersands in STR. */
86 static int
87 count_ampersands (const char *str)
89 int count = 0;
92 if (*str == '&')
93 count++;
94 } while (*str++);
95 return count;
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
102 this function. */
104 static char *
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);
110 char *r = result;
112 while (*gecos_name)
114 if (*gecos_name == '&')
116 const char *uname = user_name;
117 if (ISLOWER (*uname))
118 *r++ = TOUPPER (*uname++);
119 while (*uname)
120 *r++ = *uname++;
122 else
124 *r++ = *gecos_name;
127 gecos_name++;
129 *r = 0;
131 return result;
134 /* Return a string representing the time between WHEN and the time
135 that this function is first run. */
137 static const char *
138 idle_string (time_t when)
140 static time_t now = 0;
141 static char idle_hhmm[10];
142 time_t seconds_idle;
144 if (now == 0)
145 time (&now);
147 seconds_idle = now - when;
148 if (seconds_idle < 60) /* One minute. */
149 return " ";
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. */
163 static void
164 print_entry (const STRUCT_UTMP *utmp_ent)
166 struct stat stats;
167 time_t last_change;
168 char mesg;
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];
174 time_t tm;
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';
184 else
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;
196 else
198 mesg = '?';
199 last_change = 0;
202 printf ("%-8.*s", (int) sizeof (utmp_ent->ut_name), utmp_ent->ut_name);
204 if (include_fullname)
206 struct passwd *pw;
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);
212 if (pw == NULL)
213 printf (" %19s", " ???");
214 else
216 char *const comma = strchr (pw->pw_gecos, ',');
217 char *result;
219 if (comma)
220 *comma = '\0';
222 result = create_fullname (pw->pw_gecos, pw->pw_name);
223 printf (" %-19.19s", result);
224 free (result);
228 printf (" %c%-8.*s",
229 mesg, (int) sizeof (utmp_ent->ut_line), utmp_ent->ut_line);
231 if (include_idle)
233 if (last_change)
234 printf (" %-6s", idle_string (last_change));
235 else
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);
248 #ifdef HAVE_UT_HOST
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, ':');
261 if (display)
262 *display++ = '\0';
264 if (*ut_host)
265 /* See if we can canonicalize it. */
266 host = canon_host (ut_host);
267 if ( ! host)
268 host = ut_host;
270 if (display)
271 printf (" %s:%s", host, display);
272 else
273 printf (" %s", host);
275 #endif
277 putchar ('\n');
280 /* Display a verbose line of information about UTMP_ENT. */
282 static void
283 print_long_entry (const char name[])
285 struct passwd *pw;
287 pw = getpwnam (name);
289 printf (_("Login name: "));
290 printf ("%-28s", name);
292 printf (_("In real life: "));
293 if (pw == NULL)
295 printf (" %s", _("???\n"));
296 return;
298 else
300 char *const comma = strchr (pw->pw_gecos, ',');
301 char *result;
303 if (comma)
304 *comma = '\0';
306 result = create_fullname (pw->pw_gecos, pw->pw_name);
307 printf (" %s", result);
308 free (result);
311 putchar ('\n');
313 if (include_home_and_shell)
315 printf (_("Directory: "));
316 printf ("%-29s", pw->pw_dir);
317 printf (_("Shell: "));
318 printf (" %s", pw->pw_shell);
319 putchar ('\n');
322 if (include_project)
324 FILE *stream;
325 char buf[1024];
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");
334 if (stream)
336 int bytes;
338 printf (_("Project: "));
340 while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
341 fwrite (buf, 1, bytes, stdout);
342 fclose (stream);
345 free (project);
348 if (include_plan)
350 FILE *stream;
351 char buf[1024];
352 const char *const baseplan = "/.plan";
353 char *const 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");
360 if (stream)
362 int bytes;
364 printf (_("Plan:\n"));
366 while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
367 fwrite (buf, 1, bytes, stdout);
368 fclose (stream);
371 free (plan);
374 putchar ('\n');
377 /* Print the username of each valid entry and the number of valid entries
378 in UTMP_BUF, which should have N elements. */
380 static void
381 print_heading (void)
383 printf ("%-8s", _("Login"));
384 if (include_fullname)
385 printf (" %-19s", _(" Name"));
386 printf (" %-9s", _("TTY"));
387 if (include_idle)
388 printf (" %-6s", _("Idle"));
389 printf (" %-12s", _("When"));
390 #ifdef HAVE_UT_HOST
391 if (include_where)
392 printf (" %s", _("Where"));
393 #endif
394 putchar ('\n');
397 /* Display UTMP_BUF, which should have N entries. */
399 static void
400 scan_entries (int n, const STRUCT_UTMP *utmp_buf,
401 const int argc_names, char *const argv_names[])
403 if (include_heading)
404 print_heading ();
406 while (n--)
408 if (utmp_buf->ut_name[0]
409 #ifdef USER_PROCESS
410 && utmp_buf->ut_type == USER_PROCESS
411 #endif
414 if (argc_names)
416 int i;
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);
423 break;
426 else
427 print_entry (utmp_buf);
429 utmp_buf++;
433 /* Display a list of who is on the system, according to utmp file FILENAME. */
435 static void
436 short_pinky (const char *filename,
437 const int argc_names, char *const argv_names[])
439 int n_users;
440 STRUCT_UTMP *utmp_buf;
441 int fail = read_utmp (filename, &n_users, &utmp_buf);
443 if (fail)
444 error (1, errno, "%s", filename);
446 scan_entries (n_users, utmp_buf, argc_names, argv_names);
449 static void
450 long_pinky (const int argc_names, char *const argv_names[])
452 int i;
454 for (i = 0; i < argc_names; i++)
455 print_long_entry (argv_names[i]);
458 void
459 usage (int status)
461 if (status != 0)
462 fprintf (stderr, _("Try `%s --help' for more information.\n"),
463 program_name);
464 else
466 printf (_("Usage: %s [OPTION]... [USER]...\n"), program_name);
467 printf (_("\
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\
478 in short format\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\
484 "), UTMP_FILE);
485 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
487 exit (status);
491 main (int argc, char **argv)
493 int optc, longind;
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))
501 != -1)
503 switch (optc)
505 case 0:
506 break;
508 case 's':
509 do_short_format = 1;
510 break;
512 case 'l':
513 do_short_format = 0;
514 break;
516 case 'f':
517 include_heading = 0;
518 break;
520 case 'w':
521 include_fullname = 0;
522 break;
524 case 'i':
525 include_fullname = 0;
526 #ifdef HAVE_UT_HOST
527 include_where = 0;
528 #endif
529 break;
531 case 'q':
532 include_fullname = 0;
533 #ifdef HAVE_UT_HOST
534 include_where = 0;
535 #endif
536 include_idle = 0;
537 break;
539 case 'h':
540 include_project = 0;
541 break;
543 case 'p':
544 include_plan = 0;
545 break;
547 case 'b':
548 include_home_and_shell = 0;
549 break;
551 case_GETOPT_HELP_CHAR;
553 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
555 default:
556 usage (1);
560 if (do_short_format)
561 short_pinky (UTMP_FILE, argc - optind, argv + optind);
562 else
563 long_pinky (argc - optind, argv + optind);
565 exit (0);