summaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
Diffstat (limited to 'std')
-rw-r--r--std/fs.zc53
-rw-r--r--std/io.zc23
-rw-r--r--std/net.zc98
-rw-r--r--std/process.zc8
-rw-r--r--std/string.zc9
-rw-r--r--std/thread.zc35
6 files changed, 136 insertions, 90 deletions
diff --git a/std/fs.zc b/std/fs.zc
index a00993b..5b2cb21 100644
--- a/std/fs.zc
+++ b/std/fs.zc
@@ -15,9 +15,9 @@ include <stdlib.h>
include <stdio.h>
// Direct externs for simple functions with const char* parameters
-extern fn access(pathname: const char*, mode: int) -> int;
-extern fn unlink(pathname: const char*) -> int;
-extern fn rmdir(pathname: const char*) -> int;
+extern fn access(pathname: const char*, mode: c_int) -> c_int;
+extern fn unlink(pathname: const char*) -> c_int;
+extern fn rmdir(pathname: const char*) -> c_int;
extern fn malloc(size: usize) -> void*;
extern fn free(ptr: void*);
@@ -90,17 +90,17 @@ raw {
}
}
-extern fn _z_fs_mkdir(path: const char*) -> int;
-extern fn _z_fs_get_metadata(path: const char*, size: U64*, is_dir: int*, is_file: int*) -> int;
-extern fn _z_fs_read_entry(dir: void*, out_name: char*, buf_size: int, is_dir: int*) -> int;
+extern fn _z_fs_mkdir(path: const char*) -> c_int;
+extern fn _z_fs_get_metadata(path: const char*, size: U64*, is_dir: c_int*, is_file: c_int*) -> c_int;
+extern fn _z_fs_read_entry(dir: void*, out_name: char*, buf_size: c_int, is_dir: c_int*) -> c_int;
extern fn _z_fs_fopen(path: const char*, mode: const char*) -> void*;
-extern fn _z_fs_fclose(stream: void*) -> int;
+extern fn _z_fs_fclose(stream: void*) -> c_int;
extern fn _z_fs_fread(ptr: void*, size: usize, nmemb: usize, stream: void*) -> usize;
extern fn _z_fs_fwrite(ptr: void*, size: usize, nmemb: usize, stream: void*) -> usize;
-extern fn _z_fs_fseek(stream: void*, offset: I64, whence: int) -> int;
+extern fn _z_fs_fseek(stream: void*, offset: I64, whence: c_int) -> c_int;
extern fn _z_fs_ftell(stream: void*) -> I64;
extern fn _z_fs_opendir(name: const char*) -> void*;
-extern fn _z_fs_closedir(dir: void*) -> int;
+extern fn _z_fs_closedir(dir: void*) -> c_int;
struct File {
@@ -191,41 +191,50 @@ impl File {
}
fn exists(path: char*) -> bool {
- return access(path, Z_F_OK) == 0;
+ let zero: c_int = 0;
+ return access(path, Z_F_OK) == zero;
}
fn metadata(path: char*) -> Result<Metadata> {
let size: uint64_t;
- let is_d: int;
- let is_f: int;
+ let is_d: c_int;
+ let is_f: c_int;
- if (_z_fs_get_metadata(path, &size, &is_d, &is_f) != 0) {
+ let res = _z_fs_get_metadata(path, &size, &is_d, &is_f);
+ let non_zero: c_int = 0;
+ if (res != non_zero) {
return Result<Metadata>::Err("Failed to get metadata");
}
return Result<Metadata>::Ok(Metadata {
size: (U64)size,
- is_dir: is_d != 0,
- is_file: is_f != 0
+ is_dir: is_d != non_zero,
+ is_file: is_f != non_zero
});
}
fn create_dir(path: char*) -> Result<bool> {
- if (_z_fs_mkdir(path) != 0) {
+ let res = _z_fs_mkdir(path);
+ let zero: c_int = 0;
+ if (res != zero) {
return Result<bool>::Err("Failed to create directory");
}
return Result<bool>::Ok(true);
}
fn remove_file(path: char*) -> Result<bool> {
- if (unlink(path) != 0) {
+ let res = unlink(path);
+ let zero: c_int = 0;
+ if (res != zero) {
return Result<bool>::Err("Failed to remove file");
}
return Result<bool>::Ok(true);
}
fn remove_dir(path: char*) -> Result<bool> {
- if (rmdir(path) != 0) {
+ let res = rmdir(path);
+ let zero: c_int = 0;
+ if (res != zero) {
return Result<bool>::Err("Failed to remove directory");
}
return Result<bool>::Ok(true);
@@ -245,17 +254,19 @@ impl File {
return Result< Vec<DirEntry> >::Err("Out of memory");
}
- let is_d: int = 0;
+ let is_d: c_int = 0;
+ let is_d_zero: c_int = 0;
while (_z_fs_read_entry(dir, name_buf, 256, &is_d)) {
- if (strcmp(name_buf, ".") == 0 || strcmp(name_buf, "..") == 0) {
+ let zero_cmp: c_int = 0;
+ if (strcmp(name_buf, ".") == zero_cmp || strcmp(name_buf, "..") == zero_cmp) {
continue;
}
let s = String::new(name_buf);
let ent = DirEntry {
name: s,
- is_dir: is_d != 0
+ is_dir: is_d != is_d_zero
};
// Transfer ownership: String -> DirEntry
diff --git a/std/io.zc b/std/io.zc
index a5a7359..d9829dd 100644
--- a/std/io.zc
+++ b/std/io.zc
@@ -6,11 +6,11 @@ include <stdio.h>
include <stdarg.h>
// These work directly with const char* in extern declarations
-extern fn vprintf(fmt: const char*, ap: va_list) -> int;
+extern fn vprintf(fmt: const char*, ap: va_list) -> c_int;
// vsnprintf is problematic on macOS because it's a macro that expands to a builtin with a different signature
// so we wrap it in a C function to avoid the conflict
-extern fn _z_vsnprintf(str: char*, size: usize, fmt: const char*, ap: va_list) -> int;
+extern fn _z_vsnprintf(str: char*, size: usize, fmt: const char*, ap: va_list) -> c_int;
// EOF is typically -1, but we define it for portability
def Z_EOF = -1;
@@ -27,7 +27,7 @@ raw {
}
extern fn _z_get_stdin() -> void*;
-extern fn _z_fgetc(stream: void*) -> int;
+extern fn _z_fgetc(stream: void*) -> c_int;
fn format(fmt: char*, ...) -> char* {
static let buffer: char[1024];
@@ -40,7 +40,7 @@ fn format(fmt: char*, ...) -> char* {
return (char*)buffer;
}
-fn format_into(buffer: char*, size: usize, fmt: char*, ...) -> int {
+fn format_into(buffer: char*, size: usize, fmt: char*, ...) -> c_int {
let ap: va_list;
va_start(ap, fmt);
@@ -63,7 +63,7 @@ fn format_new(fmt: char*, ...) -> char* {
return buffer;
}
-fn print(fmt: char*, ...) -> int {
+fn print(fmt: char*, ...) -> c_int {
let ap: va_list;
va_start(ap, fmt);
let ret = vprintf(fmt, ap);
@@ -71,7 +71,7 @@ fn print(fmt: char*, ...) -> int {
return ret;
}
-fn println(fmt: char*, ...) -> int {
+fn println(fmt: char*, ...) -> c_int {
let ap: va_list;
va_start(ap, fmt);
let ret = vprintf(fmt, ap);
@@ -86,13 +86,15 @@ fn readln() -> char* {
let line: char* = malloc(cap);
if (line == NULL) return NULL;
- let c: int;
+ let c: c_int;
let std_in = _z_get_stdin();
while (true) {
c = _z_fgetc(std_in);
- if (c == Z_EOF) break;
- if (c == 10) break; // '\n'
+ let eof_c: c_int = Z_EOF;
+ if (c == eof_c) break;
+ let nl_c: c_int = 10;
+ if (c == nl_c) break; // '\n'
if (len + 1 >= cap) {
cap = cap * 2;
@@ -108,7 +110,8 @@ fn readln() -> char* {
len = len + 1;
}
- if (len == 0 && c == Z_EOF) {
+ let eof_final: c_int = Z_EOF;
+ if (len == 0 && c == eof_final) {
free(line);
return NULL;
}
diff --git a/std/net.zc b/std/net.zc
index 826b795..dce1a01 100644
--- a/std/net.zc
+++ b/std/net.zc
@@ -14,9 +14,9 @@ def Z_AF_INET = 2;
def Z_SOCK_STREAM = 1;
// Direct externs for simple socket functions
-extern fn socket(domain: int, type: int, proto: int) -> int;
-extern fn close(fd: int) -> int;
-extern fn read(fd: int, buf: void*, count: usize) -> isize;
+extern fn socket(domain: c_int, type: c_int, proto: c_int) -> c_int;
+extern fn close(fd: c_int) -> c_int;
+extern fn read(fd: c_int, buf: void*, count: usize) -> isize;
// Minimal raw block: required for struct sockaddr_in usage
// These functions encapsulate sockaddr_in setup because the struct layout
@@ -56,47 +56,63 @@ raw {
}
}
-extern fn _z_net_bind(fd: int, host: const char*, port: int) -> int;
-extern fn _z_net_connect(fd: int, host: const char*, port: int) -> int;
-extern fn _z_net_accept(fd: int) -> int;
-extern fn _z_net_write(fd: int, buf: const char*, n: usize) -> isize;
+extern fn _z_net_bind(fd: c_int, host: const char*, port: c_int) -> c_int;
+extern fn _z_net_connect(fd: c_int, host: const char*, port: c_int) -> c_int;
+extern fn _z_net_accept(fd: c_int) -> c_int;
+extern fn _z_net_write(fd: c_int, buf: const char*, n: usize) -> isize;
struct TcpStream {
- handle: int;
+ handle: c_int;
}
-extern fn strerror(errnum: int) -> char*;
+extern fn strerror(errnum: c_int) -> char*;
impl TcpStream {
fn read(self, buf: char*, len: usize) -> Result<usize> {
let n = read(self.handle - 1, (void*)buf, len);
- if (n < 0) return Result<usize>::Err(strerror(errno));
+ let zero: c_int = 0;
+ if (n < (isize)zero) return Result<usize>::Err(strerror(errno));
return Result<usize>::Ok((usize)n);
}
- fn write(self, buf: char*, len: usize) -> Result<usize> {
- let n = _z_net_write(self.handle - 1, buf, len);
- if (n < 0) return Result<usize>::Err("Write failed");
+ fn write(self, buf: u8*, len: usize) -> Result<usize> {
+ let one: c_int = 1;
+ let n: isize = _z_net_write(self.handle - one, buf, len);
+ let zero: isize = 0;
+ if (n < zero) return Result<usize>::Err("Write failed");
return Result<usize>::Ok((usize)n);
}
fn close(self) {
- if (self.handle > 0) {
- close(self.handle - 1);
- self.handle = 0;
+ let zero: c_int = 0;
+ if (self.handle > zero) {
+ let one: c_int = 1;
+ close(self.handle - one);
+ self.handle = zero;
}
}
- fn connect(host: char*, port: int) -> Result<TcpStream> {
- let fd = socket(Z_AF_INET, Z_SOCK_STREAM, 0);
- if (fd < 0) return Result<TcpStream>::Err("Failed to create socket");
+ fn connect(host: char*, port: c_int) -> Result<TcpStream> {
+ let zero: c_int = 0;
+ let fd = socket(Z_AF_INET, Z_SOCK_STREAM, zero);
+ if (fd < zero) return Result<TcpStream>::Err("Failed to create socket");
+
+ // C constants like -1
+ let neg_one: c_int = -1;
let res = _z_net_connect(fd, host, port);
- if (res == -1) { close(fd); return Result<TcpStream>::Err("Invalid address"); }
- if (res == -2) { close(fd); return Result<TcpStream>::Err("Connection failed"); }
+ if (res == neg_one) { close(fd); return Result<TcpStream>::Err("Invalid address"); }
+ // _z_net_connect might return -2? The original code had it.
+ // Assuming -2 is also possible... check implementation or just assume logic was correct.
+ // But wait, the original code had:
+ // if (res == -1) ... if (res == -2) ...
+ // I will keep it but cast strict.
+ let neg_two: c_int = -2;
+ if (res == neg_two) { close(fd); return Result<TcpStream>::Err("Connection failed"); }
- return Result<TcpStream>::Ok(TcpStream { handle: fd + 1 });
+ let one: c_int = 1;
+ return Result<TcpStream>::Ok(TcpStream { handle: fd + one });
}
}
@@ -107,32 +123,42 @@ impl Drop for TcpStream {
}
struct TcpListener {
- handle: int;
+ handle: c_int;
}
impl TcpListener {
- fn bind(host: char*, port: int) -> Result<TcpListener> {
- let fd = socket(Z_AF_INET, Z_SOCK_STREAM, 0);
- if (fd < 0) return Result<TcpListener>::Err("Failed to create socket");
+ fn bind(host: char*, port: c_int) -> Result<TcpListener> {
+ let zero: c_int = 0;
+ let fd = socket(Z_AF_INET, Z_SOCK_STREAM, zero);
+ if (fd < zero) return Result<TcpListener>::Err("Failed to create socket");
let res = _z_net_bind(fd, host, port);
- if (res == -1) { close(fd); return Result<TcpListener>::Err("Invalid address"); }
- if (res == -2) { close(fd); return Result<TcpListener>::Err("Bind failed"); }
- if (res == -3) { close(fd); return Result<TcpListener>::Err("Listen failed"); }
+ let neg_one: c_int = -1;
+ let neg_two: c_int = -2;
+ let neg_three: c_int = -3;
+
+ if (res == neg_one) { close(fd); return Result<TcpListener>::Err("Invalid address"); }
+ if (res == neg_two) { close(fd); return Result<TcpListener>::Err("Bind failed"); }
+ if (res == neg_three) { close(fd); return Result<TcpListener>::Err("Listen failed"); }
- return Result<TcpListener>::Ok(TcpListener { handle: fd + 1 });
+ let one: c_int = 1;
+ return Result<TcpListener>::Ok(TcpListener { handle: fd + one });
}
fn accept(self) -> Result<TcpStream> {
- let client_fd = _z_net_accept(self.handle - 1);
- if (client_fd < 0) return Result<TcpStream>::Err("Accept failed");
- return Result<TcpStream>::Ok(TcpStream { handle: client_fd + 1 });
+ let one: c_int = 1;
+ let client_fd = _z_net_accept(self.handle - one);
+ let zero: c_int = 0;
+ if (client_fd < zero) return Result<TcpStream>::Err("Accept failed");
+ return Result<TcpStream>::Ok(TcpStream { handle: client_fd + one });
}
fn close(self) {
- if (self.handle > 0) {
- close(self.handle - 1);
- self.handle = 0;
+ let zero: c_int = 0;
+ if (self.handle > zero) {
+ let one: c_int = 1;
+ close(self.handle - one);
+ self.handle = zero;
}
}
}
diff --git a/std/process.zc b/std/process.zc
index 3ce43b6..9f432c0 100644
--- a/std/process.zc
+++ b/std/process.zc
@@ -9,7 +9,7 @@ include <stdio.h>
include <stdlib.h>
// system() can be externed directly with const char*
-extern fn system(command: const char*) -> int;
+extern fn system(command: const char*) -> c_int;
// Minimal raw block: only for opaque FILE* types
// popen/pclose/fgets use FILE* which conflicts with void*
@@ -28,8 +28,8 @@ raw {
}
extern fn _z_popen(command: const char*, type: const char*) -> void*;
-extern fn _z_pclose(stream: void*) -> int;
-extern fn _z_fgets(s: char*, size: int, stream: void*) -> char*;
+extern fn _z_pclose(stream: void*) -> c_int;
+extern fn _z_fgets(s: char*, size: c_int, stream: void*) -> char*;
struct Output {
stdout: String;
@@ -88,7 +88,7 @@ impl Command {
let buf = (char*)malloc(buf_size);
while (true) {
- let res = _z_fgets(buf, (int)buf_size, fp);
+ let res = _z_fgets(buf, (c_int)buf_size, fp);
if (res == 0) break;
let chunk = String::from(buf);
diff --git a/std/string.zc b/std/string.zc
index 0bc9539..54f11b2 100644
--- a/std/string.zc
+++ b/std/string.zc
@@ -90,7 +90,8 @@ impl String {
}
fn eq(self, other: String*) -> bool {
- return strcmp(self.c_str(), (*other).c_str()) == 0;
+ let zero: c_int = 0;
+ return strcmp(self.c_str(), (*other).c_str()) == zero;
}
fn length(self) -> usize {
@@ -146,7 +147,8 @@ impl String {
fn starts_with(self, prefix: char*) -> bool {
let plen = strlen(prefix);
if plen > self.length() { return false; }
- return strncmp(self.c_str(), prefix, plen) == 0;
+ let zero: c_int = 0;
+ return strncmp(self.c_str(), prefix, plen) == zero;
}
fn ends_with(self, suffix: char*) -> bool {
@@ -154,7 +156,8 @@ impl String {
let len = self.length();
if slen > len { return false; }
let offset = (int)(len - slen);
- return strcmp(self.c_str() + offset, suffix) == 0;
+ let zero: c_int = 0;
+ return strcmp(self.c_str() + offset, suffix) == zero;
}
fn free(self) {
diff --git a/std/thread.zc b/std/thread.zc
index 16f3ca1..78d2547 100644
--- a/std/thread.zc
+++ b/std/thread.zc
@@ -35,11 +35,11 @@ raw {
if (ret == 0) {
*out_handle = (size_t)pt;
}
- return ret;
+ return (int)ret;
}
static int _z_thread_join(void *handle) {
- return pthread_join((pthread_t)handle, NULL);
+ return (int)pthread_join((pthread_t)handle, NULL);
}
static void _z_mutex_init(void *ptr) {
@@ -63,13 +63,13 @@ raw {
}
}
-extern fn _z_thread_spawn(ctx: void*, out: usize*) -> int;
-extern fn _z_thread_join(handle: void*) -> int;
+extern fn _z_thread_spawn(ctx: void*, out: usize*) -> c_int;
+extern fn _z_thread_join(handle: void*) -> c_int;
extern fn _z_mutex_init(ptr: void*);
extern fn _z_mutex_lock(ptr: void*);
extern fn _z_mutex_unlock(ptr: void*);
extern fn _z_mutex_destroy(ptr: void*);
-extern fn _z_usleep(micros: int);
+extern fn _z_usleep(micros: c_int);
@@ -79,26 +79,28 @@ struct Thread {
impl Thread {
fn spawn(func: fn()) -> Result<Thread> {
- let t: usize = 0;
+ let out_handle: usize = 0;
- let ctx_copy = malloc(16); // z_closure_T is 16 bytes
- if (ctx_copy == NULL) return Result<Thread>::Err("OOM");
+ let ctx = malloc(16); // z_closure_T is 16 bytes
+ if (ctx == NULL) return Result<Thread>::Err("OOM");
- memcpy(ctx_copy, &func, 16);
+ memcpy(ctx, &func, 16);
- let ret = _z_thread_spawn(ctx_copy, &t);
-
- if (ret != 0) {
- free(ctx_copy);
+ let ret = _z_thread_spawn(ctx, &out_handle);
+ let zero: c_int = 0;
+ if (ret != zero) {
+ // Failed to spawn
+ free(ctx);
return Result<Thread>::Err("Failed to create thread");
}
- return Result<Thread>::Ok(Thread { handle: (void*)t });
+ return Result<Thread>::Ok(Thread { handle: (void*)out_handle });
}
fn join(self) -> Result<bool> {
let ret = _z_thread_join(self.handle);
- if (ret != 0) return Result<bool>::Err("Join failed");
+ let zero: c_int = 0;
+ if (ret != zero) return Result<bool>::Err("Join failed");
return Result<bool>::Ok(true);
}
}
@@ -138,5 +140,6 @@ impl Drop for Mutex {
}
fn sleep_ms(ms: int) {
- _z_usleep(ms * 1000);
+ let micros: c_int = (c_int)(ms * 1000);
+ _z_usleep(micros);
}