Adds fgetat,fgettype,fgetmod. Seperates constants to multiple files

This commit is contained in:
2025-07-27 14:11:44 +02:00
parent 5b6831a501
commit dbf2af0253
8 changed files with 320 additions and 234 deletions

View File

@@ -5,57 +5,66 @@ extern __INTERNAL_fmt
extern strlen
extern umask_get
section .rodata
; Open flags
O_ACCMODE equ 00000003q
O_RDONLY equ 00000000q
O_WRONLY equ 00000001q
O_RDWR equ 00000002q
O_CREAT equ 00000100q ;create if not exist
O_EXCL equ 00000200q ;fail if file exists and O_EXCL+O_CREAT set
O_NOCTTY equ 00000400q
O_TRUNC equ 00001000q
O_APPEND equ 00002000q
O_NONBLOCK equ 00004000q
O_DSYNC equ 00010000q
FASYNC equ 00020000q
O_DIRECT equ 00040000q
O_LARGEFILE equ 00100000q
O_DIRECTORY equ 00200000q
O_NOFOLLOW equ 00400000q
O_NOATIME equ 01000000q
O_CLOEXEC equ 02000000q
__O_SYNC equ 04000000q
O_SYNC equ (__O_SYNC | O_DSYNC)
O_PATH equ 01000000q
__O_TMPFILE equ 02000000q
O_TMPFILE equ (__O_TMPFILE | O_DIRECTORY)
O_NDELAY equ O_NONBLOCK
; Permission flags
S_IXOTH equ 00001q ;o=x
S_IWOTH equ 00002q ;o=w
S_IROTH equ 00004q ;o=r
S_IRWXO equ 00007q ;o=rwx
S_IXGRP equ 00010q ;g=x
S_IWGRP equ 00020q ;g=w
S_IRGRP equ 00040q ;g=r
S_IRWXG equ 00070q ;g=rwx
S_IXUSR equ 00100q ;u=x
S_IWUSR equ 00200q ;u=w
S_IRUSR equ 00400q ;u=r
S_IRWXU equ 00700q ;u=rwx
S_ISVIX equ 0001000q ;sticky bit
S_ISGID equ 0002000q ;set-group-ID bit
S_ISUID equ 0004000q ;set-user-ID bit
section .bss
section .text
global fstatat
global fgetmod
global fopen
global fclose
global fexist
global fwrite
;----- fstatat(*file[], *statBuffer[]) -----;
; Gets information from a file, stores it into statBuffer[]
; Return value: 0 if stats received, -errno otherwise
; Used registers:
; rax* (ret)
; rdi* (arg) Pointer to file
; rsi* (arg) Pointer to statBuffer
; rdx* arg for syscall NR_newfstatat
; r10* arg(flag) for syscall NR_newfstatat
fstatat:
sub rsp, SIZE_QWORD
mov rax, NR_newfstatat
mov rdx, rsi
mov rsi, rdi
mov rdi, AT_FDCWD
xor r10, r10
syscall
add rsp, SIZE_QWORD
ret
;----- fgettype(*statBuffer[]) -----;
; Gets file type from stat buffer
; Return value: Permissions of given file or -EINVAL if statBuffer is invalid (first bit is 0)
; Used registers:
; rax* (ret)
; rdi (arg) Pointer to statBuffer[]
fgettype:
cmp byte [rdi], 0
jne .buffok
mov rax, -EINVAL
ret
.buffok:
mov rax, [rdi + ST_MODE]
and rax, 0xF000
ret
;----- fgetmod(*statBuffer[]) -----;
; Gets file mode bits (file permissions) from stat buffer
; Return value: Permissions of given file or -EINVAL if statBuffer is invalid (first bit is 0)
; Used registers:
; rax* (ret)
; rdi (arg) Pointer to stat buffer
fgetmod:
cmp byte [rdi], 0
jne .buffok
mov rax, -EINVAL
ret
.buffok:
mov rax, [rdi + ST_MODE]
and rax, 0x0FFF
ret
;----- fopen(*file[], char mode) -----;
; Opens a file
; Return value: Pointer to opened file pointer, -errno otherwise