ort_openrouter_cli/
libc.rs

1//! ort: Open Router CLI
2//! https://github.com/grahamking/ort
3//!
4//! MIT License
5//! Copyright (c) 2025 Graham King
6//!
7
8#![allow(non_camel_case_types)]
9#![allow(clippy::upper_case_acronyms)]
10
11use core::ffi::{c_char, c_int, c_long, c_uchar, c_uint, c_ushort, c_void};
12
13type c_ulong = u64;
14pub type size_t = usize;
15type ssize_t = isize;
16type clockid_t = c_int;
17type time_t = i64;
18type ino_t = u64;
19type off_t = i64;
20type dev_t = u64;
21type nlink_t = u64;
22type mode_t = u32;
23type uid_t = u32;
24type gid_t = u32;
25type blksize_t = i64;
26type blkcnt_t = i64;
27pub type pthread_t = c_ulong;
28
29pub type socklen_t = u32;
30pub type sa_family_t = u16;
31pub type in_addr_t = u32;
32pub type in_port_t = u16;
33
34pub const O_CLOEXEC: c_int = 0x80000;
35pub const O_RDONLY: c_int = 0;
36pub const O_WRONLY: c_int = 1;
37//const O_RDWR: c_int = 2;
38pub const O_CREAT: c_int = 64;
39pub const O_TRUNC: c_int = 512;
40
41pub const F_OK: i32 = 0;
42
43pub const FUTEX_WAIT: c_int = 0;
44pub const FUTEX_WAKE: c_int = 1;
45pub const SYS_FUTEX: c_long = 202; // asm/unistd_64.h __NR_futex
46
47pub const SOCK_STREAM: c_int = 1;
48pub const SOCK_CLOEXEC: c_int = O_CLOEXEC;
49pub const AF_INET: c_int = 2;
50pub const IPPROTO_TCP: i32 = 6;
51pub const TCP_FASTOPEN: i32 = 23;
52
53pub const CLOCK_MONOTONIC: clockid_t = 1;
54
55pub const DT_REG: u8 = 8;
56
57pub const PROT_NONE: c_int = 0;
58pub const PROT_READ: c_int = 1;
59pub const PROT_WRITE: c_int = 2;
60
61pub const MAP_PRIVATE: c_int = 0x0002;
62pub const MAP_ANONYMOUS: c_int = 0x0020;
63pub const MAP_STACK: c_int = 0x020000;
64
65#[repr(C)]
66#[allow(non_camel_case_types)]
67pub struct sigset_t {
68    __val: [u64; 16],
69}
70
71#[repr(C)]
72#[allow(non_camel_case_types)]
73pub struct sigaction {
74    pub sa_sigaction: usize,
75    pub sa_mask: sigset_t,
76    pub sa_flags: i32,
77    pub sa_restorer: Option<extern "C" fn()>,
78}
79
80#[repr(C)]
81pub struct in_addr {
82    pub s_addr: in_addr_t,
83}
84
85#[repr(C)]
86pub struct sockaddr_in {
87    pub sin_family: sa_family_t,
88    pub sin_port: in_port_t,
89    pub sin_addr: in_addr,
90    pub sin_zero: [u8; 8],
91}
92
93#[repr(C)]
94pub struct sockaddr {
95    pub sa_family: sa_family_t,
96    pub sa_data: [c_char; 14],
97}
98
99#[repr(C)]
100pub struct addrinfo {
101    pub ai_flags: c_int,
102    pub ai_family: c_int,
103    pub ai_socktype: c_int,
104    pub ai_protocol: c_int,
105    pub ai_addrlen: socklen_t,
106    pub ai_addr: *mut sockaddr_in,
107    pub ai_canonname: *mut c_char,
108    pub ai_next: *mut addrinfo,
109}
110
111#[repr(C)]
112pub struct timespec {
113    pub tv_sec: time_t,
114    pub tv_nsec: c_long,
115}
116
117#[repr(C)]
118pub struct dirent {
119    pub d_ino: ino_t,
120    pub d_off: off_t,
121    pub d_reclen: c_ushort,
122    pub d_type: c_uchar,
123    pub d_name: [c_char; 256],
124}
125
126#[repr(C)]
127pub struct stat {
128    pub st_dev: dev_t,
129    pub st_ino: ino_t,
130    pub st_nlink: nlink_t,
131    pub st_mode: mode_t,
132    pub st_uid: uid_t,
133    pub st_gid: gid_t,
134    __pad0: c_int,
135    pub st_rdev: dev_t,
136    pub st_size: off_t,
137    pub st_blksize: blksize_t,
138    pub st_blocks: blkcnt_t,
139    pub st_atime: time_t,
140    pub st_atime_nsec: i64,
141    pub st_mtime: time_t,
142    pub st_mtime_nsec: i64,
143    pub st_ctime: time_t,
144    pub st_ctime_nsec: i64,
145    __unused: [i64; 3],
146}
147
148#[repr(C)]
149pub struct pthread_attr_t {
150    __size: [u64; 7],
151}
152
153// Opaque C data structure, only used as pointer type
154pub enum DIR {}
155
156#[link(name = "c", kind = "dylib")]
157unsafe extern "C" {
158    pub static mut environ: *mut *mut c_char;
159
160    pub fn syscall(num: c_long, ...) -> c_long;
161
162    pub fn printf(format: *const c_char, ...) -> c_int;
163    pub fn isatty(fd: c_int) -> c_int;
164
165    pub fn read(fd: c_int, buf: *mut c_void, count: size_t) -> ssize_t;
166    pub fn write(fd: c_int, buf: *const c_void, count: size_t) -> ssize_t;
167
168    pub fn open64(path: *const c_char, oflag: c_int, ...) -> c_int;
169    pub fn open(path: *const c_char, mode: c_int) -> c_int;
170    pub fn access(path: *const c_char, mode: c_int) -> c_int;
171    pub fn close(fd: c_int) -> c_int;
172    pub fn stat(path: *const c_char, buf: *mut stat) -> c_int;
173
174    pub fn mkdir(path: *const c_char, mode: u32) -> c_int;
175    pub fn getenv(name: *const c_char) -> *const c_char;
176
177    pub fn opendir(dirname: *const c_char) -> *mut DIR;
178    pub fn readdir(dirp: *mut DIR) -> *mut dirent;
179    pub fn closedir(dirp: *mut DIR) -> c_int;
180
181    pub fn sigemptyset(set: *mut sigset_t) -> i32;
182    pub fn sigaction(signum: i32, act: *const sigaction, oldact: *mut sigaction) -> i32;
183
184    pub fn getaddrinfo(
185        node: *const c_char,
186        service: *const c_char,
187        hints: *const addrinfo,
188        res: *mut *mut addrinfo,
189    ) -> c_int;
190    pub fn freeaddrinfo(res: *mut addrinfo);
191
192    pub fn getrandom(buf: *mut c_void, buflen: usize, flags: c_uint) -> isize;
193
194    pub fn socket(domain: c_int, ty: c_int, protocol: c_int) -> c_int;
195    pub fn connect(socket: c_int, address: *const sockaddr, len: socklen_t) -> c_int;
196    pub fn setsockopt(
197        socket: c_int,
198        level: c_int,
199        name: c_int,
200        value: *const c_void,
201        option_len: socklen_t,
202    ) -> c_int;
203
204    pub fn clock_gettime(clock_id: clockid_t, tp: *mut timespec) -> c_int;
205
206    pub fn mmap(
207        addr: *mut c_void,
208        len: size_t,
209        prot: c_int,
210        flags: c_int,
211        fd: c_int,
212        offset: off_t,
213    ) -> *mut c_void;
214    pub fn mprotect(addr: *mut c_void, len: size_t, prot: c_int) -> c_int;
215
216    pub fn pthread_attr_init(attr: *mut pthread_attr_t) -> c_int;
217    pub fn pthread_attr_setstack(
218        attr: *mut pthread_attr_t,
219        stackaddr: *mut c_void,
220        stacksize: size_t,
221    ) -> c_int;
222    pub fn pthread_create(
223        native: *mut pthread_t,
224        attr: *const pthread_attr_t,
225        f: extern "C" fn(*mut c_void) -> *mut c_void,
226        value: *mut c_void,
227    ) -> c_int;
228    pub fn pthread_attr_destroy(attr: *mut pthread_attr_t) -> c_int;
229    pub fn pthread_join(native: pthread_t, value: *mut *mut c_void) -> c_int;
230
231    pub fn malloc(size: size_t) -> *mut c_void;
232    pub fn calloc(nobj: size_t, size: size_t) -> *mut c_void;
233    pub fn realloc(p: *mut c_void, size: size_t) -> *mut c_void;
234    pub fn free(p: *mut c_void);
235    pub fn abort() -> !;
236}