home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / Clocks / CorrectClock / correctclock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  1.9 KB  |  92 lines

  1. /* Bruno Bienfait 1993 */
  2.  
  3.  
  4. #include <sys/time.h>
  5. #include <stdio.h>
  6. #include<math.h>
  7.  
  8. void usage( char *progname);
  9.  
  10. void main( int argc, char *argv[])
  11. {
  12.     struct timeval tp;
  13.     struct timezone tzp;
  14.     double seconds = 0 ;
  15.     long today = 0 ;
  16.     long last_day = 0 ;
  17.     int update ;
  18.  
  19.     FILE *last_day_file ;
  20.     char file_name[] ="/usr/adm/correctclock.date" ;
  21.  
  22.     gettimeofday( &tp, &tzp);
  23.     today = tp.tv_sec / (3600 * 24) ;
  24.  
  25.     if( argc < 2 )
  26.     {
  27.         printf( "%d\n", today );
  28.         exit(0) ;
  29.     }
  30.                 
  31.     if( sscanf(argv[1], "%lf",&seconds ) != 1 )
  32.         usage(argv[0]);
  33.     
  34.     if( seconds == 0 )
  35.         exit(0) ;
  36.     
  37.     if( (last_day_file = fopen(file_name,  "r")) != (FILE * )NULL)
  38.     {
  39.         if( fscanf(last_day_file, "%d",&last_day ) != 1 || last_day <= 0)
  40.         {
  41.             fprintf(stderr, "%s is unable to read last day in %s\n", argv[0], file_name);
  42.             fclose(last_day_file);
  43.             exit(1) ;
  44.         }
  45.         
  46.         fclose( last_day_file ) ;
  47.         
  48.         if( ( today - last_day ) <= 0) /* already adjusted today */
  49.             update = 0 ;
  50.         else
  51.             update = 1 ;
  52.  
  53.     }
  54.     else
  55.         update = 0 ;
  56.  
  57.  
  58.     if( (last_day_file = fopen(file_name,  "w")) == NULL)
  59.     {
  60.         fprintf(stderr, "%s is unable to write last day in %s\n", argv[0], file_name);
  61.         exit(1) ;
  62.     }
  63.     if( fprintf( last_day_file, "%d\n", today ) < 0 )
  64.     {
  65.         fprintf(stderr, "%s is unable to write last day in %s\n", argv[0], file_name);
  66.         fclose( last_day_file ) ;
  67.         exit(1) ;
  68.     }
  69.     fclose( last_day_file ) ;
  70.     
  71.     
  72.     /*printf( "%d %ld %ld\n", ret, tp.tv_sec, tp.tv_usec); */
  73.     if( update )
  74.     {
  75.         seconds *= ( today - last_day ) ;
  76.         gettimeofday( &tp, &tzp);
  77.         tp.tv_sec += (int)floor(seconds) ;
  78.         tp.tv_usec += (int)((seconds - floor(seconds)) * 1000000) ;
  79.         if( settimeofday(&tp, &tzp) < 0)
  80.         {
  81.             fprintf(stderr, " must be root to correct clock");
  82.             exit(1);
  83.         }
  84.     }
  85. }
  86.  
  87. void usage( char *progname)
  88. {
  89.     fprintf(stderr, "usage : %s seconds\n seconds is an integer number for incrementing the clock\n\tWithout arg, I answer the number of days since midnight (0 hour), January 1, 1970\n", progname);
  90.     exit(0);
  91. }
  92.