@@ -124,42 +124,86 @@ const POSIXLY_CORRECT_BLOCK_SIZE: u64 = 512;
124124const DEFAULT_BLOCK_SIZE : u64 = 1024 ;
125125const DEFAULT_FILE_SIZE_BLOCK_SIZE : u64 = 1 ;
126126
127- /// Resolve `(file_size_block_size, block_size)` from environment variables.
128- ///
129- /// `LS_BLOCK_SIZE` and `BLOCK_SIZE` affect both values.
130- /// `BLOCKSIZE` only affects `block_size` (allocation display with `-s`).
131- /// `POSIXLY_CORRECT` sets `block_size` to 512 as a last resort.
132- /// `-k` (`opt_kb`) forces `block_size` to `DEFAULT_BLOCK_SIZE`.
133- fn resolve_block_sizes_from_env ( opt_kb : bool ) -> ( u64 , u64 ) {
134- match parse_block_size:: block_size_from_env ( & [ "LS_BLOCK_SIZE" , "BLOCK_SIZE" ] ) {
135- parse_block_size:: BlockSizeEnv :: Found ( size) => {
127+ /// The block sizes `ls` divides by, plus the units echoed next to them.
128+ struct BlockSizes {
129+ /// Divisor for file sizes (the size column of `-l`).
130+ file_size : u64 ,
131+ /// Divisor for allocated sizes (`-s`, and the `total` line).
132+ alloc : u64 ,
133+ /// Suffix echoed next to a file size, if any.
134+ file_size_suffix : Option < String > ,
135+ /// Suffix echoed next to an allocated size, if any.
136+ alloc_suffix : Option < String > ,
137+ }
138+
139+ impl BlockSizes {
140+ /// Both columns share one spec, as with `--block-size=SIZE`.
141+ fn uniform ( size : u64 , suffix : Option < String > ) -> Self {
142+ Self {
143+ file_size : size,
144+ alloc : size,
145+ file_size_suffix : suffix. clone ( ) ,
146+ alloc_suffix : suffix,
147+ }
148+ }
149+
150+ /// Plain sizes with no suffix to echo.
151+ fn plain ( file_size : u64 , alloc : u64 ) -> Self {
152+ Self {
153+ file_size,
154+ alloc,
155+ file_size_suffix : None ,
156+ alloc_suffix : None ,
157+ }
158+ }
159+ }
160+
161+ /// Resolve the block sizes from the environment, in GNU's precedence order:
162+ /// `LS_BLOCK_SIZE`/`BLOCK_SIZE` set both, `BLOCKSIZE` only the allocation
163+ /// one, `POSIXLY_CORRECT` 512 as a last resort. `-k` (`opt_kb`) resets the
164+ /// allocation block size and its unit, leaving the file-size ones alone.
165+ fn resolve_block_sizes_from_env ( opt_kb : bool ) -> BlockSizes {
166+ match parse_block_size:: block_size_from_env_with_suffix ( & [ "LS_BLOCK_SIZE" , "BLOCK_SIZE" ] ) {
167+ ( parse_block_size:: BlockSizeEnv :: Found ( size) , suffix) => {
136168 if opt_kb {
137- ( size, DEFAULT_BLOCK_SIZE )
169+ BlockSizes {
170+ file_size : size,
171+ alloc : DEFAULT_BLOCK_SIZE ,
172+ file_size_suffix : suffix,
173+ alloc_suffix : None ,
174+ }
138175 } else {
139- ( size, size )
176+ BlockSizes :: uniform ( size, suffix )
140177 }
141178 }
142- parse_block_size:: BlockSizeEnv :: SetButInvalid => ( DEFAULT_BLOCK_SIZE , DEFAULT_BLOCK_SIZE ) ,
143- parse_block_size:: BlockSizeEnv :: NotSet => {
179+ ( parse_block_size:: BlockSizeEnv :: SetButInvalid , _) => {
180+ BlockSizes :: plain ( DEFAULT_BLOCK_SIZE , DEFAULT_BLOCK_SIZE )
181+ }
182+ ( parse_block_size:: BlockSizeEnv :: NotSet , _) => {
144183 // Neither LS_BLOCK_SIZE nor BLOCK_SIZE was set; check BLOCKSIZE
145184 // which only affects allocation display, not file size.
146- match parse_block_size:: block_size_from_env ( & [ "BLOCKSIZE" ] ) {
147- parse_block_size:: BlockSizeEnv :: Found ( size) => {
185+ match parse_block_size:: block_size_from_env_with_suffix ( & [ "BLOCKSIZE" ] ) {
186+ ( parse_block_size:: BlockSizeEnv :: Found ( size) , suffix ) => {
148187 if opt_kb {
149- ( DEFAULT_FILE_SIZE_BLOCK_SIZE , DEFAULT_BLOCK_SIZE )
188+ BlockSizes :: plain ( DEFAULT_FILE_SIZE_BLOCK_SIZE , DEFAULT_BLOCK_SIZE )
150189 } else {
151- ( DEFAULT_FILE_SIZE_BLOCK_SIZE , size)
190+ BlockSizes {
191+ file_size : DEFAULT_FILE_SIZE_BLOCK_SIZE ,
192+ alloc : size,
193+ file_size_suffix : None ,
194+ alloc_suffix : suffix,
195+ }
152196 }
153197 }
154- parse_block_size:: BlockSizeEnv :: SetButInvalid => {
198+ ( parse_block_size:: BlockSizeEnv :: SetButInvalid , _ ) => {
155199 // BLOCKSIZE was set but invalid: stop lookup, use defaults.
156- ( DEFAULT_FILE_SIZE_BLOCK_SIZE , DEFAULT_BLOCK_SIZE )
200+ BlockSizes :: plain ( DEFAULT_FILE_SIZE_BLOCK_SIZE , DEFAULT_BLOCK_SIZE )
157201 }
158- parse_block_size:: BlockSizeEnv :: NotSet => {
202+ ( parse_block_size:: BlockSizeEnv :: NotSet , _ ) => {
159203 if std:: env:: var_os ( "POSIXLY_CORRECT" ) . is_some ( ) && !opt_kb {
160- ( DEFAULT_FILE_SIZE_BLOCK_SIZE , POSIXLY_CORRECT_BLOCK_SIZE )
204+ BlockSizes :: plain ( DEFAULT_FILE_SIZE_BLOCK_SIZE , POSIXLY_CORRECT_BLOCK_SIZE )
161205 } else {
162- ( DEFAULT_FILE_SIZE_BLOCK_SIZE , DEFAULT_BLOCK_SIZE )
206+ BlockSizes :: plain ( DEFAULT_FILE_SIZE_BLOCK_SIZE , DEFAULT_BLOCK_SIZE )
163207 }
164208 }
165209 }
@@ -212,6 +256,13 @@ pub struct Config {
212256 pub ( crate ) file_size_block_size : u64 ,
213257 #[ allow( dead_code) ]
214258 pub ( crate ) block_size : u64 , // is never read on Windows
259+ /// Display suffix (e.g. `"K"`) echoed back next to a file size, set when
260+ /// `--block-size`/`LS_BLOCK_SIZE`/`BLOCK_SIZE` held a suffix-only spec
261+ /// rather than a numeric one.
262+ pub ( crate ) file_size_block_size_suffix : Option < String > ,
263+ /// Display suffix echoed back next to an allocated size (`-s`, `total`).
264+ /// `BLOCKSIZE` feeds this one only; `-k` clears it.
265+ pub ( crate ) block_size_suffix : Option < String > ,
215266 pub ( crate ) width : u16 ,
216267 // Dir and vdir needs access to this field
217268 pub quoting_style : QuotingStyle ,
@@ -754,13 +805,13 @@ impl Config {
754805 SizeFormat :: Bytes
755806 } ;
756807
757- let ( file_size_block_size , block_size ) = if let Some ( opt_block_size) = opt_block_size {
808+ let block_sizes = if let Some ( opt_block_size) = opt_block_size {
758809 // --block-size command-line argument: parse it, error on invalid
759810 // If --block-size=si or --block-size=human-readable, skip numeric parsing
760811 if opt_si {
761- ( DEFAULT_FILE_SIZE_BLOCK_SIZE , 1000 )
812+ BlockSizes :: plain ( DEFAULT_FILE_SIZE_BLOCK_SIZE , 1000 )
762813 } else if opt_hr {
763- ( DEFAULT_FILE_SIZE_BLOCK_SIZE , DEFAULT_BLOCK_SIZE )
814+ BlockSizes :: plain ( DEFAULT_FILE_SIZE_BLOCK_SIZE , DEFAULT_BLOCK_SIZE )
764815 } else {
765816 let size = parse_size_non_zero_u64 ( opt_block_size) . map_err ( |error| {
766817 let ls_error = LsError :: BlockSizeParseError ( opt_block_size. clone ( ) ) ;
@@ -778,15 +829,22 @@ impl Config {
778829 )
779830 } ) ?;
780831 // --block-size overrides -k
781- ( size, size)
832+ let suffix = parse_block_size:: suffix_from_parsed_block_size ( opt_block_size) ;
833+ BlockSizes :: uniform ( size, suffix)
782834 }
783835 } else if !opt_si && !opt_hr {
784836 resolve_block_sizes_from_env ( opt_kb)
785837 } else if opt_si {
786- ( DEFAULT_FILE_SIZE_BLOCK_SIZE , 1000 )
838+ BlockSizes :: plain ( DEFAULT_FILE_SIZE_BLOCK_SIZE , 1000 )
787839 } else {
788- ( DEFAULT_FILE_SIZE_BLOCK_SIZE , DEFAULT_BLOCK_SIZE )
840+ BlockSizes :: plain ( DEFAULT_FILE_SIZE_BLOCK_SIZE , DEFAULT_BLOCK_SIZE )
789841 } ;
842+ let BlockSizes {
843+ file_size : file_size_block_size,
844+ alloc : block_size,
845+ file_size_suffix : file_size_block_size_suffix,
846+ alloc_suffix : block_size_suffix,
847+ } = block_sizes;
790848
791849 let long = {
792850 let author = options. get_flag ( options:: AUTHOR ) ;
@@ -1004,6 +1062,8 @@ impl Config {
10041062 alloc_size : options. get_flag ( options:: size:: ALLOCATION_SIZE ) ,
10051063 file_size_block_size,
10061064 block_size,
1065+ file_size_block_size_suffix,
1066+ block_size_suffix,
10071067 width,
10081068 quoting_style,
10091069 locale_quoting,
0 commit comments