Compare commits
2 Commits
5c61252061
...
1e86acd4c0
Author | SHA1 | Date | |
---|---|---|---|
1e86acd4c0 | |||
36bb1cab5c |
@ -141,5 +141,23 @@ int main()
|
||||
|
||||
printf("Press any key to restart.\n\n");
|
||||
|
||||
int ferr = fileno(stderr);
|
||||
int newerr = dup(ferr);
|
||||
if (newerr < 0)
|
||||
{
|
||||
perror("dup");
|
||||
return 1;
|
||||
}
|
||||
FILE* new_stderr = fdopen(newerr, "rw");
|
||||
if (!new_stderr)
|
||||
{
|
||||
perror("fdopen");
|
||||
return 1;
|
||||
}
|
||||
|
||||
fprintf(new_stderr, "Bye!\n");
|
||||
|
||||
fclose(new_stderr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -43,6 +43,8 @@ struct Descriptor
|
||||
Descriptor(const Descriptor& other);
|
||||
Descriptor();
|
||||
|
||||
const Descriptor& operator=(const Descriptor& other);
|
||||
|
||||
private:
|
||||
bool m_is_open;
|
||||
bool m_can_read;
|
||||
|
@ -41,3 +41,13 @@ int Descriptor::seek(long offset)
|
||||
m_offset = (uint64_t)offset;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const Descriptor& Descriptor::operator=(const Descriptor& other)
|
||||
{
|
||||
m_is_open = other.m_is_open;
|
||||
m_can_read = other.m_can_read;
|
||||
m_can_write = other.m_can_write;
|
||||
m_offset = other.m_offset;
|
||||
m_node = other.m_node;
|
||||
return other;
|
||||
}
|
@ -20,7 +20,7 @@
|
||||
|
||||
#define FNCTL_DUPFD 0
|
||||
|
||||
void sys_fcntl(Context* context, int fd, int command, uintptr_t arg)
|
||||
void sys_fcntl(Context* context, int fd, int command, [[maybe_unused]] uintptr_t arg)
|
||||
{
|
||||
if (fd >= TASK_MAX_FDS || fd < 0)
|
||||
{
|
||||
@ -34,7 +34,8 @@ void sys_fcntl(Context* context, int fd, int command, uintptr_t arg)
|
||||
return;
|
||||
}
|
||||
Descriptor& file = current_task->files[fd];
|
||||
if (command == FNCTL_DUPFD)
|
||||
if (command == FNCTL_DUPFD) // FIXME: If arg is greater than 0, return the lowest numbered available file descriptor
|
||||
// greater than or equal to arg.
|
||||
{
|
||||
int dupfd = current_task->alloc_fd();
|
||||
if (dupfd < 0)
|
||||
|
@ -8,6 +8,9 @@
|
||||
/* Open for reading and writing. */
|
||||
#define O_RDWR 3
|
||||
|
||||
/* Duplicate a file descriptor. */
|
||||
#define F_DUPFD 0
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
@ -16,6 +19,9 @@ extern "C"
|
||||
/* Opens the file specified by pathname. Returns a file descriptor on success, or -1 on error. */
|
||||
int open(const char* pathname, int flags);
|
||||
|
||||
/* Performs an operation on the file descriptor fd determined by cmd. */
|
||||
int fcntl(int fd, int cmd, ...);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -15,6 +15,7 @@
|
||||
#define SYS_close 11
|
||||
#define SYS_seek 12
|
||||
#define SYS_exec 13
|
||||
#define SYS_fcntl 14
|
||||
|
||||
#ifndef __want_syscalls
|
||||
#ifdef __cplusplus
|
||||
|
@ -45,6 +45,9 @@ extern "C"
|
||||
/* Moves the read/write file offset for fd to offset, depending on whence. */
|
||||
off_t lseek(int fd, off_t offset, int whence);
|
||||
|
||||
/* Returns a copy of the file descriptor fd. */
|
||||
int dup(int fd);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@ -8,4 +9,18 @@ extern "C"
|
||||
{
|
||||
return (int)syscall(SYS_open, pathname, flags);
|
||||
}
|
||||
|
||||
int fcntl(int fd, int cmd, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, cmd);
|
||||
long result;
|
||||
switch (cmd)
|
||||
{
|
||||
case F_DUPFD: result = syscall(SYS_fcntl, fd, cmd, va_arg(ap, int)); break;
|
||||
default: result = syscall(SYS_fcntl, fd, cmd, 0); break;
|
||||
}
|
||||
va_end(ap);
|
||||
return (int)result;
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
#include <bits/error.h>
|
||||
#include <fcntl.h>
|
||||
#include <luna.h>
|
||||
#include <luna/syscall.h>
|
||||
#include <stdarg.h>
|
||||
@ -45,6 +46,7 @@ extern "C"
|
||||
result = __luna_syscall2(number, arg0, arg1);
|
||||
break;
|
||||
}
|
||||
case SYS_fcntl:
|
||||
case SYS_seek:
|
||||
case SYS_write:
|
||||
case SYS_read:
|
||||
@ -101,4 +103,9 @@ extern "C"
|
||||
syscall(SYS_exit, status);
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
int dup(int fd)
|
||||
{
|
||||
return fcntl(fd, F_DUPFD, 0);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user