Skip to content

Commit e6a9dc3

Browse files
committed
Lecture 112 - Implementing FSTAT in user land
1 parent 8886c64 commit e6a9dc3

17 files changed

Lines changed: 74 additions & 5 deletions

File tree

PeachOS64Bit/programs/blank/blank.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ int main(int argc, char** argv)
88
int fd = fopen("@:/blank.elf", "r");
99
if (fd > 0)
1010
{
11-
12-
char buf[512] = {0};
11+
struct file_stat file_stat = {0};
12+
1313
printf("File blank.elf opened\n");
14-
fread(buf, 1, sizeof(buf), fd);
15-
14+
fstat(fd, &file_stat);
15+
printf("File size: %i\n", file_stat.filesize);
1616
fclose(fd);
1717
}
1818
else
552 Bytes
Binary file not shown.
408 Bytes
Binary file not shown.
344 Bytes
Binary file not shown.
648 Bytes
Binary file not shown.
48 Bytes
Binary file not shown.

PeachOS64Bit/programs/stdlib/src/file.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ int fread(void* buffer, size_t size, size_t count, long fd)
2121
int fseek(int fd, int offset, int whence)
2222
{
2323
return (int) peachos_fseek(fd, offset, whence);
24+
}
25+
26+
int fstat(int fd, struct file_stat* file_stat_out)
27+
{
28+
return (int) peachos_fstat(fd, file_stat_out);
2429
}

PeachOS64Bit/programs/stdlib/src/file.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
#include <stdint.h>
55
#include <stddef.h>
66

7+
typedef unsigned int FILE_STAT_FLAGS;
8+
struct file_stat
9+
{
10+
FILE_STAT_FLAGS flags;
11+
uint32_t filesize;
12+
};
13+
14+
int fstat(int fd, struct file_stat* file_stat_out);
715
int fopen(const char* filename, const char* mode);
816
void fclose(int fd);
917
int fread(void* buffer, size_t size, size_t count, long fd);

PeachOS64Bit/programs/stdlib/src/peachos.asm

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ global peachos_fopen:function
1515
global peachos_fclose:function
1616
global peachos_fread:function
1717
global peachos_fseek:function
18+
global peachos_fstat:function
1819

1920
; void print(const char* filename)
2021
print:
@@ -121,4 +122,13 @@ peachos_fseek:
121122
push qword rdi ; fd
122123
int 0x80 ; invokes the kernel
123124
add rsp, 24 ; restores the stack
124-
ret ; return
125+
ret ; return
126+
127+
; long peachos_fstat(long fd, struct file_stat* file_stat_out)
128+
peachos_fstat:
129+
mov rax, 14 ; Command 14 fstat
130+
push qword rsi ; file_stat_out
131+
push qword rdi ; fd
132+
int 0x80 ; call kernel
133+
add rsp, 16 ; restore stack
134+
ret

PeachOS64Bit/programs/stdlib/src/peachos.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ struct process_arguments
1616
char** argv;
1717
};
1818

19+
// Forward declare file stat.
20+
struct file_stat;
1921

2022
void print(const char* filename);
2123
int peachos_getkey();
@@ -36,4 +38,5 @@ int peachos_fopen(const char* filename, const char* mode);
3638
void peachos_fclose(size_t fd);
3739
long peachos_fread(void* buffer, size_t size, size_t count, long fd);
3840
long peachos_fseek(long fd, long offset, long whence);
41+
long peachos_fstat(long fd, struct file_stat* file_stat_out);
3942
#endif

0 commit comments

Comments
 (0)