#!/bin/sh
#
# Check whether -c and -w options work.
#
# Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
# Copyright (c) 2014-2018 The strace developers.
# All rights reserved.
#
# SPDX-License-Identifier: GPL-2.0-or-later

. "./init.sh"

run_prog ../sleep 0
check_prog grep

grep_log()
{
	local pattern="$1"; shift

	run_strace "$@" ../sleep 1

	grep nanosleep "$LOG" > /dev/null ||
		framework_skip_ 'sleep does not use nanosleep'

	LC_ALL=C grep -E -x -e "$pattern" "$LOG" > /dev/null || {
		echo "Pattern of expected output: $pattern"
		echo 'Actual output:'
		dump_log_and_fail_with "$STRACE $args output mismatch"
	}
}

GENERIC=' *[^ ]+ +0\.0[^n]*nanosleep *'
WALLCLOCK=' *[^ ]+ +(1\.[01]|0\.99)[^n]*nanosleep *'
WALLCLOCK1='100\.00 +(1\.[01]|0\.99)[^n]*nanosleep'
HALFCLOCK=' *[^ ]+ +0\.[567][^n]*nanosleep *'

grep_log "$GENERIC"	-c
grep_log "$GENERIC"	-c -O1
grep_log "$GENERIC"	-c -enanosleep
grep_log "$GENERIC"	-c -O1 -enanosleep
grep_log "$WALLCLOCK"	-cw
grep_log "$WALLCLOCK"	-cw -O1
grep_log "$WALLCLOCK1"	-cw -enanosleep
grep_log "$WALLCLOCK1"	-cw -O1 -enanosleep
grep_log "$HALFCLOCK"	-cw -O4.5e2ms -enanosleep
grep_log "$HALFCLOCK"	-cw -O4.5e-1s -enanosleep
grep_log "$HALFCLOCK"	-cw -O456789012ns -enanosleep

exit 0
