chiark / gitweb /
Merge branch 'master' into merge-141006
[chiark-utils.git] / cprogs / acctdump.c
index fbf7eadf24b980a619edbd4c3d2667bd077a2625..98fab9c55d4f96d0d782500894c99a5c1f62b4d1 100644 (file)
 #include <grp.h>
 #include <dirent.h>
 #include <ctype.h>
-#include <sys/stat.h>
-#include <sys/acct.h>
 #include <errno.h>
+#include <sys/stat.h>
+
+typedef unsigned long long u64;
+#include "acct.h"
+/*#include <sys/acct.h>*/
+typedef struct acct_v3 struct_acct;
 
 #include "myopt.h"
 
@@ -161,7 +165,7 @@ static void printbanner(void) {
   checkstdout();
 }
 
-static void printrecord(const struct acct *as, const char *filename) {
+static void printrecord(const struct_acct *as, const char *filename) {
   static int walkeddev;
 
   int i, dc, r;
@@ -171,11 +175,12 @@ static void printrecord(const struct acct *as, const char *filename) {
   struct deventry *deve, devlookfor;
   struct passwd *pw;
   struct group *gr;
+  time_t btime;
 
   if (raw) {
     printf("%10lu ",(unsigned long)as->ac_btime);
   } else {
-    time_t btime=as->ac_btime;
+    btime= as->ac_btime;
     tm= localtime(&btime);
     strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S",tm); buf[sizeof(buf)-1]= 0;
     printf("%19s ",buf);
@@ -192,13 +197,13 @@ static void printrecord(const struct acct *as, const char *filename) {
   else printf("%-8ld ",(long)as->ac_gid);
 
   if (raw) {
-    if (as->ac_tty == (dev_t)-1) {
+    if (!(as->ac_tty + 1) /* check for -1 without knowing type */) {
       printf("-        ");
     } else {
       printf("%08lx ",(unsigned long)as->ac_tty);
     }
   } else {
-    if (as->ac_tty == (dev_t)-1) {
+    if (!(as->ac_tty + 1)) {
       printf("-          ");
     } else {
       if (!walkeddev) {
@@ -218,11 +223,11 @@ static void printrecord(const struct acct *as, const char *filename) {
   }
 
   r= as->ac_flag;
-  for (i=1, fp= "FSDX"; *fp; fp++, i<<=1) {
+  for (i=1, fp= "FS4DX"; *fp; fp++, i<<=1) {
     if (r&i) {
       putchar(*fp);
       r &= ~i;
-    } else {
+    } else if (!isdigit(*fp)) {
       putchar(' ');
     }
   }
@@ -275,8 +280,8 @@ static void printrecord(const struct acct *as, const char *filename) {
 }
 
 static void processfile(FILE *file, const char *filename) {
+  struct_acct as;
   long pos;
-  struct acct as;
   int r;
   
   if (forwards) {
@@ -284,6 +289,7 @@ static void processfile(FILE *file, const char *filename) {
       printrecord(&as,filename);
     }
   } else {
+    long seekdist= -(long)sizeof(as);
     r= fseek(file,0,SEEK_END); if (r) { perror(filename); exit(8); }
     pos= ftell(file); if (pos==-1) { perror(filename); exit(8); }
     if (pos % sizeof(as)) { 
@@ -297,6 +303,7 @@ static void processfile(FILE *file, const char *filename) {
       r= fseek(file,pos,SEEK_SET); if (r==-1) { perror(filename); exit(8); }
       r= fread(&as,1,sizeof(as),file); if (r!=sizeof(as)) { perror(filename); exit(8); }
       printrecord(&as,filename);
+      seekdist= -2*(long)sizeof(as);
     }
   }
   if (ferror(file) || fclose(file)) { perror(filename); exit(8); }
@@ -310,8 +317,11 @@ static void processnamedfile(const char *filename) {
 }
 
 int main(int argc, const char *const *argv) {
+union { struct_acct ac; char c[1]; } xu;
   myopt(&argv,cmdinfos);
   if (!nobanner) printbanner();
+fprintf(stdout,"s=%d %d\n",(int)sizeof(struct_acct),
+       (int)((char*)&xu.ac.ac_comm - xu.c));
   if (usestdin) {
     processfile(stdin,"<standard input>");
   } else if (!*argv) {