procexec/acct_view.cThis is procexec/acct_view.c (Listing 28-2, page 596), an example from the book, The Linux Programming Interface. The source code file is copyright 2024, Michael Kerrisk, and is licensed under the GNU General Public License, version 3. This page shows the "distribution" or "book" version of the file (why are there two versions?), or the differences between the two versions. You can switch between the views using the tabs below. In the listing below, the names of Linux system calls and C library functions are hyperlinked to manual pages from the Linux man-pages project, and the names of functions implemented in the book are hyperlinked to the implementations of those functions. |
+/* acct_view.c + + Display contents of a process accounting file. +*/ #include <fcntl.h> #include <time.h> #include <sys/stat.h> #include <sys/acct.h> #include <limits.h> #include "ugid_functions.h" /* Declaration of userNameFromId() */ #include "tlpi_hdr.h" #define TIME_BUF_SIZE 100 static long long /* Convert comp_t value into long long */ comptToLL(comp_t ct) { const int EXP_SIZE = 3; /* 3-bit, base-8 exponent */ const int MANTISSA_SIZE = 13; /* Followed by 13-bit mantissa */ const int MANTISSA_MASK = (1 << MANTISSA_SIZE) - 1; long long mantissa, exp; mantissa = ct & MANTISSA_MASK; exp = (ct >> MANTISSA_SIZE) & ((1 << EXP_SIZE) - 1); return mantissa << (exp * 3); /* Power of 8 = left shift 3 bits */ } int main(int argc, char *argv[]) { int acctFile; struct acct ac; ssize_t numRead; char *s; char timeBuf[TIME_BUF_SIZE]; struct tm *loc; time_t t; if (argc != 2 || strcmp(argv[1], "--help") == 0) usageErr("%s file\n", argv[0]); acctFile = open(argv[1], O_RDONLY); if (acctFile == -1) errExit("open"); printf("command flags term. user " "start time CPU elapsed\n"); printf(" status " " time time\n"); while ((numRead = read(acctFile, &ac, sizeof(struct acct))) > 0) { if (numRead != sizeof(struct acct)) fatal("partial read"); printf("%-8.8s ", ac.ac_comm); printf("%c", (ac.ac_flag & AFORK) ? 'F' : '-') ; printf("%c", (ac.ac_flag & ASU) ? 'S' : '-') ; + + /* Not all implementations support AXSIG and ACORE */ + +#ifdef AXSIG printf("%c", (ac.ac_flag & AXSIG) ? 'X' : '-') ; +#else + printf(" "); +#endif +#ifdef ACORE printf("%c", (ac.ac_flag & ACORE) ? 'C' : '-') ; +#else + printf(" "); +#endif #ifdef __linux__ printf(" %#6lx ", (unsigned long) ac.ac_exitcode); #else /* Many other implementations provide ac_stat instead */ + /* But the BSDs don't provide this field either */ +#if ! defined(__FreeBSD__) && ! defined(__NetBSD__) && \ + ! defined(__APPLE__) printf(" %#6lx ", (unsigned long) ac.ac_stat); +#else + printf(" "); +#endif #endif s = userNameFromId(ac.ac_uid); printf("%-8.8s ", (s == NULL) ? "???" : s); t = ac.ac_btime; loc = localtime(&t); if (loc == NULL) { printf("???Unknown time??? "); } else { strftime(timeBuf, TIME_BUF_SIZE, "%Y-%m-%d %T ", loc); printf("%s ", timeBuf); } printf("%5.2f %7.2f ", (double) (comptToLL(ac.ac_utime) + comptToLL(ac.ac_stime)) / sysconf(_SC_CLK_TCK), (double) comptToLL(ac.ac_etime) / sysconf(_SC_CLK_TCK)); printf("\n"); } if (numRead == -1) errExit("read"); exit(EXIT_SUCCESS); }
Note that, in most cases, the programs rendered in these web pages are not free standing: you'll typically also need a few other source files (mostly in the lib/ subdirectory) as well. Generally, it's easier to just download the entire source tarball and build the programs with make(1). By hovering your mouse over the various hyperlinked include files and function calls above, you can see which other source files this file depends on.