init.sh 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. # source this file; set up for tests
  2. # Copyright (C) 2009-2016 Free Software Foundation, Inc.
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. # Using this file in a test
  14. # =========================
  15. #
  16. # The typical skeleton of a test looks like this:
  17. #
  18. # #!/bin/sh
  19. # . "${srcdir=.}/init.sh"; path_prepend_ .
  20. # Execute some commands.
  21. # Note that these commands are executed in a subdirectory, therefore you
  22. # need to prepend "../" to relative filenames in the build directory.
  23. # Note that the "path_prepend_ ." is useful only if the body of your
  24. # test invokes programs residing in the initial directory.
  25. # For example, if the programs you want to test are in src/, and this test
  26. # script is named tests/test-1, then you would use "path_prepend_ ../src",
  27. # or perhaps export PATH='$(abs_top_builddir)/src$(PATH_SEPARATOR)'"$$PATH"
  28. # to all tests via automake's TESTS_ENVIRONMENT.
  29. # Set the exit code 0 for success, 77 for skipped, or 1 or other for failure.
  30. # Use the skip_ and fail_ functions to print a diagnostic and then exit
  31. # with the corresponding exit code.
  32. # Exit $?
  33. # Executing a test that uses this file
  34. # ====================================
  35. #
  36. # Running a single test:
  37. # $ make check TESTS=test-foo.sh
  38. #
  39. # Running a single test, with verbose output:
  40. # $ make check TESTS=test-foo.sh VERBOSE=yes
  41. #
  42. # Running a single test, with single-stepping:
  43. # 1. Go into a sub-shell:
  44. # $ bash
  45. # 2. Set relevant environment variables from TESTS_ENVIRONMENT in the
  46. # Makefile:
  47. # $ export srcdir=../../tests # this is an example
  48. # 3. Execute the commands from the test, copy&pasting them one by one:
  49. # $ . "$srcdir/init.sh"; path_prepend_ .
  50. # ...
  51. # 4. Finally
  52. # $ exit
  53. ME_=`expr "./$0" : '.*/\(.*\)$'`
  54. # We use a trap below for cleanup. This requires us to go through
  55. # hoops to get the right exit status transported through the handler.
  56. # So use 'Exit STATUS' instead of 'exit STATUS' inside of the tests.
  57. # Turn off errexit here so that we don't trip the bug with OSF1/Tru64
  58. # sh inside this function.
  59. Exit () { set +e; (exit $1); exit $1; }
  60. # Print warnings (e.g., about skipped and failed tests) to this file number.
  61. # Override by defining to say, 9, in init.cfg, and putting say,
  62. # export ...ENVVAR_SETTINGS...; $(SHELL) 9>&2
  63. # in the definition of TESTS_ENVIRONMENT in your tests/Makefile.am file.
  64. # This is useful when using automake's parallel tests mode, to print
  65. # the reason for skip/failure to console, rather than to the .log files.
  66. : ${stderr_fileno_=2}
  67. # Note that correct expansion of "$*" depends on IFS starting with ' '.
  68. # Always write the full diagnostic to stderr.
  69. # When stderr_fileno_ is not 2, also emit the first line of the
  70. # diagnostic to that file descriptor.
  71. warn_ ()
  72. {
  73. # If IFS does not start with ' ', set it and emit the warning in a subshell.
  74. case $IFS in
  75. ' '*) printf '%s\n' "$*" >&2
  76. test $stderr_fileno_ = 2 \
  77. || { printf '%s\n' "$*" | sed 1q >&$stderr_fileno_ ; } ;;
  78. *) (IFS=' '; warn_ "$@");;
  79. esac
  80. }
  81. fail_ () { warn_ "$ME_: failed test: $@"; Exit 1; }
  82. skip_ () { warn_ "$ME_: skipped test: $@"; Exit 77; }
  83. fatal_ () { warn_ "$ME_: hard error: $@"; Exit 99; }
  84. framework_failure_ () { warn_ "$ME_: set-up failure: $@"; Exit 99; }
  85. # This is used to simplify checking of the return value
  86. # which is useful when ensuring a command fails as desired.
  87. # I.e., just doing `command ... &&fail=1` will not catch
  88. # a segfault in command for example. With this helper you
  89. # instead check an explicit exit code like
  90. # returns_ 1 command ... || fail
  91. returns_ () {
  92. # Disable tracing so it doesn't interfere with stderr of the wrapped command
  93. { set +x; } 2>/dev/null
  94. local exp_exit="$1"
  95. shift
  96. "$@"
  97. test $? -eq $exp_exit && ret_=0 || ret_=1
  98. if test "$VERBOSE" = yes && test "$gl_set_x_corrupts_stderr_" = false; then
  99. set -x
  100. fi
  101. { return $ret_; } 2>/dev/null
  102. }
  103. # Sanitize this shell to POSIX mode, if possible.
  104. DUALCASE=1; export DUALCASE
  105. if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
  106. emulate sh
  107. NULLCMD=:
  108. alias -g '${1+"$@"}'='"$@"'
  109. setopt NO_GLOB_SUBST
  110. else
  111. case `(set -o) 2>/dev/null` in
  112. *posix*) set -o posix ;;
  113. esac
  114. fi
  115. # We require $(...) support unconditionally.
  116. # We require a few additional shell features only when $EXEEXT is nonempty,
  117. # in order to support automatic $EXEEXT emulation:
  118. # - hyphen-containing alias names
  119. # - we prefer to use ${var#...} substitution, rather than having
  120. # to work around lack of support for that feature.
  121. # The following code attempts to find a shell with support for these features.
  122. # If the current shell passes the test, we're done. Otherwise, test other
  123. # shells until we find one that passes. If one is found, re-exec it.
  124. # If no acceptable shell is found, skip the current test.
  125. #
  126. # The "...set -x; P=1 true 2>err..." test is to disqualify any shell that
  127. # emits "P=1" into err, as /bin/sh from SunOS 5.11 and OpenBSD 4.7 do.
  128. #
  129. # Use "9" to indicate success (rather than 0), in case some shell acts
  130. # like Solaris 10's /bin/sh but exits successfully instead of with status 2.
  131. # Eval this code in a subshell to determine a shell's suitability.
  132. # 10 - passes all tests; ok to use
  133. # 9 - ok, but enabling "set -x" corrupts app stderr; prefer higher score
  134. # ? - not ok
  135. gl_shell_test_script_='
  136. test $(echo y) = y || exit 1
  137. f_local_() { local v=1; }; f_local_ || exit 1
  138. score_=10
  139. if test "$VERBOSE" = yes; then
  140. test -n "$( (exec 3>&1; set -x; P=1 true 2>&3) 2> /dev/null)" && score_=9
  141. fi
  142. test -z "$EXEEXT" && exit $score_
  143. shopt -s expand_aliases
  144. alias a-b="echo zoo"
  145. v=abx
  146. test ${v%x} = ab \
  147. && test ${v#a} = bx \
  148. && test $(a-b) = zoo \
  149. && exit $score_
  150. '
  151. if test "x$1" = "x--no-reexec"; then
  152. shift
  153. else
  154. # Assume a working shell. Export to subshells (setup_ needs this).
  155. gl_set_x_corrupts_stderr_=false
  156. export gl_set_x_corrupts_stderr_
  157. # Record the first marginally acceptable shell.
  158. marginal_=
  159. # Search for a shell that meets our requirements.
  160. for re_shell_ in __current__ "${CONFIG_SHELL:-no_shell}" \
  161. /bin/sh bash dash zsh pdksh fail
  162. do
  163. test "$re_shell_" = no_shell && continue
  164. # If we've made it all the way to the sentinel, "fail" without
  165. # finding even a marginal shell, skip this test.
  166. if test "$re_shell_" = fail; then
  167. test -z "$marginal_" && skip_ failed to find an adequate shell
  168. re_shell_=$marginal_
  169. break
  170. fi
  171. # When testing the current shell, simply "eval" the test code.
  172. # Otherwise, run it via $re_shell_ -c ...
  173. if test "$re_shell_" = __current__; then
  174. # 'eval'ing this code makes Solaris 10's /bin/sh exit with
  175. # $? set to 2. It does not evaluate any of the code after the
  176. # "unexpected" first '('. Thus, we must run it in a subshell.
  177. ( eval "$gl_shell_test_script_" ) > /dev/null 2>&1
  178. else
  179. "$re_shell_" -c "$gl_shell_test_script_" 2>/dev/null
  180. fi
  181. st_=$?
  182. # $re_shell_ works just fine. Use it.
  183. if test $st_ = 10; then
  184. gl_set_x_corrupts_stderr_=false
  185. break
  186. fi
  187. # If this is our first marginally acceptable shell, remember it.
  188. if test "$st_:$marginal_" = 9: ; then
  189. marginal_="$re_shell_"
  190. gl_set_x_corrupts_stderr_=true
  191. fi
  192. done
  193. if test "$re_shell_" != __current__; then
  194. # Found a usable shell. Preserve -v and -x.
  195. case $- in
  196. *v*x* | *x*v*) opts_=-vx ;;
  197. *v*) opts_=-v ;;
  198. *x*) opts_=-x ;;
  199. *) opts_= ;;
  200. esac
  201. re_shell=$re_shell_
  202. export re_shell
  203. exec "$re_shell_" $opts_ "$0" --no-reexec "$@"
  204. echo "$ME_: exec failed" 1>&2
  205. exit 127
  206. fi
  207. fi
  208. # If this is bash, turn off all aliases.
  209. test -n "$BASH_VERSION" && unalias -a
  210. # Note that when supporting $EXEEXT (transparently mapping from PROG_NAME to
  211. # PROG_NAME.exe), we want to support hyphen-containing names like test-acos.
  212. # That is part of the shell-selection test above. Why use aliases rather
  213. # than functions? Because support for hyphen-containing aliases is more
  214. # widespread than that for hyphen-containing function names.
  215. test -n "$EXEEXT" && shopt -s expand_aliases
  216. # Enable glibc's malloc-perturbing option.
  217. # This is useful for exposing code that depends on the fact that
  218. # malloc-related functions often return memory that is mostly zeroed.
  219. # If you have the time and cycles, use valgrind to do an even better job.
  220. : ${MALLOC_PERTURB_=87}
  221. export MALLOC_PERTURB_
  222. # This is a stub function that is run upon trap (upon regular exit and
  223. # interrupt). Override it with a per-test function, e.g., to unmount
  224. # a partition, or to undo any other global state changes.
  225. cleanup_ () { :; }
  226. # Emit a header similar to that from diff -u; Print the simulated "diff"
  227. # command so that the order of arguments is clear. Don't bother with @@ lines.
  228. emit_diff_u_header_ ()
  229. {
  230. printf '%s\n' "diff -u $*" \
  231. "--- $1 1970-01-01" \
  232. "+++ $2 1970-01-01"
  233. }
  234. # Arrange not to let diff or cmp operate on /dev/null,
  235. # since on some systems (at least OSF/1 5.1), that doesn't work.
  236. # When there are not two arguments, or no argument is /dev/null, return 2.
  237. # When one argument is /dev/null and the other is not empty,
  238. # cat the nonempty file to stderr and return 1.
  239. # Otherwise, return 0.
  240. compare_dev_null_ ()
  241. {
  242. test $# = 2 || return 2
  243. if test "x$1" = x/dev/null; then
  244. test -s "$2" || return 0
  245. emit_diff_u_header_ "$@"; sed 's/^/+/' "$2"
  246. return 1
  247. fi
  248. if test "x$2" = x/dev/null; then
  249. test -s "$1" || return 0
  250. emit_diff_u_header_ "$@"; sed 's/^/-/' "$1"
  251. return 1
  252. fi
  253. return 2
  254. }
  255. if diff_out_=`exec 2>/dev/null; diff -u "$0" "$0" < /dev/null` \
  256. && diff -u Makefile "$0" 2>/dev/null | grep '^[+]#!' >/dev/null; then
  257. # diff accepts the -u option and does not (like AIX 7 'diff') produce an
  258. # extra space on column 1 of every content line.
  259. if test -z "$diff_out_"; then
  260. compare_ () { diff -u "$@"; }
  261. else
  262. compare_ ()
  263. {
  264. if diff -u "$@" > diff.out; then
  265. # No differences were found, but Solaris 'diff' produces output
  266. # "No differences encountered". Hide this output.
  267. rm -f diff.out
  268. true
  269. else
  270. cat diff.out
  271. rm -f diff.out
  272. false
  273. fi
  274. }
  275. fi
  276. elif
  277. for diff_opt_ in -U3 -c '' no; do
  278. test "$diff_opt_" = no && break
  279. diff_out_=`exec 2>/dev/null; diff $diff_opt_ "$0" "$0" </dev/null` && break
  280. done
  281. test "$diff_opt_" != no
  282. then
  283. if test -z "$diff_out_"; then
  284. compare_ () { diff $diff_opt_ "$@"; }
  285. else
  286. compare_ ()
  287. {
  288. if diff $diff_opt_ "$@" > diff.out; then
  289. # No differences were found, but AIX and HP-UX 'diff' produce output
  290. # "No differences encountered" or "There are no differences between the
  291. # files.". Hide this output.
  292. rm -f diff.out
  293. true
  294. else
  295. cat diff.out
  296. rm -f diff.out
  297. false
  298. fi
  299. }
  300. fi
  301. elif cmp -s /dev/null /dev/null 2>/dev/null; then
  302. compare_ () { cmp -s "$@"; }
  303. else
  304. compare_ () { cmp "$@"; }
  305. fi
  306. # Usage: compare EXPECTED ACTUAL
  307. #
  308. # Given compare_dev_null_'s preprocessing, defer to compare_ if 2 or more.
  309. # Otherwise, propagate $? to caller: any diffs have already been printed.
  310. compare ()
  311. {
  312. # This looks like it can be factored to use a simple "case $?"
  313. # after unchecked compare_dev_null_ invocation, but that would
  314. # fail in a "set -e" environment.
  315. if compare_dev_null_ "$@"; then
  316. return 0
  317. else
  318. case $? in
  319. 1) return 1;;
  320. *) compare_ "$@";;
  321. esac
  322. fi
  323. }
  324. # An arbitrary prefix to help distinguish test directories.
  325. testdir_prefix_ () { printf gt; }
  326. # Run the user-overridable cleanup_ function, remove the temporary
  327. # directory and exit with the incoming value of $?.
  328. remove_tmp_ ()
  329. {
  330. __st=$?
  331. cleanup_
  332. # cd out of the directory we're about to remove
  333. cd "$initial_cwd_" || cd / || cd /tmp
  334. chmod -R u+rwx "$test_dir_"
  335. # If removal fails and exit status was to be 0, then change it to 1.
  336. rm -rf "$test_dir_" || { test $__st = 0 && __st=1; }
  337. exit $__st
  338. }
  339. # Given a directory name, DIR, if every entry in it that matches *.exe
  340. # contains only the specified bytes (see the case stmt below), then print
  341. # a space-separated list of those names and return 0. Otherwise, don't
  342. # print anything and return 1. Naming constraints apply also to DIR.
  343. find_exe_basenames_ ()
  344. {
  345. feb_dir_=$1
  346. feb_fail_=0
  347. feb_result_=
  348. feb_sp_=
  349. for feb_file_ in $feb_dir_/*.exe; do
  350. # If there was no *.exe file, or there existed a file named "*.exe" that
  351. # was deleted between the above glob expansion and the existence test
  352. # below, just skip it.
  353. test "x$feb_file_" = "x$feb_dir_/*.exe" && test ! -f "$feb_file_" \
  354. && continue
  355. # Exempt [.exe, since we can't create a function by that name, yet
  356. # we can't invoke [ by PATH search anyways due to shell builtins.
  357. test "x$feb_file_" = "x$feb_dir_/[.exe" && continue
  358. case $feb_file_ in
  359. *[!-a-zA-Z/0-9_.+]*) feb_fail_=1; break;;
  360. *) # Remove leading file name components as well as the .exe suffix.
  361. feb_file_=${feb_file_##*/}
  362. feb_file_=${feb_file_%.exe}
  363. feb_result_="$feb_result_$feb_sp_$feb_file_";;
  364. esac
  365. feb_sp_=' '
  366. done
  367. test $feb_fail_ = 0 && printf %s "$feb_result_"
  368. return $feb_fail_
  369. }
  370. # Consider the files in directory, $1.
  371. # For each file name of the form PROG.exe, create an alias named
  372. # PROG that simply invokes PROG.exe, then return 0. If any selected
  373. # file name or the directory name, $1, contains an unexpected character,
  374. # define no alias and return 1.
  375. create_exe_shims_ ()
  376. {
  377. case $EXEEXT in
  378. '') return 0 ;;
  379. .exe) ;;
  380. *) echo "$0: unexpected \$EXEEXT value: $EXEEXT" 1>&2; return 1 ;;
  381. esac
  382. base_names_=`find_exe_basenames_ $1` \
  383. || { echo "$0 (exe_shim): skipping directory: $1" 1>&2; return 0; }
  384. if test -n "$base_names_"; then
  385. for base_ in $base_names_; do
  386. alias "$base_"="$base_$EXEEXT"
  387. done
  388. fi
  389. return 0
  390. }
  391. # Use this function to prepend to PATH an absolute name for each
  392. # specified, possibly-$initial_cwd_-relative, directory.
  393. path_prepend_ ()
  394. {
  395. while test $# != 0; do
  396. path_dir_=$1
  397. case $path_dir_ in
  398. '') fail_ "invalid path dir: '$1'";;
  399. /*) abs_path_dir_=$path_dir_;;
  400. *) abs_path_dir_=$initial_cwd_/$path_dir_;;
  401. esac
  402. case $abs_path_dir_ in
  403. *:*) fail_ "invalid path dir: '$abs_path_dir_'";;
  404. esac
  405. PATH="$abs_path_dir_:$PATH"
  406. # Create an alias, FOO, for each FOO.exe in this directory.
  407. create_exe_shims_ "$abs_path_dir_" \
  408. || fail_ "something failed (above): $abs_path_dir_"
  409. shift
  410. done
  411. export PATH
  412. }
  413. setup_ ()
  414. {
  415. if test "$VERBOSE" = yes; then
  416. # Test whether set -x may cause the selected shell to corrupt an
  417. # application's stderr. Many do, including zsh-4.3.10 and the /bin/sh
  418. # from SunOS 5.11, OpenBSD 4.7 and Irix 5.x and 6.5.
  419. # If enabling verbose output this way would cause trouble, simply
  420. # issue a warning and refrain.
  421. if $gl_set_x_corrupts_stderr_; then
  422. warn_ "using SHELL=$SHELL with 'set -x' corrupts stderr"
  423. else
  424. set -x
  425. fi
  426. fi
  427. initial_cwd_=$PWD
  428. pfx_=`testdir_prefix_`
  429. test_dir_=`mktempd_ "$initial_cwd_" "$pfx_-$ME_.XXXX"` \
  430. || fail_ "failed to create temporary directory in $initial_cwd_"
  431. cd "$test_dir_" || fail_ "failed to cd to temporary directory"
  432. # As autoconf-generated configure scripts do, ensure that IFS
  433. # is defined initially, so that saving and restoring $IFS works.
  434. gl_init_sh_nl_='
  435. '
  436. IFS=" "" $gl_init_sh_nl_"
  437. # This trap statement, along with a trap on 0 below, ensure that the
  438. # temporary directory, $test_dir_, is removed upon exit as well as
  439. # upon receipt of any of the listed signals.
  440. for sig_ in 1 2 3 13 15; do
  441. eval "trap 'Exit $(expr $sig_ + 128)' $sig_"
  442. done
  443. }
  444. # Create a temporary directory, much like mktemp -d does.
  445. # Written by Jim Meyering.
  446. #
  447. # Usage: mktempd_ /tmp phoey.XXXXXXXXXX
  448. #
  449. # First, try to use the mktemp program.
  450. # Failing that, we'll roll our own mktemp-like function:
  451. # - try to get random bytes from /dev/urandom
  452. # - failing that, generate output from a combination of quickly-varying
  453. # sources and gzip. Ignore non-varying gzip header, and extract
  454. # "random" bits from there.
  455. # - given those bits, map to file-name bytes using tr, and try to create
  456. # the desired directory.
  457. # - make only $MAX_TRIES_ attempts
  458. # Helper function. Print $N pseudo-random bytes from a-zA-Z0-9.
  459. rand_bytes_ ()
  460. {
  461. n_=$1
  462. # Maybe try openssl rand -base64 $n_prime_|tr '+/=\012' abcd first?
  463. # But if they have openssl, they probably have mktemp, too.
  464. chars_=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
  465. dev_rand_=/dev/urandom
  466. if test -r "$dev_rand_"; then
  467. # Note: 256-length($chars_) == 194; 3 copies of $chars_ is 186 + 8 = 194.
  468. dd ibs=$n_ count=1 if=$dev_rand_ 2>/dev/null \
  469. | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
  470. return
  471. fi
  472. n_plus_50_=`expr $n_ + 50`
  473. cmds_='date; date +%N; free; who -a; w; ps auxww; ps ef; netstat -n'
  474. data_=` (eval "$cmds_") 2>&1 | gzip `
  475. # Ensure that $data_ has length at least 50+$n_
  476. while :; do
  477. len_=`echo "$data_"|wc -c`
  478. test $n_plus_50_ -le $len_ && break;
  479. data_=` (echo "$data_"; eval "$cmds_") 2>&1 | gzip `
  480. done
  481. echo "$data_" \
  482. | dd bs=1 skip=50 count=$n_ 2>/dev/null \
  483. | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
  484. }
  485. mktempd_ ()
  486. {
  487. case $# in
  488. 2);;
  489. *) fail_ "Usage: mktempd_ DIR TEMPLATE";;
  490. esac
  491. destdir_=$1
  492. template_=$2
  493. MAX_TRIES_=4
  494. # Disallow any trailing slash on specified destdir:
  495. # it would subvert the post-mktemp "case"-based destdir test.
  496. case $destdir_ in
  497. / | //) destdir_slash_=$destdir;;
  498. */) fail_ "invalid destination dir: remove trailing slash(es)";;
  499. *) destdir_slash_=$destdir_/;;
  500. esac
  501. case $template_ in
  502. *XXXX) ;;
  503. *) fail_ \
  504. "invalid template: $template_ (must have a suffix of at least 4 X's)";;
  505. esac
  506. # First, try to use mktemp.
  507. d=`unset TMPDIR; { mktemp -d -t -p "$destdir_" "$template_"; } 2>/dev/null` &&
  508. # The resulting name must be in the specified directory.
  509. case $d in "$destdir_slash_"*) :;; *) false;; esac &&
  510. # It must have created the directory.
  511. test -d "$d" &&
  512. # It must have 0700 permissions. Handle sticky "S" bits.
  513. perms=`ls -dgo "$d" 2>/dev/null` &&
  514. case $perms in drwx--[-S]---*) :;; *) false;; esac && {
  515. echo "$d"
  516. return
  517. }
  518. # If we reach this point, we'll have to create a directory manually.
  519. # Get a copy of the template without its suffix of X's.
  520. base_template_=`echo "$template_"|sed 's/XX*$//'`
  521. # Calculate how many X's we've just removed.
  522. template_length_=`echo "$template_" | wc -c`
  523. nx_=`echo "$base_template_" | wc -c`
  524. nx_=`expr $template_length_ - $nx_`
  525. err_=
  526. i_=1
  527. while :; do
  528. X_=`rand_bytes_ $nx_`
  529. candidate_dir_="$destdir_slash_$base_template_$X_"
  530. err_=`mkdir -m 0700 "$candidate_dir_" 2>&1` \
  531. && { echo "$candidate_dir_"; return; }
  532. test $MAX_TRIES_ -le $i_ && break;
  533. i_=`expr $i_ + 1`
  534. done
  535. fail_ "$err_"
  536. }
  537. # If you want to override the testdir_prefix_ function,
  538. # or to add more utility functions, use this file.
  539. test -f "$srcdir/init.cfg" \
  540. && . "$srcdir/init.cfg"
  541. setup_ "$@"
  542. # This trap is here, rather than in the setup_ function, because some
  543. # shells run the exit trap at shell function exit, rather than script exit.
  544. trap remove_tmp_ 0