diff --git a/.gitignore b/.gitignore index 0cf335fc..7a5d12f0 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,6 @@ programs/keyboard/keyboard programs/longtwo/longtwo programs/longone/longone programs/pwd/pwd +programs/which/which +programs/cd/cd programs/dist/ \ No newline at end of file diff --git a/kernel/src/scheduler.cpp b/kernel/src/scheduler.cpp index 229c35fb..05c48494 100644 --- a/kernel/src/scheduler.cpp +++ b/kernel/src/scheduler.cpp @@ -25,7 +25,7 @@ #include "physical_pointer.hpp" #include "mutex.hpp" -constexpr const bool DEBUG_SCHEDULER = true; +constexpr const bool DEBUG_SCHEDULER = false; //Provided by task_switch.s extern "C" { @@ -863,11 +863,23 @@ const std::string& scheduler::get_handle(size_t fd){ } const std::vector& scheduler::get_working_directory(){ + k_print_line(); + k_print_line("5"); + k_print_line(pcb[current_pid].working_directory.size()); + k_print_line(); return pcb[current_pid].working_directory; } void scheduler::set_working_directory(const std::vector& directory){ + k_print_line(); + k_print_line("3"); + k_print_line(directory.size()); + k_print_line(); pcb[current_pid].working_directory = directory; + k_print_line(); + k_print_line("4"); + k_print_line(pcb[current_pid].working_directory.size()); + k_print_line(); } //Provided for task_switch.s diff --git a/kernel/src/system_calls.cpp b/kernel/src/system_calls.cpp index 1610e3a9..1518c78c 100644 --- a/kernel/src/system_calls.cpp +++ b/kernel/src/system_calls.cpp @@ -131,7 +131,7 @@ void sc_read(interrupt::syscall_regs* regs){ } void sc_pwd(interrupt::syscall_regs* regs){ - auto wd = scheduler::get_working_directory(); + auto& wd = scheduler::get_working_directory(); std::string path; path += '/'; @@ -152,6 +152,7 @@ void sc_cwd(interrupt::syscall_regs* regs){ auto p = reinterpret_cast(regs->rbx); std::string path(p); + auto cwd = std::split(path, '/'); scheduler::set_working_directory(cwd); } diff --git a/kernel/src/vfs.cpp b/kernel/src/vfs.cpp index 3b6e0a31..3c18cfe0 100644 --- a/kernel/src/vfs.cpp +++ b/kernel/src/vfs.cpp @@ -29,22 +29,25 @@ int64_t vfs::open(const char* file_path){ return -std::ERROR_INVALID_FILE_PATH; } + std::vector path; + if(file[0] != '/'){ - return -std::ERROR_INVALID_FILE_PATH; + for(auto& part : scheduler::get_working_directory()){ + path.push_back(part); + } } auto parts = std::split(file, '/'); - - if(parts.empty()){ - return -std::ERROR_INVALID_FILE_PATH; + for(auto& part : parts){ + path.push_back(part); } - auto last = parts.back(); - parts.pop_back(); + auto last = path.back(); + path.pop_back(); //TODO file search should be done entirely by the file system - auto files = fat32::ls(*disks::mounted_disk(), *disks::mounted_partition(), parts); + auto files = fat32::ls(*disks::mounted_disk(), *disks::mounted_partition(), path); for(auto& f : files){ if(f.file_name == last){ diff --git a/programs/Makefile b/programs/Makefile index 217ed095..302378cd 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -1,6 +1,6 @@ .PHONY: dist default clean force_look -PROGRAMS=one hello long loop longone longtwo keyboard tsh cpuid shutdown reboot args stat cat which pwd +PROGRAMS=one hello long loop longone longtwo keyboard tsh cpuid shutdown reboot args stat cat which pwd cd default: dist diff --git a/programs/cd/Makefile b/programs/cd/Makefile new file mode 100644 index 00000000..bb1d26ab --- /dev/null +++ b/programs/cd/Makefile @@ -0,0 +1,15 @@ +.PHONY: default clean + +default: cd + +include ../../cpp.mk + +%.cpp.o: src/%.cpp + $(CC) -c $< -o $@ $(PROGRAM_FLAGS) + +cd: main.cpp.o + $(CC) -o cd main.cpp.o $(PROGRAM_LINK_FLAGS) + +clean: + rm -f *.cpp.o + rm -rf cd diff --git a/programs/cd/cat b/programs/cd/cat new file mode 100755 index 00000000..1c8abe6a Binary files /dev/null and b/programs/cd/cat differ diff --git a/programs/cd/src/main.cpp b/programs/cd/src/main.cpp new file mode 100644 index 00000000..43894e11 --- /dev/null +++ b/programs/cd/src/main.cpp @@ -0,0 +1,54 @@ +//======================================================================= +// Copyright Baptiste Wicht 2013-2014. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +//======================================================================= + +#include +#include +#include +#include + +int main(int argc, char* argv[]){ + if(argc == 1){ + print_line("Usage: cd file_path"); + exit(1); + } + + std::string path(argv[1]); + + auto fd = open(path.c_str()); + + if(fd.valid()){ + auto info = stat(*fd); + + if(info.valid()){ + if(!(info->flags & STAT_FLAG_DIRECTORY)){ + print_line("cat: error: Is not a directory"); + } else { + auto cwd = current_working_directory(); + + if(path[0] == '/'){ + cwd = "/"; + } + + auto parts = std::split(path, '/'); + for(auto& part : parts){ + cwd += part; + cwd += '/'; + } + + set_current_working_directory(cwd); + } + } else { + printf("cd: error: %s\n", std::error_message(info.error())); + } + + close(*fd); + } else { + printf("cd: error: %s\n", std::error_message(fd.error())); + } + + exit(0); +} \ No newline at end of file diff --git a/programs/which/which b/programs/which/which deleted file mode 100755 index c3d945ed..00000000 Binary files a/programs/which/which and /dev/null differ diff --git a/tlib/include/file.hpp b/tlib/include/file.hpp index d501b953..2cee5321 100644 --- a/tlib/include/file.hpp +++ b/tlib/include/file.hpp @@ -19,5 +19,6 @@ void close(size_t fd); std::expected stat(size_t fd); std::string current_working_directory(); +void set_current_working_directory(const std::string& directory); #endif diff --git a/tlib/src/file.cpp b/tlib/src/file.cpp index bb53fe08..48df6b09 100644 --- a/tlib/src/file.cpp +++ b/tlib/src/file.cpp @@ -68,3 +68,10 @@ std::string current_working_directory(){ return {buffer}; } + +void set_current_working_directory(const std::string& directory){ + asm volatile("mov rax, 305; mov rbx, %[buffer]; int 50;" + : /* No outputs */ + : [buffer] "g" (reinterpret_cast(directory.c_str())) + : "rax", "rbx"); +} diff --git a/tstl/include/vector.hpp b/tstl/include/vector.hpp index db8438d7..91689cc3 100644 --- a/tstl/include/vector.hpp +++ b/tstl/include/vector.hpp @@ -50,15 +50,15 @@ public: data = nullptr; } - _size = rhs._size; - _capacity = rhs._capacity; - - if(!rhs.empty()){ + if(_capacity < rhs._capacity){ + _capacity = rhs._capacity; data = new T[_capacity]; + } - for(size_t i = 0; i < _size; ++i){ - data[i] = rhs.data[i]; - } + _size = rhs._size; + + for(size_t i = 0; i < _size; ++i){ + data[i] = rhs.data[i]; } return *this;