chiark / gitweb /
finalise 6.0.0
[chiark-utils.git] / cprogs / acctdump.c
index 566840294c954ca3c2a6e3d8816c470d512f1905..b9040ac710f91eebe6a83af73262a2027a4e4e29 100644 (file)
@@ -42,6 +42,9 @@ typedef unsigned long long u64;
 #include <sys/acct.h>
 
 typedef struct acct_v3 struct_acct;
+#define HAVE_AC_EXITCODE
+#define HAVE_AC_FLT
+#define FIELD_AC_FLAG(as) ((as)->ac_flag)
 
 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
 
@@ -50,6 +53,8 @@ typedef struct acct_v3 struct_acct;
 #include <sys/acct.h>
 
 typedef struct acctv2 struct_acct;
+#define HAVE_AC_IO_MEM
+#define FIELD_AC_FLAG(as) ((as)->ac_flagx & ~ANVER)
 
 #else
 
@@ -77,12 +82,14 @@ static const struct cmdinfo cmdinfos[]= {
   {  0                                                 }
 };
 
+#ifdef HAVE_AC_EXITCODE
 static const char *sigabbrev[]= {
   "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "BUS", "FPE",
   "KILL", "USR1", "SEGV", "USR2", "PIPE", "ALRM", "TERM", "STKFLT",
   "CHLD", "CONT", "STOP", "TSTP", "TTIN", "TTOU", "URG", "XCPU",
   "XFSZ", "VTALRM", "PROF", "WINCH", "IO"
 };
+#endif
 
 void usagemessage(void) {
   fputs("usage: acctdump [<options>] [<file> ...]\n"
@@ -160,16 +167,28 @@ static int walkdev_cptr(const void *av, const void *bv) {
 static void printbanner(void) {
   if (raw) {
     fputs("begin date command          "
-         "uid      gid      tty dev  FSDX exit",
-         stdout);
+         "uid      gid      tty dev  FSDX "
+#ifdef HAVE_AC_EXITCODE
+         "exit"
+#endif
+         , stdout);
   } else {
     fputs("begin date and time command          "
-         "user     group    tty dev    FSDX sigexit",
-         stdout);
+         "user     group    tty dev    FSDX "
+#ifdef HAVE_AC_EXITCODE
+         "sigexit"
+#endif
+         , stdout);
   }
   if (usages) {
-    fputs("  user time   sys time  elap time   minflt   maxflt",
-         stdout);
+    fputs("  user time   sys time  elap time "
+#ifdef HAVE_AC_FLT
+         "  minflt   maxflt"
+#endif
+#ifdef HAVE_AC_IO_MEM
+         "  avg.mem      io"
+#endif
+         , stdout);
   }
   putchar('\n');
   checkstdout();
@@ -178,7 +197,7 @@ static void printbanner(void) {
 static void printrecord(const struct_acct *as, const char *filename) {
   static int walkeddev;
 
-  int i, dc, r;
+  int i, r;
   const char *fp;
   char buf[100];
   struct tm *tm;
@@ -186,17 +205,26 @@ static void printrecord(const struct_acct *as, const char *filename) {
   struct passwd *pw;
   struct group *gr;
   time_t btime;
+  char commbuf[sizeof(as->ac_comm)];
 
   if (raw) {
     printf("%10lu ",(unsigned long)as->ac_btime);
   } else {
     btime= as->ac_btime;
     tm= localtime(&btime);
-    strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S",tm); buf[sizeof(buf)-1]= 0;
+    if (tm) {
+      strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S",tm); buf[sizeof(buf)-1]= 0;
+    } else {
+      snprintf(buf,sizeof(buf),"@%lu",(unsigned long)btime);
+    }
     printf("%19s ",buf);
   }
-  
-  printf("%-16.16s ", as->ac_comm);
+
+  for (i=0; i<sizeof(as->ac_comm); i++) {
+    int c=as->ac_comm[i];
+    commbuf[i]= ((c!=0 && c<=32) || c>=127) ? '?' : c;
+  }
+  printf("%-*.*s ", (int)sizeof(commbuf),(int)sizeof(commbuf), commbuf);
   
   pw= raw ? 0 : getpwuid(as->ac_uid);
   if (pw) printf("%-8s ",pw->pw_name);
@@ -232,7 +260,7 @@ static void printrecord(const struct_acct *as, const char *filename) {
     }
   }
 
-  r= as->ac_flag;
+  r= FIELD_AC_FLAG(as);
   for (i=1, fp= "FS4DX"; *fp; fp++, i<<=1) {
     if (r&i) {
       putchar(*fp);
@@ -245,7 +273,9 @@ static void printrecord(const struct_acct *as, const char *filename) {
     printf("#%x",r);
   }
   putchar(' ');
-  
+
+#ifdef HAVE_AC_EXITCODE
+  int dc;
   dc= WCOREDUMP(as->ac_exitcode) ? 'd' : 'k';
   if (raw) {
     if (WIFEXITED(as->ac_exitcode)) {
@@ -275,14 +305,23 @@ static void printrecord(const struct_acct *as, const char *filename) {
       printf("#%04lx",(unsigned long)as->ac_exitcode);
     }
   }
+#endif /*HAVE_AC_EXITCODE*/
 
   if (usages) {
-    printf(" %10lu %10lu %10lu %8ld %8ld",
+    printf(" %10lu %10lu %10lu",
           (unsigned long)as->ac_utime,
           (unsigned long)as->ac_stime,
-          (unsigned long)as->ac_etime,
+          (unsigned long)as->ac_etime);
+#ifdef HAVE_AC_FLT
+    printf(" %8lu %8lu",
           (unsigned long)as->ac_minflt,
           (unsigned long)as->ac_majflt);
+#endif
+#ifdef HAVE_AC_IO_MEM
+    printf(" %4e %4e",
+          as->ac_mem,
+          as->ac_io);
+#endif
   }
   putchar('\n');