:
pushd ()
{
    dirname=$1
    if cd ${dirname:?"missing directory name."}   # if cd was successful
    then
        DIRSTACK="$dirname ${DIRSTACK:-$PWD' '}"
        echo $DIRSTACK
    else
        echo still in $PWD.
    fi
}

popd ()
{
    DIRSTACK=${DIRSTACK#* }
    cd ${DIRSTACK%% *}
    echo "$PWD"
}
