Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions base/cat/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "cat"
version = "0.1.0"
edition = "2021"
authors = ["vibix hackers"]
license = "MIT OR Apache-2.0"

[[bin]]
name = "cat"
path = "src/main.rs"

# Standalone package — not part of the main workspace. Built with
# `-Z build-std` against the in-repo std fork (see xtask build).
[workspace]
64 changes: 64 additions & 0 deletions base/cat/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#![feature(restricted_std)]

#[cfg(not(test))]
mod syscalls;

use std::env;
use std::fs::File;
use std::io::{self, Read, Write};
use std::process;

fn cat_reader<R: Read>(mut reader: R, stdout: &mut io::StdoutLock<'_>) -> io::Result<()> {
let mut buf = [0u8; 4096];
loop {
let n = reader.read(&mut buf)?;
if n == 0 {
break;
}
stdout.write_all(&buf[..n])?;
}
Ok(())
}

fn main() {
let args: Vec<String> = env::args().collect();
let stdout = io::stdout();
let mut stdout = stdout.lock();
let mut status = 0;

if args.len() <= 1 {
// No arguments: read stdin to stdout.
let stdin = io::stdin();
let stdin = stdin.lock();
if let Err(e) = cat_reader(stdin, &mut stdout) {
eprintln!("cat: {e}");
process::exit(1);
}
} else {
for path in &args[1..] {
if path == "-" {
let stdin = io::stdin();
let stdin = stdin.lock();
if let Err(e) = cat_reader(stdin, &mut stdout) {
eprintln!("cat: -: {e}");
status = 1;
}
} else {
match File::open(path) {
Ok(file) => {
if let Err(e) = cat_reader(file, &mut stdout) {
eprintln!("cat: {path}: {e}");
status = 1;
}
}
Err(e) => {
eprintln!("cat: {path}: {e}");
status = 1;
}
}
}
}
}

process::exit(status);
}
50 changes: 50 additions & 0 deletions base/cat/src/syscalls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//! C-ABI syscall shims required by the vibix std fork.
//!
//! The in-repo std fork links against POSIX symbols (`close`, etc.) that are
//! not provided by a system libc on vibix. We supply them here via raw
//! syscall instructions, mirroring the approach in `base/sh/src/syscalls.rs`.

use core::arch::asm;

const SYS_CLOSE: u64 = 3;

extern "C" {
fn __errno_location() -> *mut i32;
}

#[inline(always)]
unsafe fn raw1(nr: u64, a0: u64) -> i64 {
let ret: i64;
unsafe {
asm!(
"syscall",
inlateout("rax") nr => ret,
inlateout("rdi") a0 => _,
lateout("rcx") _,
lateout("r11") _,
lateout("rdx") _,
lateout("rsi") _,
lateout("r8") _,
lateout("r9") _,
lateout("r10") _,
options(nostack, preserves_flags),
);
}
ret
}

/// Convert raw syscall return to C convention: on error set errno, return -1.
#[inline]
unsafe fn cvt(r: i64) -> i64 {
if r < 0 {
unsafe { *__errno_location() = (-r) as i32 };
-1
} else {
r
}
}

#[no_mangle]
pub unsafe extern "C" fn close(fd: i32) -> i32 {
unsafe { cvt(raw1(SYS_CLOSE, fd as u64)) as i32 }
}
14 changes: 14 additions & 0 deletions base/ls/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "ls"
version = "0.1.0"
edition = "2021"
authors = ["vibix hackers"]
license = "MIT OR Apache-2.0"

[[bin]]
name = "ls"
path = "src/main.rs"

# Standalone package — not part of the main workspace. Built with
# `-Z build-std` against the in-repo std fork (see xtask build).
[workspace]
73 changes: 73 additions & 0 deletions base/ls/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#![feature(restricted_std)]

#[cfg(not(test))]
mod syscalls;

use std::env;
use std::fs;
use std::process;

fn list_dir(path: &str) -> i32 {
let entries = match fs::read_dir(path) {
Ok(entries) => entries,
Err(e) => {
eprintln!("ls: {path}: {e}");
return 1;
}
};

let mut names: Vec<(String, bool)> = Vec::new();
for entry in entries {
match entry {
Ok(entry) => {
let name = entry.file_name().to_string_lossy().into_owned();
let is_dir = entry
.file_type()
.map(|ft| ft.is_dir())
.unwrap_or(false);
names.push((name, is_dir));
}
Err(e) => {
eprintln!("ls: {path}: {e}");
return 1;
}
}
}

names.sort_by(|a, b| a.0.cmp(&b.0));

for (name, is_dir) in &names {
if *is_dir {
println!("{name}/");
} else {
println!("{name}");
}
}

0
}

fn main() {
let args: Vec<String> = env::args().collect();
let mut status = 0;

if args.len() <= 1 {
status = list_dir(".");
} else {
let show_header = args.len() > 2;
for (i, path) in args[1..].iter().enumerate() {
if show_header {
if i > 0 {
println!();
}
println!("{path}:");
}
let s = list_dir(path);
if s != 0 {
status = s;
}
}
}

process::exit(status);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
50 changes: 50 additions & 0 deletions base/ls/src/syscalls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//! C-ABI syscall shims required by the vibix std fork.
//!
//! The in-repo std fork links against POSIX symbols (`close`, etc.) that are
//! not provided by a system libc on vibix. We supply them here via raw
//! syscall instructions, mirroring the approach in `base/sh/src/syscalls.rs`.

use core::arch::asm;

const SYS_CLOSE: u64 = 3;

extern "C" {
fn __errno_location() -> *mut i32;
}

#[inline(always)]
unsafe fn raw1(nr: u64, a0: u64) -> i64 {
let ret: i64;
unsafe {
asm!(
"syscall",
inlateout("rax") nr => ret,
inlateout("rdi") a0 => _,
lateout("rcx") _,
lateout("r11") _,
lateout("rdx") _,
lateout("rsi") _,
lateout("r8") _,
lateout("r9") _,
lateout("r10") _,
options(nostack, preserves_flags),
);
}
ret
}

/// Convert raw syscall return to C convention: on error set errno, return -1.
#[inline]
unsafe fn cvt(r: i64) -> i64 {
if r < 0 {
unsafe { *__errno_location() = (-r) as i32 };
-1
} else {
r
}
}

#[no_mangle]
pub unsafe extern "C" fn close(fd: i32) -> i32 {
unsafe { cvt(raw1(SYS_CLOSE, fd as u64)) as i32 }
}
Loading
Loading