// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/cache1d.c
// Function: klistpath
CACHE1D_FIND_REC *klistpath(const char *_path, const char *mask, int type)
{
	CACHE1D_FIND_REC *rec = NULL;
	char *path;
	
	// pathsearchmode == 0: enumerates a path in the virtual filesystem
	// pathsearchmode == 1: enumerates the system filesystem path passed in
	
	path = strdup(_path);
	if (!path) return NULL;

	// we don't need any leading dots and slashes or trailing slashes either
	{
		int i,j;
		for (i=0; path[i] == '.' || toupperlookup[(int)(unsigned char)path[i]] == '/'; ) i++;
		for (j=0; (path[j] = path[i]); j++,i++) ;
		while (j>0 && toupperlookup[(int)(unsigned char)path[j-1]] == '/') j--;
		path[j] = 0;
		//buildprintf("Cleaned up path = \"%s\"\n",path);
	}
	
	if (*path && (type & CACHE1D_FIND_DIR)) {
		if (klistaddentry(&rec, "..", CACHE1D_FIND_DIR, CACHE1D_SOURCE_CURDIR) < 0) goto failure;
	}
	
	if (!(type & CACHE1D_OPT_NOSTACK)) {	// current directory and paths in the search stack
		searchpath_t *search = NULL;
		BDIR *dir;
		struct Bdirent *dirent;
		const char *d = ".";
		int stackdepth = CACHE1D_SOURCE_CURDIR;
		char buf[BMAX_PATH];

		if (pathsearchmode) d = _path;

		do {
			if (!pathsearchmode) {
				strcpy(buf, path);
				if (*path) strcat(buf, "/");
				strcat(buf, d);
			} else strcpy(buf, d);
			dir = Bopendir(buf);
			if (dir) {
				while ((dirent = Breaddir(dir))) {
					if ((dirent->name[0] == '.' && dirent->name[1] == 0) ||
						(dirent->name[0] == '.' && dirent->name[1] == '.' && dirent->name[2] == 0))
						continue;
					if ((type & CACHE1D_FIND_DIR) && !(dirent->mode & BS_IFDIR)) continue;
					if ((type & CACHE1D_FIND_FILE) && (dirent->mode & BS_IFDIR)) continue;
					if (!Bwildmatch(dirent->name, mask)) continue;
					switch (klistaddentry(&rec, dirent->name,
								(dirent->mode & BS_IFDIR) ? CACHE1D_FIND_DIR : CACHE1D_FIND_FILE,
										  stackdepth)) {
						case -1: goto failure;
						//case 1: buildprintf("%s:%s dropped for lower priority\n", d,dirent->name); break;
						//case 0: buildprintf("%s:%s accepted\n", d,dirent->name); break;
						default: break;
					}
				}
				Bclosedir(dir);
			}
			
			if (pathsearchmode) break;

			if (!search) {
				search = searchpathhead;
				stackdepth = CACHE1D_SOURCE_PATH;
			} else {
				search = search->next;
				stackdepth++;
			}
			if (search) d = search->path;
		} while (search);
	}

#ifdef WITHKPLIB
	if (!pathsearchmode) {	// next, zip files
		char buf[BMAX_PATH];
		int i, j, ftype;
		strcpy(buf,path);
		if (*path) strcat(buf,"/");
		strcat(buf,mask);
		for (kzfindfilestart(buf); kzfindfile(buf); ) {
			if (buf[0] != '|') continue;	// local files we don't need
			
			// scan for the end of the string and shift
			// everything left a char in the process
			for (i=1; (buf[i-1]=buf[i]); i++) ;
			i-=2;

			// if there's a slash at the end, this is a directory entry
			if (toupperlookup[(int)(unsigned char)buf[i]] == '/') { ftype = CACHE1D_FIND_DIR; buf[i] = 0; }
			else ftype = CACHE1D_FIND_FILE;

			// skip over the common characters at the beginning of the base path and the zip entry
			for (j=0; buf[j] && path[j]; j++) {
				if (toupperlookup[(int)(unsigned char)path[j] ] == toupperlookup[(int)(unsigned char)buf[j] ]) continue;
				break;
			}
			// we've now hopefully skipped the common path component at the beginning.
			// if that's true, we should be staring at a null byte in path and either any character in buf
			// if j==0, or a slash if j>0
			if ((!path[0] && buf[j]) || (!path[j] && toupperlookup[(int)(unsigned char)buf[j] ] == '/')) {
				if (j>0) j++;
				
				// yep, so now we shift what follows back to the start of buf and while we do that,
				// keep an eye out for any more slashes which would mean this entry has sub-entities
				// and is useless to us.
				for (i = 0; (buf[i] = buf[j]) && toupperlookup[(int)(unsigned char)buf[j]] != '/'; i++,j++) ;
				if (toupperlookup[(int)(unsigned char)buf[j]] == '/') continue;	// damn, try next entry
			} else {
				// if we're here it means we have a situation where:
				//   path = foo
				//   buf = foobar...
				// or
				//   path = foobar
				//   buf = foo...
				// which would mean the entry is higher up in the directory tree and is also useless
				continue;
			}

			if ((type & CACHE1D_FIND_DIR) && ftype != CACHE1D_FIND_DIR) continue;
			if ((type & CACHE1D_FIND_FILE) && ftype != CACHE1D_FIND_FILE) continue;
			
			// the entry is in the clear
			switch (klistaddentry(&rec, buf, ftype, CACHE1D_SOURCE_ZIP)) {
				case -1: goto failure;
				//case 1: buildprintf("<ZIP>:%s dropped for lower priority\n", buf); break;
				//case 0: buildprintf("<ZIP>:%s accepted\n", buf); break;
				default: break;
			}
		}
	}
#endif
	
	// then, grp files
	if (!pathsearchmode && !*path && (type & CACHE1D_FIND_FILE)) {
		char buf[13];
		int i,j;
		buf[12] = 0;
		for (i=0;i<MAXGROUPFILES;i++) {
			if (groupfil[i] == -1) continue;
			for(j=gnumfiles[i]-1;j>=0;j--)
			{
				Bmemcpy(buf,&gfilelist[i][j<<4],12);
				if (!Bwildmatch(buf,mask)) continue;
				switch (klistaddentry(&rec, buf, CACHE1D_FIND_FILE, CACHE1D_SOURCE_GRP)) {
					case -1: goto failure;
					//case 1: buildprintf("<GRP>:%s dropped for lower priority\n", workspace); break;
					//case 0: buildprintf("<GRP>:%s accepted\n", workspace); break;
					default: break;
				}
			}
		}
	}
	
	if (pathsearchmode && (type & CACHE1D_FIND_DRIVE)) {
		char *drives, *drp;
		drives = Bgetsystemdrives();
		if (drives) {
			for (drp=drives; *drp; drp+=strlen(drp)+1) {
				if (klistaddentry(&rec, drp, CACHE1D_FIND_DRIVE, CACHE1D_SOURCE_DRIVE) < 0) {
					free(drives);
					goto failure;
				}
			}
			free(drives);
		}
	}
	
	free(path);
	return rec;
failure:
	free(path);
	klistfree(rec);
	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
movl %edx, %ebp
movq %rsi, 0x18(%rsp)
movq %rdi, %r12
andq $0x0, 0x10(%rsp)
callq 0x6060
testq %rax, %rax
je 0x1e6e7
movq %rax, %rbx
movq %rax, %rcx
movb (%rcx), %dl
movl %edx, %eax
andb $-0x2, %al
incq %rcx
cmpb $0x2e, %al
je 0x1e370
xorl %eax, %eax
movb %dl, (%rbx,%rax)
testb %dl, %dl
je 0x1e399
movb (%rcx,%rax), %dl
incq %rax
jmp 0x1e37f
movl %eax, %ecx
cmpb $0x2f, -0x1(%rbx,%rcx)
jne 0x1e39f
decl %eax
testl %eax, %eax
jg 0x1e38e
xorl %eax, %eax
movl %eax, %eax
movb $0x0, (%rbx,%rax)
movb (%rbx), %al
movb %al, 0xf(%rsp)
testb %al, %al
sete %al
testb $0x2, %bpl
sete %cl
orb %al, %cl
jne 0x1e3da
leaq 0x49fbc(%rip), %rsi     # 0x6837e
leaq 0x10(%rsp), %rdi
pushq $0x2
popq %rdx
pushq $0x1
popq %rcx
callq 0x1e7cb
testl %eax, %eax
js 0x1e6d5
btl $0x8, %ebp
movl 0x18f31c(%rip), %eax    # 0x1ad700
jb 0x1e51c
testl %eax, %eax
sete %al
leaq 0x49f89(%rip), %rcx     # 0x6837f
cmoveq %rcx, %r12
pushq $0x1
popq %r15
xorl %r14d, %r14d
leaq 0x20(%rsp), %r13
movq %r13, %rdi
testb $0x1, %al
je 0x1e438
movq %rbx, %rsi
callq 0x6430
cmpb $0x0, 0xf(%rsp)
je 0x1e42b
movq %r13, %rdi
callq 0x6420
movw $0x2f, 0x20(%rsp,%rax)
movq %r13, %rdi
movq %r12, %rsi
callq 0x6710
jmp 0x1e440
movq %r12, %rsi
callq 0x6430
movq %r13, %rdi
callq 0x1f931
testq %rax, %rax
je 0x1e4de
movq %rax, %r12
movq %r12, %rdi
callq 0x1f976
testq %rax, %rax
je 0x1e4d1
movq %rax, %r13
movq 0x8(%rax), %rdi
cmpb $0x2e, (%rdi)
jne 0x1e480
movzbl 0x1(%rdi), %eax
testl %eax, %eax
je 0x1e454
cmpl $0x2e, %eax
jne 0x1e480
cmpb $0x0, 0x2(%rdi)
je 0x1e454
testb $0x2, %bpl
je 0x1e48d
testb $0x40, 0x11(%r13)
je 0x1e454
testb $0x1, %bpl
je 0x1e49a
testb $0x40, 0x11(%r13)
jne 0x1e454
movq 0x18(%rsp), %rsi
callq 0x1fa20
testl %eax, %eax
je 0x1e454
btl $0xe, 0x10(%r13)
movq 0x8(%r13), %rsi
movl $0x0, %edx
adcl $0x1, %edx
leaq 0x10(%rsp), %rdi
movl %r15d, %ecx
callq 0x1e7cb
cmpl $-0x1, %eax
jne 0x1e454
jmp 0x1e6d5
movq %r12, %rdi
callq 0x1fa08
leaq 0x20(%rsp), %r13
movl 0x18f21c(%rip), %eax    # 0x1ad700
testl %eax, %eax
jne 0x1e51c
testq %r14, %r14
movq %r14, %rcx
leaq 0x18f213(%rip), %rdx    # 0x1ad708
cmoveq %rdx, %rcx
movq (%rcx), %rcx
testq %rcx, %rcx
je 0x1e51c
incl %r15d
testq %r14, %r14
pushq $0x2
popq %rax
cmovel %eax, %r15d
movq 0x8(%rcx), %r12
movb $0x1, %al
movq %rcx, %r14
jmp 0x1e406
testl %eax, %eax
jne 0x1e671
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x6430
cmpb $0x0, 0xf(%rsp)
je 0x1e54c
leaq 0x20(%rsp), %rdi
callq 0x6420
movw $0x2f, 0x20(%rsp,%rax)
movq %r15, %rdi
movq 0x18(%rsp), %rsi
callq 0x6710
movq %r15, %rdi
callq 0x408e8
movabsq $0x100000000, %r13      # imm = 0x100000000
pushq $0x1
popq %r14
leaq 0x4ccba(%rip), %r12     # 0x6b230
movq %r15, %rdi
callq 0x40920
testl %eax, %eax
je 0x1e668
cmpb $0x7c, 0x20(%rsp)
jne 0x1e576
leaq 0x21(%rsp), %rax
movabsq $-0x200000000, %rcx     # imm = 0xFFFFFFFE00000000
addq %r13, %rcx
movb (%rax), %dl
movb %dl, -0x1(%rax)
incq %rax
testb %dl, %dl
jne 0x1e59c
sarq $0x20, %rcx
movb 0x20(%rsp,%rcx), %al
pushq $0x1
popq %rdx
cmpb $0x2f, %al
jne 0x1e5c2
movb $0x0, 0x20(%rsp,%rcx)
pushq $0x2
popq %rdx
movq %r14, %rcx
movzbl 0x1f(%rsp,%rcx), %esi
testq %rsi, %rsi
je 0x1e576
movzbl -0x1(%rbx,%rcx), %edi
testq %rdi, %rdi
je 0x1e5e8
movb (%rdi,%r12), %r8b
cmpb (%rsi,%r12), %r8b
jne 0x1e5e8
incq %rcx
jmp 0x1e5c5
cmpb $0x0, 0xf(%rsp)
je 0x1e5fd
cmpl $0x2f, %esi
jne 0x1e576
testb %dil, %dil
jne 0x1e576
cmpq $0x1, %rcx
movl %ecx, %esi
movl $0x0, %ecx
cmoveq %rcx, %rsi
movq %r15, %rdi
movzbl (%rdi,%rsi), %ecx
movb %cl, (%rdi)
testl %ecx, %ecx
je 0x1e623
cmpl $0x2f, %ecx
je 0x1e623
incq %rdi
jmp 0x1e60f
cmpb $0x2f, %al
sete %sil
setne %al
testb %sil, %bpl
jne 0x1e576
movl %ebp, %esi
shrb %sil
andb %al, %sil
jne 0x1e576
testb %cl, %cl
jne 0x1e576
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
movl $0x7ffffffe, %ecx       # imm = 0x7FFFFFFE
callq 0x1e7cb
cmpl $-0x1, %eax
jne 0x1e576
jmp 0x1e6d5
cmpl $0x0, 0x18f091(%rip)    # 0x1ad700
je 0x1e6eb
xorl %eax, %eax
testb $0x4, %bpl
je 0x1e6fe
testb %al, %al
jne 0x1e6fe
callq 0x1f907
testq %rax, %rax
je 0x1e6fe
movq %rax, %r14
leaq 0x10(%rsp), %r15
pushq $0x4
popq %rbp
movq %rax, %r12
cmpb $0x0, (%r12)
je 0x1e6c3
movq %r15, %rdi
movq %r12, %rsi
movl %ebp, %edx
xorl %ecx, %ecx
callq 0x1e7cb
testl %eax, %eax
js 0x1e6cd
movq %r12, %rdi
callq 0x6420
addq %rax, %r12
incq %r12
jmp 0x1e699
movq %r14, %rdi
callq 0x61c0
jmp 0x1e6fe
movq %r14, %rdi
callq 0x61c0
movq %rbx, %rdi
callq 0x61c0
movq 0x10(%rsp), %rdi
callq 0x1e325
xorl %eax, %eax
jmp 0x1e70b
testb $0x1, %bpl
sete %al
cmpb $0x0, 0xf(%rsp)
setne %cl
orb %al, %cl
je 0x1e71d
movq %rbx, %rdi
callq 0x61c0
movq 0x10(%rsp), %rax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20(%rsp), %r15
movb $0x0, 0xc(%r15)
xorl %r14d, %r14d
cmpq $0x4, %r14
je 0x1e7bc
leaq 0x7e905(%rip), %rax     # 0x9d040
cmpl $-0x1, (%rax,%r14,4)
je 0x1e7b4
leaq 0x18efe7(%rip), %rax    # 0x1ad730
movslq (%rax,%r14,4), %r13
movl %r13d, %r12d
shll $0x4, %r12d
addl $-0x10, %r12d
testq %r13, %r13
jle 0x1e7b4
leaq 0x18efdc(%rip), %rax    # 0x1ad740
movq (%rax,%r14,8), %rax
movslq %r12d, %rcx
movq (%rax,%rcx), %rdx
movq %rdx, 0x20(%rsp)
movl 0x8(%rax,%rcx), %eax
movl %eax, 0x28(%rsp)
movq %r15, %rdi
movq 0x18(%rsp), %rsi
callq 0x1fa20
testl %eax, %eax
je 0x1e7ab
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
pushq $0x1
popq %rdx
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
callq 0x1e7cb
cmpl $-0x1, %eax
je 0x1e6d5
addl $-0x10, %r12d
decq %r13
jmp 0x1e758
incq %r14
jmp 0x1e72a
cmpl $0x0, 0x18ef3d(%rip)    # 0x1ad700
sete %al
jmp 0x1e673