sys_read/open/close
This commit is contained in:
+25
-18
@@ -70,10 +70,12 @@ int sys_read(unsigned int fd, char* buf, size_t count)
|
||||
return -EBADFD; // File descriptor wasn't open
|
||||
}
|
||||
// Here fd refers to a valid opened file..
|
||||
int sz = tar_read(current_process->fdt[fd].filename,&buf);
|
||||
int sz = tar_read(current_process->fdt[fd].filename, buf, count,
|
||||
current_process->fdt[fd].cursor);
|
||||
if (sz == 0) {
|
||||
return -ENOENT;
|
||||
} else {
|
||||
current_process->fdt[fd].cursor += sz;
|
||||
return sz;
|
||||
}
|
||||
}
|
||||
@@ -82,27 +84,25 @@ int sys_read(unsigned int fd, char* buf, size_t count)
|
||||
}
|
||||
|
||||
// TODO: Should have a return value: number of bytes written on success, -1 on error (errno set)
|
||||
void sys_write(unsigned int fd, const char* buf, size_t count)
|
||||
int sys_write(unsigned int fd, const char* buf, size_t count)
|
||||
{
|
||||
switch (fd) {
|
||||
case 1: //stdout
|
||||
for (size_t i=0; i<count; i++) {
|
||||
internal_putc(buf[i], NULL);
|
||||
}
|
||||
break;
|
||||
|
||||
case 2: //stderr
|
||||
for (size_t i=0; i<count; i++) {
|
||||
internal_putc(buf[i], NULL);
|
||||
}
|
||||
break;
|
||||
return count;
|
||||
default:
|
||||
return -EBADFD;
|
||||
}
|
||||
}
|
||||
|
||||
void sys_exit(int error_code)
|
||||
int sys_exit(int error_code)
|
||||
{
|
||||
current_process->status = DEAD;
|
||||
DEBUG("exiting process PID=%u name=%s", current_process->pid, current_process->name);
|
||||
DEBUG("(pid=%u, name=%s)", current_process->pid, current_process->name);
|
||||
return error_code;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -125,29 +125,36 @@ void sys_exit(int error_code)
|
||||
*/
|
||||
struct cpu_status* syscall_handler(struct cpu_status* regs)
|
||||
{
|
||||
DEBUG("Syscall %lx with (arg0=%lx arg1=%lx)", regs->rax, regs->rdi, regs->rsi);
|
||||
|
||||
switch (regs->rax)
|
||||
{
|
||||
case 0: //sys_read
|
||||
case 0:
|
||||
DEBUG("sys_read(fd=%u, buf=%p, count=%u)", regs->rdi, regs->rsi, regs->rdx);
|
||||
regs->rax = sys_read(regs->rdi, (char*)regs->rsi, regs->rdx);
|
||||
break;
|
||||
case 1: //sys_write
|
||||
sys_write(regs->rdi, (char*)regs->rsi, regs->rdx);
|
||||
case 1:
|
||||
DEBUG("sys_write(fd=%u, buf=%p, count=%u)", regs->rdi, regs->rsi, regs->rdx);
|
||||
regs->rax = sys_write(regs->rdi, (char*)regs->rsi, regs->rdx);
|
||||
break;
|
||||
case 2:
|
||||
DEBUG("sys_open(filename=%s, flags=%u)", regs->rdi, regs->rsi);
|
||||
regs->rax = sys_open((const char*)regs->rdi, regs->rsi);
|
||||
break;
|
||||
case 3:
|
||||
DEBUG("sys_close(fd=%u)", regs->rdi);
|
||||
regs->rax = sys_close(regs->rdi);
|
||||
break;
|
||||
case 60: //sys_exit
|
||||
sys_exit(regs->rdi);
|
||||
case 60:
|
||||
DEBUG("sys_exit(error_code=%d)", regs->rdi);
|
||||
regs->rax = sys_exit(regs->rdi);
|
||||
break;
|
||||
default: // bad syscall
|
||||
default:
|
||||
DEBUG("Bad syscall! (rax=%p, rdi=%p, rsi=%p, rdx=%p)",
|
||||
regs->rax, regs->rdi, regs->rsi, regs->rdx);
|
||||
regs->rax = 0xbad515ca11;
|
||||
break;
|
||||
}
|
||||
|
||||
DEBUG("returned rax=%p (%u)", regs->rax, regs->rax);
|
||||
|
||||
return regs;
|
||||
}
|
||||
+25
-7
@@ -42,7 +42,7 @@ int tar_oct2bin(unsigned char* str, int size)
|
||||
*
|
||||
* Return:
|
||||
* $filesize - size of the file, if found
|
||||
* $0 - file not found
|
||||
* $-ENOENT - file not found
|
||||
*/
|
||||
int tar_lookup(unsigned char* archive, char* filename, char** out)
|
||||
{
|
||||
@@ -56,21 +56,39 @@ int tar_lookup(unsigned char* archive, char* filename, char** out)
|
||||
}
|
||||
ptr += (((filesize + 511) / 512) + 1) * 512;
|
||||
}
|
||||
return 0;
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
/*
|
||||
* tar_read - read a file in the TAR file
|
||||
* tar_read - read a file in the TAR archive
|
||||
* @filename: file to read (absolute path)
|
||||
* @out: out buffer (if file is found)
|
||||
* @out: where to store file data if found
|
||||
* @count: amount of bytes to read
|
||||
* @offset: read from byte offset (0 for none)
|
||||
*
|
||||
* Return:
|
||||
* $filesize - size of the file, if found
|
||||
* $0 - file not found
|
||||
* $-ENOENT - file not found
|
||||
*/
|
||||
int tar_read(char* filename, char** buf)
|
||||
int tar_read(char* filename, char* out, int count, int offset)
|
||||
{
|
||||
return tar_lookup(archive_start_addr, filename, buf);
|
||||
char* file_data;
|
||||
int filesize = tar_lookup(archive_start_addr, filename, &file_data);
|
||||
|
||||
if (filesize <= 0) {
|
||||
return filesize;
|
||||
}
|
||||
|
||||
if (offset >= filesize) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
int remaining = filesize - offset;
|
||||
int to_read = remaining < count ? remaining : count;
|
||||
|
||||
memcpy(out, file_data + offset, to_read);
|
||||
|
||||
return to_read;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user