logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

printf.sh (3448B)


  1. #!/bin/sh
  2. # SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. # SPDX-License-Identifier: MPL-2.0
  4. plans=48
  5. WD="$(dirname "$0")/../"
  6. target="${WD}/cmd/printf"
  7. . "${WD}/test-cmd/tap.sh"
  8. t esc '\b\t\n' '
  9. '
  10. t octal '\041' '!'
  11. t hex '\x7B\x7d' '{}'
  12. t_args repeat 'foo,bar,baz,' '%s,' foo bar baz
  13. var_c_upper='\c@\cA\cB\cC\cD\cE\cF\cG\cH\cI\cJ\cK\cL\cM\cN\cO\cP\cQ\cR\cS\cT\cU\cV\cW\cX\cY\cZ\c[\c\\c]\c^\c_ !"#$\%&'"'"'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\c?'
  14. t_file esc_c_upper "${WD}/test-cmd/inputs/all_ascii" "${var_c_upper}"
  15. var_c_lower='\c@\ca\cb\cc\cd\ce\cf\cg\ch\ci\cj\ck\cl\cm\cn\co\cp\cq\cr\cs\ct\cu\cv\cw\cx\cy\cz\c[\c\\c]\c^\c_ !"#$\%&'"'"'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\c?'
  16. t_file esc_c_lower "${WD}/test-cmd/inputs/all_ascii" "${var_c_lower}"
  17. t_args clear_vs_caret_esc 'foo :bar' 'foo \cH:%brat\n' 'bar\cHbaz'
  18. t_args fmt_b '
  19. !{}' '%b' '\b\t\n\041\x7B\x7d'
  20. t_args fmt_b_rightpad '!{} .' '%-6b%c' '\041\x7B\x7d' .
  21. t_args fmt_b_leftpad ' !{}.' '%6b%c' '\041\x7B\x7d' .
  22. t_args fmt_b_noarg '' '%b'
  23. t_args fmt_q_print 'foo
  24. baré
  25. ' '%q\n' foo baré
  26. t_args fmt_q_cntrl "back$'\cHdel\c?nl\cJ'" '%q' 'backdelnl
  27. '
  28. t_args fmt_q_sq "single$'\\'quote'" %q "single'quote"
  29. t_args fmt_q_dq "double$'"'"'"quote'" %q 'double"quote'
  30. t_args fmt_c 'foo' %c f oo oooo
  31. t_args fmt_d 10, %d, 10
  32. t_args fmt_Ld 10, %Ld, 10
  33. t_args fmt_i 10, %i, 10
  34. t_args fmt_o 12, %o, 10
  35. t_args fmt_u 10, %u, 10
  36. t_args fmt_x a, %x, 10
  37. t_args fmt_X A, %X, 10
  38. t_args fmt_e '1.000000e+01,' %e, 10
  39. t_args fmt_E '1.000000E+01,' %E, 10
  40. t_args fmt_f '10.000000,' %f, 10
  41. t_args fmt_Lf '10.000000,' %Lf, 10
  42. t_args fmt_F '10.000000,' %F, 10
  43. t_args fmt_g '10,' %g, 10
  44. t_args fmt_G '10,' %G, 10
  45. t_args fmt_a '0x1.4p+3,' %a, 10
  46. t_args fmt_A '0X1.4P+3,' %A, 10
  47. t_args nofmtarg '[
  48. ]' '[%s\n]'
  49. t_args nofmtconv 'foobar
  50. ' 'foobar\n' 1 2 3
  51. t_args nofmtconv_caret 'foo bar' 'foo \cHbar'
  52. t_args precision_s 'abcde' '%.5s' abcdefghijklmnopqrstuvwxyz
  53. t_args nofmt_doubledash '--' '--'
  54. t_args doubledash_fmt '10.000000' '--' '%f' 10
  55. t_args extraconv_s 'foo;bar;;' '%s;%s;%s;' foo bar
  56. t_args extraconv_d '42;69;0;' '%d;%d;%d;' 42 69
  57. # t_args --exit=1 extraconv_pos '' '%1$s'
  58. t_args fmt_star_width ' foo;bar;' '%*s;' 4 foo 2 bar
  59. t_args fmt_star_width_prec0 ' ; ;' '%*.0s;' 4 foo 2 bar
  60. t_args fmt_star_prec 'foo;ba;' '%.*s;' 4 foo 2 bar
  61. wc_c() {
  62. "$target" "$@" | wc -c | tr -cd '[:graph:]'
  63. }
  64. if arg_max=$(getconf ARG_MAX); then
  65. # GNU glibc returns 2097152 which is 16 times more than the correct value (131072)
  66. case "$(uname -s)-$arg_max" in
  67. Linux-131072) ;;
  68. Linux-*) arg_max=131072 ;;
  69. *) ;;
  70. esac
  71. # 64 should be enough but somehow {Free,Net,Open}BSD need more
  72. # 128 also wasn't enough for them
  73. # 1024 is 1/4th of _POSIX_ARG_MAX so really ought to be enough leeway
  74. arg_max=$((arg_max - 1024))
  75. arg_max=$(( (arg_max/2)*2 ))
  76. max_str=$("$target" '%*s' "$arg_max" '' | tr ' ' '=')
  77. max_esc=$("$target" '%*s' "$arg_max" '' | tr ' ' '\\')
  78. t_cmd arg_max_str "$arg_max" wc_c "$max_str"
  79. t_cmd arg_max_str:%s "$arg_max" wc_c '%s' "$max_str"
  80. t_cmd arg_max_esc "$((arg_max/2))" wc_c "$max_esc"
  81. t_cmd arg_max_esc:%b "$((arg_max/2))" wc_c '%b' "$max_esc"
  82. t_cmd arg_max_esc:%s "$arg_max" wc_c '%s' "$max_esc"
  83. else
  84. skip arg_max_str
  85. skip arg_max_str:%s
  86. skip arg_max_esc
  87. skip arg_max_esc:%b
  88. skip arg_max_esc:%s
  89. fi