#	$NetBSD: popd,v 1.10 2025/04/09 13:44:12 kre Exp $
# Copyright (c) 1991, 1993
#	The Regents of the University of California.  All rights reserved.
#
# This code is derived from software contributed to Berkeley by
# Kenneth Almquist.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#	@(#)popd	8.2 (Berkeley) 5/4/95

# pushd, popd, and dirs --- written by Chris Bertin
# Pixel Computer Inc. ...!wjh12!pixel!pixutl!chris
# as modified by Patrick Elam of GTRI and Kenneth Almquist at UW

# produce decidely sub-optimal quoting, but adequate for the purpose
__ds_quote()
{
	local A QA PP

	case "$1" in
	-z)	DSTACK=;&		# zero, then...
	-a)	PP=false;;		# append (retain arg order)

	-r)	DSTACK=;&		# zero, then...
	-p)	PP=true;;		# prepend (reverse arg order)

	*)	printf '__ds_quote usage error\n' >&2; return 1;;
	esac
	shift

	for A
	do
		QA=
		while case "${A}" in
		\'?*)
			QA="${QA}'\\''"
			A=${A#?}
			;;
		?*\'?*)
			QA="${QA}${A%%\'*}"
			A=\'${A#*\'}
			;;
		*\')
			QA="${QA}'\\"
			A=
			;&
		*)
			false
			;;
		esac do
			continue
		done

		if "${PP}"
		then
			DSTACK="'${QA}${A}'${DSTACK:+ }${DSTACK}"
		else
			DSTACK="${DSTACK}${DSTACK:+ }'${QA}${A}'"
		fi
	done
	return 0
}

pushd () {
	local IFS=' ' SAVE

	SAVE=${PWD}
	if [ "$#" = 0 ]
	then
		if [ "${#DSTACK}" = 0 ]
		then
			printf 'pushd: directory stack empty.\n' >&2
			return 1
		fi
		eval set -- ${DSTACK}
		cd -P ${1:+"$1"} || return
		shift
		__ds_quote -z "$@" || return
	else
		cd -P ${1:+"$1"} > /dev/null || return
	fi

	__ds_quote -p "${SAVE}" || return

	dirs
}

popd () {
	local IFS=' '

	if [ "${#DSTACK}" = 0 ]
	then
		printf 'popd: directory stack empty.\n' >&2
		return 1
	fi
	eval set -- ${DSTACK}
	cd -P ${1:+"$1"}
	shift
	__ds_quote -z "$@" || return
	dirs
}

dirs () {
	local IFS=' '

	printf %s "${PWD}"
	eval set -- ${DSTACK}
	test "$#" != 0 && printf " %s" "$@"
	printf \\n
	return 0
}
