/apue/tlpi-book/procexec/acct_view.c

https://gitlab.com/Gentio/my-pdf · C · 98 lines · 70 code · 18 blank · 10 comment · 15 complexity · 45073b652636e57616cacf8c53c3e32e MD5 · raw file

  1. /*************************************************************************\
  2. * Copyright (C) Michael Kerrisk, 2015. *
  3. * *
  4. * This program is free software. You may use, modify, and redistribute it *
  5. * under the terms of the GNU General Public License as published by the *
  6. * Free Software Foundation, either version 3 or (at your option) any *
  7. * later version. This program is distributed without any warranty. See *
  8. * the file COPYING.gpl-v3 for details. *
  9. \*************************************************************************/
  10. /* Listing 28-2 */
  11. #include <fcntl.h>
  12. #include <time.h>
  13. #include <sys/stat.h>
  14. #include <sys/acct.h>
  15. #include <limits.h>
  16. #include "ugid_functions.h" /* Declaration of userNameFromId() */
  17. #include "tlpi_hdr.h"
  18. #define TIME_BUF_SIZE 100
  19. static long long /* Convert comp_t value into long long */
  20. comptToLL(comp_t ct)
  21. {
  22. const int EXP_SIZE = 3; /* 3-bit, base-8 exponent */
  23. const int MANTISSA_SIZE = 13; /* Followed by 13-bit mantissa */
  24. const int MANTISSA_MASK = (1 << MANTISSA_SIZE) - 1;
  25. long long mantissa, exp;
  26. mantissa = ct & MANTISSA_MASK;
  27. exp = (ct >> MANTISSA_SIZE) & ((1 << EXP_SIZE) - 1);
  28. return mantissa << (exp * 3); /* Power of 8 = left shift 3 bits */
  29. }
  30. int
  31. main(int argc, char *argv[])
  32. {
  33. int acctFile;
  34. struct acct ac;
  35. ssize_t numRead;
  36. char *s;
  37. char timeBuf[TIME_BUF_SIZE];
  38. struct tm *loc;
  39. time_t t;
  40. if (argc != 2 || strcmp(argv[1], "--help") == 0)
  41. usageErr("%s file\n", argv[0]);
  42. acctFile = open(argv[1], O_RDONLY);
  43. if (acctFile == -1)
  44. errExit("open");
  45. printf("command flags term. user "
  46. "start time CPU elapsed\n");
  47. printf(" status "
  48. " time time\n");
  49. while ((numRead = read(acctFile, &ac, sizeof(struct acct))) > 0) {
  50. if (numRead != sizeof(struct acct))
  51. fatal("partial read");
  52. printf("%-8.8s ", ac.ac_comm);
  53. printf("%c", (ac.ac_flag & AFORK) ? 'F' : '-') ;
  54. printf("%c", (ac.ac_flag & ASU) ? 'S' : '-') ;
  55. printf("%c", (ac.ac_flag & AXSIG) ? 'X' : '-') ;
  56. printf("%c", (ac.ac_flag & ACORE) ? 'C' : '-') ;
  57. #ifdef __linux__
  58. printf(" %#6lx ", (unsigned long) ac.ac_exitcode);
  59. #else /* Many other implementations provide ac_stat instead */
  60. printf(" %#6lx ", (unsigned long) ac.ac_stat);
  61. #endif
  62. s = userNameFromId(ac.ac_uid);
  63. printf("%-8.8s ", (s == NULL) ? "???" : s);
  64. t = ac.ac_btime;
  65. loc = localtime(&t);
  66. if (loc == NULL) {
  67. printf("???Unknown time??? ");
  68. } else {
  69. strftime(timeBuf, TIME_BUF_SIZE, "%Y-%m-%d %T ", loc);
  70. printf("%s ", timeBuf);
  71. }
  72. printf("%5.2f %7.2f ", (double) (comptToLL(ac.ac_utime) +
  73. comptToLL(ac.ac_stime)) / sysconf(_SC_CLK_TCK),
  74. (double) comptToLL(ac.ac_etime) / sysconf(_SC_CLK_TCK));
  75. printf("\n");
  76. }
  77. if (numRead == -1)
  78. errExit("read");
  79. exit(EXIT_SUCCESS);
  80. }