/*
 *	This tool is for checking path names. It scans directory tree
 *	recursively from current dir, and prints names of files and
 *	directories which can cause problems when copying from/to
 *	POSIX systems. Good path name assumed to contain only alphanumeric
 *	chars (isalnum() != 0) and small set of non-alphanumeric chars.
 */
 
#include <../source/TAL/objbase.h>

V_LOCAL unsigned char extra_file_chars[]="_()-+=$[].";
V_LOCAL unsigned char extra_dir_chars[]="_/()-+=$[].";

/*
 *	We use internal filesystem stuff; therefore, LibV must be linked
 *	statically (executable size in case of VC-6 is about 28 kb).
 */

#include <../source/vfs/fsbase.h>

V_LOCAL vboolean
checkname(unsigned char *name,vboolean verbose)
{
	int i;
	unsigned char fn[_MAX_FNAME], ext[_MAX_EXT], nameext[_MAX_PATH];
	/* Extract file name from full path name */
	fsSplitPath((char*)name,NULL,(char*)fn,(char*)ext);
	fsMakePath((char*)nameext,NULL,(char*)fn,(char*)ext);
	/* Check name */
	for(i=0;nameext[i];i++)
		if(!isalnum((int)nameext[i]) && !strchr((char*)extra_file_chars,(int)nameext[i]))
			break;
	if(!nameext[i])
		return true;
	if(verbose)
		printf("File name '%s': illegal char '%c' at pos %i\n",name,nameext[i],i);
	return false;
}

V_LOCAL vboolean
checkdir(unsigned char *name,vboolean verbose)
{
	int i;
	for(i=0;name[i];i++)
		if(!isalnum((int)name[i]) && !strchr((char*)extra_dir_chars,(int)name[i]))
			break;
	if(!name[i])
		return true;
	if(verbose)
		printf("Dir name '%s': illegal char '%c' at pos %i\n",name,name[i],i);
	return false;
}

int retval = 0;

void V_CALL
checknames(char *name)
{
	if(!checkname((unsigned char*)name,true))
		retval = -1;
}

void V_CALL
checkdirs(char *name)
{
	if(!checkdir((unsigned char*)name,true))
		retval = -1;
}

int main(void)
{
	/*
	 *	Spawn recurcive enumeration of objects passing
	 *	all names to the case conversion procedure.
	 */
	lfsEnumFiles(checkdirs,checknames,true);
	return retval;
}
