[comp.sys.ibm.pc] MSDOS source of wildcard parsing LATTICE-c

jchvr@ihlpg.UUCP (06/04/87)

source of PARSE.C needed for TOUCH.C see previous posting

#include	"dos.h"
#include	"stdio.h"

/* define directory entry types */
#define	FREADONLY	0x1
#define	FHIDDEN		0x2
#define FSYSTEM		0x4
#define FLABEL		0x8
#define FDIR		0x10
#define	FARCHIVE	0x20

static union REGS rg;		/* cpu register for use of DOS calls */
static char	ar[256];

struct DTA {
	char	reserved[21];
	char	attribute;
	int	time;
	int	date;
	int	lowsize;
	int	highsize;
	char	name[13];
	char	padding;
};
static struct DTA dta;		/* copy for Disk transfer address */

static haswild(s)
char	*s;
{
	while ( *s ) {
		if ( (*s == '*') || (*s == '?') )  {
			return(1);
		}
		s++;
	}
	return(0);
}/*haswild*/

static dofile(s,f)
char	*s;
int	(*f)();
{	int i,j;
	int	res;
	
	strcpy(ar,s);

	/* set DTA */
	rg.h.ah = 0x1a;		/* set DTA */
	rg.x.dx = &(dta);
	intdos(&rg, &rg);
	
	/* start search through direc */
	rg.h.ah = 0x4e;		/* find match file */
	rg.x.cx = 0xffff;	/* all bits on */
	rg.x.dx = &(ar[0]);	/* pointer to path name */
	intdos(&rg, &rg);

	/* if nothing found then return original */
	if ( (rg.x.ax == 2) || (rg.x.ax == 18) ) {
		return((*f)(ar));
	}
	/* call func with name */
	while ( (rg.x.ax != 2) && ( rg.x.ax != 18 ) ) {
		/* do not look at labels */
		if ( (dta.attribute & FLABEL) != 0) {
			goto next;
		}
		/* do not look at dirs */
		if ( (dta.attribute & FDIR) != 0) {
			goto next;
		}
		/* else copy result and print it */		
		res = (*f)(dta.name);

next:		/* search for next entry */
		rg.h.ah = 0x4f;
		intdos(&rg, &rg);
	}
}/*dofile*/

static int	glargc;
static int	glindex;
static char	**glargv;

#define MAXARG	32
static insert(s)
char	s[];
{	int i;
	char *ptr;
	char *malloc();
	
	if (glargc >= MAXARG) {
		fprintf(stderr,"Too many arguments\n");
		exit(1);
	}
	
	/* make room for string s */
	ptr = malloc(strlen(s)+1);
	if ( ptr == NULL) {
		fprintf(stderr,"Out of memory\n");
		exit(1);
	}
	strcpy(ptr,s);
	
	/* make room at glindex by moving it all down */
	for (i=glargc; i > glindex ; i--) {
		glargv[i] = glargv[i-1];
	}
	
	/* insert s and update argc index */
	glargv[glindex] = ptr;
	glindex++;
	glargc++;
}/*insert*/

/* parse file wildcards in argv[index] while adjusting argc and index
** index is returned as index into argv where index+1 used to point
*/
_parse(argc,argv,index)
int	*argc;
char	**argv;
int	*index;
{
	char	*ptr;
	int	i;
		
	/* set global stuff */
	glargc = *argc;
	glindex= *index;
	glargv = argv;
	ptr = glargv[glindex];	/* save ptr to thing to expand */
	
	if (glindex >= glargc) {
		*index += 1;
		return;	/* no more to do */
	}
	if (haswild(ptr) == 0) {
		*index += 1;
		return;	/* nothing to do no wild */
	}
	
	/* delete item at glindex */
	glargc--;
	for (i=glindex; i<glargc; i++) {
		glargv[i]=glargv[i+1];
	}

	/* call insert routine */
	dofile(ptr,insert);
	
	/* copy global stuff */
	*argc = glargc;
	*index= glindex;
}/*_parse*/

/* parse all argv[i] starting at i=index */
_parseall(argc,argv,index)
int	*argc;
char	**argv;
int	index;
{	int i;
	while ( index < *argc ) {
		_parse(argc,argv,&index);
	}
}/*_parseall*/