1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-02 22:17:45 +00:00

Merge pull request #591 from jbcrail/fix-od

Fix od.
This commit is contained in:
Heather 2015-05-11 08:07:17 +03:00
commit c8dbe036e5

View file

@ -1,5 +1,5 @@
#![crate_name = "od"] #![crate_name = "od"]
#![feature(collections, core, old_io, old_path, rustc_private)] #![feature(rustc_private)]
/* /*
* This file is part of the uutils coreutils package. * This file is part of the uutils coreutils package.
@ -11,10 +11,11 @@
*/ */
extern crate getopts; extern crate getopts;
extern crate collections;
use collections::string::String; use std::fs::File;
use std::old_io::File; use std::io::Read;
use std::mem;
use std::path::Path;
#[derive(Debug)] #[derive(Debug)]
enum Radix { Decimal, Hexadecimal, Octal, Binary } enum Radix { Decimal, Hexadecimal, Octal, Binary }
@ -41,7 +42,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
getopts::optflag("", "version", "output version information and exit."), getopts::optflag("", "version", "output version information and exit."),
]; ];
let matches = match getopts::getopts(args.tail(), &opts) { let matches = match getopts::getopts(&args[1..], &opts) {
Ok(m) => m, Ok(m) => m,
Err(f) => panic!("Invalid options\n{}", f) Err(f) => panic!("Invalid options\n{}", f)
}; };
@ -56,13 +57,13 @@ pub fn uumain(args: Vec<String>) -> i32 {
None => { panic!("Need fname for now") ; } None => { panic!("Need fname for now") ; }
}; };
main(&input_offset_base, fname.as_slice()); main(&input_offset_base, &fname);
0 0
} }
fn main(input_offset_base: &Radix, fname: &str) { fn main(input_offset_base: &Radix, fname: &str) {
let mut f = match File::open(&Path::new(fname)) { let mut f = match File::open(Path::new(fname)) {
Ok(f) => f, Ok(f) => f,
Err(e) => panic!("file error: {}", e) Err(e) => panic!("file error: {}", e)
}; };
@ -73,12 +74,12 @@ fn main(input_offset_base: &Radix, fname: &str) {
match f.read(bytes) { match f.read(bytes) {
Ok(n) => { Ok(n) => {
print_with_radix(input_offset_base, addr); print_with_radix(input_offset_base, addr);
for b in range(0, n / std::u16::BYTES as usize) { for b in 0 .. n / mem::size_of::<u16>() {
let bs = &bytes[(2 * b) .. (2 * b + 2)]; let bs = &bytes[(2 * b) .. (2 * b + 2)];
let p: u16 = (bs[1] as u16) << 8 | bs[0] as u16; let p: u16 = (bs[1] as u16) << 8 | bs[0] as u16;
print!(" {:06o}", p); print!(" {:06o}", p);
} }
if n % std::u16::BYTES as usize == 1 { if n % mem::size_of::<u16>() == 1 {
print!(" {:06o}", bytes[n - 1]); print!(" {:06o}", bytes[n - 1]);
} }
print!("\n"); print!("\n");