// File: /workspace/github_c_makefile_star2_O0/Sidd0107[P]EXT2-File-System/ext2_ls.c
// Function: main
int main(int argc, char **argv) {
    //Checks correct number number of arguments
    if(argc != 3) {
        fprintf(stderr, "Usage: ext2_ls <image file name> <path>\n");
        exit(1);
    }
    int fd = open(argv[1], O_RDWR);
	
	char *path = argv[2];

    disk = mmap(NULL, 128 * BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    //Checks to make sure mmap didnt fail.
    if(disk == MAP_FAILED) {
	perror("mmap");
	exit(1);
    }
	//Gets superblock and group descriptor(In this assignment there is only 1 group descr.)
    struct ext2_super_block *sb = (struct ext2_super_block *)(disk + BLOCK_SIZE);
	struct ext2_group_desc *gd = (struct ext2_group_desc *)  (disk + 2*BLOCK_SIZE + ((sb -> s_block_group_nr) *  sizeof(struct ext2_group_desc)));
	//Gets details about the file in path.
	struct basic_fileinfo fileinfo = find_file(path, get_block(disk, gd -> bg_inode_table, BLOCK_SIZE));
	//Makes sure file type is directory and calls helper to print contents of that directory.
	if(fileinfo.type == 'd'){
		print_directory_contents(get_block(disk, gd -> bg_inode_table, BLOCK_SIZE), fileinfo.inode);
	}//If type is file handle appropriately to make sure it prints error or the file name
	else if(fileinfo.type == 'f'){
		if(path[strlen(path)-1] != '/'){
			printf("%s\n", path);
		}
		else{
			printf("ls: cannot access %s: Not a directory\n", path);
		}
	}
	else{
		printf("ls: cannot access %s: No such file or directory\n", path);
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x238e
movq 0x2c85(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xd05(%rip), %rsi       # 0x3082
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x10e0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x10c0
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
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, 0x2cb8(%rip)      # 0x5090
movq $-0x1, %rax
cmpq %rax, 0x2caa(%rip)      # 0x5090
jne 0x23fe
leaq 0xcbc(%rip), %rdi       # 0x30ab
callq 0x10d0
movl $0x1, %edi
callq 0x10e0
movq 0x2c8b(%rip), %rax      # 0x5090
addq $0x400, %rax            # imm = 0x400
movq %rax, -0x28(%rbp)
movq 0x2c7a(%rip), %rax      # 0x5090
addq $0x800, %rax            # imm = 0x800
movq -0x28(%rbp), %rcx
movzwl 0x5e(%rcx), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
movq 0x2c52(%rip), %rdi      # 0x5090
movq -0x30(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17b0
movq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0x13b0
movq %rax, -0x38(%rbp)
movsbl -0x38(%rbp), %eax
cmpl $0x64, %eax
jne 0x248d
movq 0x2c21(%rip), %rdi      # 0x5090
movq -0x30(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17b0
movq %rax, %rdi
movl -0x34(%rbp), %esi
callq 0x2150
jmp 0x24f7
movsbl -0x38(%rbp), %eax
cmpl $0x66, %eax
jne 0x24e3
movq -0x20(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rdi
callq 0x1060
movq %rax, %rcx
movq -0x48(%rbp), %rax
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2f, %eax
je 0x24cf
movq -0x20(%rbp), %rsi
leaq 0xbea(%rip), %rdi       # 0x30b0
movb $0x0, %al
callq 0x1080
jmp 0x24e1
movq -0x20(%rbp), %rsi
leaq 0xbda(%rip), %rdi       # 0x30b4
movb $0x0, %al
callq 0x1080
jmp 0x24f5
movq -0x20(%rbp), %rsi
leaq 0xbed(%rip), %rdi       # 0x30db
movb $0x0, %al
callq 0x1080
jmp 0x24f7
xorl %eax, %eax
addq $0x50, %rsp
popq %rbp
retq