Skip to content

Commit

Permalink
Add app/cat
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalium committed Sep 24, 2021
1 parent 32e2781 commit 9eca435
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 14 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ files : src/BOOTX64.EFI src/LIUMOS.ELF .FORCE
cp src/LIUMOS.ELF mnt/LIUMOS.ELF
mkdir -p mnt/EFI/EFI/
echo 'FS0:\\EFI\\BOOT\\BOOTX64.EFI' > mnt/startup.nsh
cp README.md mnt/

.PHONY : internal_run_loader_test

Expand Down
1 change: 1 addition & 0 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ APPS=\
argstest/argstest.bin \
browser-rs/browser-rs.bin \
browser/browser.bin \
cat/cat.bin \
dig/dig.bin \
fizzbuzz/fizzbuzz.bin \
guitest/guitest.bin \
Expand Down
7 changes: 7 additions & 0 deletions app/cat/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
NAME=cat
TARGET=$(NAME).bin
TARGET_OBJS=$(NAME).o

default: $(TARGET)

include ../liumlib/common.mk
20 changes: 20 additions & 0 deletions app/cat/cat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "../liumlib/liumlib.h"

int main(int argc, char** argv) {
// Concat
if (argc < 2) {
Print("Usage: ");
Print(argv[0]);
Print(" <file> ...\n");
exit(EXIT_FAILURE);
}
int fd = open(argv[1], 0, 0);
if(fd < 0) {
Print("Failed to open a file\n");
exit(EXIT_FAILURE);
}
char buf[1];
while(read(fd, buf, sizeof(buf)) == 1) {
putchar(buf[0]);
}
}
7 changes: 7 additions & 0 deletions app/liumlib/liumlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ static inline int puts(const char *s) {
Println(s);
return strlen(s);
}
static inline int putchar(char c) {
char s[2];
s[0] = c;
s[1] = 0;
Print(s);
return 1;
}


#endif /* GRANDPARENT_H */
80 changes: 66 additions & 14 deletions src/syscall.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ typedef uint32_t socklen_t;
struct PerProcessSyscallData {
Sheet* window_sheet;
int num_getdents64_called;
// for opening normal file: fd = 6
int idx_in_root_files;
uint64_t read_offset;
};

std::unordered_map<Process::PID, PerProcessSyscallData>
Expand Down Expand Up @@ -249,19 +252,38 @@ static int sys_bind(int sockfd, sockaddr_in* addr, socklen_t addrlen) {
}

static ssize_t sys_read(int fd, void* buf, size_t count) {
if (fd != 0) {
kprintf("%s: fd %d is not supported yet: only stdin is supported now.\n",
__func__, fd);
return ErrorNumber::kInvalid;
}
if (count < 1)
return ErrorNumber::kInvalid;
auto& proc_stdin = liumos->scheduler->GetCurrentProcess().GetStdIn();
while (proc_stdin.IsEmpty()) {
StoreIntFlagAndHalt();
if (fd == 0) {
if (count < 1)
return ErrorNumber::kInvalid;
auto& proc_stdin = liumos->scheduler->GetCurrentProcess().GetStdIn();
while (proc_stdin.IsEmpty()) {
StoreIntFlagAndHalt();
}
reinterpret_cast<uint8_t*>(buf)[0] = proc_stdin.Pop();
return 1;
}
if (fd == 6) {
auto pid = liumos->scheduler->GetCurrentProcess().GetID();
auto& ppdata = per_process_syscall_data[pid];
LoaderInfo& loader_info = *reinterpret_cast<LoaderInfo*>(
reinterpret_cast<uint64_t>(&GetLoaderInfo()) +
GetKernelStraightMappingBase());
EFIFile& file = loader_info.root_files[ppdata.idx_in_root_files];
uint64_t file_size = file.GetFileSize();
const uint8_t* src = reinterpret_cast<const uint8_t*>(
reinterpret_cast<uint64_t>(file.GetBuf()) +
GetKernelStraightMappingBase());
assert(file_size >= ppdata.read_offset);
uint64_t copy_size = std::min(file_size - ppdata.read_offset, count);
if (copy_size) {
memcpy(buf, src + ppdata.read_offset, copy_size);
ppdata.read_offset += copy_size;
}
return copy_size;
}
reinterpret_cast<uint8_t*>(buf)[0] = proc_stdin.Pop();
return 1;
kprintf("%s: fd %d is not supported yet: only stdin is supported now.\n",
__func__, fd);
return ErrorNumber::kInvalid;
}

static std::optional<Network::EtherAddr> ResolveIPv4WithTimeout(
Expand Down Expand Up @@ -453,9 +475,39 @@ __attribute__((ms_abi)) extern "C" void SyscallHandler(uint64_t* args) {
if (idx == kSyscallIndex_sys_open) {
auto pid = liumos->scheduler->GetCurrentProcess().GetID();
auto& ppdata = per_process_syscall_data[pid];
ppdata.num_getdents64_called = 0;

args[0] = 5;
const char* file_name = reinterpret_cast<const char*>(args[1]);
kprintf("file name: %s\n", file_name);

if (IsEqualString(".", file_name)) {
// for getdents64
ppdata.num_getdents64_called = 0;
args[0] = 5;
return;
}

int found_idx = -1;

LoaderInfo& loader_info = *reinterpret_cast<LoaderInfo*>(
reinterpret_cast<uint64_t>(&GetLoaderInfo()) +
GetKernelStraightMappingBase());
kprintf("&GetLoaderInfo(): %p\n", &GetLoaderInfo());
kprintf("&loader_info: %p\n", &loader_info);
for (int i = 0; i < loader_info.root_files_used; i++) {
if (IsEqualString(loader_info.root_files[i].GetFileName(), file_name)) {
found_idx = i;
break;
}
}
if (found_idx == -1) {
kprintf("not found!\n");
*((int64_t*)&args[0]) = -1;
return;
}
kprintf("found at index = %d!\n", found_idx);
ppdata.idx_in_root_files = found_idx;
ppdata.read_offset = 0;
args[0] = 6;
return;
}
if (idx == kSyscallIndex_sys_close) {
Expand Down

0 comments on commit 9eca435

Please sign in to comment.