@@ -75,18 +75,21 @@ mod options {
7575/// output format, the second one indicates if we should use the binary flag in
7676/// the untagged case.
7777/// Sanitize the `--length` argument depending on `--algorithm` and `--length`.
78- fn maybe_sanitize_length ( algo_cli : AlgoKind , input_length : Option < & str > ) -> UResult < Option < usize > > {
78+ fn maybe_sanitize_length (
79+ algo_cli : Option < AlgoKind > ,
80+ input_length : Option < & str > ,
81+ ) -> UResult < Option < usize > > {
7982 match ( algo_cli, input_length) {
8083 // No provided length is not a problem so far.
8184 ( _, None ) => Ok ( None ) ,
8285
8386 // For SHA2 and SHA3, if a length is provided, ensure it is correct.
84- ( algo @ ( AlgoKind :: Sha2 | AlgoKind :: Sha3 ) , Some ( s_len) ) => {
87+ ( Some ( algo @ ( AlgoKind :: Sha2 | AlgoKind :: Sha3 ) ) , Some ( s_len) ) => {
8588 sanitize_sha2_sha3_length_str ( algo, s_len) . map ( Some )
8689 }
8790
8891 // For BLAKE2b, if a length is provided, validate it.
89- ( AlgoKind :: Blake2b , Some ( len) ) => calculate_blake2b_length_str ( len) ,
92+ ( Some ( AlgoKind :: Blake2b ) , Some ( len) ) => calculate_blake2b_length_str ( len) ,
9093
9194 // For any other provided algorithm, check if length is 0.
9295 // Otherwise, this is an error.
@@ -99,6 +102,7 @@ fn maybe_sanitize_length(algo_cli: AlgoKind, input_length: Option<&str>) -> URes
99102pub fn uumain ( args : impl uucore:: Args ) -> UResult < ( ) > {
100103 let binary_name = uucore:: util_name ( ) ;
101104 let matches = uucore:: clap_localization:: handle_clap_result ( uu_app ( ) , args) ?;
105+
102106 let check = matches. get_flag ( options:: CHECK ) ;
103107
104108 let ignore_missing = matches. get_flag ( options:: IGNORE_MISSING ) ;
@@ -107,23 +111,20 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
107111 let strict = matches. get_flag ( options:: STRICT ) ;
108112 let status = matches. get_flag ( options:: STATUS ) ;
109113
110- // Set default algo from binary name
111- let mut algo_cli = match binary_name {
112- s if s. ends_with ( "md5sum" ) => AlgoKind :: Md5 ,
113- s if s. ends_with ( "sha1sum" ) => AlgoKind :: Sha1 ,
114- s if s. ends_with ( "sha224sum" ) => AlgoKind :: Sha224 ,
115- s if s. ends_with ( "sha256sum" ) => AlgoKind :: Sha256 ,
116- s if s. ends_with ( "sha384sum" ) => AlgoKind :: Sha384 ,
117- s if s. ends_with ( "sha512sum" ) => AlgoKind :: Sha512 ,
118- s if s. ends_with ( "b2sum" ) => AlgoKind :: Blake2b ,
119- _ => AlgoKind :: Crc ,
114+ // Set default algo from binary name. We still use None to ignore --check legacy algos.
115+ let algo_cli = match binary_name {
116+ s if s. ends_with ( "md5sum" ) => Some ( AlgoKind :: Md5 ) ,
117+ s if s. ends_with ( "sha1sum" ) => Some ( AlgoKind :: Sha1 ) ,
118+ s if s. ends_with ( "sha224sum" ) => Some ( AlgoKind :: Sha224 ) ,
119+ s if s. ends_with ( "sha256sum" ) => Some ( AlgoKind :: Sha256 ) ,
120+ s if s. ends_with ( "sha384sum" ) => Some ( AlgoKind :: Sha384 ) ,
121+ s if s. ends_with ( "sha512sum" ) => Some ( AlgoKind :: Sha512 ) ,
122+ s if s. ends_with ( "b2sum" ) => Some ( AlgoKind :: Blake2b ) ,
123+ _ => matches
124+ . get_one :: < String > ( options:: ALGORITHM )
125+ . map ( |s| AlgoKind :: from_cksum ( s) )
126+ . transpose ( ) ?,
120127 } ;
121- // cksum accepts -a
122- if binary_name. ends_with ( "cksum" ) {
123- if let Some ( algo_str) = matches. get_one :: < String > ( options:: ALGORITHM ) {
124- algo_cli = AlgoKind :: from_cksum ( algo_str) ?;
125- }
126- }
127128
128129 let input_length = matches
129130 . try_get_one :: < String > ( options:: LENGTH )
@@ -142,9 +143,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
142143
143144 if check {
144145 // cksum does not support '--check'ing legacy algorithms
145- let algo_specified =
146- !binary_name. ends_with ( "cksum" ) || matches. contains_id ( options:: ALGORITHM ) ;
147- if algo_specified && algo_cli. is_legacy ( ) {
146+ if algo_cli. is_some_and ( AlgoKind :: is_legacy) {
148147 return Err ( ChecksumError :: AlgorithmNotSupportedWithCheck . into ( ) ) ;
149148 }
150149
@@ -164,10 +163,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
164163 strict,
165164 verbose,
166165 } ;
167- if algo_specified {
168- return perform_checksum_validation ( files, Some ( algo_cli) , length, opts) ;
169- }
170- return perform_checksum_validation ( files, None , length, opts) ;
166+
167+ return perform_checksum_validation ( files, algo_cli, length, opts) ;
171168 }
172169
173170 // Not --check
@@ -177,12 +174,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
177174 print_cpu_debug_info ( ) ;
178175 }
179176
177+ // Set the default algorithm to CRC when not '--check'ing.
178+ let algo_kind = algo_cli. unwrap_or ( AlgoKind :: Crc ) ;
180179
181- let tag = matches. get_flag ( options:: TAG ) || !matches. get_flag ( options:: UNTAGGED ) ;
180+ // --untagged is cksum specific. So we use this form...
181+ let is_cksum = binary_name. ends_with ( "cksum" ) ;
182+ // clap cannot overrides_with --binary by --tag unilaterally...
183+ let tag = ( !is_cksum && std:: env:: args ( ) . any ( |a| a == "--tag" || a == "-t" ) ) || ( is_cksum && !matches. get_flag ( options:: UNTAGGED ) ) ;
182184 let binary = matches. get_flag ( options:: BINARY ) ;
183185
184186 let algo = SizedAlgoKind :: from_unsized ( algo_kind, length) ?;
185-
186187 let line_ending = LineEnding :: from_zero_flag ( matches. get_flag ( options:: ZERO ) ) ;
187188
188189 let opts = ChecksumComputeOptions {
@@ -223,17 +224,9 @@ pub fn uu_app() -> Command {
223224 . long ( options:: TAG )
224225 . help ( translate ! ( "cksum-help-tag" ) )
225226 . action ( ArgAction :: SetTrue )
226- . overrides_with ( options:: UNTAGGED )
227227 . overrides_with ( options:: BINARY )
228228 . overrides_with ( options:: TEXT ) ,
229229 )
230- . arg (
231- Arg :: new ( options:: LENGTH )
232- . long ( options:: LENGTH )
233- . short ( 'l' )
234- . help ( translate ! ( "cksum-help-length" ) )
235- . action ( ArgAction :: Set ) ,
236- )
237230 . arg (
238231 Arg :: new ( options:: RAW )
239232 . long ( options:: RAW )
@@ -268,16 +261,13 @@ pub fn uu_app() -> Command {
268261 . long ( options:: TEXT )
269262 . short ( 't' )
270263 . hide ( true )
271- . overrides_with ( options:: BINARY )
272- . action ( ArgAction :: SetTrue )
273- . requires ( options:: UNTAGGED ) ,
264+ . action ( ArgAction :: SetTrue ) ,
274265 )
275266 . arg (
276267 Arg :: new ( options:: BINARY )
277268 . long ( options:: BINARY )
278269 . short ( 'b' )
279270 . hide ( true )
280- . overrides_with ( options:: TEXT )
281271 . action ( ArgAction :: SetTrue ) ,
282272 )
283273 . arg (
@@ -336,6 +326,7 @@ pub fn uu_app() -> Command {
336326 }
337327 if binary_name. ends_with ( "cksum" ) {
338328 app = app
329+ . mut_arg ( options:: TEXT , |a : Arg | a. requires ( options:: UNTAGGED ) )
339330 . arg (
340331 Arg :: new ( options:: ALGORITHM )
341332 . long ( options:: ALGORITHM )
0 commit comments