@@ -33,6 +33,7 @@ struct Settings {
3333 runstates : Option < String > ,
3434 terminal : Option < HashSet < Teletype > > ,
3535 signal : usize ,
36+ require_handler : bool ,
3637}
3738
3839fn get_match_settings ( matches : & ArgMatches ) -> UResult < Settings > {
@@ -58,6 +59,7 @@ fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
5859 . collect :: < HashSet < _ > > ( )
5960 } ) ,
6061 signal : parse_signal_value ( matches. get_one :: < String > ( "signal" ) . unwrap ( ) ) ?,
62+ require_handler : matches. get_flag ( "require-handler" ) ,
6163 } ;
6264
6365 if ( !settings. newest
@@ -76,6 +78,24 @@ fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
7678 Ok ( settings)
7779}
7880
81+ fn find_matching_pids ( settings : & Settings ) -> Vec < ProcessInformation > {
82+ let mut pids = collect_matched_pids ( settings) ;
83+ #[ cfg( unix) ]
84+ if settings. require_handler {
85+ pids. retain ( |pid| {
86+ let mask =
87+ u64:: from_str_radix ( pid. clone ( ) . status ( ) . get ( "SigCgt" ) . unwrap ( ) , 16 ) . unwrap ( ) ;
88+ mask & ( 1 << settings. signal ) != 0
89+ } ) ;
90+ }
91+ if pids. is_empty ( ) {
92+ uucore:: error:: set_exit_code ( 1 ) ;
93+ pids
94+ } else {
95+ process_flag_o_n ( settings, & mut pids)
96+ }
97+ }
98+
7999/// # Conceptual model of `pgrep`
80100///
81101/// At first, `pgrep` command will check the patterns is legal.
@@ -94,23 +114,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
94114 let settings = get_match_settings ( & matches) ?;
95115
96116 // Collect pids
97- let pids = {
98- let mut pids = collect_matched_pids ( & settings) ;
99- #[ cfg( unix) ]
100- if matches. get_flag ( "require-handler" ) {
101- pids. retain ( |pid| {
102- let mask =
103- u64:: from_str_radix ( pid. clone ( ) . status ( ) . get ( "SigCgt" ) . unwrap ( ) , 16 ) . unwrap ( ) ;
104- mask & ( 1 << settings. signal ) != 0
105- } ) ;
106- }
107- if pids. is_empty ( ) {
108- uucore:: error:: set_exit_code ( 1 ) ;
109- pids
110- } else {
111- process_flag_o_n ( & settings, & mut pids)
112- }
113- } ;
117+ let pids = find_matching_pids ( & settings) ;
114118
115119 // Processing output
116120 let output = if matches. get_flag ( "count" ) {
0 commit comments