<commit_before>#include <errno.h>
#include "zsfunctions.h"

#ifdef _WITH_SS5
#include "constants.ss5.h"
#else
#include "constants.h"
#endif

#include "convert.h"

struct GROUP {
	char           *name;
	gid_t		id;
};

struct USER {
	char           *name;
	uid_t		id;
};

struct dirent **dirlist;
struct dirent **dirlistp;
unsigned int	direntries = 0;
unsigned int	direntriesp = 0;
int		num_groups = 0, num_users = 0;
struct USER   **user;
struct GROUP  **group;

void 
d_log(char *fmt,...)
{
#if ( debug_mode == TRUE )
	time_t		timenow;
	FILE           *file;
	va_list		ap;
	char           *debugname;
#if ( debug_altlog == TRUE )
	char           *debugpath = 0;
#endif
#endif

	if (fmt == NULL)
		return;
#if ( debug_mode == TRUE )
	va_start(ap, fmt);
	timenow = time(NULL);

#if ( debug_altlog == TRUE )
	debugpath = malloc(PATH_MAX);
	getcwd(debugpath, PATH_MAX);
	debugname = malloc(PATH_MAX);
	sprintf(debugname, "%s/%s/.debug", storage, debugpath);
	free(debugpath);
#else
	debugname = malloc(strlen(".debug") + 2);
	sprintf(debugname, ".debug");
#endif

	if ((file = fopen(debugname, "a+"))) {
		fprintf(file, "%.24s - ", ctime(&timenow));
		vfprintf(file, fmt, ap);
		fclose(file);
	}
	chmod(debugname, 0666);
	free(debugname);
#endif
	return;
}

void 
create_missing(char *f, short int l)
{
	char           *fname;

	fname = m_alloc(l + 9);
	memcpy(fname, f, l);
	memcpy(fname + l, "-missing", 9);
	createzerofile(fname);
	m_free(fname);
}

/*
 * Modified: 01.16.2002
 */
char           *
findfileext(char *fileext)
{
	int		n         , k;

	n = direntries;
	while (n--) {
		if ((k = NAMLEN(dirlist[n])) < 4)
			continue;
		if (strcasecmp(dirlist[n]->d_name + k - 4, fileext) == 0)
			return dirlist[n]->d_name;
	}
	return NULL;
}

/*
 * Modified: 2004.10.06 (psxc)
 */
char           *
findfileextparent(char *fileext)
{
	int		n         , k;

	n = direntriesp;
	while (n--) {
		if ((k = NAMLEN(dirlistp[n])) < 4)
			continue;
		if (strcasecmp(dirlistp[n]->d_name + k - 4, fileext) == 0) {
			return dirlistp[n]->d_name;
		}
	}
	return NULL;
}

/*
 * Modified: 11.12.2003 (daxxar)
 */
int 
findfileextcount(char *fileext)
{
	int		n         , fnamelen, c = 0;

	n = direntries;
	while (n--) {
		if ((fnamelen = NAMLEN(dirlist[n])) < 4)
			continue;
		if (strcasecmp((dirlist[n]->d_name + fnamelen - 4), fileext) == 0)
			c++;
	}
	return c;
}

/*
 * Modified: 01.16.2002
 */
unsigned int 
hexstrtodec(unsigned char *s)
{
	unsigned int	n = 0;
	unsigned char	r;

	while (1) {
		if ((unsigned char)*s >= 48 && (unsigned char)*s <= 57) {
			r = 48;
		} else if ((unsigned char)*s >= 65 && (unsigned char)*s <= 70) {
			r = 55;
		} else if ((unsigned char)*s >= 97 && (unsigned char)*s <= 102) {
			r = 87;
		} else if ((unsigned char)*s == 0) {
			return n;
		} else {
			return 0;
		}
		n <<= 4;
		n += *s++ - r;
	}
}

/*
 * dangling links
 */
#if defined(__linux__)
int 
selector(const struct dirent *d)
#elif defined(__NetBSD__)
	int		selector   (const struct dirent *d)
#else
int 
selector(struct dirent *d)
#endif
{
	struct stat	st;
	if ((stat(d->d_name, &st) < 0))
		return 0;
	return 1;
}

/*
 * Modified: Unknown
 */
void 
rescandir()
{
	if (direntries > 0) {
		while (direntries--) {
			free(dirlist[direntries]);
		}
		free(dirlist);
	}
	direntries = scandir(".", &dirlist, selector, 0);
}

void 
temprescandir(int usefree)
{
	if (direntries > 0 && usefree) {
		while (direntries--) {
			free(dirlist[direntries]);
		}
		free(dirlist);
	} else {
		direntries = scandir(".", &dirlist, selector, 0);
	}
}

void 
rescanparent()
{
	if (direntriesp > 0) {
		while (direntriesp--) {
			free(dirlistp[direntriesp]);
		}
		free(dirlistp);
	}
	direntriesp = scandir("..", &dirlistp, 0, 0);
}

void 
temprescanparent(int usefree)
{
	if (direntriesp > 0 && usefree) {
		while (direntriesp--) {
			free(dirlistp[direntriesp]);
		}
		free(dirlistp);
	} else {
		direntriesp = scandir("..", &dirlistp, 0, 0);
	}
}


void 
del_releasedir(char *relname)
{
	int	fnum = direntries;
	if (fnum > 0) {
		while (fnum--) {
			unlink(dirlist[fnum]->d_name);
		}
	rmdir(relname);
	}
}


/*
 * Modified: 01.16.2002
 */
void 
strtolower(char *s)
{
	while ((*s = tolower(*s)))
		s++;
}

void 
unlink_missing(char *s)
{
	char           *t, *u, *v;

	t = u = v = malloc(strlen(s) + 9);
	sprintf(t, "%s-missing", s);
	if (t)
		unlink(t);
	v = findfilename(t);
	if (v)
		unlink(v);
	free(t);
}

/*
 * Modified: 01.16.2002
 */
char 
israr(char *fileext)
{
	if ((*fileext == 'r' || *fileext == 's' || isdigit(*fileext)) &&
	    ((isdigit(*(fileext + 1)) && isdigit(*(fileext + 2))) ||
	     (*(fileext + 1) == 'a' && *(fileext + 2) == 'r')) &&
	    *(fileext + 3) == 0)
		return 1;
	return 0;
}

/*
 * Created    : 02.20.2002 Author     : dark0n3
 * 
 * Description: Checks if file is known mpeg/avi file
 */
char 
isvideo(char *fileext)
{
	switch (*fileext++) {
		case 'm':
		if (!memcmp(fileext, "pg", 3) ||
		    !memcmp(fileext, "peg", 4) ||
		    !memcmp(fileext, "2v", 3) ||
		    !memcmp(fileext, "2p", 3)) {
			return 1;
		}
		break;
	case 'a':
		if (!memcmp(fileext, "vi", 3)) {
			return 1;
		}
		break;
	}

	return 0;
}

/*
 * Modified: 2004-11-17 (psxc) - added support to modify the chars used in the progressbar
 */
void 
buffer_progress_bar(struct VARS *raceI)
{
	int		n;

	raceI->misc.progress_bar[14] = 0;
	if (raceI->total.files > 0) {
		for (n = 0; n < (raceI->total.files - raceI->total.files_missing) * 14 / raceI->total.files; n++)
			raceI->misc.progress_bar[n] = *charbar_filled;
		for (; n < 14; n++)
			raceI->misc.progress_bar[n] = *charbar_missing;
	}
}

/*
 * Modified: 01.16.2002
 */
void 
move_progress_bar(unsigned char delete, struct VARS *raceI)
{
	char           *bar;
	char	       *delbar = 0;
	int		n, m = 0;
	regex_t		preg;
	regmatch_t	pmatch[1];

//	delbar = malloc(PATH_MAX);
	delbar = convert5(del_progressmeter);
	d_log("del_progressmeter: %s\n", delbar);
	regcomp(&preg, delbar, REG_NEWLINE | REG_EXTENDED);
	/* workaround if progressbar was changed while zs-c is running */
	rescandir();

	n = direntries;

	if (delete) {
		while (n--) {
			if (regexec(&preg, dirlist[n]->d_name, 1, pmatch, 0) == 0) {
				if (!(int)pmatch[0].rm_so && (int)pmatch[0].rm_eo == (int)NAMLEN(dirlist[n])) {
					d_log("Found progress bar (%s), removing\n", dirlist[n]->d_name);
					remove(dirlist[n]->d_name);
					*dirlist[n]->d_name = 0;
					m = 1;
				}
			}
		}
		if (m) {
			regfree(&preg);
			return;
		} else {
			d_log("Progress bar could not be deleted, not found!\n");
		}
	} else {
		if (!raceI->total.files)
			return;
		bar = convert(raceI, userI, groupI, progressmeter);
		while (n--) {
			if (regexec(&preg, dirlist[n]->d_name, 1, pmatch, 0) == 0) {
				if (!(int)pmatch[0].rm_so && (int)pmatch[0].rm_eo == (int)NAMLEN(dirlist[n])) {
					if (!m) {
						d_log("Found progress bar (%s), renaming (to %s)\n", dirlist[n]->d_name, bar);
						rename(dirlist[n]->d_name, bar);
						m = 1;
					} else {
						d_log("Found (extra) progress bar (%s), removing\n", dirlist[n]->d_name);
						remove(dirlist[n]->d_name);
						*dirlist[n]->d_name = 0;
						m = 2;
					}
				}
			}
		}
		if (!m) {
			d_log("Progress bar could not be moved, creating a new one now!\n");
			createstatusbar(bar);
		}
	}
	regfree(&preg);
}

/*
 * Modified: Unknown
 */
short int 
findfile(char *filename)
{
	int		n = direntries;

	while (n--) {
#if (sfv_cleanup == TRUE)
#if (sfv_cleanup_lowercase == TRUE)
		if (!strcasecmp(dirlist[n]->d_name, filename)) {
#else
		if (!strcmp(dirlist[n]->d_name, filename)) {
#endif
#else
		if (!strcmp(dirlist[n]->d_name, filename)) {
#endif
			return 1;
		}
	}
	return 0;
}

void
removedotfiles()
{
	int		n = direntries;

	while (n--) {
		if ((!strncasecmp(dirlist[n]->d_name, ".", 1)) && (strlen(dirlist[n]->d_name) > 2)) {
			unlink(dirlist[n]->d_name);
		}
	}
}

char           *
findfilename(char *filename)
{
	int	n;

	n = direntries;
	while (n--) {
		if (!strcasecmp(dirlist[n]->d_name, filename))
			return dirlist[n]->d_name;
	}
	return NULL;
}

/*
 * Modified: 01.16.2002
 */
void 
removecomplete()
{
	char	       *mydelbar = 0;
	int		n;
	regex_t		preg;
	regmatch_t	pmatch[1];

	if (message_file_name)
		unlink(message_file_name);
	mydelbar = convert5(del_completebar);
	d_log("del_completebar: %s\n", mydelbar);
	regcomp(&preg, mydelbar, REG_NEWLINE | REG_EXTENDED);
	n = direntries;
	while (n--) {
		if (regexec(&preg, dirlist[n]->d_name, 1, pmatch, 0) == 0) {
			if ((int)pmatch[0].rm_so == 0 && (int)pmatch[0].rm_eo == (int)NAMLEN(dirlist[n])) {
				remove(dirlist[n]->d_name);
				*dirlist[n]->d_name = 0;
			}
		}
	}
	regfree(&preg);
}

/*
 * Modified: 01.16.2002
 */
short int 
matchpath(char *instr, char *path)
{
	int		pos = 0;

	if ( strlen(instr) < 2 || strlen(path) < 2 )
		return 0;
	do {
		switch (*instr) {
		case 0:
		case ' ':
			if (!strncmp(instr - pos, path, pos - 1)) {
				return 1;
			}
			pos = 0;
			break;
		default:
			pos++;
			break;
		}
	} while (*instr++);
	return 0;
}

/*
 * Modified: 01.16.2002
 */
short int 
strcomp(char *instr, char *searchstr)
{
	int		pos = 0,	k;

	k = strlen(searchstr);

	if ( strlen(instr) == 0 || k == 0 )
		return 0;

	do {
		switch (*instr) {
		case 0:
		case ',':
			if (k == pos && !strncasecmp(instr - pos, searchstr, pos)) {
				return 1;
			}
			pos = 0;
			break;
		default:
			pos++;
			break;
		}
	} while (*instr++);
	return 0;
}

short int 
matchpartialpath(char *instr, char *path)
{
	int	pos = 0;
	char   *partstring = 0;

	if ( strlen(instr) < 2 || strlen(path) < 2 )
		return 0;

	if ((partstring = malloc((strlen(path) + 2) * sizeof(char))) == NULL) {
		d_log("Failed to allocate memory (partstring)\n");
		return 0;
	}
d_log("path: '%s'\n", path);
	sprintf(partstring, "%s/", path);
d_log("partstring: '%s'\n", path);
	do {
		switch (*instr) {
		case 0:
		case ' ':
			if (!strncasecmp(instr - pos, partstring + strlen(partstring) - pos, pos)) {
				free(partstring);
				return 1;
			}
			pos = 0;
			break;
		default:
			pos++;
			break;
		}
	} while (*instr++);
	free(partstring);
	return 0;
}


/* check for matching subpath
   psxc - 2004-12-18
 */
short int 
subcomp(char *directory)
{
	int 	k = strlen(directory);
	int	m = strlen(subdir_list);
	int	pos = 0, l = 0, n = 0, j = 0;
	char	tstring[m + 1];

	if ( k < 2 )
		return 0;

	do {
		switch (subdir_list[l]) {
		case 0:
			break;
		case ',':
			tstring[j] = '\0';
			if (k <= j && !strncasecmp(tstring, directory, j - n)) {
				return 1;
			}
			pos = l;
			n = 0;
			j=0;
			break;
		case '?':
			tstring[j] = subdir_list[l];
			tstring[j+1] = '\0';
			n++;
			j++;
			break;
		default:
			tstring[j] = subdir_list[l];
			tstring[j+1] = '\0';
			pos++;
			j++;
			break;
		}
	m--;
	l++;
	} while (m);
	if (k <= j && !strncasecmp(tstring, directory, j - n)) {
		return 1;
	}
	return 0;
}

/* Checks if file exists */
short int 
fileexists(char *f)
{
	if (close(open(f, O_RDONLY)) == -1)
		return 0;
	return 1;
}

/* Create symbolic link */
void 
createlink(char *factor1, char *factor2, char *source, char *ltarget)
{

#if ( userellink == 1 )
	char		result    [MAXPATHLEN];
#endif
	char           *org = 0, *target = 0;
	int		l1        , l2, l3;
<commit_msg>debug build - please ignore.<commit_after>#include <errno.h>
#include "zsfunctions.h"

#ifdef _WITH_SS5
#include "constants.ss5.h"
#else
#include "constants.h"
#endif

#include "convert.h"

struct GROUP {
	char           *name;
	gid_t		id;
};

struct USER {
	char           *name;
	uid_t		id;
};

struct dirent **dirlist;
struct dirent **dirlistp;
unsigned int	direntries = 0;
unsigned int	direntriesp = 0;
int		num_groups = 0, num_users = 0;
struct USER   **user;
struct GROUP  **group;

void 
d_log(char *fmt,...)
{
#if ( debug_mode == TRUE )
	time_t		timenow;
	FILE           *file;
	va_list		ap;
	char           *debugname;
#if ( debug_altlog == TRUE )
	char           *debugpath = 0;
#endif
#endif

	if (fmt == NULL)
		return;
#if ( debug_mode == TRUE )
	va_start(ap, fmt);
	timenow = time(NULL);

#if ( debug_altlog == TRUE )
	debugpath = malloc(PATH_MAX);
	getcwd(debugpath, PATH_MAX);
	debugname = malloc(PATH_MAX);
	sprintf(debugname, "%s/%s/.debug", storage, debugpath);
	free(debugpath);
#else
	debugname = malloc(strlen(".debug") + 2);
	sprintf(debugname, ".debug");
#endif

	if ((file = fopen(debugname, "a+"))) {
		fprintf(file, "%.24s - ", ctime(&timenow));
		vfprintf(file, fmt, ap);
		fclose(file);
	}
	chmod(debugname, 0666);
	free(debugname);
#endif
	return;
}

void 
create_missing(char *f, short int l)
{
	char           *fname;

	fname = m_alloc(l + 9);
	memcpy(fname, f, l);
	memcpy(fname + l, "-missing", 9);
	createzerofile(fname);
	m_free(fname);
}

/*
 * Modified: 01.16.2002
 */
char           *
findfileext(char *fileext)
{
	int		n         , k;

	n = direntries;
	while (n--) {
		if ((k = NAMLEN(dirlist[n])) < 4)
			continue;
		if (strcasecmp(dirlist[n]->d_name + k - 4, fileext) == 0)
			return dirlist[n]->d_name;
	}
	return NULL;
}

/*
 * Modified: 2004.10.06 (psxc)
 */
char           *
findfileextparent(char *fileext)
{
	int		n         , k;

	n = direntriesp;
	while (n--) {
		if ((k = NAMLEN(dirlistp[n])) < 4)
			continue;
		if (strcasecmp(dirlistp[n]->d_name + k - 4, fileext) == 0) {
			return dirlistp[n]->d_name;
		}
	}
	return NULL;
}

/*
 * Modified: 11.12.2003 (daxxar)
 */
int 
findfileextcount(char *fileext)
{
	int		n         , fnamelen, c = 0;

	n = direntries;
	while (n--) {
		if ((fnamelen = NAMLEN(dirlist[n])) < 4)
			continue;
		if (strcasecmp((dirlist[n]->d_name + fnamelen - 4), fileext) == 0)
			c++;
	}
	return c;
}

/*
 * Modified: 01.16.2002
 */
unsigned int 
hexstrtodec(unsigned char *s)
{
	unsigned int	n = 0;
	unsigned char	r;

	while (1) {
		if ((unsigned char)*s >= 48 && (unsigned char)*s <= 57) {
			r = 48;
		} else if ((unsigned char)*s >= 65 && (unsigned char)*s <= 70) {
			r = 55;
		} else if ((unsigned char)*s >= 97 && (unsigned char)*s <= 102) {
			r = 87;
		} else if ((unsigned char)*s == 0) {
			return n;
		} else {
			return 0;
		}
		n <<= 4;
		n += *s++ - r;
	}
}

/*
 * dangling links
 */
#if defined(__linux__)
int 
selector(const struct dirent *d)
#elif defined(__NetBSD__)
	int		selector   (const struct dirent *d)
#else
int 
selector(struct dirent *d)
#endif
{
	struct stat	st;
	if ((stat(d->d_name, &st) < 0))
		return 0;
	return 1;
}

/*
 * Modified: Unknown
 */
void 
rescandir()
{
	if (direntries > 0) {
		while (direntries--) {
			free(dirlist[direntries]);
		}
		free(dirlist);
	}
	direntries = scandir(".", &dirlist, selector, 0);
}

void 
temprescandir(int usefree)
{
	if (direntries > 0 && usefree) {
		while (direntries--) {
			free(dirlist[direntries]);
		}
		free(dirlist);
	} else {
		direntries = scandir(".", &dirlist, selector, 0);
	}
}

void 
rescanparent()
{
	if (direntriesp > 0) {
		while (direntriesp--) {
			free(dirlistp[direntriesp]);
		}
		free(dirlistp);
	}
	direntriesp = scandir("..", &dirlistp, 0, 0);
}

void 
temprescanparent(int usefree)
{
	if (direntriesp > 0 && usefree) {
		while (direntriesp--) {
			free(dirlistp[direntriesp]);
		}
		free(dirlistp);
	} else {
		direntriesp = scandir("..", &dirlistp, 0, 0);
	}
}


void 
del_releasedir(char *relname)
{
	int	fnum = direntries;
	if (fnum > 0) {
		while (fnum--) {
			unlink(dirlist[fnum]->d_name);
		}
	rmdir(relname);
	}
}


/*
 * Modified: 01.16.2002
 */
void 
strtolower(char *s)
{
	while ((*s = tolower(*s)))
		s++;
}

void 
unlink_missing(char *s)
{
	char           *t, *u, *v;

	t = u = v = malloc(strlen(s) + 9);
	sprintf(t, "%s-missing", s);
	if (t)
		unlink(t);
	v = findfilename(t);
	if (v)
		unlink(v);
	free(t);
}

/*
 * Modified: 01.16.2002
 */
char 
israr(char *fileext)
{
	if ((*fileext == 'r' || *fileext == 's' || isdigit(*fileext)) &&
	    ((isdigit(*(fileext + 1)) && isdigit(*(fileext + 2))) ||
	     (*(fileext + 1) == 'a' && *(fileext + 2) == 'r')) &&
	    *(fileext + 3) == 0)
		return 1;
	return 0;
}

/*
 * Created    : 02.20.2002 Author     : dark0n3
 * 
 * Description: Checks if file is known mpeg/avi file
 */
char 
isvideo(char *fileext)
{
	switch (*fileext++) {
		case 'm':
		if (!memcmp(fileext, "pg", 3) ||
		    !memcmp(fileext, "peg", 4) ||
		    !memcmp(fileext, "2v", 3) ||
		    !memcmp(fileext, "2p", 3)) {
			return 1;
		}
		break;
	case 'a':
		if (!memcmp(fileext, "vi", 3)) {
			return 1;
		}
		break;
	}

	return 0;
}

/*
 * Modified: 2004-11-17 (psxc) - added support to modify the chars used in the progressbar
 */
void 
buffer_progress_bar(struct VARS *raceI)
{
	int		n;

	raceI->misc.progress_bar[14] = 0;
	if (raceI->total.files > 0) {
		for (n = 0; n < (raceI->total.files - raceI->total.files_missing) * 14 / raceI->total.files; n++)
			raceI->misc.progress_bar[n] = *charbar_filled;
		for (; n < 14; n++)
			raceI->misc.progress_bar[n] = *charbar_missing;
	}
}

/*
 * Modified: 01.16.2002
 */
void 
move_progress_bar(unsigned char delete, struct VARS *raceI)
{
	char           *bar;
	char	       *delbar = 0;
	int		n, m = 0;
	regex_t		preg;
	regmatch_t	pmatch[1];

//	delbar = malloc(PATH_MAX);
	delbar = convert5(del_progressmeter);
	d_log("del_progressmeter: %s\n", delbar);
	regcomp(&preg, delbar, REG_NEWLINE | REG_EXTENDED);
	/* workaround if progressbar was changed while zs-c is running */
	rescandir();

	n = direntries;

	if (delete) {
		while (n--) {
			if (regexec(&preg, dirlist[n]->d_name, 1, pmatch, 0) == 0) {
				if (!(int)pmatch[0].rm_so && (int)pmatch[0].rm_eo == (int)NAMLEN(dirlist[n])) {
					d_log("Found progress bar (%s), removing\n", dirlist[n]->d_name);
					remove(dirlist[n]->d_name);
					*dirlist[n]->d_name = 0;
					m = 1;
				}
			}
		}
		if (m) {
			regfree(&preg);
			return;
		} else {
			d_log("Progress bar could not be deleted, not found!\n");
		}
	} else {
		if (!raceI->total.files)
			return;
		bar = convert(raceI, userI, groupI, progressmeter);
		while (n--) {
			if (regexec(&preg, dirlist[n]->d_name, 1, pmatch, 0) == 0) {
				if (!(int)pmatch[0].rm_so && (int)pmatch[0].rm_eo == (int)NAMLEN(dirlist[n])) {
					if (!m) {
						d_log("Found progress bar (%s), renaming (to %s)\n", dirlist[n]->d_name, bar);
						rename(dirlist[n]->d_name, bar);
						m = 1;
					} else {
						d_log("Found (extra) progress bar (%s), removing\n", dirlist[n]->d_name);
						remove(dirlist[n]->d_name);
						*dirlist[n]->d_name = 0;
						m = 2;
					}
				}
			}
		}
		if (!m) {
			d_log("Progress bar could not be moved, creating a new one now!\n");
			createstatusbar(bar);
		}
	}
	regfree(&preg);
}

/*
 * Modified: Unknown
 */
short int 
findfile(char *filename)
{
	int		n = direntries;

	while (n--) {
#if (sfv_cleanup == TRUE)
#if (sfv_cleanup_lowercase == TRUE)
		if (!strcasecmp(dirlist[n]->d_name, filename)) {
#else
		if (!strcmp(dirlist[n]->d_name, filename)) {
#endif
#else
		if (!strcmp(dirlist[n]->d_name, filename)) {
#endif
			return 1;
		}
	}
	return 0;
}

void
removedotfiles()
{
	int		n = direntries;

	while (n--) {
		if ((!strncasecmp(dirlist[n]->d_name, ".", 1)) && (strlen(dirlist[n]->d_name) > 2)) {
			unlink(dirlist[n]->d_name);
		}
	}
}

char           *
findfilename(char *filename)
{
	int	n;

	n = direntries;
	while (n--) {
		if (!strcasecmp(dirlist[n]->d_name, filename))
			return dirlist[n]->d_name;
	}
	return NULL;
}

/*
 * Modified: 01.16.2002
 */
void 
removecomplete()
{
	char	       *mydelbar = 0;
	int		n;
	regex_t		preg;
	regmatch_t	pmatch[1];

	if (message_file_name)
		unlink(message_file_name);
	mydelbar = convert5(del_completebar);
	d_log("del_completebar: %s\n", mydelbar);
	regcomp(&preg, mydelbar, REG_NEWLINE | REG_EXTENDED);
	n = direntries;
	while (n--) {
		if (regexec(&preg, dirlist[n]->d_name, 1, pmatch, 0) == 0) {
			if ((int)pmatch[0].rm_so == 0 && (int)pmatch[0].rm_eo == (int)NAMLEN(dirlist[n])) {
				remove(dirlist[n]->d_name);
				*dirlist[n]->d_name = 0;
			}
		}
	}
	regfree(&preg);
}

/*
 * Modified: 01.16.2002
 */
short int 
matchpath(char *instr, char *path)
{
	int		pos = 0;

	if ( strlen(instr) < 2 || strlen(path) < 2 )
		return 0;
	do {
		switch (*instr) {
		case 0:
		case ' ':
			if (!strncmp(instr - pos, path, pos - 1)) {
				return 1;
			}
			pos = 0;
			break;
		default:
			pos++;
			break;
		}
	} while (*instr++);
	return 0;
}

/*
 * Modified: 01.16.2002
 */
short int 
strcomp(char *instr, char *searchstr)
{
	int		pos = 0,	k;

	k = strlen(searchstr);

	if ( strlen(instr) == 0 || k == 0 )
		return 0;

	do {
		switch (*instr) {
		case 0:
		case ',':
			if (k == pos && !strncasecmp(instr - pos, searchstr, pos)) {
				return 1;
			}
			pos = 0;
			break;
		default:
			pos++;
			break;
		}
	} while (*instr++);
	return 0;
}

short int 
matchpartialpath(char *instr, char *path)
{
	int	pos = 0;
	char   *partstring = 0;

	if ( strlen(instr) < 2 || strlen(path) < 2 )
		return 0;

	if ((partstring = malloc((strlen(path) + 2) * sizeof(char))) == NULL) {
		d_log("Failed to allocate memory (partstring)\n");
		return 0;
	}
d_log("path: '%s'\n", path);
	sprintf(partstring, "%s/", path);
d_log("partstring: '%s'\n", path);
	do {
		switch (*instr) {
		case 0:
		case ' ':
			if (!strncasecmp(instr - pos, partstring + strlen(partstring) - pos, pos)) {
				free(partstring);
				return 1;
			}
			pos = 0;
			break;
		default:
			pos++;
			break;
		}
	} while (*instr++);
	free(partstring);
	return 0;
}


/* check for matching subpath
   psxc - 2004-12-18
 */
short int 
subcomp(char *directory)
{
	int 	k = strlen(directory);
	int	m = strlen(subdir_list);
	int	pos = 0, l = 0, n = 0, j = 0;
	char	tstring[m + 1];

	if ( k < 2 )
		return 0;

	do {
		switch (subdir_list[l]) {
		case 0:
			break;
		case ',':
			tstring[j] = '\0';
			if (k <= j && !strncasecmp(tstring, directory, j - n)) {
				return 1;
			}
			pos = l;
			n = 0;
			j=0;
			break;
		case '?':
			tstring[j] = subdir_list[l];
			tstring[j+1] = '\0';
			n++;
			j++;
			break;
		default:
			tstring[j] = subdir_list[l];
			tstring[j+1] = '\0';
			pos++;
			j++;
			break;
		}
	m--;
	l++;
	} while (m);
	if (k <= j && !strncasecmp(tstring, directory, j - n)) {
		return 1;
	}
	return 0;
}

/* Checks if file exists */
short int 
fileexists(char *f)
{
	int	temp_p = 0;

	temp_p = open(f, O_RDONLY);
	if (temp_p == -1)
		return 0;
	close(temp_p);
	return 1;

/*	if (close(open(f, O_RDONLY)) == -1)
		return 0;
	return 1;
*/
}

/* Create symbolic link */
void 
createlink(char *factor1, char *factor2, char *source, char *ltarget)
{

#if ( userellink == 1 )
	char		result    [MAXPATHLEN];
#endif
	char           *org = 0, *target = 0;
	int		l1        , l2, l3;

{'fraction_non_alphanumeric': 0.18787959985709182, 'fraction_numerical': 0.025902107895677026, 'mean_word_length': 2.9403484075312334, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '214170', 'n_tokens_mistral': 10818, 'n_tokens_neox': 9664, 'n_words': 2195}