#include <linux/time.h>
#include <linux/timex.h>

int check_bit(long mask, long bit, char* dest, char* name);

int check_bit(long mask, long bit, char* dest, char* name)
  {
  static int first = 1;
  
  if(mask & bit)
    {
    if(first)
      {
      first = 0;
      strcat(dest, name);
      }
    else
      {
      strcat(dest, " | ");
      strcat(dest, name);
      }
    return 1;
    }
  return 0;  
  } 
  
main()
  {
  struct timex buf;
  int status;
  char statename[20];
  char statusname[150] = "\0";
  
  buf . modes = 0;
  status = adjtimex(&buf);

  switch(status)
    {
    case TIME_OK:
	strcpy(statename, "TIME_OK");
      	break;
    case TIME_ERROR:
	strcpy(statename, "TIME_ERROR");
	break;
    case TIME_INS:
	strcpy(statename, "TIME_INS");
	break;
    case TIME_DEL:
	strcpy(statename, "TIME_DEL");
	break;
    case TIME_OOP:
	strcpy(statename, "TIME_OOP");
	break;
    case TIME_WAIT:
	strcpy(statename, "TIME_WAIT");
	break;
    default:
	strcpy(statename, "wartosc nieznana");
    }

  check_bit(buf.status, STA_PLL, statusname, "STA_PLL");
  check_bit(buf.status, STA_PPSFREQ, statusname, "STA_PPSFREQ");
  check_bit(buf.status, STA_PPSTIME, statusname, "STA_PPSTIME");
  check_bit(buf.status, STA_FLL, statusname, "STA_FLL");
  check_bit(buf.status, STA_INS, statusname, "STA_INS");
  check_bit(buf.status, STA_DEL, statusname, "STA_DEL");
  check_bit(buf.status, STA_UNSYNC, statusname, "STA_UNSYNC");
  check_bit(buf.status, STA_FREQHOLD, statusname, "STA_FREQHOLD");
  check_bit(buf.status, STA_PPSSIGNAL, statusname, "STA_PPSSIGNAL");
  check_bit(buf.status, STA_PPSJITTER, statusname, "STA_PPSJITTER");
  check_bit(buf.status, STA_PPSWANDER, statusname, "STA_PPSWANDER");
  check_bit(buf.status, STA_PPSERROR, statusname, "STA_PPSERROR");
  check_bit(buf.status, STA_CLOCKERR, statusname, "STA_CLOCKERR");
  
  printf("Aktualny status synchronizacji:\n");
  printf("      time_state = %d == %s\n", status, statename);
  printf("	time_status = %ld == %s\n", buf . status, statusname);
  printf("	time_adjust = %ld\n", buf . offset);
  printf("	time_freq = %ld\n", buf . freq);
  printf("	time_maxerror = %ld\n", buf . maxerror);
  printf("	time_esterror = %ld\n", buf . esterror);
  printf("	time_constant = %ld\n", buf . constant);
  printf("	time_precision = %ld\n", buf . precision);
  printf("	time_tolerance = %ld\n", buf . tolerance);
  printf("	xtime . tv_sec = %ld\n", buf . time . tv_sec);
  printf("	xtime . tv_usec = %ld\n", buf . time . tv_usec);
  printf("	tick = %ld\n", buf . tick);
  printf("	pps_freq = %ld\n", buf . ppsfreq);
  printf("	pps_jitter = %ld\n", buf . jitter);
  printf("	pps_shift = %ld\n", buf . shift);
  printf("	pps_stabil = %ld\n", buf . stabil);
  printf("	pps_jitcnt = %ld\n", buf . jitcnt);
  printf("	pps_calcnt = %ld\n", buf . calcnt);
  printf("	pps_errcnt = %ld\n", buf . errcnt);
  printf("	pps_stbcnt = %ld\n", buf . stbcnt);
  }