elibc 0.2.0

edos kernel libc
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
use alloc::{
    string::{String, ToString},
    vec::Vec,
};
use core::{fmt, slice};
use spin::Mutex;
use thiserror::Error;

use crate::{
    sys::{Errno, SYS_IOCTL, SYS_POLL, errno, syscall3, syscall5},
    sys_open as raw_sys_open, sys_read, sys_write,
};

/// I/O Error type with proper error handling
#[derive(Debug, Error, Clone, Copy)]
pub enum IoError {
    #[error("Invalid argument")]
    InvalidInput,
    #[error("Out of memory")]
    OutOfMemory,
    #[error("Bad address/fault")]
    Fault,
    #[error("Unknown error")]
    Unknown,
    #[error("Interrupted")]
    Interrupted,
}

impl From<Errno> for IoError {
    fn from(errno: Errno) -> Self {
        match errno {
            Errno::EINVAL => IoError::InvalidInput,
            Errno::ENOMEM => IoError::OutOfMemory,
            Errno::EFAULT => IoError::Fault,
            Errno::UNKNOWN => IoError::Unknown,
            Errno::Clear => IoError::Unknown, // Shouldn't happen but handle gracefully
            _ => IoError::Unknown,
        }
    }
}

pub type IoResult<T> = Result<T, IoError>;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileType {
    File = 0,
    Directory = 1,
    Symlink = 2,
    Special = 3,
}

impl From<u8> for FileType {
    fn from(value: u8) -> Self {
        match value {
            0 => FileType::File,
            1 => FileType::Directory,
            2 => FileType::Symlink,
            _ => FileType::Special,
        }
    }
}

#[repr(C)]
#[derive(Debug, Clone)]
pub struct DirEntry {
    pub name: String,
    pub file_type: FileType,
    pub size: u64,
    pub attrs: u8,
}

#[repr(C)]
#[derive(Debug, Clone, Copy)]
struct RawDirEntry {
    name_len: u32,
    file_type: u8,
    size: u64,
    attrs: u8,
    reserved: [u8; 2],
}

#[repr(C)]
#[derive(Debug, Clone, Copy, Default)]
pub struct PollState {
    pub readable: bool,
    pub writable: bool,
    pub error: bool,
}

#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct PollFd {
    pub fd: u64,
    pub interests: PollState,
    pub result: PollState,
}

impl PollFd {
    pub fn new(fd: u64, interests: PollState) -> Self {
        Self {
            fd,
            interests,
            result: PollState::default(),
        }
    }
}

impl Default for PollFd {
    fn default() -> Self {
        Self {
            fd: 0,
            interests: PollState::default(),
            result: PollState::default(),
        }
    }
}

/// Helper function to write all bytes to a file descriptor, handling partial writes
fn write_all_to_fd(fd: u64, mut buf: &[u8]) -> IoResult<()> {
    while !buf.is_empty() {
        let result = unsafe { sys_write(fd, buf.as_ptr(), buf.len()) };
        match result {
            n if n > 0 => {
                // Partial or complete write - advance buffer
                buf = &buf[n as usize..];
            }
            0 => {
                // Zero bytes written - this shouldn't normally happen for stdout/stderr
                // but treat as retriable error
                return Err(IoError::Interrupted);
            }
            _ => {
                // Negative result indicates an error
                let errno = errno();
                return Err(IoError::from(errno));
            }
        }
    }
    Ok(())
}

/// Thread-safe standard output
pub struct Stdout {
    _private: (),
}

impl Stdout {
    const fn new() -> Self {
        Self { _private: () }
    }

    /// Write all bytes to stdout, ensuring complete write or error
    pub fn write_all(&self, buf: &[u8]) -> IoResult<()> {
        write_all_to_fd(1, buf)
    }

    /// Write formatted arguments to stdout
    pub fn write_fmt(&self, args: fmt::Arguments<'_>) -> IoResult<()> {
        struct WriteAdapter<'a> {
            stdout: &'a Stdout,
            error: Option<IoError>,
        }

        impl<'a> fmt::Write for WriteAdapter<'a> {
            fn write_str(&mut self, s: &str) -> fmt::Result {
                match self.stdout.write_all(s.as_bytes()) {
                    Ok(()) => Ok(()),
                    Err(e) => {
                        self.error = Some(e);
                        Err(fmt::Error)
                    }
                }
            }
        }

        let mut adapter = WriteAdapter {
            stdout: self,
            error: None,
        };

        match fmt::write(&mut adapter, args) {
            Ok(()) => Ok(()),
            Err(_) => Err(adapter.error.unwrap_or(IoError::Unknown)),
        }
    }
}

/// Thread-safe standard error
pub struct Stderr {
    _private: (),
}

impl Stderr {
    const fn new() -> Self {
        Self { _private: () }
    }

    /// Write all bytes to stderr, ensuring complete write or error
    pub fn write_all(&self, buf: &[u8]) -> IoResult<()> {
        write_all_to_fd(2, buf)
    }

    /// Write formatted arguments to stderr
    pub fn write_fmt(&self, args: fmt::Arguments<'_>) -> IoResult<()> {
        struct WriteAdapter<'a> {
            stderr: &'a Stderr,
            error: Option<IoError>,
        }

        impl<'a> fmt::Write for WriteAdapter<'a> {
            fn write_str(&mut self, s: &str) -> fmt::Result {
                match self.stderr.write_all(s.as_bytes()) {
                    Ok(()) => Ok(()),
                    Err(e) => {
                        self.error = Some(e);
                        Err(fmt::Error)
                    }
                }
            }
        }

        let mut adapter = WriteAdapter {
            stderr: self,
            error: None,
        };

        match fmt::write(&mut adapter, args) {
            Ok(()) => Ok(()),
            Err(_) => Err(adapter.error.unwrap_or(IoError::Unknown)),
        }
    }
}

/// Global thread-safe stdout instance
pub static STDOUT: Mutex<Stdout> = Mutex::new(Stdout::new());

/// Global thread-safe stderr instance
pub static STDERR: Mutex<Stderr> = Mutex::new(Stderr::new());

/// Read from a file descriptor into buffer
/// Returns number of bytes read on success
pub fn read_from_fd(fd: u64, buf: &mut [u8]) -> IoResult<usize> {
    if buf.is_empty() {
        return Ok(0);
    }

    let result = unsafe { sys_read(fd, buf.as_mut_ptr(), buf.len()) };
    if result < 0 {
        let errno = errno();
        Err(IoError::from(errno))
    } else {
        Ok(result as usize)
    }
}

/// Read from stdin into buffer
/// Returns number of bytes read on success
pub fn read_stdin(buf: &mut [u8]) -> IoResult<usize> {
    read_from_fd(0, buf)
}

#[derive(Debug, Clone, Copy)]
pub enum KeyEvent {
    Unicode(char),
    RawScancode(u8),
}

static KEYBOARD_FD: Mutex<Option<u64>> = Mutex::new(None);

pub fn keyboard_fd() -> Option<u64> {
    let mut fd_guard = KEYBOARD_FD.lock();
    if let Some(fd) = *fd_guard {
        return Some(fd);
    }

    match open("/dev/kbd", 0) {
        Ok(fd) => {
            *fd_guard = Some(fd);
            Some(fd)
        }
        Err(_) => None,
    }
}

fn release_keyboard_fd(fd: u64) {
    let mut fd_guard = KEYBOARD_FD.lock();
    if fd_guard
        .as_ref()
        .map(|stored| *stored == fd)
        .unwrap_or(false)
        && let Some(fd) = fd_guard.take()
    {
        let _ = crate::sys_close(fd);
    }
}

fn read_keyboard_events(
    fd: u64,
    max_events: usize,
    out_buf: &mut Vec<KeyEvent>,
) -> IoResult<usize> {
    if max_events == 0 {
        return Ok(0);
    }

    let bytes_to_read = max_events
        .checked_mul(core::mem::size_of::<u32>())
        .ok_or(IoError::InvalidInput)?;

    let mut buffer = alloc::vec![0u8; bytes_to_read];
    let bytes_read = read_from_fd(fd, &mut buffer)?;

    if bytes_read == 0 {
        return Ok(0);
    }

    let mut appended = 0;
    for chunk in buffer[..bytes_read].chunks_exact(core::mem::size_of::<u32>()) {
        if appended >= max_events {
            break;
        }

        let value = u32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]);
        out_buf.push(decode_key_event(value));
        appended += 1;
    }

    Ok(appended)
}

fn decode_key_event(value: u32) -> KeyEvent {
    if value & 0x8000_0000 != 0 {
        let scancode = (value & 0x7FFF_FFFF) as u8;
        KeyEvent::RawScancode(scancode)
    } else {
        let ch = char::from_u32(value).unwrap_or('\0');
        KeyEvent::Unicode(ch)
    }
}

/// Try to get `count` key events. This may not fill up all the buffer, usually just returns one.
pub fn get_raw_input(timeout_ms: u64, out_buf: &mut Vec<KeyEvent>, count: usize) {
    if count == 0 {
        return;
    }

    let Some(fd) = keyboard_fd() else {
        return;
    };

    let mut poll_entry = PollFd::new(
        fd,
        PollState {
            readable: true,
            writable: false,
            error: true,
        },
    );

    match poll_fds(slice::from_mut(&mut poll_entry), timeout_ms) {
        Ok(ready) => {
            if ready == 0 || !poll_entry.result.readable {
                return;
            }
        }
        Err(_) => {
            release_keyboard_fd(fd);
            return;
        }
    }

    if read_keyboard_events(fd, count, out_buf).is_err() {
        release_keyboard_fd(fd);
    }
}

/// Public helper to write all bytes to a file descriptor
pub fn write_all_fd(fd: u64, buf: &[u8]) -> IoResult<()> {
    write_all_to_fd(fd, buf)
}

/// Issue a device-specific ioctl on a file descriptor.
pub fn ioctl(fd: u64, request: u64, arg: u64, arg_len: usize, flags: u64) -> IoResult<u64> {
    let result = unsafe { syscall5(SYS_IOCTL, fd, request, arg, arg_len as u64, flags) as isize };
    if result >= 0 {
        Ok(result as u64)
    } else {
        Err(IoError::from(errno()))
    }
}

/// Query poll state for a descriptor; `timeout_ms` currently advisory.
pub fn poll_fd(fd: u64, timeout_ms: u64) -> IoResult<PollState> {
    let mut entry = PollFd::new(
        fd,
        PollState {
            readable: true,
            writable: true,
            error: true,
        },
    );

    poll_fds(slice::from_mut(&mut entry), timeout_ms)?;
    Ok(entry.result)
}

/// Poll multiple descriptors, returning the number that satisfied their interests.
pub fn poll_fds(entries: &mut [PollFd], timeout_ms: u64) -> IoResult<usize> {
    let (ptr, len) = if entries.is_empty() {
        (core::ptr::null_mut::<PollFd>(), 0)
    } else {
        (entries.as_mut_ptr(), entries.len())
    };

    let result = unsafe { syscall3(SYS_POLL, ptr as u64, len as u64, timeout_ms) as isize };

    if result >= 0 {
        Ok(result as usize)
    } else {
        Err(IoError::from(errno()))
    }
}

/// File open flags
pub mod open_flags {
    /// Append writes to end of file
    pub const O_APPEND: u64 = 0x400;
    /// Create file if it does not exist
    pub const O_CREAT: u64 = 0x40;
}

/// Open a file by path with flags. Returns an fd.
pub fn open(path: &str, flags: u64) -> IoResult<u64> {
    // Build a C string (nul-terminated) in a scratch buffer
    let mut bytes = alloc::vec::Vec::with_capacity(path.len() + 1);
    bytes.extend_from_slice(path.as_bytes());
    bytes.push(0);

    let fd = unsafe { raw_sys_open(bytes.as_ptr(), flags) };
    if fd < 0 {
        Err(IoError::from(errno()))
    } else {
        Ok(fd as u64)
    }
}

/// Read fd until EOF (or up to max_bytes if provided Some)
pub fn read_to_end(fd: u64, max_bytes: Option<usize>) -> IoResult<Vec<u8>> {
    let mut out = Vec::new();
    let mut buf = [0u8; 1024];
    loop {
        let n = read_from_fd(fd, &mut buf)?;
        if n == 0 {
            break;
        }
        out.extend_from_slice(&buf[..n]);
        if let Some(max) = max_bytes
            && out.len() >= max
        {
            break;
        }
    }
    Ok(out)
}

/// List directory contents
pub fn list_dir(path: &str) -> IoResult<Vec<DirEntry>> {
    // Build a C string (nul-terminated) in a scratch buffer
    let mut path_bytes = alloc::vec::Vec::with_capacity(path.len() + 1);
    path_bytes.extend_from_slice(path.as_bytes());
    path_bytes.push(0);

    // Allocate buffer for directory entries (start with 4KB)
    let mut buffer = alloc::vec![0u8; 4096];

    let result =
        unsafe { crate::sys_list_dir(path_bytes.as_ptr(), buffer.as_mut_ptr(), buffer.len()) };

    if result < 0 {
        return Err(IoError::from(errno()));
    }

    let bytes_read = result as usize;
    if bytes_read == 0 {
        return Ok(Vec::new());
    }

    // Parse the serialized directory entries
    let mut entries = Vec::new();
    let mut offset = 0;

    while offset + core::mem::size_of::<RawDirEntry>() <= bytes_read {
        // Read the raw directory entry header
        let raw_entry =
            unsafe { core::ptr::read_unaligned(buffer.as_ptr().add(offset) as *const RawDirEntry) };
        offset += core::mem::size_of::<RawDirEntry>();

        // Ensure we have enough bytes for the filename
        if offset + raw_entry.name_len as usize > bytes_read {
            break; // Truncated entry, stop parsing
        }

        // Extract the filename
        let name_bytes = &buffer[offset..offset + raw_entry.name_len as usize];
        let name = match core::str::from_utf8(name_bytes) {
            Ok(s) => s.to_string(),
            Err(_) => continue, // Skip invalid UTF-8 filenames
        };
        offset += raw_entry.name_len as usize;

        // Create the directory entry
        let entry = DirEntry {
            name,
            file_type: FileType::from(raw_entry.file_type),
            size: raw_entry.size,
            attrs: raw_entry.attrs,
        };

        entries.push(entry);
    }

    Ok(entries)
}

/// Get current working directory
pub fn getcwd() -> IoResult<String> {
    // Start with reasonable buffer size
    let mut buffer = alloc::vec![0u8; 512];

    let result = unsafe { crate::sys_getcwd(buffer.as_mut_ptr(), buffer.len()) };

    if result < 0 {
        return Err(IoError::from(errno()));
    }

    let length = result as usize;
    if length == 0 {
        return Ok(String::new());
    }

    // Convert to string (should include null terminator)
    let cwd_bytes = &buffer[..length - 1]; // Exclude null terminator
    match core::str::from_utf8(cwd_bytes) {
        Ok(s) => Ok(s.to_string()),
        Err(_) => Err(IoError::InvalidInput),
    }
}

/// Change current working directory
pub fn chdir(path: &str) -> IoResult<()> {
    // Build a C string (nul-terminated) in a scratch buffer
    let mut path_bytes = alloc::vec::Vec::with_capacity(path.len() + 1);
    path_bytes.extend_from_slice(path.as_bytes());
    path_bytes.push(0);

    let result = unsafe { crate::sys_chdir(path_bytes.as_ptr()) };

    if result < 0 {
        Err(IoError::from(errno()))
    } else {
        Ok(())
    }
}