/* test.c
 *
 * author : marco corvi <marco_corvi@geocities.com>
 * date   : feb 2001
 *
 * This is the user program that test the proc time-module
 * Usage:
 *        test [Times]
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


#define N 20
#define LENGTH 32

int main( int argc, char ** argv ) 
{
  int dim;
  struct timeval * gtodTimes;
  char ** procTimes;
  FILE * fp;
  int i;

  if (argc <= 1) {
    dim = N;
  } else {
    dim = atoi( argv[1] );
  }

  gtodTimes = (struct timeval *)malloc( dim * sizeof(struct timeval) );
  procTimes = (char **)malloc( dim * sizeof(char *) );
  for (i=0; i<dim; i++) {
    procTimes[i] = (char *)malloc( LENGTH * sizeof(char) );
    memset( procTimes[i], 0, LENGTH);
  }

  fp = fopen( "/proc/my_time", "r" );
  for (i=0; i<dim; ++i) {
    gettimeofday( &gtodTimes[i], NULL /* struct timezone * */ );
    /*
     * N.B. Use fgets() as this return when it encounters either EOF or
     * '\n'. The module writes ia a line-oriented fashion, putting a '\n'
     * at the end of each read buffer.
     */
    fgets( procTimes[i], LENGTH, fp);
    /*
     * Do a tight loop because "xtime" is updated with a HZ frequency,
     * i.e. every 10 msecs. On my system this is changed once every
     * two or three times.
     */
    {
      int j, k=0;
      for (j=0; j<1024*128; ++j) ++k;
    }
  }
  fclose(fp);

  for (i=0; i<dim; ++i) {
    /* strip off the '\n' from the procTimes string */
    procTimes[i][strlen(procTimes[i])-1] = 0;
    printf("Proc Time %s Time of Day %ld %ld\n",
      procTimes[i], (unsigned long)gtodTimes[i].tv_sec, 
                    (unsigned long)gtodTimes[i].tv_usec );
  }
  return 0;

}

