/*******************************************************************************
**
** Source File Name = dbinst.c 
**
** Licensed Materials - Property of IBM
**
** (C) COPYRIGHT International Business Machines Corp. 1995, 2000 
** All Rights Reserved.
**
** US Government Users Restricted Rights - Use, duplication or
** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
**
**
**    PURPOSE :
**       an example showing how to use DATABASE INSTANCE APIs in order to:
**          - Attach to an instance
**          - Retrieve the instance
**          - Change the password
**          - Reset the password
**          - Detach from the instance.
**
**    APIs USED :
**       ATTACH TO INSTANCE               sqleatin
**       GET INSTANCE                     sqlegins
**       ATTACH AND CHANGE PASSWORD       sqleatcp
**       DETACH FROM INSTANCE             sqledtin
**
**    STRUCTURES USED :
**       sqlca
**
**    OTHER FUNCTIONS DECLARED :
**       'C' COMPILER LIBRARY :
**          stdio.h  -  printf
**
**       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 :
**       - 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.
**
** 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 <string.h>
#include <stdio.h>
#include <sqlenv.h>
#include "utilapi.h"



int main (int argc, char *argv[]) {
   struct sqlca sqlca;
   char input[]="";
   char instName[SQL_INSTNAME_SZ + 1];
   char newPassword[]="NewPassword";


   /*********************************************
      the first argument is the instance name
      the second argument is the user name
      the third argument is the password
   *********************************************/
   if (argc != 4) {
      printf ("\n\nUSAGE: dbinst instance_name user_name password\n\n");
      return 1;
   }

   printf ("sample program : dbinst.c\n");

   printf ("\nATTACH TO INSTANCE API called\n");
   printf ("for instance :%s\n", argv[1]);
   /*******************************\
   * ATTACH TO INSTANCE API called *
   \*******************************/
   sqleatin (argv[1], argv[2], argv[3], &sqlca);
   API_SQL_CHECK("attach to instance");

   printf ("\nGET INSTANCE API called\n");
   /*************************\
   * GET INSTANCE API called *
   \*************************/
   instName[SQL_INSTNAME_SZ] = '\0';
   sqlegins (instName, &sqlca);
   API_SQL_CHECK("get instance name");

   printf ("current instance = %s\n", instName);
   printf ("\n\tATTACH AND CHANGE PASSWORD API\n");
   printf ("\nWarning: This API will attempt to change the instance password,\n");
   printf ("and then change back to the original password. Some systems\n");
   printf ("do not allow a change back to a recent old password and/or require\n");
   printf ("certain restrictions on password format that may not be reflected\n");
   printf ("in the new password provided. You can alter the source code to\n");
   printf ("comply with these restrictions, if they apply to your system.\n");    
   printf ("\nWould you like to run this API?\ty/n\n");

   fflush(stdin);
   scanf("%s", &input);
   if (strncmp(input,"y",1))
     return 0;
   else
   {
     printf ("\nATTACH AND CHANGE PASSWORD API called\n");
     printf ("the old password is :%s\n", argv[3]);
     printf ("the new password is :%s\n", newPassword);
     /***************************************\
     * ATTACH AND CHANGE PASSWORD API called *
     \***************************************/
     sqleatcp (argv[1], argv[2], argv[3], newPassword, &sqlca);
     API_SQL_CHECK("change password");

     printf ("\nATTACH AND CHANGE PASSWORD API called\n");
     printf ("the password is reset to :%s\n", argv[3]);
     /***************************************\
     * ATTACH AND CHANGE PASSWORD API called *
     \***************************************/
     sqleatcp (argv[1], argv[2], newPassword, argv[3], &sqlca);
     API_SQL_CHECK("reset password");

     printf ("\nDETACH FROM INSTANCE API called\n", instName);
     /*********************************\
     * DETACH FROM INSTANCE API called *
     \*********************************/
     sqledtin (&sqlca);
     API_SQL_CHECK("detach from instance");

     return 0;
   }
}
/* end of program : dbinst.c */