// File: sanaa-khan[P]mini-linux-shell/gbsh.cpp
// Function: listDirectory(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>)
void listDirectory(vector<string> tokens) {

	string temp, filename;

	// detecting if output has to be redirected to a file
	if (tokens.size() > 1) {
		if (tokens[1].size() == 1 and tokens[1][0] == '>') {
			if (tokens.size() == 2) {
				cout << "\033[1;31m" << "Error: ";
				cout << "\033[0;38m" << "Output file not provided.\n";
				return;
			}

			else if (tokens.size() == 3) {
				temp = ".";
				filename = tokens[2];
				oRedirect = true;
			}
			
			else {
				cout << "\033[1;31m" << "Error: ";
				cout << "\033[0;38m" << "Too many arguments.\n";
				return;
			}
		}

		else if (tokens.size() == 2)
			temp = tokens[1];

		else if (tokens.size() > 2) {
			if (tokens[2].size() == 1 and tokens[2][0] == '>') {
				if (tokens.size() == 3) {
					cout << "\033[1;31m" << "Error: ";
					cout << "\033[0;38m" << "Output file not provided.\n";
					return;
				}

				else if (tokens.size() == 4) {
					temp = tokens[1];
					filename = tokens[3];
					oRedirect = true;
				}

				else {
					cout << "\033[1;31m" << "Error: ";
					cout << "\033[0;38m" << "Too many arguments.\n";
					return;
				}
			}

			else {
				cout << "\033[1;31m" << "Error: ";
				cout << "\033[0;38m" << "Too many arguments.\n";
				return;
			}
		}
	}

	else
		temp = "."; // in the case of just "ls" command i.e no directory mentioned

	// output redirection
	if (oRedirect) {
		int file_desc = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0777);
		output = dup(1);

		close(1);
		dup(file_desc);
		close(file_desc);
	}
	
	if (tokens.size() > 4) {
		cout << "\033[1;31m" << "Error: ";
		cout << "\033[0;38m" << "Too many arguments.\n";
		return;
	}

	const char* path;
	path = temp.c_str();

	DIR* directory = NULL;
	dirent* current = NULL;
	unsigned int type;
	unsigned short length;
	char *name;

	directory = opendir(path);

	if (directory != NULL) {
		current = readdir(directory);
		
		while (current != NULL) {
			type = current->d_type;
			length = current->d_reclen;

			name = new char[length];

			strcpy(name, current->d_name);

			if ( (name[0] == '.' and name[1] == '\0') or (name[0] == '.' and name[1] == '.' and name[2] == '\0') ) {
				// do nothing in the case of '.' or ".."
			}

			else
			{
				switch (type) {
					case 4:
						if (oRedirect)
							cout << name << "\t";
						else
							cout << "\033[1;34m" << name << "\033[0m" << "\t";
						break;
					case 8:
						if (oRedirect)
							cout << name << "\t";
						else
							cout << "\033[24;37m" << name << "\033[0m" << "\t";
						break;
					default:
						cout << name << "\t";
				}
			}
			

			delete []name;
			current = readdir(directory);
		}

		closedir(directory);
		cout << "\n";

		if (oRedirect) {
			fflush(stdout); 

			dup2(output, 1);
			close(output);

			oRedirect = false;
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x90(%rbp)
movq %rdi, -0x8(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x2260
leaq -0x48(%rbp), %rdi
callq 0x2260
movq -0x90(%rbp), %rdi
callq 0x5780
cmpq $0x1, %rax
jbe 0x339b
movq -0x90(%rbp), %rdi
movl $0x1, %esi
callq 0x5760
movq %rax, %rdi
callq 0x2120
cmpq $0x1, %rax
jne 0x3132
movq -0x90(%rbp), %rdi
movl $0x1, %esi
callq 0x5760
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2340
movq %rax, -0x98(%rbp)
jmp 0x2fb7
movq -0x98(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3e, %eax
jne 0x3132
movq -0x90(%rbp), %rdi
callq 0x5780
cmpq $0x2, %rax
jne 0x3071
movq 0x6fe1(%rip), %rdi      # 0x9fc8
leaq 0x4077(%rip), %rsi      # 0x7065
callq 0x21b0
movq %rax, -0xa0(%rbp)
jmp 0x2ffc
movq -0xa0(%rbp), %rdi
leaq 0x4063(%rip), %rsi      # 0x706d
callq 0x21b0
jmp 0x3011
movq 0x6fb0(%rip), %rdi      # 0x9fc8
leaq 0x4056(%rip), %rsi      # 0x7075
callq 0x21b0
movq %rax, -0xa8(%rbp)
jmp 0x302d
movq -0xa8(%rbp), %rdi
leaq 0x4042(%rip), %rsi      # 0x707d
callq 0x21b0
jmp 0x3042
movl $0x1, -0x58(%rbp)
jmp 0x3801
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2100
leaq -0x28(%rbp), %rdi
callq 0x2100
jmp 0x381c
movq -0x90(%rbp), %rdi
callq 0x5780
cmpq $0x3, %rax
jne 0x30bd
leaq 0x400e(%rip), %rsi      # 0x7098
leaq -0x28(%rbp), %rdi
callq 0x2240
jmp 0x3095
movq -0x90(%rbp), %rdi
movl $0x2, %esi
callq 0x5760
movq %rax, %rsi
leaq -0x48(%rbp), %rdi
callq 0x2040
jmp 0x30b4
movb $0x1, 0x7115(%rip)      # 0xa1d0
jmp 0x312b
movq 0x6f04(%rip), %rdi      # 0x9fc8
leaq 0x3f9a(%rip), %rsi      # 0x7065
callq 0x21b0
movq %rax, -0xb0(%rbp)
jmp 0x30d9
movq -0xb0(%rbp), %rdi
leaq 0x3f86(%rip), %rsi      # 0x706d
callq 0x21b0
jmp 0x30ee
movq 0x6ed3(%rip), %rdi      # 0x9fc8
leaq 0x3f79(%rip), %rsi      # 0x7075
callq 0x21b0
movq %rax, -0xb8(%rbp)
jmp 0x310a
movq -0xb8(%rbp), %rdi
leaq 0x3f82(%rip), %rsi      # 0x709a
callq 0x21b0
jmp 0x311f
movl $0x1, -0x58(%rbp)
jmp 0x3801
jmp 0x312d
jmp 0x3399
movq -0x90(%rbp), %rdi
callq 0x5780
cmpq $0x2, %rax
jne 0x3168
movq -0x90(%rbp), %rdi
movl $0x1, %esi
callq 0x5760
movq %rax, %rsi
leaq -0x28(%rbp), %rdi
callq 0x2040
jmp 0x3163
jmp 0x3397
movq -0x90(%rbp), %rdi
callq 0x5780
cmpq $0x2, %rax
jbe 0x3395
movq -0x90(%rbp), %rdi
movl $0x2, %esi
callq 0x5760
movq %rax, %rdi
callq 0x2120
cmpq $0x1, %rax
jne 0x3325
movq -0x90(%rbp), %rdi
movl $0x2, %esi
callq 0x5760
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2340
movq %rax, -0xc0(%rbp)
jmp 0x31c7
movq -0xc0(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3e, %eax
jne 0x3325
movq -0x90(%rbp), %rdi
callq 0x5780
cmpq $0x3, %rax
jne 0x325a
movq 0x6dd5(%rip), %rdi      # 0x9fc8
leaq 0x3e6b(%rip), %rsi      # 0x7065
callq 0x21b0
movq %rax, -0xc8(%rbp)
jmp 0x3208
movq -0xc8(%rbp), %rdi
leaq 0x3e57(%rip), %rsi      # 0x706d
callq 0x21b0
jmp 0x321d
movq 0x6da4(%rip), %rdi      # 0x9fc8
leaq 0x3e4a(%rip), %rsi      # 0x7075
callq 0x21b0
movq %rax, -0xd0(%rbp)
jmp 0x3239
movq -0xd0(%rbp), %rdi
leaq 0x3e36(%rip), %rsi      # 0x707d
callq 0x21b0
jmp 0x324e
movl $0x1, -0x58(%rbp)
jmp 0x3801
movq -0x90(%rbp), %rdi
callq 0x5780
cmpq $0x4, %rax
jne 0x32b3
movq -0x90(%rbp), %rdi
movl $0x1, %esi
callq 0x5760
movq %rax, %rsi
leaq -0x28(%rbp), %rdi
callq 0x2040
jmp 0x328b
movq -0x90(%rbp), %rdi
movl $0x3, %esi
callq 0x5760
movq %rax, %rsi
leaq -0x48(%rbp), %rdi
callq 0x2040
jmp 0x32aa
movb $0x1, 0x6f1f(%rip)      # 0xa1d0
jmp 0x3321
movq 0x6d0e(%rip), %rdi      # 0x9fc8
leaq 0x3da4(%rip), %rsi      # 0x7065
callq 0x21b0
movq %rax, -0xd8(%rbp)
jmp 0x32cf
movq -0xd8(%rbp), %rdi
leaq 0x3d90(%rip), %rsi      # 0x706d
callq 0x21b0
jmp 0x32e4
movq 0x6cdd(%rip), %rdi      # 0x9fc8
leaq 0x3d83(%rip), %rsi      # 0x7075
callq 0x21b0
movq %rax, -0xe0(%rbp)
jmp 0x3300
movq -0xe0(%rbp), %rdi
leaq 0x3d8c(%rip), %rsi      # 0x709a
callq 0x21b0
jmp 0x3315
movl $0x1, -0x58(%rbp)
jmp 0x3801
jmp 0x3323
jmp 0x3393
movq 0x6c9c(%rip), %rdi      # 0x9fc8
leaq 0x3d32(%rip), %rsi      # 0x7065
callq 0x21b0
movq %rax, -0xe8(%rbp)
jmp 0x3341
movq -0xe8(%rbp), %rdi
leaq 0x3d1e(%rip), %rsi      # 0x706d
callq 0x21b0
jmp 0x3356
movq 0x6c6b(%rip), %rdi      # 0x9fc8
leaq 0x3d11(%rip), %rsi      # 0x7075
callq 0x21b0
movq %rax, -0xf0(%rbp)
jmp 0x3372
movq -0xf0(%rbp), %rdi
leaq 0x3d1a(%rip), %rsi      # 0x709a
callq 0x21b0
jmp 0x3387
movl $0x1, -0x58(%rbp)
jmp 0x3801
jmp 0x3395
jmp 0x3397
jmp 0x3399
jmp 0x33af
leaq 0x3cf6(%rip), %rsi      # 0x7098
leaq -0x28(%rbp), %rdi
callq 0x2240
jmp 0x33ad
jmp 0x33af
testb $0x1, 0x6e1a(%rip)      # 0xa1d0
je 0x341d
leaq -0x48(%rbp), %rdi
callq 0x20b0
movq %rax, %rdi
xorl %eax, %eax
movl $0x241, %esi            # imm = 0x241
movl $0x1ff, %edx            # imm = 0x1FF
callq 0x2130
movl %eax, -0xf4(%rbp)
jmp 0x33dd
movl -0xf4(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl $0x1, %edi
movl %edi, -0xf8(%rbp)
callq 0x22c0
movl -0xf8(%rbp), %edi
movl %eax, 0x6dc6(%rip)      # 0xa1c8
callq 0x2330
jmp 0x3409
movl -0x5c(%rbp), %edi
callq 0x22c0
movl -0x5c(%rbp), %edi
callq 0x2330
jmp 0x341b
jmp 0x341d
movq -0x90(%rbp), %rdi
callq 0x5780
cmpq $0x4, %rax
jbe 0x349d
movq 0x6b92(%rip), %rdi      # 0x9fc8
leaq 0x3c28(%rip), %rsi      # 0x7065
callq 0x21b0
movq %rax, -0x100(%rbp)
jmp 0x344b
movq -0x100(%rbp), %rdi
leaq 0x3c14(%rip), %rsi      # 0x706d
callq 0x21b0
jmp 0x3460
movq 0x6b61(%rip), %rdi      # 0x9fc8
leaq 0x3c07(%rip), %rsi      # 0x7075
callq 0x21b0
movq %rax, -0x108(%rbp)
jmp 0x347c
movq -0x108(%rbp), %rdi
leaq 0x3c10(%rip), %rsi      # 0x709a
callq 0x21b0
jmp 0x3491
movl $0x1, -0x58(%rbp)
jmp 0x3801
leaq -0x28(%rbp), %rdi
callq 0x20b0
movq %rax, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x68(%rbp), %rdi
callq 0x22a0
movq %rax, -0x110(%rbp)
jmp 0x34cc
movq -0x110(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
je 0x37fa
movq -0x70(%rbp), %rdi
callq 0x22d0
movq %rax, -0x118(%rbp)
jmp 0x34f4
movq -0x118(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
je 0x379a
movq -0x78(%rbp), %rax
movzbl 0x12(%rax), %eax
movl %eax, -0x7c(%rbp)
movq -0x78(%rbp), %rax
movw 0x10(%rax), %ax
movw %ax, -0x7e(%rbp)
movzwl -0x7e(%rbp), %eax
movl %eax, %edi
callq 0x2030
movq %rax, -0x120(%rbp)
jmp 0x3535
movq -0x120(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
movq -0x78(%rbp), %rsi
addq $0x13, %rsi
callq 0x21a0
movq -0x88(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2e, %eax
jne 0x3576
movq -0x88(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x0, %eax
je 0x35a5
movq -0x88(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2e, %eax
jne 0x35aa
movq -0x88(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x2e, %eax
jne 0x35aa
movq -0x88(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x0, %eax
jne 0x35aa
jmp 0x3758
movl -0x7c(%rbp), %eax
movl %eax, -0x124(%rbp)
subl $0x4, %eax
je 0x35ce
jmp 0x35ba
movl -0x124(%rbp), %eax
subl $0x8, %eax
je 0x367a
jmp 0x3723
testb $0x1, 0x6bfb(%rip)      # 0xa1d0
je 0x360a
movq -0x88(%rbp), %rsi
movq 0x69e3(%rip), %rdi      # 0x9fc8
callq 0x21b0
movq %rax, -0x130(%rbp)
jmp 0x35f3
movq -0x130(%rbp), %rdi
leaq 0x3aae(%rip), %rsi      # 0x70af
callq 0x21b0
jmp 0x3608
jmp 0x3675
movq 0x69b7(%rip), %rdi      # 0x9fc8
leaq 0x3a99(%rip), %rsi      # 0x70b1
callq 0x21b0
movq %rax, -0x138(%rbp)
jmp 0x3626
movq -0x138(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x21b0
movq %rax, -0x140(%rbp)
jmp 0x3642
movq -0x140(%rbp), %rdi
leaq 0x39be(%rip), %rsi      # 0x700e
callq 0x21b0
movq %rax, -0x148(%rbp)
jmp 0x365e
movq -0x148(%rbp), %rdi
leaq 0x3a43(%rip), %rsi      # 0x70af
callq 0x21b0
jmp 0x3673
jmp 0x3675
jmp 0x3756
testb $0x1, 0x6b4f(%rip)      # 0xa1d0
je 0x36b6
movq -0x88(%rbp), %rsi
movq 0x6937(%rip), %rdi      # 0x9fc8
callq 0x21b0
movq %rax, -0x150(%rbp)
jmp 0x369f
movq -0x150(%rbp), %rdi
leaq 0x3a02(%rip), %rsi      # 0x70af
callq 0x21b0
jmp 0x36b4
jmp 0x3721
movq 0x690b(%rip), %rdi      # 0x9fc8
leaq 0x39f5(%rip), %rsi      # 0x70b9
callq 0x21b0
movq %rax, -0x158(%rbp)
jmp 0x36d2
movq -0x158(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x21b0
movq %rax, -0x160(%rbp)
jmp 0x36ee
movq -0x160(%rbp), %rdi
leaq 0x3912(%rip), %rsi      # 0x700e
callq 0x21b0
movq %rax, -0x168(%rbp)
jmp 0x370a
movq -0x168(%rbp), %rdi
leaq 0x3997(%rip), %rsi      # 0x70af
callq 0x21b0
jmp 0x371f
jmp 0x3721
jmp 0x3756
movq -0x88(%rbp), %rsi
movq 0x6897(%rip), %rdi      # 0x9fc8
callq 0x21b0
movq %rax, -0x170(%rbp)
jmp 0x373f
movq -0x170(%rbp), %rdi
leaq 0x3962(%rip), %rsi      # 0x70af
callq 0x21b0
jmp 0x3754
jmp 0x3756
jmp 0x3758
movq -0x88(%rbp), %rax
movq %rax, -0x178(%rbp)
cmpq $0x0, %rax
je 0x3778
movq -0x178(%rbp), %rdi
callq 0x2230
movq -0x70(%rbp), %rdi
callq 0x22d0
movq %rax, -0x180(%rbp)
jmp 0x378a
movq -0x180(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x34ff
movq -0x70(%rbp), %rdi
callq 0x22f0
jmp 0x37a5
movq 0x681c(%rip), %rdi      # 0x9fc8
leaq 0x397a(%rip), %rsi      # 0x712d
callq 0x21b0
jmp 0x37ba
testb $0x1, 0x6a0f(%rip)      # 0xa1d0
je 0x37f8
movq 0x67de(%rip), %rax      # 0x9fa8
movq (%rax), %rdi
callq 0x21e0
jmp 0x37d4
movl 0x69ee(%rip), %edi      # 0xa1c8
movl $0x1, %esi
callq 0x2190
movl 0x69de(%rip), %edi      # 0xa1c8
callq 0x2330
jmp 0x37f1
movb $0x0, 0x69d8(%rip)      # 0xa1d0
jmp 0x37fa
movl $0x0, -0x58(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2100
leaq -0x28(%rbp), %rdi
callq 0x2100
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x2320
nopw %cs:(%rax,%rax)