PageRenderTime 158ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/pinky.c

https://gitlab.com/oyvholm/coreutils
C | 607 lines | 439 code | 108 blank | 60 comment | 66 complexity | cb6501d6e3993194a6e24434bebb8283 MD5 | raw file
  1. /* GNU's pinky.
  2. Copyright (C) 1992-2016 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Created by hacking who.c by Kaveh Ghazi ghazi@caip.rutgers.edu */
  14. #include <config.h>
  15. #include <getopt.h>
  16. #include <pwd.h>
  17. #include <stdio.h>
  18. #include <sys/types.h>
  19. #include "system.h"
  20. #include "canon-host.h"
  21. #include "error.h"
  22. #include "hard-locale.h"
  23. #include "readutmp.h"
  24. /* The official name of this program (e.g., no 'g' prefix). */
  25. #define PROGRAM_NAME "pinky"
  26. #define AUTHORS \
  27. proper_name ("Joseph Arceneaux"), \
  28. proper_name ("David MacKenzie"), \
  29. proper_name ("Kaveh Ghazi")
  30. char *ttyname (int);
  31. /* If true, display the hours:minutes since each user has touched
  32. the keyboard, or blank if within the last minute, or days followed
  33. by a 'd' if not within the last day. */
  34. static bool include_idle = true;
  35. /* If true, display a line at the top describing each field. */
  36. static bool include_heading = true;
  37. /* if true, display the user's full name from pw_gecos. */
  38. static bool include_fullname = true;
  39. /* if true, display the user's ~/.project file when doing long format. */
  40. static bool include_project = true;
  41. /* if true, display the user's ~/.plan file when doing long format. */
  42. static bool include_plan = true;
  43. /* if true, display the user's home directory and shell
  44. when doing long format. */
  45. static bool include_home_and_shell = true;
  46. /* if true, use the "short" output format. */
  47. static bool do_short_format = true;
  48. /* if true, display the ut_host field. */
  49. #ifdef HAVE_UT_HOST
  50. static bool include_where = true;
  51. #endif
  52. /* The strftime format to use for login times, and its expected
  53. output width. */
  54. static char const *time_format;
  55. static int time_format_width;
  56. static struct option const longopts[] =
  57. {
  58. {GETOPT_HELP_OPTION_DECL},
  59. {GETOPT_VERSION_OPTION_DECL},
  60. {NULL, 0, NULL, 0}
  61. };
  62. /* Count and return the number of ampersands in STR. */
  63. static size_t _GL_ATTRIBUTE_PURE
  64. count_ampersands (const char *str)
  65. {
  66. size_t count = 0;
  67. do
  68. {
  69. if (*str == '&')
  70. count++;
  71. } while (*str++);
  72. return count;
  73. }
  74. /* Create a string (via xmalloc) which contains a full name by substituting
  75. for each ampersand in GECOS_NAME the USER_NAME string with its first
  76. character capitalized. The caller must ensure that GECOS_NAME contains
  77. no ','s. The caller also is responsible for free'ing the return value of
  78. this function. */
  79. static char *
  80. create_fullname (const char *gecos_name, const char *user_name)
  81. {
  82. size_t rsize = strlen (gecos_name) + 1;
  83. char *result;
  84. char *r;
  85. size_t ampersands = count_ampersands (gecos_name);
  86. if (ampersands != 0)
  87. {
  88. size_t ulen = strlen (user_name);
  89. size_t product = ampersands * ulen;
  90. rsize += product - ampersands;
  91. if (xalloc_oversized (ulen, ampersands) || rsize < product)
  92. xalloc_die ();
  93. }
  94. r = result = xmalloc (rsize);
  95. while (*gecos_name)
  96. {
  97. if (*gecos_name == '&')
  98. {
  99. const char *uname = user_name;
  100. if (islower (to_uchar (*uname)))
  101. *r++ = toupper (to_uchar (*uname++));
  102. while (*uname)
  103. *r++ = *uname++;
  104. }
  105. else
  106. {
  107. *r++ = *gecos_name;
  108. }
  109. gecos_name++;
  110. }
  111. *r = 0;
  112. return result;
  113. }
  114. /* Return a string representing the time between WHEN and the time
  115. that this function is first run. */
  116. static const char *
  117. idle_string (time_t when)
  118. {
  119. static time_t now = 0;
  120. static char buf[INT_STRLEN_BOUND (long int) + 2];
  121. time_t seconds_idle;
  122. if (now == 0)
  123. time (&now);
  124. seconds_idle = now - when;
  125. if (seconds_idle < 60) /* One minute. */
  126. return " ";
  127. if (seconds_idle < (24 * 60 * 60)) /* One day. */
  128. {
  129. int hours = seconds_idle / (60 * 60);
  130. int minutes = (seconds_idle % (60 * 60)) / 60;
  131. sprintf (buf, "%02d:%02d", hours, minutes);
  132. }
  133. else
  134. {
  135. unsigned long int days = seconds_idle / (24 * 60 * 60);
  136. sprintf (buf, "%lud", days);
  137. }
  138. return buf;
  139. }
  140. /* Return a time string. */
  141. static const char *
  142. time_string (const STRUCT_UTMP *utmp_ent)
  143. {
  144. static char buf[INT_STRLEN_BOUND (intmax_t) + sizeof "-%m-%d %H:%M"];
  145. /* Don't take the address of UT_TIME_MEMBER directly.
  146. Ulrich Drepper wrote:
  147. "... GNU libc (and perhaps other libcs as well) have extended
  148. utmp file formats which do not use a simple time_t ut_time field.
  149. In glibc, ut_time is a macro which selects for backward compatibility
  150. the tv_sec member of a struct timeval value." */
  151. time_t t = UT_TIME_MEMBER (utmp_ent);
  152. struct tm *tmp = localtime (&t);
  153. if (tmp)
  154. {
  155. strftime (buf, sizeof buf, time_format, tmp);
  156. return buf;
  157. }
  158. else
  159. return timetostr (t, buf);
  160. }
  161. /* Display a line of information about UTMP_ENT. */
  162. static void
  163. print_entry (const STRUCT_UTMP *utmp_ent)
  164. {
  165. struct stat stats;
  166. time_t last_change;
  167. char mesg;
  168. #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
  169. #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
  170. char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
  171. char *p = line;
  172. /* Copy ut_line into LINE, prepending '/dev/' if ut_line is not
  173. already an absolute file name. Some system may put the full,
  174. absolute file name in ut_line. */
  175. if ( ! IS_ABSOLUTE_FILE_NAME (utmp_ent->ut_line))
  176. p = stpcpy (p, DEV_DIR_WITH_TRAILING_SLASH);
  177. stzncpy (p, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
  178. if (stat (line, &stats) == 0)
  179. {
  180. mesg = (stats.st_mode & S_IWGRP) ? ' ' : '*';
  181. last_change = stats.st_atime;
  182. }
  183. else
  184. {
  185. mesg = '?';
  186. last_change = 0;
  187. }
  188. printf ("%-8.*s", UT_USER_SIZE, UT_USER (utmp_ent));
  189. if (include_fullname)
  190. {
  191. struct passwd *pw;
  192. char name[UT_USER_SIZE + 1];
  193. stzncpy (name, UT_USER (utmp_ent), UT_USER_SIZE);
  194. pw = getpwnam (name);
  195. if (pw == NULL)
  196. /* TRANSLATORS: Real name is unknown; at most 19 characters. */
  197. printf (" %19s", _(" ???"));
  198. else
  199. {
  200. char *const comma = strchr (pw->pw_gecos, ',');
  201. char *result;
  202. if (comma)
  203. *comma = '\0';
  204. result = create_fullname (pw->pw_gecos, pw->pw_name);
  205. printf (" %-19.19s", result);
  206. free (result);
  207. }
  208. }
  209. printf (" %c%-8.*s",
  210. mesg, (int) sizeof (utmp_ent->ut_line), utmp_ent->ut_line);
  211. if (include_idle)
  212. {
  213. if (last_change)
  214. printf (" %-6s", idle_string (last_change));
  215. else
  216. /* TRANSLATORS: Idle time is unknown; at most 5 characters. */
  217. printf (" %-6s", _("?????"));
  218. }
  219. printf (" %s", time_string (utmp_ent));
  220. #ifdef HAVE_UT_HOST
  221. if (include_where && utmp_ent->ut_host[0])
  222. {
  223. char ut_host[sizeof (utmp_ent->ut_host) + 1];
  224. char *host = NULL;
  225. char *display = NULL;
  226. /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
  227. stzncpy (ut_host, utmp_ent->ut_host, sizeof (utmp_ent->ut_host));
  228. /* Look for an X display. */
  229. display = strchr (ut_host, ':');
  230. if (display)
  231. *display++ = '\0';
  232. if (*ut_host)
  233. /* See if we can canonicalize it. */
  234. host = canon_host (ut_host);
  235. if ( ! host)
  236. host = ut_host;
  237. if (display)
  238. printf (" %s:%s", host, display);
  239. else
  240. printf (" %s", host);
  241. if (host != ut_host)
  242. free (host);
  243. }
  244. #endif
  245. putchar ('\n');
  246. }
  247. /* Display a verbose line of information about UTMP_ENT. */
  248. static void
  249. print_long_entry (const char name[])
  250. {
  251. struct passwd *pw;
  252. pw = getpwnam (name);
  253. printf (_("Login name: "));
  254. printf ("%-28s", name);
  255. printf (_("In real life: "));
  256. if (pw == NULL)
  257. {
  258. /* TRANSLATORS: Real name is unknown; no hard limit. */
  259. printf (" %s", _("???\n"));
  260. return;
  261. }
  262. else
  263. {
  264. char *const comma = strchr (pw->pw_gecos, ',');
  265. char *result;
  266. if (comma)
  267. *comma = '\0';
  268. result = create_fullname (pw->pw_gecos, pw->pw_name);
  269. printf (" %s", result);
  270. free (result);
  271. }
  272. putchar ('\n');
  273. if (include_home_and_shell)
  274. {
  275. printf (_("Directory: "));
  276. printf ("%-29s", pw->pw_dir);
  277. printf (_("Shell: "));
  278. printf (" %s", pw->pw_shell);
  279. putchar ('\n');
  280. }
  281. if (include_project)
  282. {
  283. FILE *stream;
  284. char buf[1024];
  285. const char *const baseproject = "/.project";
  286. char *const project =
  287. xmalloc (strlen (pw->pw_dir) + strlen (baseproject) + 1);
  288. stpcpy (stpcpy (project, pw->pw_dir), baseproject);
  289. stream = fopen (project, "r");
  290. if (stream)
  291. {
  292. size_t bytes;
  293. printf (_("Project: "));
  294. while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
  295. fwrite (buf, 1, bytes, stdout);
  296. fclose (stream);
  297. }
  298. free (project);
  299. }
  300. if (include_plan)
  301. {
  302. FILE *stream;
  303. char buf[1024];
  304. const char *const baseplan = "/.plan";
  305. char *const plan =
  306. xmalloc (strlen (pw->pw_dir) + strlen (baseplan) + 1);
  307. stpcpy (stpcpy (plan, pw->pw_dir), baseplan);
  308. stream = fopen (plan, "r");
  309. if (stream)
  310. {
  311. size_t bytes;
  312. printf (_("Plan:\n"));
  313. while ((bytes = fread (buf, 1, sizeof (buf), stream)) > 0)
  314. fwrite (buf, 1, bytes, stdout);
  315. fclose (stream);
  316. }
  317. free (plan);
  318. }
  319. putchar ('\n');
  320. }
  321. /* Print the username of each valid entry and the number of valid entries
  322. in UTMP_BUF, which should have N elements. */
  323. static void
  324. print_heading (void)
  325. {
  326. printf ("%-8s", _("Login"));
  327. if (include_fullname)
  328. printf (" %-19s", _("Name"));
  329. printf (" %-9s", _(" TTY"));
  330. if (include_idle)
  331. printf (" %-6s", _("Idle"));
  332. printf (" %-*s", time_format_width, _("When"));
  333. #ifdef HAVE_UT_HOST
  334. if (include_where)
  335. printf (" %s", _("Where"));
  336. #endif
  337. putchar ('\n');
  338. }
  339. /* Display UTMP_BUF, which should have N entries. */
  340. static void
  341. scan_entries (size_t n, const STRUCT_UTMP *utmp_buf,
  342. const int argc_names, char *const argv_names[])
  343. {
  344. if (hard_locale (LC_TIME))
  345. {
  346. time_format = "%Y-%m-%d %H:%M";
  347. time_format_width = 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2;
  348. }
  349. else
  350. {
  351. time_format = "%b %e %H:%M";
  352. time_format_width = 3 + 1 + 2 + 1 + 2 + 1 + 2;
  353. }
  354. if (include_heading)
  355. print_heading ();
  356. while (n--)
  357. {
  358. if (IS_USER_PROCESS (utmp_buf))
  359. {
  360. if (argc_names)
  361. {
  362. int i;
  363. for (i = 0; i < argc_names; i++)
  364. if (STREQ_LEN (UT_USER (utmp_buf), argv_names[i], UT_USER_SIZE))
  365. {
  366. print_entry (utmp_buf);
  367. break;
  368. }
  369. }
  370. else
  371. print_entry (utmp_buf);
  372. }
  373. utmp_buf++;
  374. }
  375. }
  376. /* Display a list of who is on the system, according to utmp file FILENAME. */
  377. static void
  378. short_pinky (const char *filename,
  379. const int argc_names, char *const argv_names[])
  380. {
  381. size_t n_users;
  382. STRUCT_UTMP *utmp_buf = NULL;
  383. if (read_utmp (filename, &n_users, &utmp_buf, 0) != 0)
  384. error (EXIT_FAILURE, errno, "%s", quotef (filename));
  385. scan_entries (n_users, utmp_buf, argc_names, argv_names);
  386. IF_LINT (free (utmp_buf));
  387. }
  388. static void
  389. long_pinky (const int argc_names, char *const argv_names[])
  390. {
  391. int i;
  392. for (i = 0; i < argc_names; i++)
  393. print_long_entry (argv_names[i]);
  394. }
  395. void
  396. usage (int status)
  397. {
  398. if (status != EXIT_SUCCESS)
  399. emit_try_help ();
  400. else
  401. {
  402. printf (_("Usage: %s [OPTION]... [USER]...\n"), program_name);
  403. fputs (_("\
  404. \n\
  405. -l produce long format output for the specified USERs\n\
  406. -b omit the user's home directory and shell in long format\n\
  407. -h omit the user's project file in long format\n\
  408. -p omit the user's plan file in long format\n\
  409. -s do short format output, this is the default\n\
  410. "), stdout);
  411. fputs (_("\
  412. -f omit the line of column headings in short format\n\
  413. -w omit the user's full name in short format\n\
  414. -i omit the user's full name and remote host in short format\n\
  415. -q omit the user's full name, remote host and idle time\n\
  416. in short format\n\
  417. "), stdout);
  418. fputs (HELP_OPTION_DESCRIPTION, stdout);
  419. fputs (VERSION_OPTION_DESCRIPTION, stdout);
  420. printf (_("\
  421. \n\
  422. A lightweight 'finger' program; print user information.\n\
  423. The utmp file will be %s.\n\
  424. "), UTMP_FILE);
  425. emit_ancillary_info (PROGRAM_NAME);
  426. }
  427. exit (status);
  428. }
  429. int
  430. main (int argc, char **argv)
  431. {
  432. int optc;
  433. int n_users;
  434. initialize_main (&argc, &argv);
  435. set_program_name (argv[0]);
  436. setlocale (LC_ALL, "");
  437. bindtextdomain (PACKAGE, LOCALEDIR);
  438. textdomain (PACKAGE);
  439. atexit (close_stdout);
  440. while ((optc = getopt_long (argc, argv, "sfwiqbhlp", longopts, NULL)) != -1)
  441. {
  442. switch (optc)
  443. {
  444. case 's':
  445. do_short_format = true;
  446. break;
  447. case 'l':
  448. do_short_format = false;
  449. break;
  450. case 'f':
  451. include_heading = false;
  452. break;
  453. case 'w':
  454. include_fullname = false;
  455. break;
  456. case 'i':
  457. include_fullname = false;
  458. #ifdef HAVE_UT_HOST
  459. include_where = false;
  460. #endif
  461. break;
  462. case 'q':
  463. include_fullname = false;
  464. #ifdef HAVE_UT_HOST
  465. include_where = false;
  466. #endif
  467. include_idle = false;
  468. break;
  469. case 'h':
  470. include_project = false;
  471. break;
  472. case 'p':
  473. include_plan = false;
  474. break;
  475. case 'b':
  476. include_home_and_shell = false;
  477. break;
  478. case_GETOPT_HELP_CHAR;
  479. case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
  480. default:
  481. usage (EXIT_FAILURE);
  482. }
  483. }
  484. n_users = argc - optind;
  485. if (!do_short_format && n_users == 0)
  486. {
  487. error (0, 0, _("no username specified; at least one must be\
  488. specified when using -l"));
  489. usage (EXIT_FAILURE);
  490. }
  491. if (do_short_format)
  492. short_pinky (UTMP_FILE, n_users, argv + optind);
  493. else
  494. long_pinky (n_users, argv + optind);
  495. return EXIT_SUCCESS;
  496. }