@@ -15,12 +15,10 @@ use uucore::libc::S_IWGRP;
1515use uucore:: translate;
1616use uucore:: utmpx:: { self , Utmpx , UtmpxRecord , time} ;
1717
18- use std:: io:: BufReader ;
19- use std:: io:: prelude:: * ;
20-
2118use std:: fs:: File ;
19+ use std:: io;
20+ use std:: io:: prelude:: * ;
2221use std:: os:: unix:: fs:: MetadataExt ;
23-
2422use std:: path:: PathBuf ;
2523
2624fn get_long_usage ( ) -> String {
@@ -96,9 +94,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
9694 } ;
9795
9896 if do_short_format {
99- pk. short_pinky ( ) ;
97+ pk. write_short ( & mut io :: stdout ( ) ) ? ;
10098 } else {
101- pk. long_pinky ( ) ;
99+ pk. write_long ( & mut io :: stdout ( ) ) ? ;
102100 }
103101 Ok ( ( ) )
104102}
@@ -167,7 +165,7 @@ fn gecos_to_fullname(pw: &Passwd) -> Option<String> {
167165}
168166
169167impl Pinky {
170- fn print_entry ( & self , ut : & UtmpxRecord ) {
168+ fn write_entry ( & self , writer : & mut impl Write , ut : & UtmpxRecord ) -> io :: Result < ( ) > {
171169 let mut pts_path = PathBuf :: from ( "/dev" ) ;
172170 pts_path. push ( ut. tty_device ( ) . as_str ( ) ) ;
173171
@@ -187,7 +185,7 @@ impl Pinky {
187185 last_change = 0 ;
188186 }
189187
190- print ! ( "{1:<8.0$}" , utmpx:: UT_NAMESIZE , ut. user( ) ) ;
188+ write ! ( writer , "{1:<8.0$}" , utmpx:: UT_NAMESIZE , ut. user( ) ) ? ;
191189
192190 if self . include_fullname {
193191 let fullname = if let Ok ( pw) = Passwd :: locate ( ut. user ( ) . as_ref ( ) ) {
@@ -196,23 +194,28 @@ impl Pinky {
196194 None
197195 } ;
198196 if let Some ( fullname) = fullname {
199- print ! ( " {fullname:<19.19}" ) ;
197+ write ! ( writer , " {fullname:<19.19}" ) ? ;
200198 } else {
201- print ! ( " {:19}" , " ???" ) ;
199+ write ! ( writer , " {:19}" , " ???" ) ? ;
202200 }
203201 }
204202
205- print ! ( " {mesg}{:<8.*}" , utmpx:: UT_LINESIZE , ut. tty_device( ) ) ;
203+ write ! (
204+ writer,
205+ " {mesg}{:<8.*}" ,
206+ utmpx:: UT_LINESIZE ,
207+ ut. tty_device( )
208+ ) ?;
206209
207210 if self . include_idle {
208211 if last_change == 0 {
209- print ! ( " {:<6}" , "?????" ) ;
212+ write ! ( writer , " {:<6}" , "?????" ) ? ;
210213 } else {
211- print ! ( " {:<6}" , idle_string( last_change) ) ;
214+ write ! ( writer , " {:<6}" , idle_string( last_change) ) ? ;
212215 }
213216 }
214217
215- print ! ( " {}" , time_string( ut) ) ;
218+ write ! ( writer , " {}" , time_string( ut) ) ? ;
216219
217220 if self . include_where {
218221 let s: String = if self . do_lookup {
@@ -222,86 +225,87 @@ impl Pinky {
222225 } ;
223226
224227 if !s. is_empty ( ) {
225- print ! ( " {s}" ) ;
228+ write ! ( writer , " {s}" ) ? ;
226229 }
227230 }
228231
229- println ! ( ) ;
232+ writeln ! ( writer) ?;
233+ Ok ( ( ) )
230234 }
231235
232- fn print_heading ( & self ) {
233- print ! ( "{:<8}" , translate!( "pinky-column-login" ) ) ;
236+ fn write_heading ( & self , writer : & mut impl Write ) -> io :: Result < ( ) > {
237+ write ! ( writer , "{:<8}" , translate!( "pinky-column-login" ) ) ? ;
234238 if self . include_fullname {
235- print ! ( " {:<19}" , translate!( "pinky-column-name" ) ) ;
239+ write ! ( writer , " {:<19}" , translate!( "pinky-column-name" ) ) ? ;
236240 }
237- print ! ( " {:<9}" , translate!( "pinky-column-tty" ) ) ;
241+ write ! ( writer , " {:<9}" , translate!( "pinky-column-tty" ) ) ? ;
238242 if self . include_idle {
239- print ! ( " {:<6}" , translate!( "pinky-column-idle" ) ) ;
243+ write ! ( writer , " {:<6}" , translate!( "pinky-column-idle" ) ) ? ;
240244 }
241- print ! ( " {:<16}" , translate!( "pinky-column-when" ) ) ;
245+ write ! ( writer , " {:<16}" , translate!( "pinky-column-when" ) ) ? ;
242246 if self . include_where {
243- print ! ( " {}" , translate!( "pinky-column-where" ) ) ;
247+ write ! ( writer , " {}" , translate!( "pinky-column-where" ) ) ? ;
244248 }
245- println ! ( ) ;
249+ writeln ! ( writer) ?;
250+ Ok ( ( ) )
246251 }
247252
248- fn short_pinky ( & self ) {
253+ fn write_short ( & self , writer : & mut impl Write ) -> io :: Result < ( ) > {
249254 if self . include_heading {
250- self . print_heading ( ) ;
255+ self . write_heading ( writer ) ? ;
251256 }
252257 for ut in Utmpx :: iter_all_records ( ) {
253258 if ut. is_user_process ( )
254259 && ( self . names . is_empty ( ) || self . names . iter ( ) . any ( |n| n. as_str ( ) == ut. user ( ) ) )
255260 {
256- self . print_entry ( & ut) ;
261+ self . write_entry ( writer , & ut) ? ;
257262 }
258263 }
264+ Ok ( ( ) )
259265 }
260266
261- fn long_pinky ( & self ) {
267+ fn write_long ( & self , writer : & mut impl Write ) -> io :: Result < ( ) > {
262268 for u in & self . names {
263- print ! (
269+ write ! (
270+ writer,
264271 "{} {u:<28}{} " ,
265272 translate!( "pinky-login-name-label" ) ,
266273 translate!( "pinky-real-life-label" )
267- ) ;
274+ ) ? ;
268275 if let Ok ( pw) = Passwd :: locate ( u. as_str ( ) ) {
269276 let fullname = gecos_to_fullname ( & pw) . unwrap_or_default ( ) ;
270277 let user_dir = pw. user_dir . unwrap_or_default ( ) ;
271278 let user_shell = pw. user_shell . unwrap_or_default ( ) ;
272- println ! ( " {fullname}" ) ;
279+ writeln ! ( writer , " {fullname}" ) ? ;
273280 if self . include_home_and_shell {
274- print ! ( "{} {user_dir:<29}" , translate!( "pinky-directory-label" ) ) ;
275- println ! ( "{} {user_shell}" , translate!( "pinky-shell-label" ) ) ;
281+ write ! (
282+ writer,
283+ "{} {user_dir:<29}" ,
284+ translate!( "pinky-directory-label" )
285+ ) ?;
286+ writeln ! ( writer, "{} {user_shell}" , translate!( "pinky-shell-label" ) ) ?;
276287 }
277288 if self . include_project {
278289 let mut p = PathBuf :: from ( & user_dir) ;
279290 p. push ( ".project" ) ;
280- if let Ok ( f ) = File :: open ( p) {
281- print ! ( "{} " , translate!( "pinky-project-label" ) ) ;
282- read_to_console ( f ) ;
291+ if let Ok ( mut reader ) = File :: open ( p) {
292+ write ! ( writer , "{} " , translate!( "pinky-project-label" ) ) ? ;
293+ io :: copy ( & mut reader , writer ) ? ;
283294 }
284295 }
285296 if self . include_plan {
286297 let mut p = PathBuf :: from ( & user_dir) ;
287298 p. push ( ".plan" ) ;
288- if let Ok ( f ) = File :: open ( p) {
289- println ! ( "{}:" , translate!( "pinky-plan-label" ) ) ;
290- read_to_console ( f ) ;
299+ if let Ok ( mut reader ) = File :: open ( p) {
300+ writeln ! ( writer , "{}:" , translate!( "pinky-plan-label" ) ) ? ;
301+ io :: copy ( & mut reader , writer ) ? ;
291302 }
292303 }
293- println ! ( ) ;
304+ writeln ! ( writer ) ? ;
294305 } else {
295- println ! ( " ???" ) ;
306+ writeln ! ( writer , " ???" ) ? ;
296307 }
297308 }
298- }
299- }
300-
301- fn read_to_console < F : Read > ( f : F ) {
302- let mut reader = BufReader :: new ( f) ;
303- let mut iobuf = Vec :: new ( ) ;
304- if reader. read_to_end ( & mut iobuf) . is_ok ( ) {
305- print ! ( "{}" , String :: from_utf8_lossy( & iobuf) ) ;
309+ Ok ( ( ) )
306310 }
307311}
0 commit comments