Avoid spurious failure on x86 solaris2.9 when using c89.
[coreutils.git] / src / pinky.c
blob8aa1d42d911fd79e1c095c77112dbcb9fe0f5e46
1 /* GNU's pinky.
2 Copyright (C) 1992-1997, 1999-2004 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 <sys/types.h>
26 #include "system.h"
28 #include "error.h"
29 #include "hard-locale.h"
30 #include "inttostr.h"
31 #include "readutmp.h"
33 /* The official name of this program (e.g., no `g' prefix). */
34 #define PROGRAM_NAME "pinky"
36 #define AUTHORS "Joseph Arceneaux", "David MacKenzie", "Kaveh Ghazi"
38 #ifndef MAXHOSTNAMELEN
39 # define MAXHOSTNAMELEN 64
40 #endif
42 #ifndef S_IWGRP
43 # define S_IWGRP 020
44 #endif
46 char *ttyname ();
48 /* The name this program was run with. */
49 const char *program_name;
51 /* If true, display the hours:minutes since each user has touched
52 the keyboard, or blank if within the last minute, or days followed
53 by a 'd' if not within the last day. */
54 static bool include_idle = true;
56 /* If true, display a line at the top describing each field. */
57 static bool include_heading = true;
59 /* if true, display the user's full name from pw_gecos. */
60 static bool include_fullname = true;
62 /* if true, display the user's ~/.project file when doing long format. */
63 static bool include_project = true;
65 /* if true, display the user's ~/.plan file when doing long format. */
66 static bool include_plan = true;
68 /* if true, display the user's home directory and shell
69 when doing long format. */
70 static bool include_home_and_shell = true;
72 /* if true, use the "short" output format. */
73 static bool do_short_format = true;
75 /* if true, display the ut_host field. */
76 #ifdef HAVE_UT_HOST
77 static bool include_where = true;
78 #endif
80 /* The strftime format to use for login times, and its expected
81 output width. */
82 static char const *time_format;
83 static int time_format_width;
85 static struct option const longopts[] =
87 {GETOPT_HELP_OPTION_DECL},
88 {GETOPT_VERSION_OPTION_DECL},
89 {NULL, 0, NULL, 0}
92 /* Count and return the number of ampersands in STR. */
94 static size_t
95 count_ampersands (const char *str)
97 size_t count = 0;
100 if (*str == '&')
101 count++;
102 } while (*str++);
103 return count;
106 /* Create a string (via xmalloc) which contains a full name by substituting
107 for each ampersand in GECOS_NAME the USER_NAME string with its first
108 character capitalized. The caller must ensure that GECOS_NAME contains
109 no `,'s. The caller also is responsible for free'ing the return value of
110 this function. */
112 static char *
113 create_fullname (const char *gecos_name, const char *user_name)
115 size_t rsize = strlen (gecos_name) + 1;
116 char *result;
117 char *r;
118 size_t ampersands = count_ampersands (gecos_name);
120 if (ampersands != 0)
122 size_t ulen = strlen (user_name);
123 size_t product = ampersands * ulen;
124 rsize += product - ampersands;
125 if (xalloc_oversized (ulen, ampersands) || rsize < product)
126 xalloc_die ();
129 r = result = xmalloc (rsize);
131 while (*gecos_name)
133 if (*gecos_name == '&')
135 const char *uname = user_name;
136 if (ISLOWER (*uname))
137 *r++ = TOUPPER (*uname++);
138 while (*uname)
139 *r++ = *uname++;
141 else
143 *r++ = *gecos_name;
146 gecos_name++;
148 *r = 0;
150 return result;
153 /* Return a string representing the time between WHEN and the time
154 that this function is first run. */
156 static const char *
157 idle_string (time_t when)
159 static time_t now = 0;
160 static char buf[INT_STRLEN_BOUND (long int) + 2];
161 time_t seconds_idle;
163 if (now == 0)
164 time (&now);
166 seconds_idle = now - when;
167 if (seconds_idle < 60) /* One minute. */
168 return " ";
169 if (seconds_idle < (24 * 60 * 60)) /* One day. */
171 int hours = seconds_idle / (60 * 60);
172 int minutes = (seconds_idle % (60 * 60)) / 60;
173 sprintf (buf, "%02d:%02d", hours, minutes);
175 else
177 unsigned long int days = seconds_idle / (24 * 60 * 60);
178 sprintf (buf, "%lud", days);
180 return buf;
183 /* Return a time string. */
184 static const char *
185 time_string (const STRUCT_UTMP *utmp_ent)
187 static char buf[INT_STRLEN_BOUND (intmax_t) + sizeof "-%m-%d %H:%M"];
189 /* Don't take the address of UT_TIME_MEMBER directly.
190 Ulrich Drepper wrote:
191 ``... GNU libc (and perhaps other libcs as well) have extended
192 utmp file formats which do not use a simple time_t ut_time field.
193 In glibc, ut_time is a macro which selects for backward compatibility
194 the tv_sec member of a struct timeval value.'' */
195 time_t t = UT_TIME_MEMBER (utmp_ent);
196 struct tm *tmp = localtime (&t);
198 if (tmp)
200 strftime (buf, sizeof buf, time_format, tmp);
201 return buf;
203 else
204 return TYPE_SIGNED (time_t) ? imaxtostr (t, buf) : umaxtostr (t, buf);
207 /* Display a line of information about UTMP_ENT. */
209 static void
210 print_entry (const STRUCT_UTMP *utmp_ent)
212 struct stat stats;
213 time_t last_change;
214 char mesg;
216 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
217 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
219 char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
221 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
222 already an absolute pathname. Some system may put the full,
223 absolute pathname in ut_line. */
224 if (utmp_ent->ut_line[0] == '/')
226 strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
227 line[sizeof (utmp_ent->ut_line)] = '\0';
229 else
231 strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
232 strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
233 line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
236 if (stat (line, &stats) == 0)
238 mesg = (stats.st_mode & S_IWGRP) ? ' ' : '*';
239 last_change = stats.st_atime;
241 else
243 mesg = '?';
244 last_change = 0;
247 printf ("%-8.*s", UT_USER_SIZE, UT_USER (utmp_ent));
249 if (include_fullname)
251 struct passwd *pw;
252 char name[UT_USER_SIZE + 1];
254 strncpy (name, UT_USER (utmp_ent), UT_USER_SIZE);
255 name[UT_USER_SIZE] = '\0';
256 pw = getpwnam (name);
257 if (pw == NULL)
258 printf (" %19s", " ???");
259 else
261 char *const comma = strchr (pw->pw_gecos, ',');
262 char *result;
264 if (comma)
265 *comma = '\0';
267 result = create_fullname (pw->pw_gecos, pw->pw_name);
268 printf (" %-19.19s", result);
269 free (result);
273 printf (" %c%-8.*s",
274 mesg, (int) sizeof (utmp_ent->ut_line), utmp_ent->ut_line);
276 if (include_idle)
278 if (last_change)
279 printf (" %-6s", idle_string (last_change));
280 else
281 printf (" %-6s", "???");
284 printf (" %s", time_string (utmp_ent));
286 #ifdef HAVE_UT_HOST
287 if (include_where && utmp_ent->ut_host[0])
289 extern char *canon_host ();
290 char ut_host[sizeof (utmp_ent->ut_host) + 1];
291 char *host = 0, *display = 0;
293 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
294 strncpy (ut_host, utmp_ent->ut_host, (int) sizeof (utmp_ent->ut_host));
295 ut_host[sizeof (utmp_ent->ut_host)] = '\0';
297 /* Look for an X display. */
298 display = strchr (ut_host, ':');
299 if (display)
300 *display++ = '\0';
302 if (*ut_host)
303 /* See if we can canonicalize it. */
304 host = canon_host (ut_host);
305 if ( ! host)
306 host = ut_host;
308 if (display)
309 printf (" %s:%s", host, display);
310 else
311 printf (" %s", host);
313 if (host != ut_host)
314 free (host);
316 #endif
318 putchar ('\n');
321 /* Display a verbose line of information about UTMP_ENT. */
323 static void
324 print_long_entry (const char name[])
326 struct passwd *pw;
328 pw = getpwnam (name);
330 printf (_("Login name: "));
331 printf ("%-28s", name);
333 printf (_("In real life: "));
334 if (pw == NULL)
336 printf (" %s", _("???\n"));
337 return;
339 else
341 char *const comma = strchr (pw->pw_gecos, ',');
342 char *result;
344 if (comma)
345 *comma = '\0';
347 result = create_fullname (pw->pw_gecos, pw->pw_name);
348 printf (" %s", result);
349 free (result);
352 putchar ('\n');
354 if (include_home_and_shell)
356 printf (_("Directory: "));
357 printf ("%-29s", pw->pw_dir);
358 printf (_("Shell: "));
359 printf (" %s", pw->pw_shell);
360 putchar ('\n');
363 if (include_project)
365 FILE *stream;
366 char buf[1024];
367 const char *const baseproject = "/.project";
368 char *const project =
369 xmalloc (strlen (pw->pw_dir) + strlen (baseproject) + 1);
371 strcpy (project, pw->pw_dir);
372 strcat (project, baseproject);
374 stream = fopen (project, "r");
375 if (stream)
377 size_t bytes;
379 printf (_("Project: "));
381 while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
382 fwrite (buf, 1, bytes, stdout);
383 fclose (stream);
386 free (project);
389 if (include_plan)
391 FILE *stream;
392 char buf[1024];
393 const char *const baseplan = "/.plan";
394 char *const plan =
395 xmalloc (strlen (pw->pw_dir) + strlen (baseplan) + 1);
397 strcpy (plan, pw->pw_dir);
398 strcat (plan, baseplan);
400 stream = fopen (plan, "r");
401 if (stream)
403 size_t bytes;
405 printf (_("Plan:\n"));
407 while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
408 fwrite (buf, 1, bytes, stdout);
409 fclose (stream);
412 free (plan);
415 putchar ('\n');
418 /* Print the username of each valid entry and the number of valid entries
419 in UTMP_BUF, which should have N elements. */
421 static void
422 print_heading (void)
424 printf ("%-8s", _("Login"));
425 if (include_fullname)
426 printf (" %-19s", _("Name"));
427 printf (" %-9s", _(" TTY"));
428 if (include_idle)
429 printf (" %-6s", _("Idle"));
430 printf (" %-*s", time_format_width, _("When"));
431 #ifdef HAVE_UT_HOST
432 if (include_where)
433 printf (" %s", _("Where"));
434 #endif
435 putchar ('\n');
438 /* Display UTMP_BUF, which should have N entries. */
440 static void
441 scan_entries (size_t n, const STRUCT_UTMP *utmp_buf,
442 const int argc_names, char *const argv_names[])
444 if (hard_locale (LC_TIME))
446 time_format = "%Y-%m-%d %H:%M";
447 time_format_width = 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2;
449 else
451 time_format = "%b %e %H:%M";
452 time_format_width = 3 + 1 + 2 + 1 + 2 + 1 + 2;
455 if (include_heading)
456 print_heading ();
458 while (n--)
460 if (IS_USER_PROCESS (utmp_buf))
462 if (argc_names)
464 int i;
466 for (i = 0; i < argc_names; i++)
467 if (strncmp (UT_USER (utmp_buf), argv_names[i], UT_USER_SIZE)
468 == 0)
470 print_entry (utmp_buf);
471 break;
474 else
475 print_entry (utmp_buf);
477 utmp_buf++;
481 /* Display a list of who is on the system, according to utmp file FILENAME. */
483 static void
484 short_pinky (const char *filename,
485 const int argc_names, char *const argv_names[])
487 size_t n_users;
488 STRUCT_UTMP *utmp_buf;
490 if (read_utmp (filename, &n_users, &utmp_buf) != 0)
491 error (EXIT_FAILURE, errno, "%s", filename);
493 scan_entries (n_users, utmp_buf, argc_names, argv_names);
496 static void
497 long_pinky (const int argc_names, char *const argv_names[])
499 int i;
501 for (i = 0; i < argc_names; i++)
502 print_long_entry (argv_names[i]);
505 void
506 usage (int status)
508 if (status != EXIT_SUCCESS)
509 fprintf (stderr, _("Try `%s --help' for more information.\n"),
510 program_name);
511 else
513 printf (_("Usage: %s [OPTION]... [USER]...\n"), program_name);
514 fputs (_("\
516 -l produce long format output for the specified USERs\n\
517 -b omit the user's home directory and shell in long format\n\
518 -h omit the user's project file in long format\n\
519 -p omit the user's plan file in long format\n\
520 -s do short format output, this is the default\n\
521 "), stdout);
522 fputs (_("\
523 -f omit the line of column headings in short format\n\
524 -w omit the user's full name in short format\n\
525 -i omit the user's full name and remote host in short format\n\
526 -q omit the user's full name, remote host and idle time\n\
527 in short format\n\
528 "), stdout);
529 fputs (HELP_OPTION_DESCRIPTION, stdout);
530 fputs (VERSION_OPTION_DESCRIPTION, stdout);
531 printf (_("\
533 A lightweight `finger' program; print user information.\n\
534 The utmp file will be %s.\n\
535 "), UTMP_FILE);
536 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
538 exit (status);
542 main (int argc, char **argv)
544 int optc;
545 int n_users;
547 initialize_main (&argc, &argv);
548 program_name = argv[0];
549 setlocale (LC_ALL, "");
550 bindtextdomain (PACKAGE, LOCALEDIR);
551 textdomain (PACKAGE);
553 atexit (close_stdout);
555 while ((optc = getopt_long (argc, argv, "sfwiqbhlp", longopts, NULL)) != -1)
557 switch (optc)
559 case 's':
560 do_short_format = true;
561 break;
563 case 'l':
564 do_short_format = false;
565 break;
567 case 'f':
568 include_heading = false;
569 break;
571 case 'w':
572 include_fullname = false;
573 break;
575 case 'i':
576 include_fullname = false;
577 #ifdef HAVE_UT_HOST
578 include_where = false;
579 #endif
580 break;
582 case 'q':
583 include_fullname = false;
584 #ifdef HAVE_UT_HOST
585 include_where = false;
586 #endif
587 include_idle = false;
588 break;
590 case 'h':
591 include_project = false;
592 break;
594 case 'p':
595 include_plan = false;
596 break;
598 case 'b':
599 include_home_and_shell = false;
600 break;
602 case_GETOPT_HELP_CHAR;
604 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
606 default:
607 usage (EXIT_FAILURE);
611 n_users = argc - optind;
613 if (!do_short_format && n_users == 0)
615 error (0, 0, _("no username specified; at least one must be\
616 specified when using -l"));
617 usage (EXIT_FAILURE);
620 if (do_short_format)
621 short_pinky (UTMP_FILE, n_users, argv + optind);
622 else
623 long_pinky (n_users, argv + optind);
625 exit (EXIT_SUCCESS);