New SysV emulation of BSD's gettimeofday(2)

Thad P Floryan thad at cup.portal.com
Mon Jul 9 15:14:13 AEST 1990


This is a "better" SysV emulation of the BSD ``gettimeofday(2)''

The README says it all.

Thad Floryan [ thad at cup.portal.com (OR) ..!sun!portal!cup.portal.com!thad ]

#	This is a shell archive.
#	Remove everything above and including the cut line.
#	Then run the rest of the file through sh.
#----cut here-----cut here-----cut here-----cut here----#
#!/bin/sh
# shar:    Shell Archiver
#	Run the following text with /bin/sh to create:
#	README
#	Makefile
#	gettod.c
#	gettod.2p
#	dotime.c
# This archive created: Sun Jul  8 22:03:30 1990
echo shar: extracting README
sed 's/^X//' << \SHAR_EOF > README
XAndy Poling just posted a notice regarding the availability of his ports of
Xvarious BSD networking software.  Great!  I, too, have been working on some
Xports since my posting of my dissatisfaction of some of the programs which
Xaccompanied the WIN/3B packages I purchased earlier this year; to date I have
Xftp, ftpd, finger, fingerd, etc. working rock-solid.  Perhaps Andy and I
Xshould coordinate our efforts.
X
XThe reason for THIS source posting is that the first thing I saw in Andy's
Xbind.tar.Z posting was a "gettimeofday()" routine.  I also wrote one last
Xmonth, and mine WILL work with the BSD ftp and other programs; note that mine
X"sorta" mimics the 1 uS clock (using times() to get, on SysV, a 1/60 second
Xresolution which IS more useful for ftp transfer statistics, and will NOT
Xbomb due to the check for a null pointer (which a lot of BSD software seems
Xto delight in (ab)using).  I don't know what ancient C compiler the BSD-type
Xpeople use to develop their software, but seven (7) different modern C
Xcompilers that I use all gripe about the BSD code (and, yes, I *DO* have BSD
Xinclude files) with the most common problem being mis-cast assignments.
X
XIn any event, what I've enclosed are:
X
X	Makefile	- for the gettimeofday() for SysV
X	gettod.c	- my interpretation of gettimeofday() solely based on
X			  the "man" page and adjusted to "do the right thing"
X			  based on observation how it is used in the BSD Tahoe
X			  software found on uunet.uu.net in pub/*
X	gettod.2p	- man page for gettimeofday().  This has been sitting
X			  on my system since mid-1987 and I don't know its
X			  origin; I might have gotten it from `The WELL' back
X			  when I snarfed anything that wasn't nailed down  :-)
X	dotime.c	- "test" program from which I determined how times()
X			  could be used for the gettimeofday() emulation.
X			  The displayed "bootime" can be checked by doing:
X				$ who -b
X
XThad
X
XThad Floryan [ thad at cup.portal.com (OR) ..!sun!portal!cup.portal.com!thad ]
SHAR_EOF
if test 1968 -ne "`wc -c README`"
then
echo shar: error transmitting README '(should have been 1968 characters)'
fi
echo shar: extracting Makefile
sed 's/^X//' << \SHAR_EOF > Makefile
X# Makefile for Thad's gettimeofday() on a UNIXPC (a SYSVR2 variant)
X#
X#
X#CC=		gcc
XCC=		cc
X
XCFLAGS=		-O
X
Xgettod.o :	gettod.c
X		$(CC) $(CFLAGS) -c gettod.c
SHAR_EOF
if test 154 -ne "`wc -c Makefile`"
then
echo shar: error transmitting Makefile '(should have been 154 characters)'
fi
echo shar: extracting gettod.c
sed 's/^X//' << \SHAR_EOF > gettod.c
X/*
X *	SystemV simulation of bsd's gettimeofday(2).
X *
X *	Thad Floryan, 24-June-1990.
X */
X
X#include <sys/types.h>
X#include <sys/times.h>
X
Xstruct timeval {
X	long	tv_sec;		/* seconds */
X	long	tv_usec;	/* and microseconds */
X};
X
Xstruct timezone {
X	int	tz_minuteswest;	/* minutes west of Greenwich */
X	int	tz_dsttime;	/* type of dst correction */
X};
X
X#define	DST_NONE	0	/* not on dst */
X#define	DST_USA		1	/* USA style dst */
X#define	DST_AUST	2	/* Australian style dst */
X#define	DST_WET		3	/* Western European dst */
X#define	DST_MET		4	/* Middle European dst */
X#define	DST_EET		5	/* Eastern European dst */
X
X
Xgettimeofday( tp, tz )
X    struct timeval *tp;	/* long tv_sec  secs since 1-jan-1970	*/
X			/* long tv_usec microseconds fraction	*/
X    struct timezone *tz;/* int tz_minuteswest of GMT		*/
X			/* int tz_dsttime = DST_* if apply DST	*/
X{
X    extern long time(), times();
X    extern long timezone;
X    struct tms dummy;
X
X    tp->tv_sec = time((long *) 0);
X    tp->tv_usec = (times(&dummy) % 60L) * 16666L; /* 1/60 = .016666 S */
X    if (tz != (struct timezone *)0) {
X	tz->tz_minuteswest = (int) (timezone / 60L);  /* convert sec to min */
X	tz->tz_dsttime = DST_USA;		/* assume USA DST handling */
X    }
X}
SHAR_EOF
if test 1208 -ne "`wc -c gettod.c`"
then
echo shar: error transmitting gettod.c '(should have been 1208 characters)'
fi
echo shar: extracting gettod.2p
sed 's/^X//' << \SHAR_EOF > gettod.2p
X.TH GETTIMEOFDAY 2P
X.UC 4
X.SH NAME
Xgettimeofday \- get precise date and time
X.SH SYNOPSIS
X.nf
X.ft B
X#include <sys/time.h>
X.PP
X.ft B
Xgettimeofday(tp, tzp)
Xstruct timeval *tp;
Xstruct timezone *tzp;
X.fi
X.SH DESCRIPTION
X.I Gettimeofday
Xreturns the system's notion of the current Greenwich time and
Xthe current time zone.  Time returned is expressed relative
Xin seconds and microseconds since midnight January 1, 1970.
X.PP
XThe structures pointed to by
X.I tp
Xand
X.I tzp
Xare defined in 
X.I <sys/time.h>
Xas:
X.PP
X.nf
X.RS
X.DT
Xstruct timeval {
X	u_long	tv_sec;		/* seconds since Jan. 1, 1970 */
X	long	tv_usec;		/* and microseconds */
X};
X.sp 1
Xstruct timezone {
X	int	tz_minuteswest;	/* of Greenwich */
X	int	tz_dsttime;	/* type of dst correction to apply */
X};
X.RE
X.fi
X.PP
XThe 
X.I timezone
Xstructure indicates the local time zone
X(measured in minutes of time westward from Greenwich),
Xand a flag that, if nonzero, indicates that
XDaylight Saving time applies locally during
Xthe appropriate part of the year.
X.PP
XOnly the super-user may set the time of day.
X.PP
XTime IS precise enough to believe the microsecond values.
X.SH RETURN
XA 0 return value indicates that the call succeeded.
XA \-1 return value indicates an error occurred, and in this
Xcase an error code is stored into the global variable \fIerrno\fP.
X.SH "ERRORS
XThe following error codes may be set in \fIerrno\fP:
X.TP 15
X[EFAULT]
XAn argument address referenced invalid memory.
X.SH "SEE ALSO"
Xintro(0p),
Xdate(1), ctime(3), gettimeofday(2)
X.SH TIMING
XTo be determined.
X.SH CAVEATS
X
XOn any system, unless measured and compensated for,
Xinterrupt handling will cause
Xtimes to vary.  As an example, any measurement which spans an even 
Xmultiple of 10 milliseconds will incur an overhead of (very roughly) 
X900 microseconds [on a VAX750!] due to handling clock interrupt.
X
XTimeslices for other processes are another obvious source of variation.
SHAR_EOF
if test 1883 -ne "`wc -c gettod.2p`"
then
echo shar: error transmitting gettod.2p '(should have been 1883 characters)'
fi
echo shar: extracting dotime.c
sed 's/^X//' << \SHAR_EOF > dotime.c
X#include <sys/types.h>
X#include <sys/times.h>
X
Xmain()
X{
X	extern long time(), times();
X	extern char *ctime();
X
X	long time1, time2;
X	long etime1, etime2;
X	long uptime;
X	long boottime;
X
X	struct tms crap;
X
X	time1 = time((long *) 0);
X	etime1 = times(&crap);
X	sleep(3);
X	time2 = time((long *) 0);
X	etime2 = times(&crap);
X
X	printf("time  : %ld - %ld = %ld\n", time2, time1, time2-time1);
X	printf("times : %ld - %ld = %ld\n", etime2, etime1, etime2-etime1);
X	printf("etime2/60 = %ld\n", etime2/60L);
X	uptime = etime2/60L;
X	boottime = time2 - uptime;
X	printf("boottime was %s\n", ctime(&boottime));
X
X}
SHAR_EOF
if test 593 -ne "`wc -c dotime.c`"
then
echo shar: error transmitting dotime.c '(should have been 593 characters)'
fi
#	End of shell archive
exit 0



More information about the Comp.sys.att mailing list