#! /bin/sh
# PCP QA Test No. 542
# pmdate buffer overflow ... #666344
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

status=0	# success is the default!
trap "rm -f $tmp.*; exit \$status" 0 1 2 3 15

# real QA test starts here
for fmt in "%H:%M" "%A %B" "%c" "some stuff to pad: %c"
do
    echo
    echo "=== $fmt ==="
    match=false
    echo >$tmp.err

    # try 3 times to avoid clock changes between the date(1) and pmdate(1)
    # executions
    #
    for i in 1 2 3
    do
	# need to dodge AM/PM and timezone abbreviation and move year
	# to the end and change "16 May" to "May 16", and deal with
	# leading spaces for days before the 10th, for %c on Linux
	#
	x1=`date +"$fmt" \
	    | sed \
	        -e 's/ AM//' \
		-e '/ PM/{
s/ PM//
s/ 00:/ 12:/
s/ 01:/ 13:/
s/ 02:/ 14:/
s/ 03:/ 15:/
s/ 04:/ 16:/
s/ 05:/ 17:/
s/ 06:/ 18:/
s/ 07:/ 19:/
s/ 08:/ 20:/
s/ 09:/ 21:/
s/ 10:/ 22:/
s/ 11:/ 23:/
}' \
		-e 's/ [A-Z][A-Z][A-Z]$//' \
		-e 's/ [A-Z][A-Z][A-Z][A-Z]$//' \
		-e 's/ \([1-2][0-9][0-9][0-9]\) \(.*\)/ \2 \1/' \
		-e 's/\([A-Z][a-z][a-z]\) 0\([1-9]\) /\1  \2 /' \
		-e 's/\([ 1-3][0-9]\) \([A-Z][a-z][a-z]\)/\2 \1/'`
	x2=`pmdate "$fmt"`
	if [ "$x1" = "$x2" ]
	then
	    match=true
	    break
	else
	    echo "date -> \"`date +"$fmt"`\" -> \"$x1\"" >>$tmp.err
	    echo "pmdate -> \"$x2\"" >>$tmp.err
	fi
    done
    if $match
    then
	echo "match"
    else
	echo "errors ..."
	cat $tmp.err
	status=1
    fi
done

# error checks
#
echo
echo "=== error checks ==="

$sudo rm -f core* $seq.core*
pmdate "%A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B %A%B"
_check_core

exit
