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

	int len = 50;
	char cwd[len];

	getcwd(cwd, len);

	if (tokens.size() == 1)
		cout << cwd << "\n";

	// for redirecting output
	else if (tokens[1].size() == 1 and tokens[1][0] == '>') {
		if (tokens.size() == 3) {
			string filename = tokens[2];

			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);
			
			cout << cwd;
			
			fflush(stdout); 

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

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

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x60(%rbp)
movq %rdi, -0x8(%rbp)
movl $0x32, -0xc(%rbp)
movl -0xc(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x18(%rbp)
movq %rax, %rcx
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x68(%rbp)
movq %rdi, %rsp
movq %rax, -0x20(%rbp)
movslq -0xc(%rbp), %rsi
callq 0x20d0
movq -0x60(%rbp), %rdi
callq 0x5780
cmpq $0x1, %rax
jne 0x38ac
movq -0x68(%rbp), %rsi
movq 0x6735(%rip), %rdi      # 0x9fc8
callq 0x21b0
movq %rax, %rdi
leaq 0x388b(%rip), %rsi      # 0x712d
callq 0x21b0
jmp 0x3a81
movq -0x60(%rbp), %rdi
movl $0x1, %esi
callq 0x5760
movq %rax, %rdi
callq 0x2120
cmpq $0x1, %rax
jne 0x3a7f
movq -0x60(%rbp), %rdi
movl $0x1, %esi
callq 0x5760
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2340
movsbl (%rax), %eax
cmpl $0x3e, %eax
jne 0x3a7f
movq -0x60(%rbp), %rdi
callq 0x5780
cmpq $0x3, %rax
jne 0x39e2
movq -0x60(%rbp), %rdi
movl $0x2, %esi
callq 0x5760
movq %rax, %rsi
leaq -0x40(%rbp), %rdi
movq %rdi, -0x78(%rbp)
callq 0x20c0
movq -0x78(%rbp), %rdi
callq 0x20b0
movq %rax, %rdi
xorl %eax, %eax
movl $0x241, %esi            # imm = 0x241
movl $0x1ff, %edx            # imm = 0x1FF
callq 0x2130
movl %eax, -0x6c(%rbp)
jmp 0x3945
movl -0x6c(%rbp), %eax
movl %eax, -0x44(%rbp)
movl $0x1, %edi
movl %edi, -0x7c(%rbp)
callq 0x22c0
movl -0x7c(%rbp), %edi
movl %eax, 0x6867(%rip)      # 0xa1c8
callq 0x2330
jmp 0x3968
movl -0x44(%rbp), %edi
callq 0x22c0
movl -0x44(%rbp), %edi
callq 0x2330
jmp 0x397a
movq -0x68(%rbp), %rsi
movq 0x6643(%rip), %rdi      # 0x9fc8
callq 0x21b0
jmp 0x398c
movq 0x6615(%rip), %rax      # 0x9fa8
movq (%rax), %rdi
callq 0x21e0
jmp 0x399d
movl 0x6825(%rip), %edi      # 0xa1c8
movl $0x1, %esi
callq 0x2190
movl 0x6815(%rip), %edi      # 0xa1c8
callq 0x2330
jmp 0x39ba
leaq -0x40(%rbp), %rdi
callq 0x2100
jmp 0x3a7d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x2100
jmp 0x3a8d
movq -0x60(%rbp), %rdi
callq 0x5780
cmpq $0x3, %rax
jae 0x3a37
movq 0x65d0(%rip), %rdi      # 0x9fc8
leaq 0x3666(%rip), %rsi      # 0x7065
callq 0x21b0
movq %rax, %rdi
leaq 0x365f(%rip), %rsi      # 0x706d
callq 0x21b0
movq 0x65ae(%rip), %rdi      # 0x9fc8
leaq 0x3654(%rip), %rsi      # 0x7075
callq 0x21b0
movq %rax, %rdi
leaq 0x364d(%rip), %rsi      # 0x707d
callq 0x21b0
jmp 0x3a7b
movq 0x658a(%rip), %rdi      # 0x9fc8
leaq 0x3620(%rip), %rsi      # 0x7065
callq 0x21b0
movq %rax, %rdi
leaq 0x3619(%rip), %rsi      # 0x706d
callq 0x21b0
movq 0x6568(%rip), %rdi      # 0x9fc8
leaq 0x360e(%rip), %rsi      # 0x7075
callq 0x21b0
movq %rax, %rdi
leaq 0x3624(%rip), %rsi      # 0x709a
callq 0x21b0
jmp 0x3a7d
jmp 0x3a7f
jmp 0x3a81
movq -0x18(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x2320
nopw %cs:(%rax,%rax)