/************************************************************************
 *									*
 *			Opening a Library				*
 *			=================				*
 *									*
 *	Copyright © 1993 by Mark Little, All Rights Reserved		*
 *									*
 * This source and program are provided as part of the OZAmiga Cover    *
 * Disk and may not be used in any other publication or training course *
 * without the written permission of the Author.			*
 *									*
 ************************************************************************
 *									*
 * Function								*
 * ========								*
 *									*
 * This program demonstrates how to open a library			*
 *									*
 ************************************************************************
 *									*
 * Source File:		  library.c					*
 * Source Version:	  1.0						*
 * Destination File:	  library					*
 *									*
 * Source Computer:	  Amiga 3000					*
 * Operating System:	  AmigaDOS					*
 * O/S Version:		  2.04						*
 * Compiler:		  SAS C						*
 * Version:		  6.0						*
 * Destination Computer:  Any Amiga					*
 *									*
 ************************************************************************
 *									*
 * Revision History							*
 * ================							*
 *									*
 * 13-Feb-93								*
 * ---------								*
 *									*
 *    Original Version							*
 *									*
 ************************************************************************/

/************************************************************************
 *			INCLUDE Files					*
 ************************************************************************
 These files usually define external functions which will be called by the
 program. They can be functions supplied with the compiler, or they can
 be functions you have written. File names enclosed in angle brackets will
 be looked for along the search path, while file names enclosed in double
 quotes (") will be looked for in the current directory only.
 ************************************************************************/

#include    <stdio.h>
#include    <exec/exec.h>

/*======================================================================*
 *			Compiler Specific Files				*
 *======================================================================*
 The SAS compiler uses prototypes to ensure that data items which are
 passed to called procedures are the correct type. Your compiler may or
 may not require this. If you have a proto directory in you INCLUDE:
 directory, you may want to remove the #ifdef and #endif statements.
 *======================================================================*/

#ifdef __SASC_60
   #include    <proto/exec.h>
#endif

/************************************************************************
 *			Definations					*
 ************************************************************************
 These lines define structures which will be used in the program.
 This allows changes to be made in many places by only changing one line.
 ************************************************************************/


/************************************************************************
 *			GLOBAL Variables				*
 ************************************************************************
 Variables defined outside of the program modules can be accessed by all
 program modules in this file and by other modules which are linked to
 this file. Variables defined inside the opening braces of a program module
 are only accessable by that module. The section declares a variable as
 type BTest and initilalises the data items in that structure.
 It also allocates a Pointer of type BTest. This means that the compiler
 should produce a warning if it is set to point at any other data type.
 ************************************************************************/


/*======================================================================*
 *			Force a Larger WorkBench Window			*
 *======================================================================*
 *** CAUTION *** This following statements are designed for the SAS C
 compiler version 6.0 and defines the type of window to be opened if this
 program is run from WorkBench instead of the CLI. To stop this from being
 compiled by other compilers, it is enclosed in a conditional clause.
 My compiler defines __SASC_60 which means this statement will be compiled
 into the program.
 You should also remember that some compilers may produce programs which
 may crah if they are run from the WorkBench when they should be run from
 the CLI - Rebuild this program and see what happens!
 *======================================================================*/

#ifdef __SASC_60
		char	__stdiowin[] =
					"CON:0/0/640/200/Learning C - ";
#endif

/*======================================================================*
 *			Program Variables				*
 *======================================================================*
 These statements tell the compiler what the names of the variables used
 in this program are. They also define what type of information is to be
 stored in the variables.
 *======================================================================*/

	char	Name[80];
   struct   Library     *LibraryBase;

/************************************************************************
 *			MAIN PROGRAM					*
 ************************************************************************
 The module named "main" is the starting point of execution. Your compiler
 may open resources such as DOS, EXEC and other libraries before main is
 called. Consult your compiler documentation for details of what it can
 do.
 ************************************************************************/

void main(void)
{

/*======================================================================*
 *			Ask For the Library Name			*
 *======================================================================*/

   printf("Enter the Name of the Library, eg, 'dos.library': ");
   scanf("%s",Name);

/*======================================================================*
 *			Open the Selected Library			*
 *======================================================================*
 A Library is a group of related functions which can be opended by any
 number of programs. Unliked linked libraries, where a fresh copy is
 inserted into each program, these libraries are shared with each program
 using the same code.
 ** NOTE ** If a library opens, then it will set the return as TRUE. This
 means the "if" statements will be carried out. Look at the exec/exec.h
 for information on what is in the library structure.
 *======================================================================*/

   if (LibraryBase = OpenLibrary(Name,0))
   {
      printf("%s is located at address %#04x\n",Name,LibraryBase);
      printf("%s",LibraryBase->lib_IdString);
      printf("Version: %d.%d\n",LibraryBase->lib_Version,
				LibraryBase->lib_Revision);
      printf("Open Count is %d\n",LibraryBase->lib_OpenCnt);
      CloseLibrary(LibraryBase);       /* Close It - It's not used      */
   }
   else
      printf("Oops - %s didn't Open\n",Name);
}
