RustForge

foundry

Foundry OS: the operating system built on the Rustbelt kernel

foundry / rm.rs
694 bytes ยท raw
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
//! rm path... -- delete files, and empty directories (shell alias: del).
//! (Real rm needs -r for directories; this one just tries rmdir.)
#![no_std]
#![no_main]

#[macro_use]
#[path = "ulib.rs"]
mod ulib;

use ulib::*;

fn main() -> i64 {
    if argc() < 2 {
        eprintln!("usage: rm path...");
        return 2;
    }
    let mut status = 0;
    for i in 1..argc() {
        let path = arg(i).unwrap_or("");
        let mut r = unlink(path);
        if r == -EISDIR {
            r = rmdir(path); // fails with ENOTEMPTY if there's something in it
        }
        if r < 0 {
            eprintln!("rm: {}: {}", path, strerror(r));
            status = 1;
        }
    }
    status
}