44// file that was distributed with this source code.
55
66use clap:: { crate_version, Arg , ArgAction , Command } ;
7- use maps_format_parser:: parse_map_line;
7+ use maps_format_parser:: { parse_map_line, MapLine } ;
88use std:: env;
99use std:: fs;
1010use std:: io:: Error ;
@@ -49,11 +49,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
4949 }
5050 }
5151
52- match parse_maps ( pid) {
53- Ok ( total) => println ! ( " total {total:>16}K" ) ,
54- Err ( _) => {
55- set_exit_code ( 1 ) ;
56- }
52+ if matches. get_flag ( options:: DEVICE ) {
53+ output_device_format ( pid) . map_err ( |_| set_exit_code ( 1 ) ) . ok ( ) ;
54+ } else {
55+ output_default_format ( pid)
56+ . map_err ( |_| set_exit_code ( 1 ) )
57+ . ok ( ) ;
5758 }
5859 }
5960
@@ -74,21 +75,70 @@ fn parse_cmdline(pid: &str) -> Result<String, Error> {
7475 Ok ( cmdline. into ( ) )
7576}
7677
77- fn parse_maps ( pid : & str ) -> Result < u64 , Error > {
78+ fn process_maps < F > ( pid : & str , mut process_line : F ) -> Result < ( ) , Error >
79+ where
80+ F : FnMut ( & MapLine ) ,
81+ {
7882 let path = format ! ( "/proc/{pid}/maps" ) ;
7983 let contents = fs:: read_to_string ( path) ?;
80- let mut total = 0 ;
8184
8285 for line in contents. lines ( ) {
8386 let map_line = parse_map_line ( line) ?;
87+ process_line ( & map_line) ;
88+ }
89+
90+ Ok ( ( ) )
91+ }
92+
93+ fn output_default_format ( pid : & str ) -> Result < ( ) , Error > {
94+ let mut total = 0 ;
95+
96+ process_maps ( pid, |map_line| {
8497 println ! (
8598 "{} {:>6}K {} {}" ,
8699 map_line. address, map_line. size_in_kb, map_line. perms, map_line. mapping
87100 ) ;
88101 total += map_line. size_in_kb ;
89- }
102+ } ) ?;
103+
104+ println ! ( " total {total:>16}K" ) ;
105+
106+ Ok ( ( ) )
107+ }
108+
109+ fn output_device_format ( pid : & str ) -> Result < ( ) , Error > {
110+ let mut total_mapped = 0 ;
111+ let mut total_writeable_private = 0 ;
112+ let mut total_shared = 0 ;
113+
114+ println ! ( "Address Kbytes Mode Offset Device Mapping" ) ;
115+
116+ process_maps ( pid, |map_line| {
117+ println ! (
118+ "{} {:>7} {} {} {} {}" ,
119+ map_line. address,
120+ map_line. size_in_kb,
121+ map_line. perms,
122+ map_line. offset,
123+ map_line. device,
124+ map_line. mapping
125+ ) ;
126+ total_mapped += map_line. size_in_kb ;
90127
91- Ok ( total)
128+ if map_line. perms . writable && !map_line. perms . shared {
129+ total_writeable_private += map_line. size_in_kb ;
130+ }
131+
132+ if map_line. perms . shared {
133+ total_shared += map_line. size_in_kb ;
134+ }
135+ } ) ?;
136+
137+ println ! (
138+ "mapped: {total_mapped}K writeable/private: {total_writeable_private}K shared: {total_shared}K"
139+ ) ;
140+
141+ Ok ( ( ) )
92142}
93143
94144pub fn uu_app ( ) -> Command {
0 commit comments