// File: /workspace/github_c_makefile_star2_O2/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 0x145b
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
pushq $0x2
popq %r15
movl %r15d, %esi
xorl %eax, %eax
callq 0x10f0
movl %eax, %r14d
movq 0x10(%rbx), %r12
movq %r12, %rdi
callq 0x1060
leal 0x1(%rax), %edi
callq 0x10e0
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rsi
callq 0x1050
movq %r12, %rdi
callq 0x1060
movl %eax, %eax
movb $0x0, (%rbx,%rax)
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl %r14d, %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x2df8(%rip)      # 0x40b8
cmpq $-0x1, %rax
je 0x1479
movq %rax, %r12
addq $0x1400, %r12           # imm = 0x1400
movq %r12, 0x2de5(%rip)      # 0x40c0
pushq $0x8
popq %rdi
callq 0x10e0
movq %rax, %r14
movq %rbx, %rdi
callq 0x1060
cmpl $0x1, %eax
jne 0x12fe
cmpb $0x2f, (%rbx)
jne 0x12fe
cmpb $0x0, 0x1(%rbx)
je 0x1322
movq %rbx, %rdi
callq 0x1490
movq 0x2dab(%rip), %rsi      # 0x40b8
movq %rbx, %rdi
movq %r14, %rdx
callq 0x15c1
movl %eax, %r15d
movq 0x2d9e(%rip), %r12      # 0x40c0
movq %r14, -0x30(%rbp)
movq %rbx, -0x38(%rbp)
movslq %r15d, %rax
movq %rax, %rcx
shlq $0x7, %rcx
leaq (%r12,%rcx), %rsi
addq $-0x80, %rsi
pushq $0x20
popq %rcx
leaq -0xc0(%rbp), %rdi
rep  movsl (%rsi), %es:(%rdi)
movl %eax, %edi
callq 0x1505
testl %eax, %eax
je 0x1429
testb $0x40, -0xbf(%rbp)
je 0x1429
xorl %r13d, %r13d
movslq -0x98(%rbp,%r13,4), %r14
testq %r14, %r14
je 0x1406
shlq $0xa, %r14
movq 0x2d35(%rip), %r15      # 0x40b8
addq %r14, %r15
xorl %ebx, %ebx
cmpl $0x3ff, %ebx            # imm = 0x3FF
jg 0x13fe
cmpl $0x0, (%r15)
jne 0x139f
movzwl 0x4(%r15), %eax
addl %eax, %ebx
jmp 0x1388
movq %rsp, -0x40(%rbp)
movzbl 0x6(%r15), %eax
movq %rsp, %r12
addl $0x10, %eax
andl $-0x10, %eax
subq %rax, %r12
movq %r12, %rsp
leaq 0x8(%r15), %rsi
movzbl 0x6(%r15), %edx
movq %r12, %rdi
callq 0x1040
movzbl 0x6(%r15), %eax
movb $0x0, (%r12,%rax)
leaq 0xc62(%rip), %rdi       # 0x203b
movq %r12, %rsi
xorl %eax, %eax
callq 0x1080
movzwl 0x4(%r15), %eax
addl %eax, %ebx
movq 0x2cc7(%rip), %rax      # 0x40b8
addq %r14, %rax
leaq (%rbx,%rax), %r15
movq -0x40(%rbp), %rsp
jmp 0x1388
incq %r13
jmp 0x1367
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
movq 0x2bc8(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
pushq $0x2
popq %r14
movl %r14d, %edi
callq 0x1130
leaq 0xbfa(%rip), %rsi       # 0x2040
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x10a0
movl %r14d, %edi
callq 0x1110
movq 0x2b96(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb98(%rip), %rdi       # 0x2004
pushq $0x31
popq %rsi
pushq $0x1
popq %rdx
callq 0x1120
jmp 0x1485
leaq 0xbb6(%rip), %rdi       # 0x2036
callq 0x1100
pushq $0x1
popq %rdi
callq 0x1110
nopl (%rax)