// File: /workspace/github_c_makefile_star2_O1/sheheryarbutt[P]Ext2-File-System/ext2_ls.c
// Function: main
int main(int argc, char **argv) {
	if(argc != 3) { 
        fprintf(stderr, "Usage: ext2_ls <image file name> <Absolute path>\n");
         exit(1);
    }
    int fd = open(argv[1], O_RDWR);

	char *path = malloc(sizeof(char)*((unsigned int)strlen(argv[2]) + 1));
	strcpy(path, argv[2]);
	path[(unsigned int)strlen(argv[2])] = '\0';

    disk = mmap(NULL, 128 * EXT2_BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if(disk == MAP_FAILED) {
		perror("mmap");
		exit(1);
 	}

	inode_table = (struct ext2_inode *)(disk + (5*EXT2_BLOCK_SIZE));

	unsigned int * parent_block= malloc(sizeof(unsigned int *));
	int cur_inode;
	if((unsigned int)strlen(path)==1 && strcmp(path,"/")==0){
		cur_inode=2;
	}
	else{
		remove_trailing_slash(path);
	 	cur_inode=pathTraversal(path,disk,parent_block);
	}
	
	struct ext2_inode inode = inode_table[cur_inode-1];// represents the current inode
	if(valid(cur_inode) && (inode.i_mode & EXT2_S_IFDIR) == EXT2_S_IFDIR){//inode of the directory
		//S_ISDIR(inode.i_mode)
		int size=0;

		int current_i_block_index=0;
		int current_i_block=inode.i_block[current_i_block_index];
		// READ THROUGH ALL THE BLOCKS
		while (current_i_block){
			struct ext2_dir_entry *direntry = (struct ext2_dir_entry *)(disk + current_i_block*EXT2_BLOCK_SIZE +size); 
			//read through each individual block
			while(size<EXT2_BLOCK_SIZE){
				if(direntry->inode){
				char name [direntry->name_len+1]; strncpy(name,direntry->name,direntry->name_len);
				name[direntry->name_len]='\0';	
				printf("%s\n ",name);
				 //add to size so we can increment the pointer to point to next entry!
				size+=direntry->rec_len;
				direntry = (struct ext2_dir_entry *)(disk + current_i_block*EXT2_BLOCK_SIZE +size);
				}else{ 
					size+=direntry->rec_len;
				}
			}// end of inner while
			size=0;
			current_i_block_index++;
			current_i_block=inode.i_block[current_i_block_index];
		}//end of outter while
	}
	else{
		fprintf(stderr, "ERROR: %s\n", strerror(ENOENT)); 
	  	exit(ENOENT);
	}
	free(path); free(parent_block);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
cmpl $0x3, %edi
jne 0x14cf
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x10f0
movl %eax, %r14d
movq 0x10(%rbx), %r15
movq %r15, %rdi
callq 0x1060
leal 0x1(%rax), %edi
callq 0x10e0
movq %rax, %rbx
movq %rax, %rdi
movq %r15, %rsi
callq 0x1050
movq %r15, %rdi
callq 0x1060
movl %eax, %eax
movb $0x0, (%rbx,%rax)
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %r14d, %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x2d76(%rip)      # 0x40b8
cmpq $-0x1, %rax
je 0x14d4
addq $0x1400, %rax           # imm = 0x1400
movq %rax, 0x2d67(%rip)      # 0x40c0
movl $0x8, %edi
callq 0x10e0
movq %rax, %r14
movq %rbx, %rdi
callq 0x1060
cmpl $0x1, %eax
jne 0x138a
leaq 0xcc1(%rip), %rsi       # 0x203b
movq %rbx, %rdi
callq 0x1090
testl %eax, %eax
je 0x14c0
movq %rbx, %rdi
callq 0x14dc
movq 0x2d1f(%rip), %rsi      # 0x40b8
movq %rbx, %rdi
movq %r14, %rdx
callq 0x163c
movq %r14, -0x30(%rbp)
movq %rbx, -0x38(%rbp)
movq 0x2d0d(%rip), %rcx      # 0x40c0
cltq
movq %rax, %rdx
shlq $0x7, %rdx
leaq (%rcx,%rdx), %rsi
addq $-0x80, %rsi
leaq -0xc0(%rbp), %rdi
movl $0x20, %ecx
rep  movsl (%rsi), %es:(%rdi)
movl %eax, %edi
callq 0x1551
testl %eax, %eax
je 0x14ca
testb $0x40, -0xbf(%rbp)
je 0x14ca
movl -0x98(%rbp), %eax
testl %eax, %eax
je 0x149d
xorl %ecx, %ecx
movq %rcx, -0x40(%rbp)
shll $0xa, %eax
movslq %eax, %r14
movq 0x2ca9(%rip), %r13      # 0x40b8
addq %r14, %r13
xorl %ebx, %ebx
cmpl $0x0, (%r13)
je 0x1478
movq %rsp, %r15
movzbl 0x6(%r13), %eax
movq %rsp, %r12
addl $0x10, %eax
andl $-0x10, %eax
subq %rax, %r12
movq %r12, %rsp
leaq 0x8(%r13), %rsi
movzbl 0x6(%r13), %edx
movq %r12, %rdi
callq 0x1040
movzbl 0x6(%r13), %eax
movb $0x0, (%r12,%rax)
leaq 0xbe9(%rip), %rdi       # 0x203d
movq %r12, %rsi
xorl %eax, %eax
callq 0x1080
movzwl 0x4(%r13), %eax
addl %eax, %ebx
movq 0x2c4c(%rip), %rax      # 0x40b8
addq %r14, %rax
leaq (%rbx,%rax), %r13
movq %r15, %rsp
jmp 0x147f
movzwl 0x4(%r13), %eax
addl %eax, %ebx
cmpl $0x400, %ebx            # imm = 0x400
jl 0x1414
movq -0x40(%rbp), %rcx
movl -0x94(%rbp,%rcx,4), %eax
incq %rcx
testl %eax, %eax
jne 0x13fe
movq -0x38(%rbp), %rdi
callq 0x1030
movq -0x30(%rbp), %rdi
callq 0x1030
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x2, %eax
jmp 0x13a4
callq 0x1178
callq 0x1150
callq 0x11b1
nopl (%rax)