/*
This C program lists the active sap users of a specified SAP instance
by calling an ABAP function module. The program can be placed to
/sapnmt/SID/exe, and can be used just before stopping the system.
*/

#include 
#include 
#include 
#include "saprfc.h"
#include "sapitab.h"
 
main( void )
{
   static RFC_OPTIONS        rfc_opt;
   static RFC_CONNOPT_R3ONLY rfc_connopt_r3only;
   static RFC_PARAMETER      exporting[1];
   static RFC_PARAMETER      importing[1];
   static RFC_TABLE          tables[1];
   RFC_HANDLE                handle;
   RFC_RC                    rc;
   char *                    exception_ptr;
   ITAB_H                    ltab;
   char                      lst[100];
   char *                    lpt;
   int                       line, lnr, len;
 
   rfc_opt.client   = "055";
   rfc_opt.user     = "TEST";
   rfc_opt.language = "E";
   rfc_opt.password = "INITPASS";
   rfc_opt.trace    = 0;
   rfc_opt.mode     = RFC_MODE_R3ONLY;
   rfc_opt.connopt = &rfc_connopt_r3only;
 
   rfc_connopt_r3only.hostname = "nti68mto";
   rfc_connopt_r3only.sysnr = 9;
 
   ltab    = ItCreate( "BARMI", 100, 0, 0 );
 
   importing[0].name = NULL;
   exporting[0].name = NULL;
   tables[0].name = "A";
   tables[0].nlen = 1;
   tables[0].type = TYPC;
   tables[0].ithandle = ltab;
   tables[0].leng = 100;
 
   handle = RfcOpen( &rfc_opt );
   if( handle == RFC_HANDLE_NULL )
   {
        return 1;
   }
   rc = RfcCallReceive( handle,
                        "Z_TST",
                        exporting,
                        importing,
                        tables,
                        &exception_ptr );
   RfcClose( handle );
   if (rc) return 1;
 
   lnr = ItFill( ltab );
   for ( line=1; line <= lnr; line++)
   {
     lpt = (char *) ItGetLine( ltab, line );
     len = ab_csize( lpt, 100 );
     memcpy( lst, (char *) ItGetLine( ltab, line ), len);
     lst[len] = '\0';
     fprintf( stdout, "%s\n", lst );
   }
   return 0;
}