/*
	extwild.c

	ワイルドカードの展開
*/

#include <stdio.h>
#include <dos.h>
#include <string.h>
#include <malloc.h>

#include <ryosuke.h>

int extwildcard(char *wildcard, char *namebuf[], int maxnames)
	/* 返値 : 見つかったファイルの個数(≦maxnames)			     */
	/* 各ファイルネームを格納するための領域は逐一 malloc される｡ */
	{
	char			*p,path[80],buf[80];
	struct find_t	fdat;
	bool 			first;
	uint			ret;
	int				foundnum;
									/* wildcard から パス名部分を取り出す */
	strcpy(path,wildcard);
	if ((p = strrchr(path, '\\')) != NULL)
		*++p = '\0';
	else
		path[0] = '\0';

	for (first=YES,foundnum=0;  foundnum < maxnames;  first=NO,foundnum++) {
		if (first)
			ret = _dos_findfirst(wildcard, _A_NORMAL, &fdat);
		else
			ret = _dos_findnext(&fdat);
		if (ret != 0)
			break;

		strcpy(buf, path);
		strcat(buf, fdat.name);
		if ((namebuf[foundnum] = malloc(strlen(buf)+1)) == NULL)
			break;
		else
			strcpy(namebuf[foundnum], buf);
		}
	return foundnum;
	}
