// File: sanaa-khan[P]mini-linux-shell/gbsh.cpp
// Function: listEnvVars(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 listEnvVars(vector<string> tokens) {

	string filename;

	char cwd[100];
	getcwd(cwd, 100);

	setenv("SHELL", cwd, 1);

	// for output redirection
	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 name not provided.\n";
				return;
			}

			else if (tokens.size() == 3) {
				filename = tokens[2];
				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;
		}
	}

	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);
	}

	// outputting variables
	int index = 0;
	char* current = environ[index];

	while (current != NULL) {
		cout << current << "\n";
		current = environ[++index];
	}

	// redirecting output back
	if (oRedirect) {
		fflush(stdout); 

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

		oRedirect = false;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0xb8(%rbp)
movq %rdi, -0x8(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x2260
leaq -0x90(%rbp), %rdi
movl $0x64, %esi
callq 0x20d0
leaq -0x90(%rbp), %rsi
leaq 0x344f(%rip), %rdi      # 0x704d
movl $0x1, %edx
callq 0x22b0
movq -0xb8(%rbp), %rdi
callq 0x5780
cmpq $0x1, %rax
jbe 0x3e43
movq -0xb8(%rbp), %rdi
movl $0x1, %esi
callq 0x5760
movq %rax, %rdi
callq 0x2120
cmpq $0x1, %rax
jne 0x3dd0
movq -0xb8(%rbp), %rdi
movl $0x1, %esi
callq 0x5760
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2340
movq %rax, -0xc0(%rbp)
jmp 0x3c67
movq -0xc0(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3e, %eax
jne 0x3dd0
movq -0xb8(%rbp), %rdi
callq 0x5780
cmpq $0x2, %rax
jne 0x3d21
movq 0x6331(%rip), %rdi      # 0x9fc8
leaq 0x33c7(%rip), %rsi      # 0x7065
callq 0x21b0
movq %rax, -0xc8(%rbp)
jmp 0x3cac
movq -0xc8(%rbp), %rdi
leaq 0x33b3(%rip), %rsi      # 0x706d
callq 0x21b0
jmp 0x3cc1
movq 0x6300(%rip), %rdi      # 0x9fc8
leaq 0x33a6(%rip), %rsi      # 0x7075
callq 0x21b0
movq %rax, -0xd0(%rbp)
jmp 0x3cdd
movq -0xd0(%rbp), %rdi
leaq 0x33dd(%rip), %rsi      # 0x70c8
callq 0x21b0
jmp 0x3cf2
movl $0x1, -0xa0(%rbp)
jmp 0x3f8c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x2100
jmp 0x3f9e
movq -0xb8(%rbp), %rdi
callq 0x5780
cmpq $0x3, %rax
jne 0x3d5b
movq -0xb8(%rbp), %rdi
movl $0x2, %esi
callq 0x5760
movq %rax, %rsi
leaq -0x28(%rbp), %rdi
callq 0x2040
jmp 0x3d52
movb $0x1, 0x6477(%rip)      # 0xa1d0
jmp 0x3dcc
movq 0x6266(%rip), %rdi      # 0x9fc8
leaq 0x32fc(%rip), %rsi      # 0x7065
callq 0x21b0
movq %rax, -0xd8(%rbp)
jmp 0x3d77
movq -0xd8(%rbp), %rdi
leaq 0x32e8(%rip), %rsi      # 0x706d
callq 0x21b0
jmp 0x3d8c
movq 0x6235(%rip), %rdi      # 0x9fc8
leaq 0x32db(%rip), %rsi      # 0x7075
callq 0x21b0
movq %rax, -0xe0(%rbp)
jmp 0x3da8
movq -0xe0(%rbp), %rdi
leaq 0x32e4(%rip), %rsi      # 0x709a
callq 0x21b0
jmp 0x3dbd
movl $0x1, -0xa0(%rbp)
jmp 0x3f8c
jmp 0x3dce
jmp 0x3e41
movq 0x61f1(%rip), %rdi      # 0x9fc8
leaq 0x3287(%rip), %rsi      # 0x7065
callq 0x21b0
movq %rax, -0xe8(%rbp)
jmp 0x3dec
movq -0xe8(%rbp), %rdi
leaq 0x3273(%rip), %rsi      # 0x706d
callq 0x21b0
jmp 0x3e01
movq 0x61c0(%rip), %rdi      # 0x9fc8
leaq 0x3266(%rip), %rsi      # 0x7075
callq 0x21b0
movq %rax, -0xf0(%rbp)
jmp 0x3e1d
movq -0xf0(%rbp), %rdi
leaq 0x326f(%rip), %rsi      # 0x709a
callq 0x21b0
jmp 0x3e32
movl $0x1, -0xa0(%rbp)
jmp 0x3f8c
jmp 0x3e43
testb $0x1, 0x6386(%rip)      # 0xa1d0
je 0x3eba
leaq -0x28(%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 0x3e71
movl -0xf4(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl $0x1, %edi
movl %edi, -0xf8(%rbp)
callq 0x22c0
movl -0xf8(%rbp), %edi
movl %eax, 0x632f(%rip)      # 0xa1c8
callq 0x2330
jmp 0x3ea0
movl -0xa4(%rbp), %edi
callq 0x22c0
movl -0xa4(%rbp), %edi
callq 0x2330
jmp 0x3eb8
jmp 0x3eba
movl $0x0, -0xa8(%rbp)
movq 0x6105(%rip), %rax      # 0x9fd0
movq (%rax), %rax
movslq -0xa8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb0(%rbp)
cmpq $0x0, -0xb0(%rbp)
je 0x3f44
movq -0xb0(%rbp), %rsi
movq 0x60d0(%rip), %rdi      # 0x9fc8
callq 0x21b0
movq %rax, -0x100(%rbp)
jmp 0x3f06
movq -0x100(%rbp), %rdi
leaq 0x3219(%rip), %rsi      # 0x712d
callq 0x21b0
jmp 0x3f1b
movq 0x60ae(%rip), %rax      # 0x9fd0
movq (%rax), %rax
movl -0xa8(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0xa8(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb0(%rbp)
jmp 0x3ee0
testb $0x1, 0x6285(%rip)      # 0xa1d0
je 0x3f82
movq 0x6054(%rip), %rax      # 0x9fa8
movq (%rax), %rdi
callq 0x21e0
jmp 0x3f5e
movl 0x6264(%rip), %edi      # 0xa1c8
movl $0x1, %esi
callq 0x2190
movl 0x6254(%rip), %edi      # 0xa1c8
callq 0x2330
jmp 0x3f7b
movb $0x0, 0x624e(%rip)      # 0xa1d0
movl $0x0, -0xa0(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x2100
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0x98(%rbp), %rdi
callq 0x2320
nopw (%rax,%rax)