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
| #![no_std]
#![no_main]
extern crate alloc;
#[macro_use]
#[path = "ulib.rs"]
mod ulib;
use alloc::{format, string::String};
use ulib::*;
fn main() -> i64 {
let (Some(src), Some(dst), 3) = (arg(1), arg(2), argc()) else {
eprintln!("usage: cp source dest");
return 2;
};
let mut st = Stat::default();
let mut dst = String::from(dst);
if stat(&dst, &mut st) == 0 && st.kind == KIND_DIR {
let name = src.rsplit('/').next().unwrap_or(src);
dst = format!("{}/{}", dst.trim_end_matches('/'), name);
}
let from = open(src, 0);
if from < 0 {
eprintln!("cp: {}: {}", src, strerror(from));
return 1;
}
let to = open(&dst, O_CREAT | O_TRUNC);
if to < 0 {
eprintln!("cp: {}: {}", dst, strerror(to));
close(from);
return 1;
}
let mut buf = [0u8; 512];
let mut status = 0;
loop {
let n = read(from, &mut buf);
if n <= 0 || write_all(to, &buf[..n.max(0) as usize]) < 0 {
if n < 0 {
eprintln!("cp: {}: {}", src, strerror(n));
status = 1;
}
break;
}
}
close(from);
close(to);
status
}
|