3 * autoinit.c - SAS/C autoinitialization functions for bsdsocket.library
5 * Copyright © 1994 AmiTCP/IP Group,
6 * Network Solutions Development Inc.
8 * Copyright © 2005 Pavel Fedin
11 #include <exec/types.h>
12 #include <exec/libraries.h>
14 #include <intuition/intuition.h>
16 #include <proto/socket.h>
17 #include <proto/exec.h>
18 #include <proto/intuition.h>
24 #include <bsdsocket/socketbasetags.h>
28 struct Library
*SocketBase
= NULL
;
30 static const char SOCKETNAME
[] = "bsdsocket.library";
32 #define SOCKETVERSION 4 /* minimum bsdsocket version to use */
34 extern STRPTR _ProgramName
; /* startup module defines this :-) */
39 /*#if sizeof(int) != sizeof(long)
40 #error short ints not supported!
44 /****** net.lib/autoinit *********************************************
47 autoinit - Autoinitialization Functions
50 LONG _STI_200_openSockets(void)
52 void _STD_200_closeSockets(void)
55 These functions open and close the bsdsocket.library at the startup
56 and exit of the program, respectively. For a program to use these
57 functions, it must be linked with netlib:net.lib (or some variant).
58 These functions are linked in only if the program references the
59 global symbol "SocketBase".
61 If the library can be opened, the _STI_200_openSockets() calls
62 bsdsocket.library function SocketBaseTags() to tell the library the
63 address and the size of the errno variable of the calling program,
64 the program name (to be used in syslog() messages) and the address
65 of the h_error variable (in which the name resolver errors are
69 _STI_200_openSockets() also checks that the system version is at
70 least 37. It also puts up a requester if the bsdsocket.library is
71 not found or is of wrong version.
73 The autoinitialization and autotermination functions are features
74 specific to the SAS C6. However, these functions can be used with
75 other (ANSI) C compilers, too. Example follows:
77 \* at start of main() *\
79 atexit(_STD_200_closeSockets);
80 if (_STI_200_openSockets() != 0)
84 The same autoinitialization won't work for both SAS C 6.3 and SAS C
85 6.50 or latter. Only way to terminate an initialization function is
86 by exit() call with SAS C 6.3 binary. If an autoinitialization
87 function is terminated by exit() call with SAS C 6.50 binary, the
88 autotermination functions won't be called. Due this braindamage
89 these compilers require separate net.lib libraries.
92 bsdsocket.library/SocketBaseTags(),
93 SAS/C 6 User's Guide p. 145 for details of autoinitialization and
94 autotermination functions.
96 *****************************************************************************
99 /* SAS C 6.50 kludge */
101 #if __VERSION__ > 6 || __REVISION__ >= 50
102 #define exit(x) return(x)
107 * Using __stdargs prevents creation of register arguments entry point.
108 * If both stack args and reg. args entry points are created, this
109 * function is called _twice_, which is not wanted.
111 * The number 200 in the function names is the priority assigned to
112 * shared library autoinitialization functions by SAS/C 6.50.
114 LONG STDARGS CONSTRUCTOR
_STI_200_openSockets(void)
116 struct Library
*IntuitionBase
;
122 if ((*(struct Library
**)4)->lib_Version
< 37)
126 * Open bsdsocket.library
128 if ((SocketBase
= OpenLibrary((STRPTR
)SOCKETNAME
, SOCKETVERSION
)) != NULL
) {
130 * Succesfull. Now tell bsdsocket.library:
131 * - the address of our errno
132 * - the address of our h_errno
135 if (SocketBaseTags(SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno
))), &errno
,
136 SBTM_SETVAL(SBTC_HERRNOLONGPTR
), &h_errno
,
137 SBTM_SETVAL(SBTC_LOGTAGPTR
), _ProgramName
,
143 IntuitionBase
= OpenLibrary("intuition.library", 36);
145 if (IntuitionBase
!= NULL
) {
146 struct EasyStruct libraryES
;
148 libraryES
.es_StructSize
= sizeof(libraryES
);
149 libraryES
.es_Flags
= 0;
150 libraryES
.es_Title
= _ProgramName
;
151 libraryES
.es_TextFormat
= "Unable to open bsdsocket.library version 4 or later";
152 libraryES
.es_GadgetFormat
= "Exit %s";
154 EasyRequestArgs(NULL
, &libraryES
, NULL
, (APTR
)&_ProgramName
);
156 CloseLibrary(IntuitionBase
);
161 void STDARGS DESTRUCTOR
_STD_200_closeSockets(void)
164 CloseLibrary(SocketBase
);