More clean ups of ERROR_NONE and != nil usage

This commit is contained in:
gingerBill
2024-08-04 11:37:49 +01:00
parent a241168142
commit 28666414bc
15 changed files with 224 additions and 297 deletions
+32 -44
View File
@@ -488,7 +488,7 @@ open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Err
if handle == -1 {
return INVALID_HANDLE, Errno(get_last_error())
}
return handle, ERROR_NONE
return handle, nil
}
close :: proc(fd: Handle) -> Errno {
@@ -509,12 +509,12 @@ read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
if bytes_read == -1 {
return -1, Errno(get_last_error())
}
return int(bytes_read), ERROR_NONE
return int(bytes_read), nil
}
write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
if len(data) == 0 {
return 0, ERROR_NONE
return 0, nil
}
to_write := min(c.size_t(len(data)), MAX_RW)
@@ -522,7 +522,7 @@ write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
if bytes_written == -1 {
return -1, Errno(get_last_error())
}
return int(bytes_written), ERROR_NONE
return int(bytes_written), nil
}
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
@@ -530,15 +530,14 @@ seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
if res == -1 {
return -1, Errno(get_last_error())
}
return res, ERROR_NONE
return res, nil
}
file_size :: proc(fd: Handle) -> (i64, Errno) {
s, err := _fstat(fd)
if err != ERROR_NONE {
return -1, err
}
return s.size, ERROR_NONE
file_size :: proc(fd: Handle) -> (size: i64, err: Errno) {
size = -1
s := _fstat(fd) or_return
size = s.size
return
}
rename :: proc(old_path, new_path: string) -> Errno {
@@ -584,7 +583,7 @@ remove_directory :: proc(path: string) -> Errno {
is_file_handle :: proc(fd: Handle) -> bool {
s, err := _fstat(fd)
if err != ERROR_NONE {
if err != nil {
return false
}
return S_ISREG(s.mode)
@@ -598,7 +597,7 @@ is_file_path :: proc(path: string, follow_links: bool = true) -> bool {
} else {
s, err = _lstat(path)
}
if err != ERROR_NONE {
if err != nil {
return false
}
return S_ISREG(s.mode)
@@ -606,7 +605,7 @@ is_file_path :: proc(path: string, follow_links: bool = true) -> bool {
is_dir_handle :: proc(fd: Handle) -> bool {
s, err := _fstat(fd)
if err != ERROR_NONE {
if err != nil {
return false
}
return S_ISDIR(s.mode)
@@ -620,7 +619,7 @@ is_dir_path :: proc(path: string, follow_links: bool = true) -> bool {
} else {
s, err = _lstat(path)
}
if err != ERROR_NONE {
if err != nil {
return false
}
return S_ISDIR(s.mode)
@@ -641,7 +640,7 @@ fcntl :: proc(fd: int, cmd: int, arg: int) -> (int, Errno) {
if result < 0 {
return 0, Errno(get_last_error())
}
return int(result), ERROR_NONE
return int(result), nil
}
// NOTE(bill): Uses startup to initialize it
@@ -650,22 +649,16 @@ stdin: Handle = 0
stdout: Handle = 1
stderr: Handle = 2
last_write_time :: proc(fd: Handle) -> (File_Time, Errno) {
s, err := _fstat(fd)
if err != ERROR_NONE {
return 0, err
}
last_write_time :: proc(fd: Handle) -> (time: File_Time, err: Errno) {
s := _fstat(fd) or_return
modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
return File_Time(modified), ERROR_NONE
return File_Time(modified), nil
}
last_write_time_by_name :: proc(name: string) -> (File_Time, Errno) {
s, err := _stat(name)
if err != ERROR_NONE {
return 0, err
}
last_write_time_by_name :: proc(name: string) -> (time: File_Time, err: Errno) {
s := _stat(name) or_return
modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
return File_Time(modified), ERROR_NONE
return File_Time(modified), nil
}
@private
@@ -677,7 +670,7 @@ _stat :: proc(path: string) -> (OS_Stat, Errno) {
if result == -1 {
return s, Errno(get_last_error())
}
return s, ERROR_NONE
return s, nil
}
@private
@@ -691,7 +684,7 @@ _lstat :: proc(path: string) -> (OS_Stat, Errno) {
if res == -1 {
return s, Errno(get_last_error())
}
return s, ERROR_NONE
return s, nil
}
@private
@@ -701,7 +694,7 @@ _fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
if result == -1 {
return s, Errno(get_last_error())
}
return s, ERROR_NONE
return s, nil
}
@private
@@ -710,7 +703,7 @@ _fdopendir :: proc(fd: Handle) -> (Dir, Errno) {
if dirp == cast(Dir)nil {
return nil, Errno(get_last_error())
}
return dirp, ERROR_NONE
return dirp, nil
}
@private
@@ -736,7 +729,7 @@ _readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool)
err = Errno(get_last_error())
return
}
err = ERROR_NONE
err = nil
if result == nil {
end_of_stream = true
@@ -764,22 +757,17 @@ _readlink :: proc(path: string) -> (string, Errno) {
delete(buf)
buf = make([]byte, bufsz)
} else {
return strings.string_from_ptr(&buf[0], rc), ERROR_NONE
return strings.string_from_ptr(&buf[0], rc), nil
}
}
return "", Errno{}
}
absolute_path_from_handle :: proc(fd: Handle) -> (string, Errno) {
absolute_path_from_handle :: proc(fd: Handle) -> (path: string, err: Errno) {
buf: [MAX_PATH]byte
_, err := fcntl(int(fd), F_GETPATH, int(uintptr(&buf[0])))
if err != ERROR_NONE {
return "", err
}
path := strings.clone_from_cstring(cstring(&buf[0]))
return path, err
_ = fcntl(int(fd), F_GETPATH, int(uintptr(&buf[0]))) or_return
return strings.clone_from_cstring(cstring(&buf[0]))
}
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
@@ -799,7 +787,7 @@ absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
path = strings.clone(string(cstring(path_ptr)))
return path, ERROR_NONE
return path, nil
}
access :: proc(path: string, mask: int) -> (bool, Errno) {
@@ -810,7 +798,7 @@ access :: proc(path: string, mask: int) -> (bool, Errno) {
if result == -1 {
return false, Errno(get_last_error())
}
return true, ERROR_NONE
return true, nil
}
lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) {