Compare commits
3 Commits
3a45f4af53
...
1b4f48b92c
Author | SHA1 | Date | |
---|---|---|---|
1b4f48b92c | |||
427662d5f1 | |||
7db215819e |
12
apps/sh.cpp
12
apps/sh.cpp
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,8 @@ class StringView
|
||||
|
||||
StringView(const StringView&);
|
||||
|
||||
StringView& operator=(const StringView&);
|
||||
|
||||
Result<String> to_string();
|
||||
|
||||
const char* chars() const
|
||||
|
@ -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
|
||||
{
|
||||
|
@ -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");
|
||||
|
Loading…
Reference in New Issue
Block a user