// File: /workspace/github_c_makefile_star2_O0/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
subq $0xf0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x1271
movq 0x2da2(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xda4(%rip), %rsi       # 0x2004
movb $0x0, %al
callq 0x10a0
movl $0x1, %edi
callq 0x1110
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x10f0
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x1060
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edi
shlq $0x0, %rdi
callq 0x10e0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x1050
movq -0x20(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x1060
movq %rax, %rcx
movq -0xd8(%rbp), %rax
movl %ecx, %ecx
movb $0x0, (%rax,%rcx)
movl -0x14(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x20000, %esi          # imm = 0x20000
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x1070
movq %rax, 0x2da7(%rip)      # 0x40b0
movq $-0x1, %rax
cmpq %rax, 0x2d99(%rip)      # 0x40b0
jne 0x132f
leaq 0xd16(%rip), %rdi       # 0x2036
callq 0x1100
movl $0x1, %edi
callq 0x1110
movq 0x2d7a(%rip), %rax      # 0x40b0
addq $0x1400, %rax           # imm = 0x1400
movq %rax, 0x2d75(%rip)      # 0x40b8
movl $0x8, %edi
callq 0x10e0
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0x1060
cmpl $0x1, %eax
jne 0x137d
movq -0x20(%rbp), %rdi
leaq 0xcd1(%rip), %rsi       # 0x203b
callq 0x1090
cmpl $0x0, %eax
jne 0x137d
movl $0x2, -0x2c(%rbp)
jmp 0x139d
movq -0x20(%rbp), %rdi
callq 0x15d0
movq -0x20(%rbp), %rdi
movq 0x2d1f(%rip), %rsi      # 0x40b0
movq -0x28(%rbp), %rdx
callq 0x17a0
movl %eax, -0x2c(%rbp)
movq 0x2d14(%rip), %rsi      # 0x40b8
movl -0x2c(%rbp), %eax
subl $0x1, %eax
cltq
shlq $0x7, %rax
addq %rax, %rsi
leaq -0xac(%rbp), %rdi
movl $0x80, %edx
callq 0x10b0
movl -0x2c(%rbp), %edi
callq 0x1680
cmpl $0x0, %eax
je 0x1579
movzwl -0xac(%rbp), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x1579
movl $0x0, -0xb0(%rbp)
movl $0x0, -0xb4(%rbp)
movslq -0xb4(%rbp), %rax
movl -0x84(%rbp,%rax,4), %eax
movl %eax, -0xb8(%rbp)
cmpl $0x0, -0xb8(%rbp)
je 0x1577
movq 0x2c88(%rip), %rax      # 0x40b0
movl -0xb8(%rbp), %ecx
shll $0xa, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movslq -0xb0(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xc0(%rbp)
cmpl $0x400, -0xb0(%rbp)     # imm = 0x400
jge 0x1545
movq -0xc0(%rbp), %rax
cmpl $0x0, (%rax)
je 0x1529
movq -0xc0(%rbp), %rax
movzbl 0x6(%rax), %ecx
movl %ecx, %eax
incl %eax
movq %rsp, %rdx
movq %rdx, -0xc8(%rbp)
addl $0x10, %ecx
andl $-0x10, %ecx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0xe0(%rbp)
movq %rdi, %rsp
movq %rax, -0xd0(%rbp)
movq -0xc0(%rbp), %rsi
addq $0x8, %rsi
movq -0xc0(%rbp), %rax
movzbl 0x6(%rax), %eax
movl %eax, %edx
callq 0x1040
movq -0xe0(%rbp), %rsi
movq -0xc0(%rbp), %rax
movzbl 0x6(%rax), %eax
movb $0x0, (%rsi,%rax)
leaq 0xb65(%rip), %rdi       # 0x203d
movb $0x0, %al
callq 0x1080
movq -0xc0(%rbp), %rax
movzwl 0x4(%rax), %eax
addl -0xb0(%rbp), %eax
movl %eax, -0xb0(%rbp)
movq 0x2bb3(%rip), %rax      # 0x40b0
movl -0xb8(%rbp), %ecx
shll $0xa, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movslq -0xb0(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xc0(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, %rsp
jmp 0x1540
movq -0xc0(%rbp), %rax
movzwl 0x4(%rax), %eax
addl -0xb0(%rbp), %eax
movl %eax, -0xb0(%rbp)
jmp 0x1448
movl $0x0, -0xb0(%rbp)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
movslq -0xb4(%rbp), %rax
movl -0x84(%rbp,%rax,4), %eax
movl %eax, -0xb8(%rbp)
jmp 0x1414
jmp 0x15b6
movq 0x2a78(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0xe8(%rbp)
movl $0x2, %edi
callq 0x1120
movq -0xe8(%rbp), %rdi
movq %rax, %rdx
leaq 0xa9d(%rip), %rsi       # 0x2042
movb $0x0, %al
callq 0x10a0
movl $0x2, %edi
callq 0x1110
movq -0x20(%rbp), %rdi
callq 0x1030
movq -0x28(%rbp), %rdi
callq 0x1030
xorl %eax, %eax
movq %rbp, %rsp
popq %rbp
retq
nop