diff --git a/src/uu/free/src/free.rs b/src/uu/free/src/free.rs index 22acb84..8e24f0c 100644 --- a/src/uu/free/src/free.rs +++ b/src/uu/free/src/free.rs @@ -8,54 +8,62 @@ use clap::ArgAction; use clap::{crate_version, Command}; use std::env; use std::fs; +use std::io::Error; use std::process; use uucore::{error::UResult, format_usage, help_about, help_usage}; const ABOUT: &str = help_about!("free.md"); const USAGE: &str = help_usage!("free.md"); -fn parse_meminfo() -> Result<(u64, u64, u64, u64, u64, u64, u64, u64, u64, u64), std::io::Error> { +struct MemInfo { + total: u64, + free: u64, + available: u64, + shared: u64, + buffers: u64, + cached: u64, + swap_total: u64, + swap_free: u64, + swap_used: u64, + reclaimable: u64, +} + +fn parse_meminfo() -> Result { let contents = fs::read_to_string("/proc/meminfo")?; - let mut total = 0; - let mut free = 0; - let mut available = 0; - let mut shared = 0; - let mut buffers = 0; - let mut cached = 0; - let mut swap_total = 0; - let mut swap_free = 0; - let mut reclaimable = 0; + let mut mem_info = MemInfo { + total: 0, + free: 0, + available: 0, + shared: 0, + buffers: 0, + cached: 0, + swap_total: 0, + swap_free: 0, + swap_used: 0, + reclaimable: 0, + }; for line in contents.lines() { if let Some((key, value)) = line.split_once(':') { let parsed_value = parse_meminfo_value(value)?; match key.trim() { - "MemTotal" => total = parsed_value, - "MemFree" => free = parsed_value, - "MemAvailable" => available = parsed_value, - "Shmem" => shared = parsed_value, - "Buffers" => buffers = parsed_value, - "Cached" => cached = parsed_value, - "SwapTotal" => swap_total = parsed_value, - "SwapFree" => swap_free = parsed_value, - "SReclaimable" => reclaimable = parsed_value, + "MemTotal" => mem_info.total = parsed_value, + "MemFree" => mem_info.free = parsed_value, + "MemAvailable" => mem_info.available = parsed_value, + "Shmem" => mem_info.shared = parsed_value, + "Buffers" => mem_info.buffers = parsed_value, + "Cached" => mem_info.cached = parsed_value, + "SwapTotal" => mem_info.swap_total = parsed_value, + "SwapFree" => mem_info.swap_free = parsed_value, + "SReclaimable" => mem_info.reclaimable = parsed_value, _ => {} } } } - Ok(( - total, - free, - available, - shared, - buffers, - cached, - swap_total, - swap_free, - swap_total - swap_free, - reclaimable, - )) + mem_info.swap_used = mem_info.swap_total - mem_info.swap_free; + + Ok(mem_info) } #[uucore::main] @@ -64,47 +72,43 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let wide = matches.get_flag("wide"); match parse_meminfo() { - Ok(( - total, - free, - available, - shared, - buffers, - cached, - swap_total, - swap_free, - swap_used, - reclaimable, - )) => { - let buff_cache = if wide { buffers } else { buffers + cached }; - let cache = if wide { cached } else { 0 }; - let used = total - free; + Ok(mem_info) => { + let buff_cache = if wide { + mem_info.buffers + } else { + mem_info.buffers + mem_info.cached + }; + let cache = if wide { mem_info.cached } else { 0 }; + let used = mem_info.total - mem_info.free; if wide { println!(" total used free shared buffers cache available"); println!( "Mem: {:12} {:12} {:12} {:12} {:12} {:12} {:12}", - total, + mem_info.total, used, - free, - shared, + mem_info.free, + mem_info.shared, buff_cache, - cache + reclaimable, - available + cache + mem_info.reclaimable, + mem_info.available ); } else { println!(" total used free shared buff/cache available"); println!( "Mem: {:12} {:12} {:12} {:12} {:12} {:12}", - total, + mem_info.total, used, - free, - shared, - buff_cache + reclaimable, - available + mem_info.free, + mem_info.shared, + buff_cache + mem_info.reclaimable, + mem_info.available ); } - println!("Swap: {:12} {:12} {:12}", swap_total, swap_used, swap_free); + println!( + "Swap: {:12} {:12} {:12}", + mem_info.swap_total, mem_info.swap_used, mem_info.swap_free + ); } Err(e) => { eprintln!("free: failed to read memory info: {}", e);