Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / network / stacks / AROSTCP / netlib / timerinit.c
blob72bfd95b3fbd6ea732eeba6eef7c6c16459b2c7c
1 /* $Id$
3 * timerinit.c - SAS C auto initialization functions for timer device
5 * Copyright © 1994 AmiTCP/IP Group,
6 * Network Solutions Development Inc.
7 * All rights reserved.
8 */
10 /****** net.lib/autoinit_timer.device *****************************************
12 NAME
13 timerinit - SAS C Autoinitialization Functions for timer.device
15 SYNOPSIS
16 #include <time.h>
18 int daylight;
19 long timezone;
20 char *tzname[2];
22 void tzset(void);
24 #include <sys/time.h>
26 struct Device *TimerBase;
28 LONG _STI_200_openTimer(void);
29 void _STD_200_closeTimer(void);
31 FUNCTION
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.
42 NOTES
43 The time zone information is got from the environment variable named
44 TZ. The format for this variable is:
46 zzznnnddd
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
52 version).
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);
64 _STI_200_openTimer();
66 The tzset() does nothing. All the necessary initialization is done at
67 the autoinit function.
69 BUGS
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.
79 SEE ALSO
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>
89 #include <dos/dos.h>
90 #include <devices/timer.h>
91 #include <proto/exec.h>
92 #include <proto/dos.h>
94 #include <stdlib.h>
95 #include <sys/time.h>
97 #include <time.h>
99 /* SAS C 6.50 kludge */
100 #if __VERSION__ > 6 || __REVISION__ >= 50
101 #define exit(x) return(x)
102 #endif
104 struct Device *TimerBase = 0L;
105 static void *unit;
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);
121 int __daylight = 0;
122 long __timezone = 0;
123 char *__tzname[2] = { 0 };
124 char *_TZ = NULL;
126 char __zone_string[12] = "GMT0";
129 * Locale information is included here, since the 2.1 includes may be hard
130 * to find.
133 /* This structure must only be allocated by locale.library and is READ-ONLY! */
134 struct Locale
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
155 LONG __stdargs
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.
170 void *LocaleBase;
171 struct Locale *thisLocale = NULL;
172 short dstoff = 0;
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 */
185 short len, i;
186 long value;
187 BPTR file = Open("ENV:TZ", MODE_OLDFILE);
188 if (file) {
189 len = Read(file, __zone_string, sizeof(__zone_string) - 1);
190 if (len > 3) {
192 * make sure the string is 0 terminated and does not have the
193 * newline at the end
195 for (i = 0; i < len; i++)
196 if (__zone_string[i] < ' ')
197 break;
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
209 dstoff += 3;
212 Close(file);
217 * Update local time seconds to GMT translation
219 __timezone = (short)__time_zone.tz_minuteswest * (short)60;
220 __local_to_GMT += __timezone;
223 * tzset() stuff
225 if (dstoff > 3) {
226 __daylight = 1;
228 else
229 dstoff = 3;
231 __zone_string[3] = '\0'; /* terminate time zone name */
232 __tzname[0] = __zone_string;
233 __tzname[1] = __zone_string + dstoff;
235 return 0;
238 exit(20);
241 void __stdargs
242 _STD_200_closeTimer(void)
244 struct timerequest dummyTimer = { 0 };
245 if (!TimerBase)
246 return;
248 dummyTimer.tr_node.io_Device = TimerBase;
249 dummyTimer.tr_node.io_Unit = unit;
250 CloseDevice((struct IORequest*)&dummyTimer);
253 void
254 tzset(void)