208 Chapter 6
Shared Library Management Routines
Initializers for Shared Libraries
C Source for _INITIALIZER (file init.c)
#include <dl.h>
/*
* Global initializer used by shared libraries that have
* registered it:
*/
void _INITIALIZER(shl_t hand, int loading)
{
void (*load_unload)();
if (loading)
shl_findsym(&hand, “load”, TYPE_PROCEDURE, (void *)
&load_unload);
else
shl_findsym(&hand, “unload”, TYPE_PROCEDURE, (void *)
&load_unload(;
(*load_unload( (); /* call the function */
}
The following two source files show shared libraries that have registered
_INITIALIZER.
C Source for libunits.sl
#include <stdio.h>
#include <dl.h>
void load() /* called after libunits.sl loaded */
{
printf(“libunits.sl loaded\n”);
}
void unload() /* called after libunits.sl unloaded
*/
{
printf(“libunits.sl unloaded\n”);
}
extern void _INITIALIZER();
float in_to_cm(float in) /* convert inches to centimeters */
{
return (in * 2.54);
}
float gal_to_l(float gal) /* convert gallons to litres */
{
return (gal * 3.79);
}
float oz_to_g(float oz) /* convert ounces to grams */
{
return (oz * 28.35);
}