home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1644 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  7.9 KB

  1. From: thad@cup.portal.com (Thad P Floryan)
  2. Newsgroups: alt.sources
  3. Subject: one SystemV emulation of BSD's "gettimeofday(2)"
  4. Message-ID: <32432@cup.portal.com>
  5. Date: 4 Aug 90 05:54:15 GMT
  6.  
  7. This is a "better" SysV emulation of the BSD ``gettimeofday(2)'' which I've
  8. used to port the 4.3BSD "Tahoe" networking software suite to a 3B1 (SysV).
  9.  
  10. Several correspondents have subsequently informed me (since my original posting
  11. to unix-pc.sources) that my assumptions regarding 60Hz don't apply to all SysV
  12. boxes ... some have a 100Hz resolution in their clocks and the code following
  13. must be adjusted to compensate.
  14.  
  15. The README says (said :-) it all.
  16.  
  17. Thad Floryan [ thad@cup.portal.com (OR) ..!sun!portal!cup.portal.com!thad ]
  18.  
  19. #    This is a shell archive.
  20. #    Remove everything above and including the cut line.
  21. #    Then run the rest of the file through sh.
  22. #----cut here-----cut here-----cut here-----cut here----#
  23. #!/bin/sh
  24. # shar:    Shell Archiver
  25. #    Run the following text with /bin/sh to create:
  26. #    README
  27. #    Makefile
  28. #    gettod.c
  29. #    gettod.2p
  30. #    dotime.c
  31. # This archive created: Sun Jul  8 22:03:30 1990
  32. echo shar: extracting README
  33. sed 's/^X//' << \SHAR_EOF > README
  34. XAndy Poling just posted a notice regarding the availability of his ports of
  35. Xvarious BSD networking software.  Great!  I, too, have been working on some
  36. Xports since my posting of my dissatisfaction of some of the programs which
  37. Xaccompanied the WIN/3B packages I purchased earlier this year; to date I have
  38. Xftp, ftpd, finger, fingerd, etc. working rock-solid.  Perhaps Andy and I
  39. Xshould coordinate our efforts.
  40. X
  41. XThe reason for THIS source posting is that the first thing I saw in Andy's
  42. Xbind.tar.Z posting was a "gettimeofday()" routine.  I also wrote one last
  43. Xmonth, and mine WILL work with the BSD ftp and other programs; note that mine
  44. X"sorta" mimics the 1 uS clock (using times() to get, on SysV, a 1/60 second
  45. Xresolution which IS more useful for ftp transfer statistics, and will NOT
  46. Xbomb due to the check for a null pointer (which a lot of BSD software seems
  47. Xto delight in (ab)using).  I don't know what ancient C compiler the BSD-type
  48. Xpeople use to develop their software, but seven (7) different modern C
  49. Xcompilers that I use all gripe about the BSD code (and, yes, I *DO* have BSD
  50. Xinclude files) with the most common problem being mis-cast assignments.
  51. X
  52. XIn any event, what I've enclosed are:
  53. X
  54. X    Makefile    - for the gettimeofday() for SysV
  55. X    gettod.c    - my interpretation of gettimeofday() solely based on
  56. X              the "man" page and adjusted to "do the right thing"
  57. X              based on observation how it is used in the BSD Tahoe
  58. X              software found on uunet.uu.net in pub/*
  59. X    gettod.2p    - man page for gettimeofday().  This has been sitting
  60. X              on my system since mid-1987 and I don't know its
  61. X              origin; I might have gotten it from `The WELL' back
  62. X              when I snarfed anything that wasn't nailed down  :-)
  63. X    dotime.c    - "test" program from which I determined how times()
  64. X              could be used for the gettimeofday() emulation.
  65. X              The displayed "bootime" can be checked by doing:
  66. X                $ who -b
  67. X
  68. XThad
  69. X
  70. XThad Floryan [ thad@cup.portal.com (OR) ..!sun!portal!cup.portal.com!thad ]
  71. SHAR_EOF
  72. if test 1968 -ne "`wc -c README`"
  73. then
  74. echo shar: error transmitting README '(should have been 1968 characters)'
  75. fi
  76. echo shar: extracting Makefile
  77. sed 's/^X//' << \SHAR_EOF > Makefile
  78. X# Makefile for Thad's gettimeofday() on a UNIXPC (a SYSVR2 variant)
  79. X#
  80. X#
  81. X#CC=        gcc
  82. XCC=        cc
  83. X
  84. XCFLAGS=        -O
  85. X
  86. Xgettod.o :    gettod.c
  87. X        $(CC) $(CFLAGS) -c gettod.c
  88. SHAR_EOF
  89. if test 154 -ne "`wc -c Makefile`"
  90. then
  91. echo shar: error transmitting Makefile '(should have been 154 characters)'
  92. fi
  93. echo shar: extracting gettod.c
  94. sed 's/^X//' << \SHAR_EOF > gettod.c
  95. X/*
  96. X *    SystemV simulation of bsd's gettimeofday(2).
  97. X *
  98. X *    Thad Floryan, 24-June-1990.
  99. X */
  100. X
  101. X#include <sys/types.h>
  102. X#include <sys/times.h>
  103. X
  104. Xstruct timeval {
  105. X    long    tv_sec;        /* seconds */
  106. X    long    tv_usec;    /* and microseconds */
  107. X};
  108. X
  109. Xstruct timezone {
  110. X    int    tz_minuteswest;    /* minutes west of Greenwich */
  111. X    int    tz_dsttime;    /* type of dst correction */
  112. X};
  113. X
  114. X#define    DST_NONE    0    /* not on dst */
  115. X#define    DST_USA        1    /* USA style dst */
  116. X#define    DST_AUST    2    /* Australian style dst */
  117. X#define    DST_WET        3    /* Western European dst */
  118. X#define    DST_MET        4    /* Middle European dst */
  119. X#define    DST_EET        5    /* Eastern European dst */
  120. X
  121. X
  122. Xgettimeofday( tp, tz )
  123. X    struct timeval *tp;    /* long tv_sec  secs since 1-jan-1970    */
  124. X            /* long tv_usec microseconds fraction    */
  125. X    struct timezone *tz;/* int tz_minuteswest of GMT        */
  126. X            /* int tz_dsttime = DST_* if apply DST    */
  127. X{
  128. X    extern long time(), times();
  129. X    extern long timezone;
  130. X    struct tms dummy;
  131. X
  132. X    tp->tv_sec = time((long *) 0);
  133. X    tp->tv_usec = (times(&dummy) % 60L) * 16666L; /* 1/60 = .016666 S */
  134. X    if (tz != (struct timezone *)0) {
  135. X    tz->tz_minuteswest = (int) (timezone / 60L);  /* convert sec to min */
  136. X    tz->tz_dsttime = DST_USA;        /* assume USA DST handling */
  137. X    }
  138. X}
  139. SHAR_EOF
  140. if test 1208 -ne "`wc -c gettod.c`"
  141. then
  142. echo shar: error transmitting gettod.c '(should have been 1208 characters)'
  143. fi
  144. echo shar: extracting gettod.2p
  145. sed 's/^X//' << \SHAR_EOF > gettod.2p
  146. X.TH GETTIMEOFDAY 2P
  147. X.UC 4
  148. X.SH NAME
  149. Xgettimeofday \- get precise date and time
  150. X.SH SYNOPSIS
  151. X.nf
  152. X.ft B
  153. X#include <sys/time.h>
  154. X.PP
  155. X.ft B
  156. Xgettimeofday(tp, tzp)
  157. Xstruct timeval *tp;
  158. Xstruct timezone *tzp;
  159. X.fi
  160. X.SH DESCRIPTION
  161. X.I Gettimeofday
  162. Xreturns the system's notion of the current Greenwich time and
  163. Xthe current time zone.  Time returned is expressed relative
  164. Xin seconds and microseconds since midnight January 1, 1970.
  165. X.PP
  166. XThe structures pointed to by
  167. X.I tp
  168. Xand
  169. X.I tzp
  170. Xare defined in 
  171. X.I <sys/time.h>
  172. Xas:
  173. X.PP
  174. X.nf
  175. X.RS
  176. X.DT
  177. Xstruct timeval {
  178. X    u_long    tv_sec;        /* seconds since Jan. 1, 1970 */
  179. X    long    tv_usec;        /* and microseconds */
  180. X};
  181. X.sp 1
  182. Xstruct timezone {
  183. X    int    tz_minuteswest;    /* of Greenwich */
  184. X    int    tz_dsttime;    /* type of dst correction to apply */
  185. X};
  186. X.RE
  187. X.fi
  188. X.PP
  189. XThe 
  190. X.I timezone
  191. Xstructure indicates the local time zone
  192. X(measured in minutes of time westward from Greenwich),
  193. Xand a flag that, if nonzero, indicates that
  194. XDaylight Saving time applies locally during
  195. Xthe appropriate part of the year.
  196. X.PP
  197. XOnly the super-user may set the time of day.
  198. X.PP
  199. XTime IS precise enough to believe the microsecond values.
  200. X.SH RETURN
  201. XA 0 return value indicates that the call succeeded.
  202. XA \-1 return value indicates an error occurred, and in this
  203. Xcase an error code is stored into the global variable \fIerrno\fP.
  204. X.SH "ERRORS
  205. XThe following error codes may be set in \fIerrno\fP:
  206. X.TP 15
  207. X[EFAULT]
  208. XAn argument address referenced invalid memory.
  209. X.SH "SEE ALSO"
  210. Xintro(0p),
  211. Xdate(1), ctime(3), gettimeofday(2)
  212. X.SH TIMING
  213. XTo be determined.
  214. X.SH CAVEATS
  215. X
  216. XOn any system, unless measured and compensated for,
  217. Xinterrupt handling will cause
  218. Xtimes to vary.  As an example, any measurement which spans an even 
  219. Xmultiple of 10 milliseconds will incur an overhead of (very roughly) 
  220. X900 microseconds [on a VAX750!] due to handling clock interrupt.
  221. X
  222. XTimeslices for other processes are another obvious source of variation.
  223. SHAR_EOF
  224. if test 1883 -ne "`wc -c gettod.2p`"
  225. then
  226. echo shar: error transmitting gettod.2p '(should have been 1883 characters)'
  227. fi
  228. echo shar: extracting dotime.c
  229. sed 's/^X//' << \SHAR_EOF > dotime.c
  230. X#include <sys/types.h>
  231. X#include <sys/times.h>
  232. X
  233. Xmain()
  234. X{
  235. X    extern long time(), times();
  236. X    extern char *ctime();
  237. X
  238. X    long time1, time2;
  239. X    long etime1, etime2;
  240. X    long uptime;
  241. X    long boottime;
  242. X
  243. X    struct tms crap;
  244. X
  245. X    time1 = time((long *) 0);
  246. X    etime1 = times(&crap);
  247. X    sleep(3);
  248. X    time2 = time((long *) 0);
  249. X    etime2 = times(&crap);
  250. X
  251. X    printf("time  : %ld - %ld = %ld\n", time2, time1, time2-time1);
  252. X    printf("times : %ld - %ld = %ld\n", etime2, etime1, etime2-etime1);
  253. X    printf("etime2/60 = %ld\n", etime2/60L);
  254. X    uptime = etime2/60L;
  255. X    boottime = time2 - uptime;
  256. X    printf("boottime was %s\n", ctime(&boottime));
  257. X
  258. X}
  259. SHAR_EOF
  260. if test 593 -ne "`wc -c dotime.c`"
  261. then
  262. echo shar: error transmitting dotime.c '(should have been 593 characters)'
  263. fi
  264. #    End of shell archive
  265. exit 0
  266.