home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / tcp / amitcp / amitcp-api-22.lha / AmiTCP-2.2 / src / netlib / timerinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-18  |  5.9 KB  |  212 lines

  1. /* $Header: /u/opi/86/jraja/ohtatcp/amitcp/src/netlib/RCS/timerinit.c,v 1.1 1993/10/18 06:22:50 jraja Exp $
  2.  *
  3.  * time.c --- SAS C auto initialization functions for timer device
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>, jraja <Jarno.Rajahalme@hut.fi>
  6.  *
  7.  * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  8.  *                  Helsinki University of Technology, Finland.
  9.  *                  All rights reserved.
  10.  *
  11.  * Created      : Sat Mar 20 03:31:29 1993 ppessi
  12.  * Last modified: Sat Mar 20 07:40:05 1993 ppessi
  13.  *
  14.  * $Log: timerinit.c,v $
  15.  * Revision 1.1  1993/10/18  06:22:50  jraja
  16.  * Initial revision
  17.  *
  18.  * Revision 2.0  93/03/20  17:31:52  17:31:52  ppessi (Pekka Pessi)
  19.  * initial netlib version..
  20.  * 
  21.  */
  22.  
  23. /****** net.lib/timerinit *********************************************
  24. *
  25. *   NAME   
  26. *       timerinit - SAS C Autoinitialization Functions for timer.device
  27. *
  28. *   SYNOPSIS
  29. *       _STIopenTimer()
  30. *
  31. *       void _STIopenTimer(void)
  32. *
  33. *       _STDcloseTimer()
  34. *
  35. *       void _STDcloseTimer(void)
  36. *
  37. *   FUNCTION
  38. *       These functions open and close the timer.device at the
  39. *       startup and exit of the program, respectively. For a
  40. *       program to use these functions, it must be linked with
  41. *       netlib:net.lib.
  42. *
  43. *       The opened device base is stored in the TimerBase global
  44. *       variable.
  45. *
  46. *       If the device can be opened, the _STIopenTimer() sets up the
  47. *       time zone information, which is used by the gettimeofday()
  48. *       function. 
  49. *
  50. *   NOTES
  51. *
  52. *       The time zone information is got from the environment variable
  53. *       named TZ. The format for this variable is:
  54. *
  55. *           zzznnnddd
  56. *
  57. *       where zzz is three letter identifier for the time zone (for
  58. *       example GMT), and the nnn is hours west from Greenwich on
  59. *       range [-23,24] (negative values are to east). The last field
  60. *       is the abbreviation for the local daylight saving time zone
  61. *       (which is not interpreted by this version).
  62. *       
  63. *       If the TZ environment variable cannot be found, Greenwich Mean
  64. *       Time (GMT) is used instead.
  65. *       
  66. *       The autoinitialization and autotermination functions are
  67. *       features specific to the SAS C6. However, these functions
  68. *       can be used with other (ANSI) C compilers, too. Example
  69. *       follows: 
  70. *
  71. *       \* at start of main() *\
  72. *
  73. *       atexit(_STDcloseTimer);
  74. *       _STDopenTimer();
  75. *
  76. *   BUGS
  77. *       TZ "hours west from GMT" should be interpreted as float.
  78. *
  79. *   SEE ALSO
  80. *       net.lib/gettimeofday(),
  81. *       SAS/C 6 User's Guide p. 145 for details of
  82. *       autoinitialization and autotermination functions.  
  83. *****************************************************************************
  84. *
  85. */
  86.  
  87. #include <exec/types.h>
  88. #include <exec/devices.h>
  89. #include <dos/dos.h>
  90. #include <devices/timer.h>
  91. #include <proto/exec.h>
  92. #include <proto/dos.h>
  93.  
  94. #include <stdlib.h>
  95. #include <sys/time.h>
  96.  
  97. struct Device *TimerBase = 0L;
  98. static void *unit;
  99.  
  100. /*
  101.  * Time zone support for the gettimeofday. Zeroes default to the GMT
  102.  * without daylight saving.
  103.  */
  104. struct timezone __time_zone = {0,0};
  105. /*
  106.  * Seconds to to the system time (seconds from 00:00 1.1.1978) 
  107.  * to the GMT (seconds from 00:00 1.1.1970).
  108.  * _STIopenTimer() adds the local time seconds west from GMT to this
  109.  * value, so the local time gets converted to the GMT.
  110.  */
  111. long __local_to_GMT = ((8L*365 + 8/4)*24*60*60);
  112.  
  113. /* 
  114.  * Locale information is included here, since the 2.1 includes may be hard 
  115.  * to find.
  116.  */
  117.  
  118. /* This structure must only be allocated by locale.library and is READ-ONLY! */
  119. struct Locale
  120. {
  121.     STRPTR    loc_LocaleName;      /* locale's name         */
  122.     STRPTR    loc_LanguageName;      /* language of this locale     */
  123.     STRPTR    loc_PrefLanguages[10];      /* preferred languages     */
  124.     ULONG    loc_Flags;          /* always 0 for now         */
  125.  
  126.     ULONG    loc_CodeSet;          /* always 0 for now         */
  127.     ULONG    loc_CountryCode;      /* user's country code     */
  128.     ULONG    loc_TelephoneCode;      /* country's telephone code     */
  129.     LONG    loc_GMTOffset;          /* minutes from GMT         */
  130.  
  131. /* deleted the rest to save space */
  132. };
  133.  
  134. void CloseLocale( struct Locale *locale );
  135. struct Locale *OpenLocale( STRPTR name );
  136.  
  137. #pragma libcall LocaleBase CloseLocale 2A 801
  138. #pragma libcall LocaleBase OpenLocale 9C 801
  139.  
  140.  
  141. void __stdargs
  142. _STIopenTimer(void)
  143. {
  144.   struct timerequest dummyTimer = { 0 };
  145.  
  146.   if (!TimerBase && !OpenDevice("timer.device", UNIT_VBLANK, 
  147.                 (struct IORequest *)&dummyTimer, 0L)) {
  148.     TimerBase = dummyTimer.tr_node.io_Device;
  149.     unit = dummyTimer.tr_node.io_Unit;
  150.     if (TimerBase->dd_Library.lib_Version >= 36) {
  151.       /*
  152.        * Initialize time zone information for the gettimeofday()
  153.        * First try to open locale (2.1 and up), and if that fails,
  154.        * try to read environment variable TZ.
  155.        */
  156.       void *LocaleBase;
  157.       struct Locale *thisLocale = NULL;
  158.  
  159.       if ((LocaleBase = OpenLibrary("locale.library", 38)) != NULL) {
  160.     if ((thisLocale = OpenLocale(NULL)) != NULL) {
  161.       /*
  162.        * Update time zone minutes west from GMT.
  163.        */
  164.       __time_zone.tz_minuteswest = thisLocale->loc_GMTOffset;
  165.       CloseLocale(thisLocale);
  166.     }
  167.     CloseLibrary(LocaleBase);
  168.       }
  169.       if (!thisLocale) { /* if locale information was not available */
  170.     short len;
  171.     long value;
  172.     char zone[10];
  173.     BPTR file = Open("ENV:TZ", MODE_OLDFILE);
  174.     if (file) {
  175.       len = Read(file, zone, sizeof(zone));
  176.       if (len > 3) {
  177.         zone[len] = '\000';
  178.         /* should interpret floats as well! */
  179.         if (StrToLong(zone+3, &value) > 0) {
  180.           /*
  181.            * Update time zone minutes west from GMT.
  182.            */
  183.           __time_zone.tz_minuteswest = (short)value * (short)60;
  184.         }
  185.       }
  186.       Close(file);
  187.     }
  188.       }
  189.  
  190.       /*
  191.        * Update local time seconds to GMT translation
  192.        */
  193.       __local_to_GMT += (short)__time_zone.tz_minuteswest * (short)60;
  194.  
  195.       return;
  196.     }
  197.   }
  198.   exit(20);
  199. }
  200.  
  201. void __stdargs
  202. _STDcloseTimer(void)
  203. {
  204.   struct timerequest dummyTimer = { 0 };
  205.   if (!TimerBase)
  206.     return;
  207.  
  208.   dummyTimer.tr_node.io_Device = TimerBase;
  209.   dummyTimer.tr_node.io_Unit = unit;
  210.   CloseDevice((struct IORequest*)&dummyTimer);
  211. }
  212.