/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>
#include <string.h>

#include <rms.h>
#include <descrip.h>
#include <starlet.h>
#include <lib$routines.h>
#include <ssdef.h>
#include <rmsdef.h>

void linelen(char *fnm,int *mrs,int *lrl)
{
   struct FAB myfab;
   struct XABFHC myxab;
   myfab = cc$rms_fab;
   myxab = cc$rms_xabfhc;
   myfab.fab$l_fna = fnm;
   myfab.fab$b_fns = strlen(fnm);
   myfab.fab$l_xab = (char *)&myxab;
   SYS$OPEN(&myfab,0,0);
   SYS$CLOSE(&myfab,0,0);
   *mrs = myfab.fab$w_mrs;
   *lrl = myxab.xab$w_lrl;
}

void search(char *find)
{

   int context,status,mrs,lrl;
   char result[255];
   struct dsc$descriptor_s finddesc,resultdesc;
   context = 0;
   finddesc.dsc$w_length = strlen(find);
   finddesc.dsc$b_dtype = DSC$K_DTYPE_T;
   finddesc.dsc$b_class = DSC$K_CLASS_S;
   finddesc.dsc$a_pointer = find;
   resultdesc.dsc$w_length = sizeof(result);
   resultdesc.dsc$b_dtype = DSC$K_DTYPE_T;
   resultdesc.dsc$b_class = DSC$K_CLASS_S;
   resultdesc.dsc$a_pointer = result;
   for(;;)
   {
      status = lib$find_file(&finddesc,&resultdesc,&context,0,0,0,0);
      if (status != RMS$_NORMAL) break;
      *(strchr(result,' ')) = '\0';
      linelen(result,&mrs,&lrl);
      printf("%s %d %d\n",result,mrs,lrl);
   }
   lib$find_file_end(&context);
}

int main()
{
   search("*.com");
   return SS$_NORMAL;
}
