/*  c_next_file_vms.c - return file names in order  */

#include    <stdio.h>
#include    <stdlib.h>
#ifdef VMS
#include    <descrip.h>
#include    <rms.h>
#include    <ssdef.h>
#endif

#define     NAME_LENGTH     256

static struct  dsc$descriptor in_name;
static struct  dsc$descriptor in_def;

static int     context, flags, status, zero;

int c_next_file_init(char *name)  /*  initialization routine  */
{

    /*  fill in the name and default name descriptors  */

    in_name.dsc$w_length  = strlen(name);
    in_name.dsc$a_pointer = name;

    in_def.dsc$w_length  = strlen(name);
    in_def.dsc$a_pointer = name;

    /*  close context if necessary  */

    if (context) lib$find_file_end(&context);

    /*  initialize flags  */

    context = flags = status = zero = 0;

    return (1);
}
char *c_next_file()   /*  return file names in order  */
{
    static char result[NAME_LENGTH], *cp;
    static      $DESCRIPTOR(result_d,result);

    /*  get the next file name  */

    if (lib$find_file(&in_name,&result_d,&context,
                      &in_def,&zero,&status,&flags) != RMS$_NMF
                      && status != SS$_NOSUCHFILE) {

        /*  set string length & cut out ".][" if it's there  */

        if ( (cp=strstr(result," ")) ) *cp = '\0';
        if ( (cp=strstr(result,"][")) ) strcpy(cp,(cp+2));
        for (cp=result; *cp != '\0'; cp++) *cp = tolower(*cp);

        return(result);
    }
    else return((char *) NULL);
}    

/* c_next_file_vms.c */
