tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / workbench / network / stacks / AROSTCP / netlib / autoinit.c
blob116bdba9f78a9f224faa488befbf896e4ff3b887
1 /* $Id$
3 * autoinit.c - SAS/C autoinitialization functions for bsdsocket.library
5 * Copyright © 1994 AmiTCP/IP Group,
6 * Network Solutions Development Inc.
7 * All rights reserved.
8 * Copyright © 2005 - 2011 Pavel Fedin
9 */
11 #include <bsdsocket/socketbasetags.h>
12 #include <exec/types.h>
13 #include <exec/libraries.h>
14 #include <intuition/intuition.h>
15 #include <proto/socket.h>
16 #include <proto/exec.h>
17 #include <proto/intuition.h>
19 #include <stdlib.h>
20 #include <errno.h>
22 #include "conf.h"
24 struct Library *SocketBase = NULL;
26 static const char SOCKETNAME[] = "bsdsocket.library";
28 #define SOCKETVERSION 4 /* minimum bsdsocket version to use */
30 extern STRPTR _ProgramName; /* startup module defines this :-) */
32 /****** net.lib/autoinit *********************************************
34 NAME
35 autoinit - Autoinitialization Functions
37 SYNOPSIS
38 LONG _STI_200_openSockets(void)
40 void _STD_200_closeSockets(void)
42 FUNCTION
43 These functions open and close the bsdsocket.library at the startup
44 and exit of the program, respectively. For a program to use these
45 functions, it must be linked with netlib:net.lib (or some variant).
46 These functions are linked in only if the program references the
47 global symbol "SocketBase".
49 If the library can be opened, the _STI_200_openSockets() calls
50 bsdsocket.library function SocketBaseTags() to tell the library the
51 address and the size of the errno variable of the calling program,
52 the program name (to be used in syslog() messages) and the address
53 of the h_error variable (in which the name resolver errors are
54 returned).
56 NOTES
57 _STI_200_openSockets() also checks that the system version is at
58 least 37. It also puts up a requester if the bsdsocket.library is
59 not found or is of wrong version.
61 The autoinitialization and autotermination functions are features
62 specific to the SAS C6. However, these functions can be used with
63 other (ANSI) C compilers, too. Example follows:
65 \* at start of main() *\
67 atexit(_STD_200_closeSockets);
68 if (_STI_200_openSockets() != 0)
69 exit(20);
71 BUGS
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 bsdsocket.library/SocketBaseTags(),
81 SAS/C 6 User's Guide p. 145 for details of autoinitialization and
82 autotermination functions.
84 *****************************************************************************
87 /* SAS C 6.50 kludge */
88 #ifdef SASC
89 #if __VERSION__ > 6 || __REVISION__ >= 50
90 #define exit(x) return x
91 #endif
92 #endif
94 /* AROS kludge, see below */
95 #ifdef __AROS__
97 #include <aros/symbolsets.h>
99 #undef CONSTRUCTOR
100 #define CONSTRUCTOR
101 #define exit(x) return x
102 #endif
105 * Using __stdargs prevents creation of register arguments entry point.
106 * If both stack args and reg. args entry points are created, this
107 * function is called _twice_, which is not wanted.
109 * The number 200 in the function names is the priority assigned to
110 * shared library autoinitialization functions by SAS/C 6.50.
112 LONG STDARGS CONSTRUCTOR _STI_200_openSockets(void)
114 struct Library *IntuitionBase;
117 * Open bsdsocket.library
119 if ((SocketBase = OpenLibrary((STRPTR)SOCKETNAME, SOCKETVERSION)) != NULL) {
121 * Succesfull. Now tell bsdsocket.library:
122 * - the address of our errno
123 * - our program name
124 * h_errno is not processed here any more, now it's thread-safe
126 if (SocketBaseTags(SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))), &errno,
127 SBTM_SETVAL(SBTC_LOGTAGPTR), _ProgramName,
128 TAG_END))
129 exit(30);
130 return 0;
133 IntuitionBase = OpenLibrary("intuition.library", 36);
135 if (IntuitionBase != NULL) {
136 struct EasyStruct libraryES;
138 libraryES.es_StructSize = sizeof(libraryES);
139 libraryES.es_Flags = 0;
140 libraryES.es_Title = _ProgramName;
141 libraryES.es_TextFormat = "Unable to open bsdsocket.library version 4 or later";
142 libraryES.es_GadgetFormat = "Exit %s";
144 EasyRequestArgs(NULL, &libraryES, NULL, (APTR)&_ProgramName);
146 CloseLibrary(IntuitionBase);
148 exit(20);
151 void STDARGS DESTRUCTOR _STD_200_closeSockets(void)
153 if (SocketBase) {
154 CloseLibrary(SocketBase);
155 SocketBase = NULL;
159 #ifdef __AROS__
162 * Unfortunately AROS startup code does not support early exit().
163 * This can be considered a serious C++ compatibility problem since
164 * constructors of static objects may throw exceptions,
165 * involving exit() or abort() calls.
168 static ULONG AROS_openSockets(void)
170 return _STI_200_openSockets() ? FALSE : TRUE;
173 ADD2INIT(AROS_openSockets, 10);
174 ADD2EXIT(_STD_200_closeSockets, 10);
176 #endif