/*******************************************************************************
**
** Source File Name = d_dbmcon.c  1.3 
**
** Licensed Materials - Property of IBM
**
** (C) COPYRIGHT International Business Machines Corp. 1995, 1999 
** All Rights Reserved.
**
** US Government Users Restricted Rights - Use, duplication or
** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
**
**
**    PURPOSE :
**       This program is an example of how APIs are implemented in order to
**       obtain the Database Manager Configuration Defaults.
**
**    APIs USED :
**       GET DATABASE MANAGER CONFIGURATION DEFAULTS       sqlfdsys()
**       ATTACH TO INSTANCE                                sqleatin()
**       DETACH FROM INSTANCE                              sqledtin()
**
**    STRUCTURES USED :
**       sqlfupd
**       sqlca
**
**    OTHER FUNCTIONS USED :
**       'C' COMPILER LIBRARY :
**          stdio.h  -  printf
**
**       internal :
**          print_info :      Displays a directory of databases
**          alloc_mem :       Allocates the proper amount of memory for the
**                            GET DATABASE CONFIGURATION API to fill with
**                            data.
**
**       external :
**          check_error :     Checks for SQLCODE error, and prints out any
**          [in util.c]          related information available.
**                               This procedure is located in the util.c file.
**
**    EXTERNAL DEPENDENCIES :
**       - Ensure existence of database for precompile purposes.
**       - Compile and link with the IBM Cset++ compiler (AIX and OS/2)
**         or the Microsoft Visual C++ compiler (Windows) 
**         or the compiler supported on your platform.
**       - a database by the name of "DBCONF" does not exist.
**
** For more information about these samples see the README file.
**
** For more information on programming in C, see the:
**   -  "Programming in C and C++" section of the Application Development Guide
** For more information on Building C Applications, see the:
**   -  "Building C Applications" section of the Application Building Guide.
**
** For more information on the SQL language see the SQL Reference.
**
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlutil.h>
#include <sqlenv.h>
#include "util.h"

#ifdef DB268K
/* Need to include ASLM for 68K applications */
#include <LibraryManager.h>
#endif


#define  CHECKERR(CE_STR)   if (check_error (CE_STR, &sqlca) != 0) return 1;
#define  LISTNUMBER 2
int alloc_mem (struct sqlfupd *);
int print_info (struct sqlfupd *);

int main (int argc, char *argv[]) {
   unsigned short idx;
   struct sqlfupd itemList[LISTNUMBER];
   struct sqlca sqlca;

   char instName[SQL_INSTNAME_SZ + 1];
   alloc_mem (itemList);

#ifdef DB268K
   /* Before making any API calls for 68K environment,
      need to initial the Library Manager */
	InitLibraryManager(0,kCurrentZone,kNormalMemory);
	atexit(CleanupLibraryManager);
#endif

   printf ("this is sample program : d_dbmcon.c\n");

   /*********************************************
    If no arguments are passed, assume local instance, otherwise
      the first argument is the instance name
      the second argument is the user name
      the third argument is the password
   *********************************************/
   if (argc !=1 && argc != 4) {
      printf ("\nUSAGE: d_dbmcon [instance/node_name user_name password]\n\n");
      return 1;
   }

   if (argc == 4)
   {
      printf ("ATTACHed TO INSTANCE API called for instance :%s:\n", argv[1]);
      /*******************************\
      * ATTACH TO INSTANCE API called *
      \*******************************/
      sqleatin (argv[1], argv[2], argv[3], &sqlca);
      CHECKERR ("attach to instance");
   }

   printf ("Listing the default database manager configurations\n");

   /********************************************************\
   * GET DATABASE MANAGER CONFIGURATION DEFAULTS API called *
   \********************************************************/
   sqlfdsys (LISTNUMBER, itemList, &sqlca);
   CHECKERR("get database manager config");

   print_info (itemList);

   for (idx = 0; idx < LISTNUMBER; idx++) {
      free (itemList[idx].ptrvalue);
   } /* endfor */

   if (argc == 4)
   {  printf ("DETACHed FROM INSTANCE API called\n", instName);
      /*********************************\
      * DETACH FROM INSTANCE API called *
      \*********************************/
      sqledtin (&sqlca);
      CHECKERR ("detach from instance");
   }

   return 0;
}

/*******************************************************************************
* this procedure allocates the required memory to each section of the array.
* Each member of the array must be appointed to a specific field in regards
* to the output of the GET DATABASE CONFIGURATION API call
*******************************************************************************/
int alloc_mem (struct sqlfupd *itemList) {
   itemList[0].token = SQLF_KTN_MAXAGENTS;
   itemList[0].ptrvalue = (char *)malloc(sizeof(int));

   itemList[1].token = SQLF_KTN_NUMDB;
   itemList[1].ptrvalue = (char *)malloc(sizeof(short));

   return 0;
}

int print_info (struct sqlfupd *itemList) {
   printf ("Max. number of Agents                 : %d\n",
      *(int *)itemList[0].ptrvalue);
   printf ("Number of concurrent active DB allowed: %d\n",
      *(short *)itemList[1].ptrvalue);
   printf ("\n");

   return 0;
}