LibC/Kernel: Implement ttyname

This commit is contained in:
Bananymous 2024-07-30 11:41:18 +03:00
parent ca774dfeb5
commit e5bb843059
5 changed files with 25 additions and 0 deletions

View File

@ -168,6 +168,7 @@ namespace Kernel
BAN::ErrorOr<long> sys_smo_delete(SharedMemoryObjectManager::Key); BAN::ErrorOr<long> sys_smo_delete(SharedMemoryObjectManager::Key);
BAN::ErrorOr<long> sys_smo_map(SharedMemoryObjectManager::Key); BAN::ErrorOr<long> sys_smo_map(SharedMemoryObjectManager::Key);
BAN::ErrorOr<long> sys_ttyname(int fildes, char* storage);
BAN::ErrorOr<long> sys_isatty(int fildes); BAN::ErrorOr<long> sys_isatty(int fildes);
BAN::ErrorOr<long> sys_tty_ctrl(int fildes, int command, int flags); BAN::ErrorOr<long> sys_tty_ctrl(int fildes, int command, int flags);

View File

@ -1497,6 +1497,20 @@ namespace Kernel
return m_mapped_regions.back()->vaddr(); return m_mapped_regions.back()->vaddr();
} }
BAN::ErrorOr<long> Process::sys_ttyname(int fildes, char* storage)
{
LockGuard _(m_process_lock);
TRY(validate_pointer_access(storage, TTY_NAME_MAX));
auto inode = TRY(m_open_file_descriptors.inode_of(fildes));
if (!inode->is_tty())
return BAN::Error::from_errno(ENOTTY);
auto path = TRY(m_open_file_descriptors.path_of(fildes));
ASSERT(path.size() < TTY_NAME_MAX);
strncpy(storage, path.data(), path.size());
storage[path.size()] = '\0';
return 0;
}
BAN::ErrorOr<long> Process::sys_isatty(int fildes) BAN::ErrorOr<long> Process::sys_isatty(int fildes)
{ {
LockGuard _(m_process_lock); LockGuard _(m_process_lock);

View File

@ -62,6 +62,7 @@ __BEGIN_DECLS
#define PATH_MAX 256 #define PATH_MAX 256
#define LOGIN_NAME_MAX 256 #define LOGIN_NAME_MAX 256
#define HOST_NAME_MAX 255 #define HOST_NAME_MAX 255
#define TTY_NAME_MAX PATH_MAX
__END_DECLS __END_DECLS

View File

@ -82,6 +82,7 @@ __BEGIN_DECLS
O(SYS_GETSOCKOPT, getsockopt) \ O(SYS_GETSOCKOPT, getsockopt) \
O(SYS_SETSOCKOPT, setsockopt) \ O(SYS_SETSOCKOPT, setsockopt) \
O(SYS_REALPATH, realpath) \ O(SYS_REALPATH, realpath) \
O(SYS_TTYNAME, ttyname) \
enum Syscall enum Syscall
{ {

View File

@ -488,3 +488,11 @@ char* getlogin(void)
endpwent(); endpwent();
return buffer; return buffer;
} }
char* ttyname(int fildes)
{
static char storage[_POSIX_TTY_NAME_MAX];
if (syscall(SYS_TTYNAME, fildes, storage) == -1)
return nullptr;
return storage;
}