⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 check_y2k.c

📁 网络时间协议NTP 源码 版本v4.2.0b 该源码用于linux平台下
💻 C
📖 第 1 页 / 共 2 页
字号:
/* check_y2k.c -- test ntp code constructs for Y2K correctness 	Y2KFixes [*/  /*	Code invoked by `make check`. Not part of ntpd and not to be	installed.	On any code I even wonder about, I've cut and pasted the code	here and ran it as a test case just to be sure.	For code not in "ntpd" proper, we have tried to call most 	repaired functions from herein to properly test them	(something never done before!). This has found several bugs,	not normal Y2K bugs, that will strike in Y2K so repair them	we did.	Program exits with 0 on success, 1 on Y2K failure (stdout messages).	Exit of 2 indicates internal logic bug detected OR failure of	what should be our correct formulas.	While "make check" should only check logic for source within that	specific directory, this check goes outside the scope of the local	directory.  It's not a perfect world (besides, there is a lot of	interdependence here, and it really needs to be tested in	a controled order).   *//* { definitions lifted from ntpd.c to allow us to complie with      "#include ntp.h".  I have not taken the time to reduce the clutter. */#ifdef HAVE_CONFIG_H# include <config.h>#endif#include "ntpd.h"#ifdef HAVE_UNISTD_H# include <unistd.h>#endif#ifdef HAVE_SYS_STAT_H# include <sys/stat.h>#endif#include <stdio.h>#include <errno.h>#ifndef SYS_WINNT# if !defined(VMS)	/*wjm*/#  include <sys/param.h># endif /* VMS */# if HAVE_SYS_SIGNAL_H#  include <sys/signal.h># endif /* HAVE_SYS_SIGNAL_H */# include <sys/signal.h># ifdef HAVE_SYS_IOCTL_H#  include <sys/ioctl.h># endif /* HAVE_SYS_IOCTL_H */# if !defined(VMS)	/*wjm*/#  include <sys/resource.h># endif /* VMS */#else# include <signal.h># include <process.h># include <io.h># include "../libntp/log.h"#endif /* SYS_WINNT */#if defined(HAVE_RTPRIO)# ifdef HAVE_SYS_RESOURCE_H#  include <sys/resource.h># endif# ifdef HAVE_SYS_LOCK_H#  include <sys/lock.h># endif# include <sys/rtprio.h>#else# ifdef HAVE_PLOCK#  ifdef HAVE_SYS_LOCK_H#	include <sys/lock.h>#  endif# endif#endif#if defined(HAVE_SCHED_SETSCHEDULER)# ifdef HAVE_SCHED_H#  include <sched.h># else#  ifdef HAVE_SYS_SCHED_H#   include <sys/sched.h>#  endif# endif#endif#if defined(HAVE_SYS_MMAN_H)# include <sys/mman.h>#endif#ifdef HAVE_TERMIOS_H# include <termios.h>#endif#ifdef SYS_DOMAINOS# include <apollo/base.h>#endif /* SYS_DOMAINOS *//* } end definitions lifted from ntpd.c */#include "ntp_calendar.h"#include "parse.h"#define GoodLeap(Year) (((Year)%4 || (!((Year)%100) && (Year)%400)) ? 0 : 13 )volatile int debug = 0;		/* debugging requests for parse stuff */char const *progname = "check_y2k";longDays ( int Year )		/* return number of days since year "0" */{    long  Return;		/* this is a known to be good algorithm */    Return = Year * 365;	/* first aproximation to the value */    if ( Year >= 1 )    {		/* see notes in libparse/parse.c if you want a PROPER		 * **generic algorithm. */	Return += (Year+3) / 4;		/* add in (too many) leap days */	Return -= (Year-1) / 100;	/* reduce by (too many) centurys */	Return += (Year-1) / 400;	/* get final answer */    }    return Return;}static int  year0 = 1900;	/* sarting year for NTP time */static int  yearend;		/* ending year we test for NTP time.				    * 32-bit systems: through 2036, the				      **year in which NTP time overflows.				    * 64-bit systems: a reasonable upper				      **limit (well, maybe somewhat beyond				      **reasonable, but well before the				      **max time, by which time the earth				      **will be dead.) */static time_t Time;static struct tm LocalTime;#define Error(year) if ( (year)>=2036 && LocalTime.tm_year < 110 ) \	Warnings++; else Fatals++intmain( void ){    int Fatals;    int Warnings;    int  year;    Time = time( (time_t *)NULL )#ifdef TESTTIMEOFFSET		+ test_time_offset#endif	;    LocalTime = *localtime( &Time );    year = ( sizeof( u_long ) > 4 ) 	/* save max span using year as temp */		? ( 400 * 3 ) 		/* three greater gregorian cycles */		: ((int)(0x7FFFFFFF / 365.242 / 24/60/60)* 2 ); /*32-bit limit*/			/* NOTE: will automacially expand test years on			 * 64 bit machines.... this may cause some of the			 * existing ntp logic to fail for years beyond			 * 2036 (the current 32-bit limit). If all checks			 * fail ONLY beyond year 2036 you may ignore such			 * errors, at least for a decade or so. */    yearend = year0 + year;    puts( " internal self check" );  {		/* verify our own logic used to verify repairs */    unsigned long days;    if ( year0 >= yearend )    {	fprintf( stdout, "year0=%d NOT LESS THAN yearend=%d  (span=%d)\n",		(int)year0, (int)yearend, (int)year );	exit(2);    }   {    int  save_year;    save_year = LocalTime.tm_year;	/* save current year */    year = 1980;    LocalTime.tm_year = year - 1900;    Fatals = Warnings = 0;    Error(year);		/* should increment Fatals */    if ( Fatals == 0 )     {	fprintf( stdout, 	    "%4d: %s(%d): FATAL DID NOT INCREMENT  (Fatals=%d Warnings=%d)\n",	    (int)year, __FILE__, __LINE__, (int)Fatals, (int)Warnings );	exit(2);    }    year = 2100;		/* test year > limit but CURRENT year < limit */    Fatals = Warnings = 0;    Error(year);		/* should increment Fatals */    if ( Warnings == 0 )     {	fprintf( stdout, 	    "%4d: %s(%d): WARNING DID NOT INCREMENT  (Fatals=%d Warnings=%d)\n",	    (int)year, __FILE__, __LINE__, (int)Fatals, (int)Warnings );	exit(2);    }    Fatals = Warnings = 0;    LocalTime.tm_year = year - 1900;	/* everything > limit */    Error(1980);		/* should increment Fatals */    if ( Fatals == 0 )     {	fprintf( stdout, 	    "%4d: %s(%d): FATALS DID NOT INCREMENT  (Fatals=%d Warnings=%d)\n",	    (int)year, __FILE__, __LINE__, (int)Fatals, (int)Warnings );	exit(2);    }    LocalTime.tm_year = save_year;   }    days = 365+1;		/* days in year 0 + 1 more day */    for ( year = 1; year <= 2500; year++ )    {	long   Test;	Test = Days( year );	if ( days != Test )	{	    fprintf( stdout, "%04d: Days() DAY COUNT ERROR: s/b=%ld was=%ld\n", 		year, (long)days, (long)Test );	    exit(2);		/* would throw off many other tests */	}	Test = julian0(year);		/* compare with julian0() macro */	if ( days != Test )	{	    fprintf( stdout, "%04d: julian0() DAY COUNT ERROR: s/b=%ld was=%ld\n", 		year, (long)days, (long)Test );	    exit(2);		/* would throw off many other tests */	}	days += 365;	if ( isleap_4(year) ) days++;    }    if ( isleap_4(1999) )    {	fprintf( stdout, "isleap_4(1999) REPORTED TRUE\n" );	exit(2);    }    if ( !isleap_4(2000) )    {	fprintf( stdout, "isleap_4(2000) REPORTED FALSE\n" );	exit(2);    }    if ( isleap_4(2001) )    {	fprintf( stdout, "isleap_4(1999) REPORTED TRUE\n" );	exit(2);    }    if ( !isleap_tm(2000-1900) )    {	fprintf( stdout, "isleap_tm(100) REPORTED FALSE\n" );	exit(2);    }  }    Fatals = Warnings = 0;    puts( " include/ntp.h" );  {		/* test our new isleap_*() #define "functions" */        for ( year = 1400; year <= 2200; year++ )    {	int  LeapSw;	int  IsLeapSw;	LeapSw = GoodLeap(year);	IsLeapSw = isleap_4(year);	if ( !!LeapSw != !!IsLeapSw )	{	    Error(year);	    fprintf( stdout, 		"  %4d %2d %3d *** ERROR\n", year, LeapSw, IsLeapSw );	    break;	}	IsLeapSw = isleap_tm(year-1900);	if ( !!LeapSw != !!IsLeapSw )	{	    Error(year);	    fprintf( stdout, 		"  %4d %2d %3d *** ERROR\n", year, LeapSw, IsLeapSw );	    break;	}    }  }    puts( " include/ntp_calendar.h" );  {		/* I belive this is good, but just to be sure... */	/* we are testing this #define */#define is_leapyear(y) (y%4 == 0 && !(y%100 == 0 && !(y%400 == 0)))    for ( year = 1400; year <= 2200; year++ )    {	int  LeapSw;	LeapSw = GoodLeap(year);	if ( !(!LeapSw) != !(!is_leapyear(year)) )	{	    Error(year);	    fprintf( stdout, 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -