Compare commits

..

3 Commits

Author SHA1 Message Date
1b4f48b92c
sh: Display the working directory as part of the prompt
All checks were successful
continuous-integration/drone/push Build is passing
2023-04-11 22:45:33 +02:00
427662d5f1
kernel+libc: Add getcwd() 2023-04-11 22:45:13 +02:00
7db215819e
exec: Respect the program's working directory 2023-04-11 22:44:25 +02:00
8 changed files with 112 additions and 3 deletions

View File

@ -74,7 +74,17 @@ int main(int argc, char** argv)
while (1)
{
if (file == "-") fputs(getuid() == 0 ? "sh# " : "sh$ ", stdout);
if (file == "-")
{
char* cwd = getcwd(NULL, 0);
if (!cwd)
{
perror("getcwd");
return 1;
}
printf("sh %s%c ", cwd, getuid() == 0 ? '#' : '$');
free(cwd);
}
char cmd[4096];
char* rc = fgets(cmd, sizeof(cmd), f);

View File

@ -36,3 +36,22 @@ Result<u64> sys_chdir(Registers*, SyscallArgs args)
return 0;
}
}
Result<u64> sys_getcwd(Registers*, SyscallArgs args)
{
u8* buf = (u8*)args[0];
usize size = (usize)args[1];
Thread* current = Scheduler::current();
StringView cwd = current->current_directory_path.view();
if (cwd.is_empty()) cwd = "/"_sv;
usize cwd_size = cwd.length() + 1;
if (cwd_size > size) return cwd_size;
if (!MemoryManager::copy_to_user(buf, cwd.chars(), cwd_size)) return err(EFAULT);
return cwd_size;
}

View File

@ -41,7 +41,7 @@ Result<u64> sys_execve(Registers* regs, SyscallArgs args)
// FIXME: Make sure argv & envp are not too big.
auto inode = TRY(VFS::resolve_path(path.chars(), current->auth));
auto inode = TRY(VFS::resolve_path(path.chars(), current->auth, current->current_directory));
if (!VFS::can_execute(inode, current->auth)) return err(EACCES);

View File

@ -100,6 +100,9 @@ extern "C"
/* Change the current working directory. */
int chdir(const char* path);
/* Retrieve the current working directory. */
char* getcwd(char* buf, size_t size);
#ifdef __cplusplus
}
#endif

View File

@ -235,4 +235,69 @@ extern "C"
long rc = syscall(SYS_chdir, path);
__errno_return(rc, int);
}
static ssize_t __getcwd_wrapper(char* buf, size_t size)
{
long rc = syscall(SYS_getcwd, buf, size);
__errno_return(rc, ssize_t);
}
char* getcwd(char* buf, size_t size)
{
if (buf)
{
ssize_t rc = __getcwd_wrapper(buf, size);
if (rc < 0) return nullptr;
if (rc > (ssize_t)size)
{
errno = ERANGE;
return nullptr;
}
return buf;
}
else if (size)
{
buf = (char*)malloc(size);
if (!buf) return nullptr;
ssize_t rc = __getcwd_wrapper(buf, size);
if (rc < 0)
{
free(buf);
return nullptr;
}
if (rc > (ssize_t)size)
{
free(buf);
errno = ERANGE;
return nullptr;
}
return buf;
}
else
{
buf = (char*)malloc(1024);
if (!buf) return nullptr;
ssize_t rc = __getcwd_wrapper(buf, 1024);
if (rc < 0)
{
free(buf);
return nullptr;
}
if (rc > 1024)
{
free(buf);
return getcwd(NULL, rc);
}
return buf;
}
}
}

View File

@ -16,6 +16,8 @@ class StringView
StringView(const StringView&);
StringView& operator=(const StringView&);
Result<String> to_string();
const char* chars() const

View File

@ -4,7 +4,7 @@
_e(exit) _e(clock_gettime) _e(mmap) _e(munmap) _e(usleep) _e(open) _e(close) _e(read) _e(getpid) _e(write) \
_e(lseek) _e(mkdir) _e(execve) _e(mknod) _e(fork) _e(waitpid) _e(getppid) _e(fcntl) _e(getdents) _e(getuid) \
_e(geteuid) _e(getgid) _e(getegid) _e(setuid) _e(setgid) _e(seteuid) _e(setegid) _e(chmod) _e(chown) \
_e(ioctl) _e(stat) _e(fstat) _e(chdir)
_e(ioctl) _e(stat) _e(fstat) _e(chdir) _e(getcwd)
enum Syscalls
{

View File

@ -30,6 +30,16 @@ StringView::StringView(const char* c_str, usize length)
m_length = length;
}
StringView& StringView::operator=(const StringView& other)
{
if (&other == this) return *this;
m_string = other.m_string;
m_length = other.m_length;
return *this;
}
const char& StringView::operator[](usize index) const
{
expect(index < m_length, "index out of range");