3 * timerinit.c - SAS C auto initialization functions for timer device
5 * Copyright © 1994 AmiTCP/IP Group,
6 * Network Solutions Development Inc.
10 /****** net.lib/autoinit_timer.device *****************************************
13 timerinit - SAS C Autoinitialization Functions for timer.device
26 struct Device *TimerBase;
28 LONG _STI_200_openTimer(void);
29 void _STD_200_closeTimer(void);
32 These functions open and close the timer.device at the startup and
33 exit of the program, respectively. For a program to use these
34 functions, it must be linked with netlib:net.lib.
36 The opened device base is stored in the TimerBase global variable.
38 If the device can be opened, the _STIopenTimer() sets up the time zone
39 information, which is used by the gettimeofday() function and the time
40 conversion routines of the C-library.
43 The time zone information is got from the environment variable named
44 TZ. The format for this variable is:
48 where zzz is three letter identifier for the time zone (for example
49 GMT), and the nnn is hours west from Greenwich on range [-23,24]
50 (negative values are to east). The last field is the abbreviation for
51 the local daylight saving time zone (which is not interpreted by this
54 If the TZ environment variable cannot be found, Greenwich Mean Time
55 (GMT) is used instead.
57 The autoinitialization and autotermination functions are features
58 specific to the SAS C6. However, these functions can be used with
59 other (ANSI) C compilers, too. Example follows:
61 \* at start of main() *\
63 atexit(_STD_200_closeTimer);
66 The tzset() does nothing. All the necessary initialization is done at
67 the autoinit function.
70 TZ "hours west from GMT" should be interpreted as float.
72 The same autoinitialization won't work for both SAS C 6.3 and SAS C
73 6.50 or latter. Only way to terminate an initialization function is
74 by exit() call with SAS C 6.3 binary. If an autoinitialization
75 function is terminated by exit() call with SAS C 6.50 binary, the
76 autotermination functions won't be called. Due this braindamage
77 these compilers require separate net.lib libraries.
80 net.lib/gettimeofday(),
81 SAS/C 6 User's Guide p. 145 for details of autoinitialization and
82 autotermination functions.
83 *****************************************************************************
87 #include <exec/types.h>
88 #include <exec/devices.h>
90 #include <devices/timer.h>
91 #include <proto/exec.h>
92 #include <proto/dos.h>
99 /* SAS C 6.50 kludge */
100 #if __VERSION__ > 6 || __REVISION__ >= 50
101 #define exit(x) return(x)
104 struct Device
*TimerBase
= 0L;
108 * Time zone support for the gettimeofday. Zeroes default to the GMT
109 * without daylight saving.
111 struct timezone __time_zone
= {0,0};
114 * Seconds to to the system time (seconds from 00:00 1.1.1978)
115 * to the GMT (seconds from 00:00 1.1.1970).
116 * _STIopenTimer() adds the local time seconds west from GMT to this
117 * value, so the local time gets converted to the GMT.
119 long __local_to_GMT
= ((8L*365 + 8/4)*24*60*60);
123 char *__tzname
[2] = { 0 };
126 char __zone_string
[12] = "GMT0";
129 * Locale information is included here, since the 2.1 includes may be hard
133 /* This structure must only be allocated by locale.library and is READ-ONLY! */
136 STRPTR loc_LocaleName
; /* locale's name */
137 STRPTR loc_LanguageName
; /* language of this locale */
138 STRPTR loc_PrefLanguages
[10]; /* preferred languages */
139 ULONG loc_Flags
; /* always 0 for now */
141 ULONG loc_CodeSet
; /* always 0 for now */
142 ULONG loc_CountryCode
; /* user's country code */
143 ULONG loc_TelephoneCode
; /* country's telephone code */
144 LONG loc_GMTOffset
; /* minutes from GMT */
146 /* deleted the rest to save space */
149 void CloseLocale( struct Locale
*locale
);
150 struct Locale
*OpenLocale( STRPTR name
);
152 #pragma libcall LocaleBase CloseLocale 2A 801
153 #pragma libcall LocaleBase OpenLocale 9C 801
156 _STI_200_openTimer(void)
158 struct timerequest dummyTimer
= { 0 };
160 if (!TimerBase
&& !OpenDevice("timer.device", UNIT_VBLANK
,
161 (struct IORequest
*)&dummyTimer
, 0L)) {
162 TimerBase
= dummyTimer
.tr_node
.io_Device
;
163 unit
= dummyTimer
.tr_node
.io_Unit
;
164 if (TimerBase
->dd_Library
.lib_Version
>= 36) {
166 * Initialize time zone information for the gettimeofday()
167 * First try to open locale (2.1 and up), and if that fails,
168 * try to read environment variable TZ.
171 struct Locale
*thisLocale
= NULL
;
174 if ((LocaleBase
= OpenLibrary("locale.library", 38)) != NULL
) {
175 if ((thisLocale
= OpenLocale(NULL
)) != NULL
) {
177 * Update time zone minutes west from GMT.
179 __time_zone
.tz_minuteswest
= thisLocale
->loc_GMTOffset
;
180 CloseLocale(thisLocale
);
182 CloseLibrary(LocaleBase
);
184 if (!thisLocale
) { /* if locale information was not available */
187 BPTR file
= Open("ENV:TZ", MODE_OLDFILE
);
189 len
= Read(file
, __zone_string
, sizeof(__zone_string
) - 1);
192 * make sure the string is 0 terminated and does not have the
195 for (i
= 0; i
< len
; i
++)
196 if (__zone_string
[i
] < ' ')
198 __zone_string
[i
] = '\0';
200 /* should interpret floats as well! */
201 if ((dstoff
= StrToLong(__zone_string
+3, &value
)) > 0) {
203 * Update time zone minutes west from GMT.
205 __time_zone
.tz_minuteswest
= (short)value
* (short)60;
207 * Set the offset to the possible DST zone name
217 * Update local time seconds to GMT translation
219 __timezone
= (short)__time_zone
.tz_minuteswest
* (short)60;
220 __local_to_GMT
+= __timezone
;
231 __zone_string
[3] = '\0'; /* terminate time zone name */
232 __tzname
[0] = __zone_string
;
233 __tzname
[1] = __zone_string
+ dstoff
;
242 _STD_200_closeTimer(void)
244 struct timerequest dummyTimer
= { 0 };
248 dummyTimer
.tr_node
.io_Device
= TimerBase
;
249 dummyTimer
.tr_node
.io_Unit
= unit
;
250 CloseDevice((struct IORequest
*)&dummyTimer
);