1 /* Reformat numbers like 11505426432 to the more human-readable 11G
2 Copyright (C) 2012-2023 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
20 #include <stdckdint.h>
22 #include <sys/types.h>
32 #include "set-fields.h"
38 /* The official name of this program (e.g., no 'g' prefix). */
39 #define PROGRAM_NAME "numfmt"
41 #define AUTHORS proper_name ("Assaf Gordon")
43 /* Exit code when some numbers fail to convert. */
44 enum { EXIT_CONVERSION_WARNINGS
= 2 };
48 FROM_OPTION
= CHAR_MAX
+ 1,
66 scale_none
, /* the default: no scaling. */
67 scale_auto
, /* --from only. */
70 scale_IEC_I
/* 'i' suffix is required. */
73 static char const *const scale_from_args
[] =
75 "none", "auto", "si", "iec", "iec-i", nullptr
78 static enum scale_type
const scale_from_types
[] =
80 scale_none
, scale_auto
, scale_SI
, scale_IEC
, scale_IEC_I
83 static char const *const scale_to_args
[] =
85 "none", "si", "iec", "iec-i", nullptr
88 static enum scale_type
const scale_to_types
[] =
90 scale_none
, scale_SI
, scale_IEC
, scale_IEC_I
103 static char const *const round_args
[] =
105 "up", "down", "from-zero", "towards-zero", "nearest", nullptr
108 static enum round_type
const round_types
[] =
110 round_ceiling
, round_floor
, round_from_zero
, round_to_zero
, round_nearest
122 static char const *const inval_args
[] =
124 "abort", "fail", "warn", "ignore", nullptr
127 static enum inval_type
const inval_types
[] =
129 inval_abort
, inval_fail
, inval_warn
, inval_ignore
132 static struct option
const longopts
[] =
134 {"from", required_argument
, nullptr, FROM_OPTION
},
135 {"from-unit", required_argument
, nullptr, FROM_UNIT_OPTION
},
136 {"to", required_argument
, nullptr, TO_OPTION
},
137 {"to-unit", required_argument
, nullptr, TO_UNIT_OPTION
},
138 {"round", required_argument
, nullptr, ROUND_OPTION
},
139 {"padding", required_argument
, nullptr, PADDING_OPTION
},
140 {"suffix", required_argument
, nullptr, SUFFIX_OPTION
},
141 {"grouping", no_argument
, nullptr, GROUPING_OPTION
},
142 {"delimiter", required_argument
, nullptr, 'd'},
143 {"field", required_argument
, nullptr, FIELD_OPTION
},
144 {"debug", no_argument
, nullptr, DEBUG_OPTION
},
145 {"-debug", no_argument
, nullptr, DEV_DEBUG_OPTION
},
146 {"header", optional_argument
, nullptr, HEADER_OPTION
},
147 {"format", required_argument
, nullptr, FORMAT_OPTION
},
148 {"invalid", required_argument
, nullptr, INVALID_OPTION
},
149 {"zero-terminated", no_argument
, nullptr, 'z'},
150 {GETOPT_HELP_OPTION_DECL
},
151 {GETOPT_VERSION_OPTION_DECL
},
152 {nullptr, 0, nullptr, 0}
155 /* If delimiter has this value, blanks separate fields. */
156 enum { DELIMITER_DEFAULT
= CHAR_MAX
+ 1 };
158 /* Maximum number of digits we can safely handle
159 without precision loss, if scaling is 'none'. */
160 enum { MAX_UNSCALED_DIGITS
= LDBL_DIG
};
162 /* Maximum number of digits we can work with.
163 This is equivalent to 999Q.
164 NOTE: 'long double' can handle more than that, but there's
165 no official suffix assigned beyond Quetta (1000^10). */
166 enum { MAX_ACCEPTABLE_DIGITS
= 33 };
168 static enum scale_type scale_from
= scale_none
;
169 static enum scale_type scale_to
= scale_none
;
170 static enum round_type round_style
= round_from_zero
;
171 static enum inval_type inval_style
= inval_abort
;
172 static char const *suffix
= nullptr;
173 static uintmax_t from_unit_size
= 1;
174 static uintmax_t to_unit_size
= 1;
175 static int grouping
= 0;
176 static char *padding_buffer
= nullptr;
177 static idx_t padding_buffer_size
= 0;
178 static intmax_t padding_width
= 0;
179 static int zero_padding_width
= 0;
180 static long int user_precision
= -1;
181 static char const *format_str
= nullptr;
182 static char *format_str_prefix
= nullptr;
183 static char *format_str_suffix
= nullptr;
185 /* By default, any conversion error will terminate the program. */
186 static int conv_exit_code
= EXIT_CONVERSION_WARNINGS
;
189 /* auto-pad each line based on skipped whitespace. */
190 static int auto_padding
= 0;
192 /* field delimiter */
193 static int delimiter
= DELIMITER_DEFAULT
;
195 /* line delimiter. */
196 static unsigned char line_delim
= '\n';
198 /* if non-zero, the first 'header' lines from STDIN are skipped. */
199 static uintmax_t header
= 0;
201 /* Debug for users: print warnings to STDERR about possible
202 error (similar to sort's debug). */
205 /* will be set according to the current locale. */
206 static char const *decimal_point
;
207 static int decimal_point_length
;
209 /* debugging for developers. Enables devmsg(). */
210 static bool dev_debug
= false;
214 default_scale_base (enum scale_type scale
)
230 static char const zero_and_valid_suffixes
[] = "0KMGTPEZYRQ";
231 static char const *valid_suffixes
= 1 + zero_and_valid_suffixes
;
234 valid_suffix (const char suf
)
236 return strchr (valid_suffixes
, suf
) != nullptr;
240 suffix_power (const char suf
)
244 case 'K': /* kilo or kibi. */
247 case 'M': /* mega or mebi. */
250 case 'G': /* giga or gibi. */
253 case 'T': /* tera or tebi. */
256 case 'P': /* peta or pebi. */
259 case 'E': /* exa or exbi. */
262 case 'Z': /* zetta or 2**70. */
265 case 'Y': /* yotta or 2**80. */
268 case 'R': /* ronna or 2**90. */
271 case 'Q': /* quetta or 2**100. */
274 default: /* should never happen. assert? */
279 static inline char const *
280 suffix_power_char (int power
)
322 /* Similar to 'powl(3)' but without requiring 'libm'. */
324 powerld (long double base
, int x
)
326 long double result
= base
;
328 return 1; /* note for test coverage: this is never
329 reached, as 'powerld' won't be called if
330 there's no suffix, hence, no "power". */
332 /* TODO: check for overflow, inf? */
338 /* Similar to 'fabs(3)' but without requiring 'libm'. */
339 static inline long double
340 absld (long double val
)
342 return val
< 0 ? -val
: val
;
345 /* Scale down 'val', returns 'updated val' and 'x', such that
346 val*base^X = original val
347 Similar to "frexpl(3)" but without requiring 'libm',
348 allowing only integer scale, limited functionality and error checking. */
350 expld (long double val
, int base
, int /*output */ *x
)
354 if (val
>= -LDBL_MAX
&& val
<= LDBL_MAX
)
356 while (absld (val
) >= base
)
367 /* EXTREMELY limited 'ceil' - without 'libm'.
368 Assumes values that fit in intmax_t. */
369 static inline intmax_t
370 simple_round_ceiling (long double val
)
372 intmax_t intval
= val
;
378 /* EXTREMELY limited 'floor' - without 'libm'.
379 Assumes values that fit in intmax_t. */
380 static inline intmax_t
381 simple_round_floor (long double val
)
383 return -simple_round_ceiling (-val
);
386 /* EXTREMELY limited 'round away from zero'.
387 Assumes values that fit in intmax_t. */
388 static inline intmax_t
389 simple_round_from_zero (long double val
)
391 return val
< 0 ? simple_round_floor (val
) : simple_round_ceiling (val
);
394 /* EXTREMELY limited 'round away to zero'.
395 Assumes values that fit in intmax_t. */
396 static inline intmax_t
397 simple_round_to_zero (long double val
)
402 /* EXTREMELY limited 'round' - without 'libm'.
403 Assumes values that fit in intmax_t. */
404 static inline intmax_t
405 simple_round_nearest (long double val
)
407 return val
< 0 ? val
- 0.5 : val
+ 0.5;
411 static inline long double
412 simple_round (long double val
, enum round_type t
)
415 intmax_t intmax_mul
= val
/ INTMAX_MAX
;
416 val
-= (long double) INTMAX_MAX
* intmax_mul
;
421 rval
= simple_round_ceiling (val
);
425 rval
= simple_round_floor (val
);
428 case round_from_zero
:
429 rval
= simple_round_from_zero (val
);
433 rval
= simple_round_to_zero (val
);
437 rval
= simple_round_nearest (val
);
441 /* to silence the compiler - this should never happen. */
445 return (long double) INTMAX_MAX
* intmax_mul
+ rval
;
448 enum simple_strtod_error
451 SSE_OK_PRECISION_LOSS
,
455 /* the following are returned by 'simple_strtod_human'. */
456 SSE_VALID_BUT_FORBIDDEN_SUFFIX
,
461 /* Read an *integer* INPUT_STR,
462 but return the integer value in a 'long double' VALUE
463 hence, no UINTMAX_MAX limitation.
464 NEGATIVE is updated, and is stored separately from the VALUE
465 so that signbit() isn't required to determine the sign of -0..
466 ENDPTR is required (unlike strtod) and is used to store a pointer
467 to the character after the last character used in the conversion.
469 Note locale'd grouping is not supported,
470 nor is skipping of white-space supported.
473 SSE_OK - valid number.
474 SSE_OK_PRECISION_LOSS - if more than 18 digits were used.
475 SSE_OVERFLOW - if more than 33 digits (999Q) were used.
476 SSE_INVALID_NUMBER - if no digits were found. */
477 static enum simple_strtod_error
478 simple_strtod_int (char const *input_str
,
479 char **endptr
, long double *value
, bool *negative
)
481 enum simple_strtod_error e
= SSE_OK
;
485 bool found_digit
= false;
487 if (*input_str
== '-')
495 *endptr
= (char *) input_str
;
496 while (c_isdigit (**endptr
))
498 int digit
= (**endptr
) - '0';
505 if (digits
> MAX_UNSCALED_DIGITS
)
506 e
= SSE_OK_PRECISION_LOSS
;
508 if (digits
> MAX_ACCEPTABLE_DIGITS
)
517 && ! STREQ_LEN (*endptr
, decimal_point
, decimal_point_length
))
518 return SSE_INVALID_NUMBER
;
528 /* Read a floating-point INPUT_STR represented as "NNNN[.NNNNN]",
529 and return the value in a 'long double' VALUE.
530 ENDPTR is required (unlike strtod) and is used to store a pointer
531 to the character after the last character used in the conversion.
532 PRECISION is optional and used to indicate fractions are present.
534 Note locale'd grouping is not supported,
535 nor is skipping of white-space supported.
538 SSE_OK - valid number.
539 SSE_OK_PRECISION_LOSS - if more than 18 digits were used.
540 SSE_OVERFLOW - if more than 33 digits (999Q) were used.
541 SSE_INVALID_NUMBER - if no digits were found. */
542 static enum simple_strtod_error
543 simple_strtod_float (char const *input_str
,
549 enum simple_strtod_error e
= SSE_OK
;
554 /* TODO: accept locale'd grouped values for the integral part. */
555 e
= simple_strtod_int (input_str
, endptr
, value
, &negative
);
556 if (e
!= SSE_OK
&& e
!= SSE_OK_PRECISION_LOSS
)
559 /* optional decimal point + fraction. */
560 if (STREQ_LEN (*endptr
, decimal_point
, decimal_point_length
))
563 long double val_frac
= 0;
566 (*endptr
) += decimal_point_length
;
567 enum simple_strtod_error e2
=
568 simple_strtod_int (*endptr
, &ptr2
, &val_frac
, &neg_frac
);
569 if (e2
!= SSE_OK
&& e2
!= SSE_OK_PRECISION_LOSS
)
571 if (e2
== SSE_OK_PRECISION_LOSS
)
572 e
= e2
; /* propagate warning. */
574 return SSE_INVALID_NUMBER
;
576 /* number of digits in the fractions. */
577 size_t exponent
= ptr2
- *endptr
;
579 val_frac
= ((long double) val_frac
) / powerld (10, exponent
);
581 /* TODO: detect loss of precision (only really 18 digits
582 of precision across all digits (before and after '.')). */
592 *precision
= exponent
;
599 /* Read a 'human' INPUT_STR represented as "NNNN[.NNNNN] + suffix",
600 and return the value in a 'long double' VALUE,
601 with the precision of the input returned in PRECISION.
602 ENDPTR is required (unlike strtod) and is used to store a pointer
603 to the character after the last character used in the conversion.
604 ALLOWED_SCALING determines the scaling supported.
607 support locale'd grouping
608 accept scientific and hex floats (probably use strtold directly)
611 SSE_OK - valid number.
612 SSE_OK_PRECISION_LOSS - if more than LDBL_DIG digits were used.
613 SSE_OVERFLOW - if more than 33 digits (999Q) were used.
614 SSE_INVALID_NUMBER - if no digits were found.
615 SSE_VALID_BUT_FORBIDDEN_SUFFIX
617 SSE_MISSING_I_SUFFIX */
618 static enum simple_strtod_error
619 simple_strtod_human (char const *input_str
,
620 char **endptr
, long double *value
, size_t *precision
,
621 enum scale_type allowed_scaling
)
624 /* 'scale_auto' is checked below. */
625 int scale_base
= default_scale_base (allowed_scaling
);
627 devmsg ("simple_strtod_human:\n input string: %s\n"
628 " locale decimal-point: %s\n"
629 " MAX_UNSCALED_DIGITS: %d\n",
630 quote_n (0, input_str
),
631 quote_n (1, decimal_point
),
632 MAX_UNSCALED_DIGITS
);
634 enum simple_strtod_error e
=
635 simple_strtod_float (input_str
, endptr
, value
, precision
);
636 if (e
!= SSE_OK
&& e
!= SSE_OK_PRECISION_LOSS
)
639 devmsg (" parsed numeric value: %Lf\n"
640 " input precision = %d\n", *value
, (int)*precision
);
642 if (**endptr
!= '\0')
644 /* process suffix. */
646 /* Skip any blanks between the number and suffix. */
647 while (isblank (to_uchar (**endptr
)))
650 if (!valid_suffix (**endptr
))
651 return SSE_INVALID_SUFFIX
;
653 if (allowed_scaling
== scale_none
)
654 return SSE_VALID_BUT_FORBIDDEN_SUFFIX
;
656 power
= suffix_power (**endptr
);
657 (*endptr
)++; /* skip first suffix character. */
659 if (allowed_scaling
== scale_auto
&& **endptr
== 'i')
661 /* auto-scaling enabled, and the first suffix character
662 is followed by an 'i' (e.g. Ki, Mi, Gi). */
664 (*endptr
)++; /* skip second ('i') suffix character. */
665 devmsg (" Auto-scaling, found 'i', switching to base %d\n",
669 *precision
= 0; /* Reset, to select precision based on scale. */
672 if (allowed_scaling
== scale_IEC_I
)
677 return SSE_MISSING_I_SUFFIX
;
680 long double multiplier
= powerld (scale_base
, power
);
682 devmsg (" suffix power=%d^%d = %Lf\n", scale_base
, power
, multiplier
);
684 /* TODO: detect loss of precision and overflows. */
685 (*value
) = (*value
) * multiplier
;
687 devmsg (" returning value: %Lf (%LG)\n", *value
, *value
);
694 simple_strtod_fatal (enum simple_strtod_error err
, char const *input_str
)
696 char const *msgid
= nullptr;
700 case SSE_OK_PRECISION_LOSS
:
702 /* should never happen - this function isn't called when OK. */
706 msgid
= N_("value too large to be converted: %s");
709 case SSE_INVALID_NUMBER
:
710 msgid
= N_("invalid number: %s");
713 case SSE_VALID_BUT_FORBIDDEN_SUFFIX
:
714 msgid
= N_("rejecting suffix in input: %s (consider using --from)");
717 case SSE_INVALID_SUFFIX
:
718 msgid
= N_("invalid suffix in input: %s");
721 case SSE_MISSING_I_SUFFIX
:
722 msgid
= N_("missing 'i' suffix in input: %s (e.g Ki/Mi/Gi)");
727 if (inval_style
!= inval_ignore
)
728 error (conv_exit_code
, 0, gettext (msgid
), quote (input_str
));
731 /* Convert VAL to a human format string using PRECISION in BUF of size
732 BUF_SIZE. Use SCALE, GROUP, and ROUND to format. Return
733 the number of bytes needed to represent VAL. If this number is not
734 less than BUF_SIZE, the buffer is too small; if it is negative, the
735 formatting failed for some reason. */
737 double_to_human (long double val
, int precision
,
738 char *buf
, idx_t buf_size
,
739 enum scale_type scale
, int group
, enum round_type round
)
741 char fmt
[sizeof "%'0.*Lfi%s%s%s" + INT_STRLEN_BOUND (zero_padding_width
)];
748 if (zero_padding_width
)
749 pfmt
+= sprintf (pfmt
, "0%d", zero_padding_width
);
751 devmsg ("double_to_human:\n");
753 if (scale
== scale_none
)
755 val
*= powerld (10, precision
);
756 val
= simple_round (val
, round
);
757 val
/= powerld (10, precision
);
760 " no scaling, returning (grouped) value: %'.*Lf\n" :
761 " no scaling, returning value: %.*Lf\n", precision
, val
);
763 strcpy (pfmt
, ".*Lf%s");
765 return snprintf (buf
, buf_size
, fmt
, precision
, val
,
766 suffix
? suffix
: "");
769 /* Scaling requested by user. */
770 double scale_base
= default_scale_base (scale
);
772 /* Normalize val to scale. */
774 val
= expld (val
, scale_base
, &power
);
775 devmsg (" scaled value to %Lf * %0.f ^ %d\n", val
, scale_base
, power
);
777 /* Perform rounding. */
778 int power_adjust
= 0;
779 if (user_precision
!= -1)
780 power_adjust
= MIN (power
* 3, user_precision
);
781 else if (absld (val
) < 10)
783 /* for values less than 10, we allow one decimal-point digit,
784 so adjust before rounding. */
788 val
*= powerld (10, power_adjust
);
789 val
= simple_round (val
, round
);
790 val
/= powerld (10, power_adjust
);
792 /* two special cases after rounding:
793 1. a "999.99" can turn into 1000 - so scale down
794 2. a "9.99" can turn into 10 - so don't display decimal-point. */
795 if (absld (val
) >= scale_base
)
801 /* should "7.0" be printed as "7" ?
802 if removing the ".0" is preferred, enable the fourth condition. */
803 int show_decimal_point
= (val
!= 0) && (absld (val
) < 10) && (power
> 0);
804 /* && (absld (val) > simple_round_floor (val))) */
806 devmsg (" after rounding, value=%Lf * %0.f ^ %d\n", val
, scale_base
, power
);
808 strcpy (pfmt
, ".*Lf%s%s%s");
810 int prec
= user_precision
== -1 ? show_decimal_point
: user_precision
;
812 return snprintf (buf
, buf_size
, fmt
, prec
, val
,
813 suffix_power_char (power
),
814 &"i"[! (scale
== scale_IEC_I
&& 0 < power
)],
815 suffix
? suffix
: "");
818 /* Convert a string of decimal digits, N_STRING, with an optional suffix
819 to an integral value. Suffixes are handled as with --from=auto.
820 Upon successful conversion, return that value.
821 If it cannot be converted, give a diagnostic and exit. */
823 unit_to_umax (char const *n_string
)
826 char const *c_string
= n_string
;
827 char *t_string
= nullptr;
828 size_t n_len
= strlen (n_string
);
831 char const *suffixes
= valid_suffixes
;
833 /* Adjust suffixes so K=1000, Ki=1024, KiB=invalid. */
834 if (n_len
&& ! c_isdigit (n_string
[n_len
- 1]))
836 t_string
= xmalloc (n_len
+ 2);
837 end
= t_string
+ n_len
- 1;
838 memcpy (t_string
, n_string
, n_len
);
840 if (*end
== 'i' && 2 <= n_len
&& ! c_isdigit (*(end
- 1)))
846 suffixes
= zero_and_valid_suffixes
;
852 s_err
= xstrtoumax (c_string
, &end
, 10, &n
, suffixes
);
854 if (s_err
!= LONGINT_OK
|| *end
|| n
== 0)
857 error (EXIT_FAILURE
, 0, _("invalid unit size: %s"), quote (n_string
));
868 if (status
!= EXIT_SUCCESS
)
873 Usage: %s [OPTION]... [NUMBER]...\n\
876 Reformat NUMBER(s), or the numbers from standard input if none are specified.\n\
878 emit_mandatory_arg_note ();
880 --debug print warnings about invalid input\n\
883 -d, --delimiter=X use X instead of whitespace for field delimiter\n\
886 --field=FIELDS replace the numbers in these input fields (default=1);\n\
890 --format=FORMAT use printf style floating-point FORMAT;\n\
891 see FORMAT below for details\n\
894 --from=UNIT auto-scale input numbers to UNITs; default is 'none';\n\
898 --from-unit=N specify the input unit size (instead of the default 1)\n\
901 --grouping use locale-defined grouping of digits, e.g. 1,000,000\n\
902 (which means it has no effect in the C/POSIX locale)\n\
905 --header[=N] print (without converting) the first N header lines;\n\
906 N defaults to 1 if not specified\n\
909 --invalid=MODE failure mode for invalid numbers: MODE can be:\n\
910 abort (default), fail, warn, ignore\n\
913 --padding=N pad the output to N characters; positive N will\n\
914 right-align; negative N will left-align;\n\
915 padding is ignored if the output is wider than N;\n\
916 the default is to automatically pad if a whitespace\n\
920 --round=METHOD use METHOD for rounding when scaling; METHOD can be:\n\
921 up, down, from-zero (default), towards-zero, nearest\n\
924 --suffix=SUFFIX add SUFFIX to output numbers, and accept optional\n\
925 SUFFIX in input numbers\n\
928 --to=UNIT auto-scale output numbers to UNITs; see UNIT below\n\
931 --to-unit=N the output unit size (instead of the default 1)\n\
934 -z, --zero-terminated line delimiter is NUL, not newline\n\
936 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
937 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
941 UNIT options:\n"), stdout
);
943 none no auto-scaling is done; suffixes will trigger an error\n\
946 auto accept optional single/two letter suffix:\n\
950 1Mi = 1048576,\n"), stdout
);
952 si accept optional single letter suffix:\n\
957 iec accept optional single letter suffix:\n\
962 iec-i accept optional two-letter suffix:\n\
968 FIELDS supports cut(1) style field ranges:\n\
969 N N'th field, counted from 1\n\
970 N- from N'th field, to end of line\n\
971 N-M from N'th to M'th field (inclusive)\n\
972 -M from first to M'th field (inclusive)\n\
974 Multiple fields/ranges can be separated with commas\n\
978 FORMAT must be suitable for printing one floating-point argument '%f'.\n\
979 Optional quote (%'f) will enable --grouping (if supported by current locale).\n\
980 Optional width value (%10f) will pad output. Optional zero (%010f) width\n\
981 will zero pad the number. Optional negative values (%-10f) will left align.\n\
982 Optional precision (%.1f) will override the input determined precision.\n\
986 Exit status is 0 if all input numbers were successfully converted.\n\
987 By default, %s will stop at the first conversion error with exit status 2.\n\
988 With --invalid='fail' a warning is printed for each conversion error\n\
989 and the exit status is 2. With --invalid='warn' each conversion error is\n\
990 diagnosed, but the exit status is 0. With --invalid='ignore' conversion\n\
991 errors are not diagnosed and the exit status is 0.\n\
998 $ %s --to=iec 2048\n\
1000 $ %s --to=iec-i 4096\n\
1002 $ echo 1K | %s --from=si\n\
1004 $ echo 1K | %s --from=iec\n\
1006 $ df -B1 | %s --header --field 2-4 --to=si\n\
1007 $ ls -l | %s --header --field 5 --to=iec\n\
1008 $ ls -lh | %s --header --field 5 --from=iec --padding=10\n\
1009 $ ls -lh | %s --header --field 5 --from=iec --format %%10f\n"),
1010 program_name
, program_name
, program_name
,
1011 program_name
, program_name
, program_name
,
1012 program_name
, program_name
, program_name
);
1013 emit_ancillary_info (PROGRAM_NAME
);
1018 /* Given 'fmt' (a printf(3) compatible format string), extracts the following:
1019 1. padding (e.g. %20f)
1020 2. alignment (e.g. %-20f)
1021 3. grouping (e.g. %'f)
1023 Only a limited subset of printf(3) syntax is supported.
1026 support %e %g etc. rather than just %f
1029 1. This function sets the global variables:
1030 padding_width, grouping,
1031 format_str_prefix, format_str_suffix
1032 2. The function aborts on any errors. */
1034 parse_format_string (char const *fmt
)
1037 size_t prefix_len
= 0;
1039 char *endptr
= nullptr;
1040 bool zero_padding
= false;
1042 for (i
= 0; !(fmt
[i
] == '%' && fmt
[i
+ 1] != '%'); i
+= (fmt
[i
] == '%') + 1)
1045 error (EXIT_FAILURE
, 0,
1046 _("format %s has no %% directive"), quote (fmt
));
1053 size_t skip
= strspn (fmt
+ i
, " ");
1060 else if (fmt
[i
] == '0')
1062 zero_padding
= true;
1069 intmax_t pad
= strtoimax (fmt
+ i
, &endptr
, 10);
1073 if (debug
&& padding_width
&& !(zero_padding
&& pad
> 0))
1074 error (0, 0, _("--format padding overriding --padding"));
1076 /* Set padding width and alignment. On overflow, set widths to
1077 large values that cause later code to avoid undefined behavior
1078 and fail at a reasonable point. */
1080 padding_width
= pad
;
1084 zero_padding_width
= MIN (pad
, INT_MAX
);
1086 padding_width
= pad
;
1092 error (EXIT_FAILURE
, 0, _("format %s ends in %%"), quote (fmt
));
1098 user_precision
= strtol (fmt
+ i
, &endptr
, 10);
1099 if (errno
== ERANGE
|| user_precision
< 0 || SIZE_MAX
< user_precision
1100 || isblank (fmt
[i
]) || fmt
[i
] == '+')
1102 /* Note we disallow negative user_precision to be
1103 consistent with printf(1). POSIX states that
1104 negative precision is only supported (and ignored)
1105 when used with '.*f'. glibc at least will malform
1106 output when passed a direct negative precision. */
1107 error (EXIT_FAILURE
, 0,
1108 _("invalid precision in format %s"), quote (fmt
));
1114 error (EXIT_FAILURE
, 0, _("invalid format %s,"
1115 " directive must be %%[0]['][-][N][.][N]f"),
1120 for (; fmt
[i
] != '\0'; i
+= (fmt
[i
] == '%') + 1)
1121 if (fmt
[i
] == '%' && fmt
[i
+ 1] != '%')
1122 error (EXIT_FAILURE
, 0, _("format %s has too many %% directives"),
1126 format_str_prefix
= ximemdup0 (fmt
, prefix_len
);
1127 if (fmt
[suffix_pos
] != '\0')
1128 format_str_suffix
= xstrdup (fmt
+ suffix_pos
);
1130 devmsg ("format String:\n input: %s\n grouping: %s\n"
1131 " padding width: %jd\n"
1132 " prefix: %s\n suffix: %s\n",
1133 quote_n (0, fmt
), (grouping
) ? "yes" : "no",
1135 quote_n (1, format_str_prefix
? format_str_prefix
: ""),
1136 quote_n (2, format_str_suffix
? format_str_suffix
: ""));
1139 /* Parse a numeric value (with optional suffix) from a string.
1140 Returns a long double value, with input precision.
1142 If there's an error converting the string to value - exits with
1145 If there are any trailing characters after the number
1146 (besides a valid suffix) - exits with an error. */
1147 static enum simple_strtod_error
1148 parse_human_number (char const *str
, long double /*output */ *value
,
1151 char *ptr
= nullptr;
1153 enum simple_strtod_error e
=
1154 simple_strtod_human (str
, &ptr
, value
, precision
, scale_from
);
1155 if (e
!= SSE_OK
&& e
!= SSE_OK_PRECISION_LOSS
)
1157 simple_strtod_fatal (e
, str
);
1161 if (ptr
&& *ptr
!= '\0')
1163 if (inval_style
!= inval_ignore
)
1164 error (conv_exit_code
, 0, _("invalid suffix in input %s: %s"),
1165 quote_n (0, str
), quote_n (1, ptr
));
1166 e
= SSE_INVALID_SUFFIX
;
1172 /* Print the given VAL, using the requested representation.
1173 The number is printed to STDOUT, with padding and alignment. */
1175 prepare_padded_number (const long double val
, size_t precision
,
1178 /* Generate Output. */
1179 size_t precision_used
= user_precision
== -1 ? precision
: user_precision
;
1181 /* Can't reliably print too-large values without auto-scaling. */
1183 expld (val
, 10, &x
);
1185 if (scale_to
== scale_none
1186 && x
+ precision_used
> MAX_UNSCALED_DIGITS
)
1188 if (inval_style
!= inval_ignore
)
1191 error (conv_exit_code
, 0,
1192 _("value/precision too large to be printed: '%Lg/%zu'"
1193 " (consider using --to)"), val
, precision_used
);
1195 error (conv_exit_code
, 0,
1196 _("value too large to be printed: '%Lg'"
1197 " (consider using --to)"), val
);
1202 if (x
> MAX_ACCEPTABLE_DIGITS
- 1)
1204 if (inval_style
!= inval_ignore
)
1205 error (conv_exit_code
, 0, _("value too large to be printed: '%Lg'"
1206 " (cannot handle values > 999Q)"), val
);
1212 int numlen
= double_to_human (val
, precision_used
,
1213 padding_buffer
, padding_buffer_size
,
1214 scale_to
, grouping
, round_style
);
1216 if (numlen
< 0 || ckd_sub (&growth
, numlen
, padding_buffer_size
- 1))
1217 error (EXIT_FAILURE
, 0,
1218 _("failed to prepare value '%Lf' for printing"), val
);
1221 padding_buffer
= xpalloc (padding_buffer
, &padding_buffer_size
,
1225 devmsg ("formatting output:\n value: %Lf\n humanized: %s\n",
1226 val
, quote (padding_buffer
));
1231 int buf_width
= mbswidth (padding_buffer
,
1232 MBSW_REJECT_INVALID
| MBSW_REJECT_UNPRINTABLE
);
1235 if (padding_width
< 0)
1237 if (padding_width
< -buf_width
)
1238 pad
= padding_width
+ buf_width
;
1242 if (buf_width
< padding_width
)
1243 pad
= padding_width
- buf_width
;
1253 print_padded_number (intmax_t padding
)
1255 if (format_str_prefix
)
1256 fputs (format_str_prefix
, stdout
);
1258 for (intmax_t p
= padding
; 0 < p
; p
--)
1261 fputs (padding_buffer
, stdout
);
1263 for (intmax_t p
= padding
; p
< 0; p
++)
1266 if (format_str_suffix
)
1267 fputs (format_str_suffix
, stdout
);
1270 /* Converts the TEXT number string to the requested representation,
1271 and handles automatic suffix addition. */
1273 process_suffixed_number (char *text
, long double *result
,
1274 size_t *precision
, long int field
)
1276 if (suffix
&& strlen (text
) > strlen (suffix
))
1278 char *possible_suffix
= text
+ strlen (text
) - strlen (suffix
);
1280 if (STREQ (suffix
, possible_suffix
))
1282 /* trim suffix, ONLY if it's at the end of the text. */
1283 *possible_suffix
= '\0';
1284 devmsg ("trimming suffix %s\n", quote (suffix
));
1287 devmsg ("no valid suffix found\n");
1290 /* Skip white space - always. */
1292 while (*p
&& isblank (to_uchar (*p
)))
1295 /* setup auto-padding. */
1298 padding_width
= text
< p
|| 1 < field
? strlen (text
) : 0;
1299 devmsg ("setting Auto-Padding to %jd characters\n", padding_width
);
1302 long double val
= 0;
1303 enum simple_strtod_error e
= parse_human_number (p
, &val
, precision
);
1304 if (e
== SSE_OK_PRECISION_LOSS
&& debug
)
1305 error (0, 0, _("large input value %s: possible precision loss"),
1308 if (from_unit_size
!= 1 || to_unit_size
!= 1)
1309 val
= (val
* from_unit_size
) / to_unit_size
;
1313 return (e
== SSE_OK
|| e
== SSE_OK_PRECISION_LOSS
);
1316 /* Return a pointer to the beginning of the next field in line.
1317 The line pointer is moved to the end of the next field. */
1319 next_field (char **line
)
1321 char *field_start
= *line
;
1322 char *field_end
= field_start
;
1324 if (delimiter
!= DELIMITER_DEFAULT
)
1326 if (*field_start
!= delimiter
)
1328 while (*field_end
&& *field_end
!= delimiter
)
1331 /* else empty field */
1335 /* keep any space prefix in the returned field */
1336 while (*field_end
&& field_sep (*field_end
))
1339 while (*field_end
&& ! field_sep (*field_end
))
1349 include_field (uintmax_t field
)
1351 struct field_range_pair
*p
= frp
;
1355 while (p
->lo
!= UINTMAX_MAX
)
1357 if (p
->lo
<= field
&& p
->hi
>= field
)
1364 /* Convert and output the given field. If it is not included in the set
1365 of fields to process just output the original */
1367 process_field (char *text
, uintmax_t field
)
1369 long double val
= 0;
1370 size_t precision
= 0;
1371 bool valid_number
= true;
1373 if (include_field (field
))
1376 process_suffixed_number (text
, &val
, &precision
, field
);
1380 valid_number
= prepare_padded_number (val
, precision
, &padding
);
1383 print_padded_number (padding
);
1385 fputs (text
, stdout
);
1388 fputs (text
, stdout
);
1390 return valid_number
;
1393 /* Convert number in a given line of text.
1394 NEWLINE specifies whether to output a '\n' for this "line". */
1396 process_line (char *line
, bool newline
)
1399 uintmax_t field
= 0;
1400 bool valid_number
= true;
1404 next
= next_field (&line
);
1408 /* nul terminate the current field string and process */
1411 if (! process_field (next
, field
))
1412 valid_number
= false;
1414 fputc ((delimiter
== DELIMITER_DEFAULT
) ?
1415 ' ' : delimiter
, stdout
);
1420 /* end of the line, process the last field and finish */
1421 if (! process_field (next
, field
))
1422 valid_number
= false;
1429 putchar (line_delim
);
1431 return valid_number
;
1435 main (int argc
, char **argv
)
1437 int valid_numbers
= 1;
1440 initialize_main (&argc
, &argv
);
1441 set_program_name (argv
[0]);
1442 locale_ok
= !!setlocale (LC_ALL
, "");
1443 bindtextdomain (PACKAGE
, LOCALEDIR
);
1444 textdomain (PACKAGE
);
1447 /* Enabled extended precision if needed. */
1451 decimal_point
= nl_langinfo (RADIXCHAR
);
1452 if (decimal_point
== nullptr || strlen (decimal_point
) == 0)
1453 decimal_point
= ".";
1454 decimal_point_length
= strlen (decimal_point
);
1456 atexit (close_stdout
);
1460 int c
= getopt_long (argc
, argv
, "d:z", longopts
, nullptr);
1468 scale_from
= XARGMATCH ("--from", optarg
,
1469 scale_from_args
, scale_from_types
);
1472 case FROM_UNIT_OPTION
:
1473 from_unit_size
= unit_to_umax (optarg
);
1478 XARGMATCH ("--to", optarg
, scale_to_args
, scale_to_types
);
1481 case TO_UNIT_OPTION
:
1482 to_unit_size
= unit_to_umax (optarg
);
1486 round_style
= XARGMATCH ("--round", optarg
, round_args
, round_types
);
1489 case GROUPING_OPTION
:
1493 case PADDING_OPTION
:
1494 if (((xstrtoimax (optarg
, nullptr, 10, &padding_width
, "")
1495 & ~LONGINT_OVERFLOW
)
1497 || padding_width
== 0)
1498 error (EXIT_FAILURE
, 0, _("invalid padding value %s"),
1500 /* TODO: We probably want to apply a specific --padding
1501 to --header lines too. */
1506 error (EXIT_FAILURE
, 0, _("multiple field specifications"));
1507 set_fields (optarg
, SETFLD_ALLOW_DASH
);
1511 /* Interpret -d '' to mean 'use the NUL byte as the delimiter.' */
1512 if (optarg
[0] != '\0' && optarg
[1] != '\0')
1513 error (EXIT_FAILURE
, 0,
1514 _("the delimiter must be a single character"));
1515 delimiter
= optarg
[0];
1530 case DEV_DEBUG_OPTION
:
1538 if (xstrtoumax (optarg
, nullptr, 10, &header
, "") != LONGINT_OK
1540 error (EXIT_FAILURE
, 0, _("invalid header value %s"),
1550 format_str
= optarg
;
1553 case INVALID_OPTION
:
1554 inval_style
= XARGMATCH ("--invalid", optarg
,
1555 inval_args
, inval_types
);
1558 case_GETOPT_HELP_CHAR
;
1559 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
1562 usage (EXIT_FAILURE
);
1566 if (format_str
!= nullptr && grouping
)
1567 error (EXIT_FAILURE
, 0, _("--grouping cannot be combined with --format"));
1569 if (debug
&& ! locale_ok
)
1570 error (0, 0, _("failed to set locale"));
1572 /* Warn about no-op. */
1573 if (debug
&& scale_from
== scale_none
&& scale_to
== scale_none
1574 && !grouping
&& (padding_width
== 0) && (format_str
== nullptr))
1575 error (0, 0, _("no conversion option specified"));
1578 parse_format_string (format_str
);
1582 if (scale_to
!= scale_none
)
1583 error (EXIT_FAILURE
, 0, _("grouping cannot be combined with --to"));
1584 if (debug
&& (strlen (nl_langinfo (THOUSEP
)) == 0))
1585 error (0, 0, _("grouping has no effect in this locale"));
1588 auto_padding
= (padding_width
== 0 && delimiter
== DELIMITER_DEFAULT
);
1590 if (inval_style
!= inval_abort
)
1595 if (debug
&& header
)
1596 error (0, 0, _("--header ignored with command-line input"));
1598 for (; optind
< argc
; optind
++)
1599 valid_numbers
&= process_line (argv
[optind
], true);
1603 char *line
= nullptr;
1604 size_t line_allocated
= 0;
1607 while (header
-- && getdelim (&line
, &line_allocated
,
1608 line_delim
, stdin
) > 0)
1609 fputs (line
, stdout
);
1611 while ((len
= getdelim (&line
, &line_allocated
,
1612 line_delim
, stdin
)) > 0)
1614 bool newline
= line
[len
- 1] == line_delim
;
1616 line
[len
- 1] = '\0';
1617 valid_numbers
&= process_line (line
, newline
);
1621 error (EXIT_FAILURE
, errno
, _("error reading input"));
1624 if (debug
&& !valid_numbers
)
1625 error (0, 0, _("failed to convert some of the input numbers"));
1627 int exit_status
= EXIT_SUCCESS
;
1629 && inval_style
!= inval_warn
&& inval_style
!= inval_ignore
)
1630 exit_status
= EXIT_CONVERSION_WARNINGS
;
1632 main_exit (exit_status
);