popd revision 1.10
11.10Skre#	$NetBSD: popd,v 1.10 2025/04/09 13:44:12 kre Exp $
21.4Sjtc# Copyright (c) 1991, 1993
31.4Sjtc#	The Regents of the University of California.  All rights reserved.
41.1Scgd#
51.1Scgd# This code is derived from software contributed to Berkeley by
61.1Scgd# Kenneth Almquist.
71.1Scgd#
81.1Scgd# Redistribution and use in source and binary forms, with or without
91.1Scgd# modification, are permitted provided that the following conditions
101.1Scgd# are met:
111.1Scgd# 1. Redistributions of source code must retain the above copyright
121.1Scgd#    notice, this list of conditions and the following disclaimer.
131.1Scgd# 2. Redistributions in binary form must reproduce the above copyright
141.1Scgd#    notice, this list of conditions and the following disclaimer in the
151.1Scgd#    documentation and/or other materials provided with the distribution.
161.1Scgd#
171.1Scgd# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181.1Scgd# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191.1Scgd# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201.1Scgd# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211.1Scgd# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221.1Scgd# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231.1Scgd# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241.1Scgd# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251.1Scgd# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261.1Scgd# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271.1Scgd# SUCH DAMAGE.
281.1Scgd#
291.7Schristos#	@(#)popd	8.2 (Berkeley) 5/4/95
301.1Scgd
311.1Scgd# pushd, popd, and dirs --- written by Chris Bertin
321.1Scgd# Pixel Computer Inc. ...!wjh12!pixel!pixutl!chris
331.1Scgd# as modified by Patrick Elam of GTRI and Kenneth Almquist at UW
341.1Scgd
351.10Skre# produce decidely sub-optimal quoting, but adequate for the purpose
361.10Skre__ds_quote()
371.10Skre{
381.10Skre	local A QA PP
391.10Skre
401.10Skre	case "$1" in
411.10Skre	-z)	DSTACK=;&		# zero, then...
421.10Skre	-a)	PP=false;;		# append (retain arg order)
431.10Skre
441.10Skre	-r)	DSTACK=;&		# zero, then...
451.10Skre	-p)	PP=true;;		# prepend (reverse arg order)
461.10Skre
471.10Skre	*)	printf '__ds_quote usage error\n' >&2; return 1;;
481.10Skre	esac
491.10Skre	shift
501.10Skre
511.10Skre	for A
521.10Skre	do
531.10Skre		QA=
541.10Skre		while case "${A}" in
551.10Skre		\'?*)
561.10Skre			QA="${QA}'\\''"
571.10Skre			A=${A#?}
581.10Skre			;;
591.10Skre		?*\'?*)
601.10Skre			QA="${QA}${A%%\'*}"
611.10Skre			A=\'${A#*\'}
621.10Skre			;;
631.10Skre		*\')
641.10Skre			QA="${QA}'\\"
651.10Skre			A=
661.10Skre			;&
671.10Skre		*)
681.10Skre			false
691.10Skre			;;
701.10Skre		esac do
711.10Skre			continue
721.10Skre		done
731.10Skre
741.10Skre		if "${PP}"
751.10Skre		then
761.10Skre			DSTACK="'${QA}${A}'${DSTACK:+ }${DSTACK}"
771.10Skre		else
781.10Skre			DSTACK="${DSTACK}${DSTACK:+ }'${QA}${A}'"
791.10Skre		fi
801.10Skre	done
811.10Skre	return 0
821.10Skre}
831.10Skre
841.1Scgdpushd () {
851.10Skre	local IFS=' ' SAVE
861.10Skre
871.10Skre	SAVE=${PWD}
881.10Skre	if [ "$#" = 0 ]
891.10Skre	then
901.10Skre		if [ "${#DSTACK}" = 0 ]
911.10Skre		then
921.10Skre			printf 'pushd: directory stack empty.\n' >&2
931.1Scgd			return 1
941.1Scgd		fi
951.10Skre		eval set -- ${DSTACK}
961.10Skre		cd -P ${1:+"$1"} || return
971.10Skre		shift
981.10Skre		__ds_quote -z "$@" || return
991.10Skre	else
1001.10Skre		cd -P ${1:+"$1"} > /dev/null || return
1011.1Scgd	fi
1021.10Skre
1031.10Skre	__ds_quote -p "${SAVE}" || return
1041.10Skre
1051.1Scgd	dirs
1061.1Scgd}
1071.1Scgd
1081.1Scgdpopd () {
1091.10Skre	local IFS=' '
1101.10Skre
1111.10Skre	if [ "${#DSTACK}" = 0 ]
1121.10Skre	then
1131.10Skre		printf 'popd: directory stack empty.\n' >&2
1141.1Scgd		return 1
1151.1Scgd	fi
1161.10Skre	eval set -- ${DSTACK}
1171.10Skre	cd -P ${1:+"$1"}
1181.1Scgd	shift
1191.10Skre	__ds_quote -z "$@" || return
1201.1Scgd	dirs
1211.1Scgd}
1221.1Scgd
1231.1Scgddirs () {
1241.10Skre	local IFS=' '
1251.10Skre
1261.10Skre	printf %s "${PWD}"
1271.10Skre	eval set -- ${DSTACK}
1281.10Skre	test "$#" != 0 && printf " %s" "$@"
1291.10Skre	printf \\n
1301.1Scgd	return 0
1311.1Scgd}
132