Add cd command

This commit is contained in:
Baptiste Wicht 2014-02-23 23:07:58 +01:00
parent 1e4873759d
commit 934f6f3a18
12 changed files with 112 additions and 17 deletions

2
.gitignore vendored
View File

@ -24,4 +24,6 @@ programs/keyboard/keyboard
programs/longtwo/longtwo
programs/longone/longone
programs/pwd/pwd
programs/which/which
programs/cd/cd
programs/dist/

View File

@ -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<std::string>& 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<std::string>& 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

View File

@ -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<const char*>(regs->rbx);
std::string path(p);
auto cwd = std::split(path, '/');
scheduler::set_working_directory(cwd);
}

View File

@ -29,22 +29,25 @@ int64_t vfs::open(const char* file_path){
return -std::ERROR_INVALID_FILE_PATH;
}
std::vector<std::string> 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){

View File

@ -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

15
programs/cd/Makefile Normal file
View File

@ -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

BIN
programs/cd/cat Executable file

Binary file not shown.

54
programs/cd/src/main.cpp Normal file
View File

@ -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 <file.hpp>
#include <system.hpp>
#include <errors.hpp>
#include <print.hpp>
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);
}

Binary file not shown.

View File

@ -19,5 +19,6 @@ void close(size_t fd);
std::expected<stat_info> stat(size_t fd);
std::string current_working_directory();
void set_current_working_directory(const std::string& directory);
#endif

View File

@ -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<size_t>(directory.c_str()))
: "rax", "rbx");
}

View File

@ -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;