Listing 2. Parsing the Accounting File

/*
 * paread.c: Linux program to demonstrate reading a
 * process accounting record into memory.
 */

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/acct.h>

int
main (int argc, char *argv[])
{

   int fd;
   int bytesread;

   struct acct a; /* accounting record */

   if (argc == 1)
   {
      printf("You must supply a filename"
        " on the cmd line\n");
      return -1;
   }


   fd = open (argv[1], O_RDONLY);
   if (fd == -1)
   {
      perror ("Problem opening specified file");
      return -1;
   }

   /*
    * Read and print command name from each record
    * in the file
    */
   while ((bytesread =
          read (fd, &a, sizeof (struct acct))) > 0)
   {
      printf ("%s\n", a.ac_comm);
   }
   return 0;
}