-
Notifications
You must be signed in to change notification settings - Fork 0
Add userspace /bin/cat and /bin/ls #909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.