+# Run a command with a timeout and a time count.
+# Options:
+# -o file write the user time into file (REQUIRED). see `-o` in `man time`
+# -a append the time to the file (instead of overwriting it). see `-a` in `man time`
+saferun()
+{
+ local stop=false
+ local o=
+ local a=
+ while [ $stop = false ]; do
+ case "$1" in
+ -o) o="$2"; shift; shift;;
+ -a) a="-a"; shift;;
+ *) stop=true
+ esac
+ done
+ if test -d "$1"; then
+ find $1 | sort
+ elif test -n "$TIME"; then
+ $TIME -o "$o" $a $TIMEOUT "$@"
+ else
+ if test -n "$a"; then echo 0 >> "$o"; else echo 0 > "$o"; fi
+ $TIMEOUT "$@"
+ fi
+}
+
+# Output a timestamp attribute for XML, or an empty line
+timestamp()
+{
+ if test -n "$TIMESTAMP"; then
+ echo "timestamp='`$TIMESTAMP`'"
+ else
+ echo ""
+ fi
+
+}
+
+# Get platform specific commands ##########################
+
+# Detect a working timeout
+if sh -c "timelimit echo" 1>/dev/null 2>&1; then
+ TIMEOUT="timelimit -t 600"
+elif sh -c "timeout 1 echo" 1>/dev/null 2>&1; then
+ TIMEOUT="timeout 600s"
+else
+ echo "No timelimit or timeout command detected. Tests may hang :("
+fi
+
+# Detect a working time command
+if env time --quiet -f%U true 2>/dev/null; then
+ TIME="env time --quiet -f%U"
+elif env time -f%U true 2>/dev/null; then
+ TIME="env time -f%U"
+else
+ TIME=
+fi
+
+# Detect a working date command
+if date -Iseconds >/dev/null 2>&1; then
+ TIMESTAMP="date -Iseconds"
+else
+ TIMESTAMP=
+fi
+