fastshell 0.3.3

Lightweight Bash + Python runtime SDK for mobile AI agents
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
// Copyright (c) 2025 xiefujin <490021684@qq.com>
// Licensed under Apache-2.0, see LICENSE file for full license terms.

use crate::shell::{list_processes, CommandOutput, Shell};
use sha1::Sha1;
use sha2::Digest;
use std::io::Read;

impl Shell {
    pub fn cmd_sha1sum(&self, args: &[&str], stdin: Option<&str>) -> CommandOutput {
        cmd_hashsum_sha1(self, args, stdin)
    }

    pub fn cmd_sum(&self, args: &[&str], stdin: Option<&str>) -> CommandOutput {
        let mut files = Vec::new();
        for arg in args {
            if !arg.starts_with('-') {
                files.push(arg.to_string());
            }
        }

        if files.is_empty() {
            let input = match stdin {
                Some(s) => s,
                None => return CommandOutput::error("sum: missing operand\n".to_string(), 1),
            };
            let checksum = bsd_sum(input.as_bytes());
            let blocks = (input.len() + 1023) / 1024;
            return CommandOutput::success(format!("{:05} {:>5}\n", checksum, blocks));
        }

        let mut output = String::new();
        for file in &files {
            match self.vfs.read(file, &self.cwd) {
                Ok(data) => {
                    let checksum = bsd_sum(&data);
                    let blocks = (data.len() + 1023) / 1024;
                    output.push_str(&format!("{:05} {:>5} {}\n", checksum, blocks, file));
                }
                Err(e) => return CommandOutput::error(format!("sum: {}: {}\n", file, e), 1),
            }
        }
        CommandOutput::success(output)
    }

    pub fn cmd_pidof(&self, args: &[&str]) -> CommandOutput {
        if args.is_empty() {
            return CommandOutput::error("pidof: missing program name\n".to_string(), 1);
        }

        let procs = match list_processes() {
            Ok(p) => p,
            Err(e) => return CommandOutput::error(format!("pidof: {}\n", e), 1),
        };

        let mut output = String::new();
        for arg in args {
            if arg.starts_with('-') {
                continue;
            }
            let name = arg;
            for proc in &procs {
                if &proc.comm == name || proc.comm.starts_with(name) {
                    output.push_str(&format!("{} ", proc.pid));
                }
            }
        }

        if output.is_empty() {
            CommandOutput {
                stdout: String::new(),
                stderr: String::new(),
                exit_code: 1,
            }
        } else {
            output.pop();
            output.push('\n');
            CommandOutput::success(output)
        }
    }

    pub fn cmd_nproc(&self, args: &[&str]) -> CommandOutput {
        let mut all = false;
        for arg in args {
            if *arg == "--all" {
                all = true;
            }
        }

        let count = if all {
            num_cpus::get()
        } else {
            num_cpus::get_physical()
        };

        CommandOutput::success(format!("{}\n", count))
    }

    pub fn cmd_tty(&self, _args: &[&str]) -> CommandOutput {
        #[cfg(unix)]
        {
            let fd = unsafe { libc::ttyname(0) };
            if fd.is_null() {
                CommandOutput {
                    stdout: "not a tty\n".to_string(),
                    stderr: String::new(),
                    exit_code: 1,
                }
            } else {
                let name = unsafe { std::ffi::CStr::from_ptr(fd).to_string_lossy().to_string() };
                CommandOutput::success(name + "\n")
            }
        }
        #[cfg(not(unix))]
        {
            CommandOutput {
                stdout: "not a tty\n".to_string(),
                stderr: String::new(),
                exit_code: 1,
            }
        }
    }

    pub fn cmd_clear(&self, _args: &[&str]) -> CommandOutput {
        CommandOutput::success("\x1b[2J\x1b[H".to_string())
    }

    pub fn cmd_sync(&self, _args: &[&str]) -> CommandOutput {
        #[cfg(unix)]
        unsafe {
            libc::sync();
        }
        CommandOutput::success(String::new())
    }

    pub fn cmd_nice(&self, args: &[&str]) -> CommandOutput {
        let mut adjustment = 10i32;
        let mut command: Option<String> = None;
        let mut cmd_args = Vec::new();

        let mut i = 0;
        while i < args.len() {
            match args[i] {
                "-n" => {
                    if i + 1 < args.len() {
                        adjustment = args[i + 1].parse().unwrap_or(10);
                        i += 1;
                    }
                }
                arg if arg.starts_with("-n") && arg.len() > 2 => {
                    adjustment = arg[2..].parse().unwrap_or(10);
                }
                arg if arg.starts_with('-') && arg.len() > 1 => {
                    if let Ok(adj) = arg[1..].parse::<i32>() {
                        adjustment = adj;
                    }
                }
                _ => {
                    if command.is_none() {
                        command = Some(args[i].to_string());
                    } else {
                        cmd_args.push(args[i].to_string());
                    }
                }
            }
            i += 1;
        }

        #[cfg(unix)]
        {
            if adjustment != 0 {
                unsafe {
                    libc::nice(adjustment);
                }
            }
        }

        match command {
            Some(cmd) => {
                let vfs_root = self.vfs.root().to_path_buf();
                let cwd = if self.cwd == "/" {
                    vfs_root.clone()
                } else {
                    vfs_root.join(self.cwd.trim_start_matches('/'))
                };
                let cmd_args_ref: Vec<&str> = cmd_args.iter().map(|s| s.as_str()).collect();
                let output = std::process::Command::new(&cmd)
                    .args(&cmd_args_ref)
                    .current_dir(&cwd)
                    .output();
                match output {
                    Ok(out) => CommandOutput {
                        stdout: String::from_utf8_lossy(&out.stdout).to_string(),
                        stderr: String::from_utf8_lossy(&out.stderr).to_string(),
                        exit_code: out.status.code().unwrap_or(-1),
                    },
                    Err(e) => CommandOutput::error(format!("nice: {}\n", e), 1),
                }
            }
            None => CommandOutput::success(String::new()),
        }
    }

    pub fn cmd_chown(&self, args: &[&str]) -> CommandOutput {
        if args.len() < 2 {
            return CommandOutput::error("chown: missing operand\n".to_string(), 1);
        }

        let owner_spec = args[0];
        let files = &args[1..];

        #[cfg(unix)]
        {
            let (uid, gid) = parse_owner(owner_spec);
            for file in files {
                if file.starts_with('-') {
                    continue;
                }
                let resolved = match self.vfs.resolve(file, &self.cwd) {
                    Ok(p) => p,
                    Err(e) => return CommandOutput::error(format!("chown: {}: {}\n", file, e), 1),
                };
                let path_c = std::ffi::CString::new(resolved.to_string_lossy().as_bytes()).unwrap();
                let ret = unsafe { libc::chown(path_c.as_ptr(), uid, gid) };
                if ret != 0 {
                    let e = std::io::Error::last_os_error();
                    if e.raw_os_error() != Some(1) {
                        return CommandOutput::error(format!("chown: {}: {}\n", file, e), 1);
                    }
                }
            }
        }

        CommandOutput::success(String::new())
    }

    pub fn cmd_chgrp(&self, args: &[&str]) -> CommandOutput {
        if args.len() < 2 {
            return CommandOutput::error("chgrp: missing operand\n".to_string(), 1);
        }

        let group_spec = args[0];
        let files = &args[1..];

        #[cfg(unix)]
        {
            let gid = parse_group(group_spec);
            for file in files {
                if file.starts_with('-') {
                    continue;
                }
                let resolved = match self.vfs.resolve(file, &self.cwd) {
                    Ok(p) => p,
                    Err(e) => return CommandOutput::error(format!("chgrp: {}: {}\n", file, e), 1),
                };
                let path_c = std::ffi::CString::new(resolved.to_string_lossy().as_bytes()).unwrap();
                unsafe {
                    libc::chown(path_c.as_ptr(), u32::MAX, gid);
                }
            }
        }

        CommandOutput::success(String::new())
    }

    pub fn cmd_groups(&self, _args: &[&str]) -> CommandOutput {
        #[cfg(unix)]
        {
            let _gid = unsafe { libc::getgid() };
            let _count = 0i32;
            let groups = unsafe { libc::getgroups(0, std::ptr::null_mut()) };
            let mut gids = vec![0u32; groups as usize];
            unsafe { libc::getgroups(groups, gids.as_mut_ptr()) };

            let pw = unsafe { libc::getpwuid(libc::getuid()) };
            let user_name = if pw.is_null() {
                "?".to_string()
            } else {
                unsafe {
                    std::ffi::CStr::from_ptr((*pw).pw_name)
                        .to_string_lossy()
                        .to_string()
                }
            };

            let mut names = Vec::new();
            for &g in &gids {
                let gr = unsafe { libc::getgrgid(g) };
                if !gr.is_null() {
                    let name = unsafe {
                        std::ffi::CStr::from_ptr((*gr).gr_name)
                            .to_string_lossy()
                            .to_string()
                    };
                    names.push(name);
                }
            }

            CommandOutput::success(format!("{} : {}\n", user_name, names.join(" ")))
        }
        #[cfg(not(unix))]
        {
            CommandOutput::success(String::new())
        }
    }

    pub fn cmd_dd(&self, args: &[&str]) -> CommandOutput {
        let mut ifile = String::new();
        let mut ofile = String::new();
        let mut bs: usize = 512;
        let mut count: Option<usize> = None;
        let mut skip: usize = 0;
        let mut seek: usize = 0;

        let mut i = 0;
        while i < args.len() {
            match args[i] {
                "if" => {
                    if i + 1 < args.len() {
                        ifile = args[i + 1].to_string();
                        i += 1;
                    }
                }
                "of" => {
                    if i + 1 < args.len() {
                        ofile = args[i + 1].to_string();
                        i += 1;
                    }
                }
                "bs" => {
                    if i + 1 < args.len() {
                        bs = parse_dd_size(args[i + 1]);
                        i += 1;
                    }
                }
                "count" => {
                    if i + 1 < args.len() {
                        count = Some(args[i + 1].parse().unwrap_or(0));
                        i += 1;
                    }
                }
                "skip" => {
                    if i + 1 < args.len() {
                        skip = args[i + 1].parse().unwrap_or(0);
                        i += 1;
                    }
                }
                "seek" => {
                    if i + 1 < args.len() {
                        seek = args[i + 1].parse().unwrap_or(0);
                        i += 1;
                    }
                }
                arg if arg.contains('=') => {
                    let parts: Vec<&str> = arg.splitn(2, '=').collect();
                    match parts[0] {
                        "if" => ifile = parts[1].to_string(),
                        "of" => ofile = parts[1].to_string(),
                        "bs" => bs = parse_dd_size(parts[1]),
                        "count" => count = parts[1].parse().ok(),
                        "skip" => skip = parts[1].parse().unwrap_or(0),
                        "seek" => seek = parts[1].parse().unwrap_or(0),
                        _ => {}
                    }
                }
                _ => {}
            }
            i += 1;
        }

        let input = if ifile.is_empty() {
            std::io::stdin()
                .bytes()
                .filter_map(|b| b.ok())
                .collect::<Vec<u8>>()
        } else {
            match self.vfs.read(&ifile, &self.cwd) {
                Ok(d) => d,
                Err(e) => return CommandOutput::error(format!("dd: {}: {}\n", ifile, e), 1),
            }
        };

        let start = skip * bs;
        let data = if start < input.len() {
            &input[start..]
        } else {
            &[]
        };

        let max_bytes = count.unwrap_or(usize::MAX) * bs;
        let to_write = &data[..max_bytes.min(data.len())];

        let written;
        if !ofile.is_empty() {
            let resolved = match self.vfs.resolve(&ofile, &self.cwd) {
                Ok(p) => p,
                Err(e) => return CommandOutput::error(format!("dd: {}: {}\n", ofile, e), 1),
            };
            let mut f = match std::fs::OpenOptions::new()
                .write(true)
                .create(true)
                .truncate(true)
                .open(&resolved)
            {
                Ok(f) => f,
                Err(e) => return CommandOutput::error(format!("dd: {}: {}\n", ofile, e), 1),
            };
            use std::io::Write;
            if seek > 0 {
                f.write_all(&vec![0u8; seek * bs]).ok();
            }
            f.write_all(to_write).ok();
            written = to_write.len();
        } else {
            use std::io::Write;
            std::io::stdout().write_all(to_write).ok();
            written = to_write.len();
        }

        let blocks = (written + bs - 1) / bs;
        CommandOutput {
            stdout: format!(
                "{}+0 records in\n{}+0 records out\n{} bytes transferred\n",
                blocks, blocks, written
            ),
            stderr: String::new(),
            exit_code: 0,
        }
    }

    pub fn cmd_od(&self, args: &[&str], stdin: Option<&str>) -> CommandOutput {
        let mut format = "o";
        let mut files = Vec::new();

        let mut i = 0;
        while i < args.len() {
            match args[i] {
                "-t" => {
                    if i + 1 < args.len() {
                        format = args[i + 1];
                        i += 1;
                    }
                }
                "-A" | "-j" | "-N" | "-w" => {
                    i += 1;
                } // skip these flags
                arg if !arg.starts_with('-') => files.push(arg.to_string()),
                _ => {}
            }
            i += 1;
        }

        let data = if files.is_empty() {
            match stdin {
                Some(s) => s.as_bytes().to_vec(),
                None => return CommandOutput::error("od: missing input\n".to_string(), 1),
            }
        } else {
            let mut all = Vec::new();
            for file in &files {
                match self.vfs.read(file, &self.cwd) {
                    Ok(d) => all.extend_from_slice(&d),
                    Err(e) => return CommandOutput::error(format!("od: {}: {}\n", file, e), 1),
                }
            }
            all
        };

        let mut output = String::new();
        for (offset, chunk) in data.chunks(16).enumerate() {
            output.push_str(&format!("{:07o} ", offset * 16));
            match format {
                "x" | "x1" => {
                    for &byte in chunk {
                        output.push_str(&format!(" {:02x}", byte));
                    }
                }
                "d" | "d2" => {
                    for pair in chunk.chunks(2) {
                        let val = if pair.len() == 2 {
                            i16::from_le_bytes([pair[0], pair[1]])
                        } else {
                            pair[0] as i16
                        };
                        output.push_str(&format!(" {:>6}", val));
                    }
                }
                _ => {
                    for &byte in chunk {
                        output.push_str(&format!(" {:03o}", byte));
                    }
                }
            }
            output.push('\n');
        }
        output.push_str(&format!("{:07o}\n", data.len()));

        CommandOutput::success(output)
    }

    pub fn cmd_uptime(&self, _args: &[&str]) -> CommandOutput {
        #[cfg(any(target_os = "linux", target_os = "android"))]
        {
            if let Ok(content) = std::fs::read_to_string("/proc/uptime") {
                let uptime: f64 = content
                    .split_whitespace()
                    .next()
                    .unwrap_or("0")
                    .parse()
                    .unwrap_or(0.0);
                let days = (uptime / 86400.0) as u64;
                let hours = ((uptime % 86400.0) / 3600.0) as u64;
                let mins = ((uptime % 3600.0) / 60.0) as u64;
                let up = if days > 0 {
                    format!(
                        "{} day{}, {:>2}:{:02}",
                        days,
                        if days > 1 { "s" } else { "" },
                        hours,
                        mins
                    )
                } else {
                    format!("{:>2}:{:02}", hours, mins)
                };
                return CommandOutput::success(format!("  up {}\n", up));
            }
        }
        #[cfg(target_os = "macos")]
        {
            let output = std::process::Command::new("sysctl")
                .arg("-n")
                .arg("kern.boottime")
                .output()
                .ok();
            if let Some(out) = output {
                let s = String::from_utf8_lossy(&out.stdout);
                if let Some(rest) = s.trim().strip_prefix("{ sec = ") {
                    if let Some(pos) = rest.find(',') {
                        if let Ok(boot_secs) = rest[..pos].parse::<f64>() {
                            let now = std::time::SystemTime::now()
                                .duration_since(std::time::UNIX_EPOCH)
                                .unwrap_or_default()
                                .as_secs_f64();
                            let uptime = now - boot_secs;
                            let days = (uptime / 86400.0) as u64;
                            let hours = ((uptime % 86400.0) / 3600.0) as u64;
                            let mins = ((uptime % 3600.0) / 60.0) as u64;
                            let up = if days > 0 {
                                format!(
                                    "{} day{}, {:>2}:{:02}",
                                    days,
                                    if days > 1 { "s" } else { "" },
                                    hours,
                                    mins
                                )
                            } else {
                                format!("{:>2}:{:02}", hours, mins)
                            };
                            return CommandOutput::success(format!("  up {}\n", up));
                        }
                    }
                }
            }
        }
        CommandOutput::success("  up ?\n".to_string())
    }

    pub fn cmd_free(&self, _args: &[&str]) -> CommandOutput {
        #[cfg(any(target_os = "linux", target_os = "android"))]
        {
            if let Ok(content) = std::fs::read_to_string("/proc/meminfo") {
                let mut total = 0u64;
                let mut free = 0u64;
                let mut avail = 0u64;
                for line in content.lines() {
                    if line.starts_with("MemTotal:") {
                        total = line
                            .split_whitespace()
                            .nth(1)
                            .unwrap_or("0")
                            .parse()
                            .unwrap_or(0);
                    } else if line.starts_with("MemFree:") {
                        free = line
                            .split_whitespace()
                            .nth(1)
                            .unwrap_or("0")
                            .parse()
                            .unwrap_or(0);
                    } else if line.starts_with("MemAvailable:") {
                        avail = line
                            .split_whitespace()
                            .nth(1)
                            .unwrap_or("0")
                            .parse()
                            .unwrap_or(0);
                    }
                }
                let used = total.saturating_sub(if avail > 0 { avail } else { free });
                let mut out = String::new();
                out.push_str(&format!(
                    "{:>10} {:>10} {:>10} {:>10} {:>10} {:>10}\n",
                    "total", "used", "free", "shared", "buff/cache", "available"
                ));
                out.push_str(&format!("Mem: {:>9} {:>9} {:>9}\n", total, used, free));
                return CommandOutput::success(out);
            }
        }
        #[cfg(target_os = "macos")]
        {
            let output = std::process::Command::new("vm_stat").output().ok();
            let mut page_size = 4096u64;
            let mut free_pages = 0u64;
            let mut active_pages = 0u64;
            let mut inactive_pages = 0u64;
            let mut wired_pages = 0u64;

            if let Some(o) = output {
                let s = String::from_utf8_lossy(&o.stdout);
                for line in s.lines() {
                    if line.contains("page size") {
                        page_size = line
                            .split_whitespace()
                            .last()
                            .unwrap_or("4096")
                            .parse()
                            .unwrap_or(4096);
                    }
                    if line.starts_with("Pages free:") {
                        free_pages = line
                            .split_whitespace()
                            .last()
                            .unwrap_or("0")
                            .parse::<u64>()
                            .unwrap_or(0)
                            % 1000000;
                    }
                    if line.starts_with("Pages active:") {
                        active_pages = line
                            .split_whitespace()
                            .last()
                            .unwrap_or("0")
                            .parse::<u64>()
                            .unwrap_or(0)
                            % 1000000;
                    }
                    if line.starts_with("Pages inactive:") {
                        inactive_pages = line
                            .split_whitespace()
                            .last()
                            .unwrap_or("0")
                            .parse::<u64>()
                            .unwrap_or(0)
                            % 1000000;
                    }
                    if line.starts_with("Pages wired down:") {
                        wired_pages = line
                            .split_whitespace()
                            .last()
                            .unwrap_or("0")
                            .parse::<u64>()
                            .unwrap_or(0)
                            % 1000000;
                    }
                }
            }

            let free = free_pages * page_size / 1024;
            let used = (active_pages + inactive_pages + wired_pages) * page_size / 1024;
            let total = free + used;

            let mut out = String::new();
            out.push_str(&format!(
                "{:>10} {:>10} {:>10} {:>10} {:>10} {:>10}\n",
                "total", "used", "free", "shared", "buff/cache", "available"
            ));
            out.push_str(&format!("Mem: {:>9} {:>9} {:>9}\n", total, used, free));
            return CommandOutput::success(out);
        }
        #[allow(unreachable_code)]
        CommandOutput::success(String::new())
    }

    pub fn cmd_nslookup(&self, args: &[&str]) -> CommandOutput {
        let host = args
            .iter()
            .find(|a| !a.starts_with('-'))
            .copied()
            .unwrap_or("");
        if host.is_empty() {
            return CommandOutput::error("nslookup: missing hostname\n".to_string(), 1);
        }

        if let Some(perm) = self.check_network_permission(host) {
            return perm;
        }

        let output = std::process::Command::new("nslookup").arg(host).output();

        match output {
            Ok(o) => CommandOutput {
                stdout: String::from_utf8_lossy(&o.stdout).to_string(),
                stderr: String::from_utf8_lossy(&o.stderr).to_string(),
                exit_code: o.status.code().unwrap_or(1),
            },
            Err(e) => CommandOutput::error(format!("nslookup: {}\n", e), 1),
        }
    }
}

fn parse_dd_size(s: &str) -> usize {
    let s = s.trim();
    if let Some(rest) = s.strip_suffix('K') {
        rest.parse::<f64>().unwrap_or(0.0) as usize * 1024
    } else if let Some(rest) = s.strip_suffix('M') {
        rest.parse::<f64>().unwrap_or(0.0) as usize * 1024 * 1024
    } else if let Some(rest) = s.strip_suffix('G') {
        rest.parse::<f64>().unwrap_or(0.0) as usize * 1024 * 1024 * 1024
    } else {
        s.parse().unwrap_or(512)
    }
}

fn bsd_sum(data: &[u8]) -> u16 {
    let mut checksum: u16 = 0;
    for &byte in data {
        checksum = checksum.rotate_right(1).wrapping_add(byte as u16);
    }
    checksum
}

fn cmd_hashsum_sha1(shell: &Shell, args: &[&str], stdin: Option<&str>) -> CommandOutput {
    let mut check = false;
    let mut files = Vec::new();

    for arg in args {
        match *arg {
            "-c" | "--check" => check = true,
            arg if !arg.starts_with('-') => files.push(arg.to_string()),
            _ => {}
        }
    }

    if check {
        let input = if files.is_empty() {
            match stdin {
                Some(s) => s.to_string(),
                None => {
                    return CommandOutput::error("sha1sum: missing checksum file\n".to_string(), 1)
                }
            }
        } else {
            match shell.vfs.read_to_string(&files[0], &shell.cwd) {
                Ok(c) => c,
                Err(e) => {
                    return CommandOutput::error(format!("sha1sum: {}: {}\n", files[0], e), 1)
                }
            }
        };
        let mut output = String::new();
        let mut fail = 0usize;
        for line in input.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }
            let parts: Vec<&str> = line.splitn(3, ' ').collect();
            if parts.len() < 2 {
                fail += 1;
                continue;
            }
            let expected = parts[0];
            let path = parts[parts.len() - 1];
            if path == "-" {
                continue;
            }
            match shell.vfs.read(path, &shell.cwd) {
                Ok(data) => {
                    let actual = format!("{:x}", Sha1::digest(&data));
                    if actual == expected {
                        output.push_str(&format!("{}: OK\n", path));
                    } else {
                        output.push_str(&format!("{}: FAILED\n", path));
                        fail += 1;
                    }
                }
                Err(e) => {
                    output.push_str(&format!("sha1sum: {}: {}\n", path, e));
                    fail += 1;
                }
            }
        }
        let ec = if fail > 0 { 1 } else { 0 };
        return CommandOutput {
            stdout: output,
            stderr: String::new(),
            exit_code: ec,
        };
    }

    if files.is_empty() {
        let input = match stdin {
            Some(s) => s,
            None => return CommandOutput::error("sha1sum: missing operand\n".to_string(), 1),
        };
        let hash = format!("{:x}", Sha1::digest(input.as_bytes()));
        return CommandOutput::success(format!("{}  -\n", hash));
    }

    let mut output = String::new();
    for file in &files {
        match shell.vfs.read(file, &shell.cwd) {
            Ok(data) => output.push_str(&format!(
                "{}  {}\n",
                format!("{:x}", Sha1::digest(&data)),
                file
            )),
            Err(e) => output.push_str(&format!("sha1sum: {}: {}\n", file, e)),
        }
    }
    CommandOutput::success(output)
}

#[cfg(unix)]
fn parse_owner(spec: &str) -> (u32, u32) {
    let (user_str, group_str) = if let Some(pos) = spec.find(':') {
        (&spec[..pos], &spec[pos + 1..])
    } else if let Some(pos) = spec.find('.') {
        (&spec[..pos], &spec[pos + 1..])
    } else {
        (spec, "")
    };

    let uid = parse_user(user_str).unwrap_or(u32::MAX);
    let gid = if group_str.is_empty() {
        u32::MAX
    } else {
        parse_group(group_str)
    };
    (uid, gid)
}

#[cfg(unix)]
fn parse_user(s: &str) -> Option<u32> {
    if let Ok(uid) = s.parse::<u32>() {
        return Some(uid);
    }
    let cname = std::ffi::CString::new(s).ok()?;
    let pw = unsafe { libc::getpwnam(cname.as_ptr()) };
    if pw.is_null() {
        None
    } else {
        Some(unsafe { (*pw).pw_uid })
    }
}

#[cfg(unix)]
fn parse_group(s: &str) -> u32 {
    if let Ok(gid) = s.parse::<u32>() {
        return gid;
    }
    let cname = std::ffi::CString::new(s).ok().unwrap_or_default();
    let gr = unsafe { libc::getgrnam(cname.as_ptr()) };
    if gr.is_null() {
        u32::MAX
    } else {
        unsafe { (*gr).gr_gid }
    }
}