bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] init.sh: don’t assume gzip


From: Paul Eggert
Subject: [PATCH] init.sh: don’t assume gzip
Date: Mon, 4 Apr 2022 10:59:08 -0700

* tests/init.sh (rand_bytes_): Don’t assume gzip is installed.
I found this while testing gzip installation on a platform where I
had removed the installed gzip.  gzip is executed only on
platforms lacking mktemp and /dev/urandom so this code is rarely
used; however, these platforms might also lack gzip since gzip
is neither specified by POSIX or required by the GNU Coding Standards.
---
 ChangeLog     | 10 ++++++++++
 tests/init.sh | 43 ++++++++++++++++++++++++-------------------
 2 files changed, 34 insertions(+), 19 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 0f88fceed0..fb5802d61b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2022-04-04  Paul Eggert  <eggert@cs.ucla.edu>
+
+       init.sh: don’t assume gzip
+       * tests/init.sh (rand_bytes_): Don’t assume gzip is installed.
+       I found this while testing gzip installation on a platform where I
+       had removed the installed gzip.  gzip is executed only on
+       platforms lacking mktemp and /dev/urandom so this code is rarely
+       used; however, these platforms might also lack gzip since gzip
+       is neither specified by POSIX or required by the GNU Coding Standards.
+
 2022-03-30  Paul Eggert  <eggert@cs.ucla.edu>
 
        glob: sync better with glibc
diff --git a/tests/init.sh b/tests/init.sh
index 933fdd40f3..d5d37c98f8 100644
--- a/tests/init.sh
+++ b/tests/init.sh
@@ -271,12 +271,10 @@ test -n "$EXEEXT" && test -n "$BASH_VERSION" && shopt -s 
expand_aliases
 #
 # First, try to use the mktemp program.
 # Failing that, we'll roll our own mktemp-like function:
-#  - try to get random bytes from /dev/urandom
+#  - try to get random bytes from /dev/urandom, mapping them to file-name bytes
 #  - failing that, generate output from a combination of quickly-varying
-#      sources and gzip.  Ignore non-varying gzip header, and extract
-#      "random" bits from there.
-#  - given those bits, map to file-name bytes using tr, and try to create
-#      the desired directory.
+#      sources and awk.
+#  - try to create the desired directory.
 #  - make only $MAX_TRIES_ attempts
 
 # Helper function.  Print $N pseudo-random bytes from a-zA-Z0-9.
@@ -296,20 +294,27 @@ rand_bytes_ ()
     return
   fi
 
-  n_plus_50_=`expr $n_ + 50`
-  cmds_='date; date +%N; free; who -a; w; ps auxww; ps -ef'
-  data_=` (eval "$cmds_") 2>&1 | gzip `
-
-  # Ensure that $data_ has length at least 50+$n_
-  while :; do
-    len_=`echo "$data_"|wc -c`
-    test $n_plus_50_ -le $len_ && break;
-    data_=` (echo "$data_"; eval "$cmds_") 2>&1 | gzip `
-  done
-
-  echo "$data_" \
-    | dd bs=1 skip=50 count=$n_ 2>/dev/null \
-    | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
+  # Fall back on quickly-varying sources + awk.
+  # Limit awk program to 7th Edition Unix so that it works even on Solaris 10.
+
+  (date; date +%N; free; who -a; w; ps auxww; ps -ef) 2>&1 | awk '
+     BEGIN {
+       n = '"$n_"'
+       for (i = 0; i < 256; i++)
+         ordinal[sprintf ("%c", i)] = i
+     }
+     {
+       for (i = 1; i <= length; i++)
+         a[ai++ % n] += ordinal[substr ($0, i, 1)]
+     }
+     END {
+       chars = "'"$chars_"'"
+       charslen = length (chars)
+       for (i = 0; i < n; i++)
+         printf "%s", substr (chars, a[i] % charslen + 1, 1)
+       printf "\n"
+     }
+  '
 }
 
 mktempd_ ()
-- 
2.35.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]