/**
 * @file   SCFCRCPY.c
 * @brief  Sample program in C to copy files via the FLUC record interface
 * @author limes datentechnik gmbh
 ******************************************************************************/

/* defines code page for literals for IBM mainframes to German */
#ifdef __ZOS__
#  pragma convert(1141)
#endif

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#include"FLCRECLB.h"

/**
 * This test program takes four parameters to convert files via the
 * FLUC record interface.
 *
 * @param  argc must be 5
 * @param  argv contains the strings for FCROPN
 * @return 8=parameter error
 *         6=open file error
 *         4=read write error
 *         2=close error
 *         0=Success
 */
int main(int argc, char * argv[])
{
   void*                   pvRed;
   void*                   pvWrt;
   char                    acBuf[65536];
   char                    acSta[2048];
   int                     siLn1,siLn2,siLen,siSta;
   int                     siRtc,siHlp,siWat,siDep,i;
   int                     siGod=1;
   char*                   h;
   char*                   a[5]={"","","","",""};

/* defines environment variable LANG to German EBCDIC for IBM mainframes */
#ifdef __ZOS__
   __setenv("LANG","de_DE.IBM-1141",1);
#endif

   if (argc==2) { /* parse HOST parameter string */
      i=2; a[0]=argv[0]; a[1]=argv[1];
      h=strchr(a[1],',');
      if (h!=NULL) { h[0]=0x00; a[2]=h+1; i++; }
      h=strchr(a[2],',');
      if (h!=NULL) { h[0]=0x00; a[3]=h+1; i++; }
      h=strchr(a[3],',');
      if (h!=NULL) { h[0]=0x00; a[4]=h+1; i++; }
      argc=i; argv=a;
   }

   siDep=1;
   if (argc!=5) {
      fprintf(stderr,"Parameter count (%d) not correct\n",argc);
      fprintf(stderr,"Given parameters:\n");
      for (i=0;i<argc;i++) fprintf(stderr,"--> P%2.2d(%s)\n",i,argv[i]);
      fprintf(stderr,"Required parameters:\n");
      fprintf(stderr,"%s \"read file string\" \"read format string\""
         " \"write file string\" \"write format string\"\n",argv[0]);
      siWat=FLC_READ_FILE; siLen=sizeof(acBuf); siHlp=0;
      FCRSYN(&siRtc,&siWat,&siDep,&siHlp,NULL,&siLen,acBuf);
      fprintf(stderr,"READ FILE STRING   : \"%s\"\n",acBuf);
      siWat=FLC_READ_FORMAT;  siLen=sizeof(acBuf); siHlp=0;
      FCRSYN(&siRtc,&siWat,&siDep,&siHlp,NULL,&siLen,acBuf);
      fprintf(stderr,"READ FORMAT STRING : \"%s\"\n",acBuf);
      siWat=FLC_WRITE_FILE;   siLen=sizeof(acBuf); siHlp=0;
      FCRSYN(&siRtc,&siWat,&siDep,&siHlp,NULL,&siLen,acBuf);
      fprintf(stderr,"WRITE FILE STRING  : \"%s\"\n",acBuf);
      siWat=FLC_WRITE_FORMAT; siLen=sizeof(acBuf); siHlp=0;
      FCRSYN(&siRtc,&siWat,&siDep,&siHlp,NULL,&siLen,acBuf);
      fprintf(stderr,"WRITE FORMAT STRING: \"%s\"\n",acBuf);
      exit(8);
   }

   siLn1=strlen(argv[1]); siLn2=strlen(argv[2]); siSta=sizeof(acSta);
   FCROPN(&pvRed,&siRtc,&siLn1,argv[1],&siLn2,argv[2],&siSta,acSta);
   if (pvRed==NULL) {
      fprintf(stderr,"Open file for read operation failed\n");
      fprintf(stderr,"FCROPN(&pvRed,%d,%d,\"%s\",%d,\"%s\");\n\n",
              siRtc,siLn1,argv[1],siLn2,argv[2]);
      siLen=sizeof(acBuf);
      FCRMSG(&siHlp,&siRtc,&siLen,acBuf);
      fprintf(stderr,"ERRMSG:\n%s\n\n",acBuf);
      siLen=sizeof(acBuf);
      FCRTRC(&siHlp,&siRtc,&siLen,acBuf);
      fprintf(stderr,"ERRTRC:\n%s\n"  ,acBuf);
      exit(6);
   }

   /*Use null-terminated strings*/
   FCROPN(&pvWrt,&siRtc,NULL,argv[3],NULL,argv[4],&siSta,acSta);
   if (pvWrt==NULL) {
      fprintf(stderr,"Open file for write operation failed\n");
      fprintf(stderr,"FCROPN(&pvWrt,%d,%d,\"%s\",%d,\"%s\");\n\n",
              siRtc,siLn1,argv[3],siLn2,argv[4]);
      siLen=sizeof(acBuf);
      FCRMSG(&siHlp,&siRtc,&siLen,acBuf);
      fprintf(stderr,"ERRMSG:\n%s\n\n",acBuf);
      siLen=sizeof(acBuf); FCRTRC(&siHlp,&siRtc,&siLen,acBuf);
      fprintf(stderr,"ERRTRC:\n%s\n"  ,acBuf);
      FCRCLS(&pvRed,NULL,NULL,NULL,NULL,NULL);
      exit(6);
   }

   for (siLen=sizeof(acBuf),FCRGET(&pvRed,&siRtc,&siLen,acBuf); siRtc==0;
        siLen=sizeof(acBuf),FCRGET(&pvRed,&siRtc,&siLen,acBuf)) {
      FCRPUT(&pvWrt,&siRtc,&siLen,acBuf);
      if (siRtc) {
         fprintf(stderr,"Write data to file failed\n");
         fprintf(stderr,"FCRPUT(&pvWrt,%d,%d,acBuf);\n\n",
                 siRtc,siLen);
         siLen=sizeof(acBuf);
         FCRMSG(&siHlp,&siRtc,&siLen,acBuf);
         fprintf(stderr,"ERRMSG:\n%s\n\n",acBuf);
         siLen=sizeof(acBuf);
         FCRTRC(&siHlp,&siRtc,&siLen,acBuf);
         fprintf(stderr,"ERRTRC:\n%s\n"  ,acBuf);
         FCRCLS(&pvRed,NULL,NULL,NULL,NULL,NULL);
         FCRCLS(&pvWrt,NULL,NULL,NULL,NULL,NULL);
         exit(4);
      }
   }
   if (siRtc!=FLMRTC_OK && siRtc!=FLMRTC_EOF) {
      fprintf(stderr,"Read data from file failed\n");
      fprintf(stderr,"FCRGET(&pvRed,%d,%d,acBuf);\n\n",
              siRtc,(int)sizeof(acBuf));
      siLen=sizeof(acBuf);
      FCRMSG(&siHlp,&siRtc,&siLen,acBuf);
      fprintf(stderr,"ERRMSG:\n%s\n\n",acBuf);
      siLen=sizeof(acBuf);
      FCRTRC(&siHlp,&siRtc,&siLen,acBuf);
      fprintf(stderr,"ERRTRC:\n%s\n"  ,acBuf);
      FCRCLS(&pvRed,NULL,NULL,NULL,NULL,NULL);
      FCRCLS(&pvWrt,NULL,NULL,NULL,NULL,NULL);
      exit(4);
   }

   siLen=sizeof(acBuf);
   FCRCLS(&pvWrt,&siRtc,&siGod,NULL,&siLen,acBuf);
   if (siRtc) {
      fprintf(stderr,"Close file for write operation failed\n");
      fprintf(stderr,"FCRCLS(&pvWrt,%d);\n\n",siRtc);
      siLen=sizeof(acBuf);
      FCRMSG(&siHlp,&siRtc,&siLen,acBuf);
      fprintf(stderr,"ERRMSG:\n%s\n\n",acBuf);
      siLen=sizeof(acBuf);
      FCRTRC(&siHlp,&siRtc,&siLen,acBuf);
      fprintf(stderr,"ERRTRC:\n%s\n"  ,acBuf);
      FCRCLS(&pvRed,NULL,NULL,NULL,NULL,NULL);
      exit(2);
   } else {
      fprintf(stderr,"Statistics for FCROPN(\"%s\",\"%s\")\n",argv[3],argv[4]);
      fprintf(stderr,"%s",acBuf);
   }

   siLen=sizeof(acBuf);
   FCRCLS(&pvRed,&siRtc,&siGod,NULL,&siLen,acBuf);
   if (siRtc) {
      fprintf(stderr,"Close file for read operation failed\n");
      fprintf(stderr,"FCRCLS(&pvRed,%d);\n\n",siRtc);
      siLen=sizeof(acBuf);
      FCRMSG(&siHlp,&siRtc,&siLen,acBuf);
      fprintf(stderr,"ERRMSG:\n%s\n\n",acBuf);
      siLen=sizeof(acBuf);
      FCRTRC(&siHlp,&siRtc,&siLen,acBuf);
      fprintf(stderr,"ERRTRC:\n%s\n"  ,acBuf);
      exit(2);
   } else {
      fprintf(stderr,"Statistics for FCROPN(\"%s\",\"%s\")\n",argv[1],argv[2]);
      fprintf(stderr,"%s",acBuf);
   }

   exit(0);
}

/**********************************************************************/
