ftp.nice.ch/pub/next/tools/system/correctclock.N.bs.tar.gz#/correctclock/correctclock.c

This is correctclock.c in view mode; [Download] [Up]

/* Bruno Bienfait 1993 */


#include <sys/time.h>
#include <stdio.h>
#include<math.h>

void usage( char *progname);

void main( int argc, char *argv[])
{
	struct timeval tp;
	struct timezone tzp;
	double seconds = 0 ;
	long today = 0 ;
	long last_day = 0 ;
	int update ;

	FILE *last_day_file ;
	char file_name[] ="/usr/adm/correctclock.date" ;

	gettimeofday( &tp, &tzp);
	today = tp.tv_sec / (3600 * 24) ;

	if( argc < 2 )
	{
		printf( "%d\n", today );
		exit(0) ;
	}
				
	if( sscanf(argv[1], "%lf",&seconds ) != 1 )
		usage(argv[0]);
	
	if( seconds == 0 )
		exit(0) ;
	
	if( (last_day_file = fopen(file_name,  "r")) != (FILE * )NULL)
	{
		if( fscanf(last_day_file, "%d",&last_day ) != 1 || last_day <= 0)
		{
			fprintf(stderr, "%s is unable to read last day in %s\n", argv[0], file_name);
			fclose(last_day_file);
			exit(1) ;
		}
		
		fclose( last_day_file ) ;
		
		if( ( today - last_day ) <= 0) /* already adjusted today */
			update = 0 ;
		else
			update = 1 ;

	}
	else
		update = 0 ;


	if( (last_day_file = fopen(file_name,  "w")) == NULL)
	{
		fprintf(stderr, "%s is unable to write last day in %s\n", argv[0], file_name);
		exit(1) ;
	}
	if( fprintf( last_day_file, "%d\n", today ) < 0 )
	{
		fprintf(stderr, "%s is unable to write last day in %s\n", argv[0], file_name);
		fclose( last_day_file ) ;
		exit(1) ;
	}
	fclose( last_day_file ) ;
	
	
	/*printf( "%d %ld %ld\n", ret, tp.tv_sec, tp.tv_usec); */
	if( update )
	{
		seconds *= ( today - last_day ) ;
		gettimeofday( &tp, &tzp);
		tp.tv_sec += (int)floor(seconds) ;
		tp.tv_usec += (int)((seconds - floor(seconds)) * 1000000) ;
		if( settimeofday(&tp, &tzp) < 0)
		{
			fprintf(stderr, " must be root to correct clock");
			exit(1);
		}
	}
}

void usage( char *progname)
{
	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);
	exit(0);
}
	

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.