1 1.10 kre # $NetBSD: pushd,v 1.10 2025/04/09 13:44:12 kre Exp $ 2 1.4 jtc # Copyright (c) 1991, 1993 3 1.4 jtc # The Regents of the University of California. All rights reserved. 4 1.1 cgd # 5 1.1 cgd # This code is derived from software contributed to Berkeley by 6 1.1 cgd # Kenneth Almquist. 7 1.1 cgd # 8 1.1 cgd # Redistribution and use in source and binary forms, with or without 9 1.1 cgd # modification, are permitted provided that the following conditions 10 1.1 cgd # are met: 11 1.1 cgd # 1. Redistributions of source code must retain the above copyright 12 1.1 cgd # notice, this list of conditions and the following disclaimer. 13 1.1 cgd # 2. Redistributions in binary form must reproduce the above copyright 14 1.1 cgd # notice, this list of conditions and the following disclaimer in the 15 1.1 cgd # documentation and/or other materials provided with the distribution. 16 1.1 cgd # 17 1.1 cgd # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 1.1 cgd # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 1.1 cgd # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 1.1 cgd # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 1.1 cgd # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 1.1 cgd # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 1.1 cgd # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 1.1 cgd # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 1.1 cgd # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 1.1 cgd # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 1.1 cgd # SUCH DAMAGE. 28 1.1 cgd # 29 1.7 christos # @(#)pushd 8.2 (Berkeley) 5/4/95 30 1.1 cgd 31 1.1 cgd # pushd, popd, and dirs --- written by Chris Bertin 32 1.1 cgd # Pixel Computer Inc. ...!wjh12!pixel!pixutl!chris 33 1.1 cgd # as modified by Patrick Elam of GTRI and Kenneth Almquist at UW 34 1.1 cgd 35 1.10 kre # produce decidely sub-optimal quoting, but adequate for the purpose 36 1.10 kre __ds_quote() 37 1.10 kre { 38 1.10 kre local A QA PP 39 1.10 kre 40 1.10 kre case "$1" in 41 1.10 kre -z) DSTACK=;& # zero, then... 42 1.10 kre -a) PP=false;; # append (retain arg order) 43 1.10 kre 44 1.10 kre -r) DSTACK=;& # zero, then... 45 1.10 kre -p) PP=true;; # prepend (reverse arg order) 46 1.10 kre 47 1.10 kre *) printf '__ds_quote usage error\n' >&2; return 1;; 48 1.10 kre esac 49 1.10 kre shift 50 1.10 kre 51 1.10 kre for A 52 1.10 kre do 53 1.10 kre QA= 54 1.10 kre while case "${A}" in 55 1.10 kre \'?*) 56 1.10 kre QA="${QA}'\\''" 57 1.10 kre A=${A#?} 58 1.10 kre ;; 59 1.10 kre ?*\'?*) 60 1.10 kre QA="${QA}${A%%\'*}" 61 1.10 kre A=\'${A#*\'} 62 1.10 kre ;; 63 1.10 kre *\') 64 1.10 kre QA="${QA}'\\" 65 1.10 kre A= 66 1.10 kre ;& 67 1.10 kre *) 68 1.10 kre false 69 1.10 kre ;; 70 1.10 kre esac do 71 1.10 kre continue 72 1.10 kre done 73 1.10 kre 74 1.10 kre if "${PP}" 75 1.10 kre then 76 1.10 kre DSTACK="'${QA}${A}'${DSTACK:+ }${DSTACK}" 77 1.10 kre else 78 1.10 kre DSTACK="${DSTACK}${DSTACK:+ }'${QA}${A}'" 79 1.10 kre fi 80 1.10 kre done 81 1.10 kre return 0 82 1.10 kre } 83 1.10 kre 84 1.1 cgd pushd () { 85 1.10 kre local IFS=' ' SAVE 86 1.10 kre 87 1.10 kre SAVE=${PWD} 88 1.10 kre if [ "$#" = 0 ] 89 1.10 kre then 90 1.10 kre if [ "${#DSTACK}" = 0 ] 91 1.10 kre then 92 1.10 kre printf 'pushd: directory stack empty.\n' >&2 93 1.1 cgd return 1 94 1.1 cgd fi 95 1.10 kre eval set -- ${DSTACK} 96 1.10 kre cd -P ${1:+"$1"} || return 97 1.10 kre shift 98 1.10 kre __ds_quote -z "$@" || return 99 1.10 kre else 100 1.10 kre cd -P ${1:+"$1"} > /dev/null || return 101 1.1 cgd fi 102 1.10 kre 103 1.10 kre __ds_quote -p "${SAVE}" || return 104 1.10 kre 105 1.1 cgd dirs 106 1.1 cgd } 107 1.1 cgd 108 1.1 cgd popd () { 109 1.10 kre local IFS=' ' 110 1.10 kre 111 1.10 kre if [ "${#DSTACK}" = 0 ] 112 1.10 kre then 113 1.10 kre printf 'popd: directory stack empty.\n' >&2 114 1.1 cgd return 1 115 1.1 cgd fi 116 1.10 kre eval set -- ${DSTACK} 117 1.10 kre cd -P ${1:+"$1"} 118 1.1 cgd shift 119 1.10 kre __ds_quote -z "$@" || return 120 1.1 cgd dirs 121 1.1 cgd } 122 1.1 cgd 123 1.1 cgd dirs () { 124 1.10 kre local IFS=' ' 125 1.10 kre 126 1.10 kre printf %s "${PWD}" 127 1.10 kre eval set -- ${DSTACK} 128 1.10 kre test "$#" != 0 && printf " %s" "$@" 129 1.10 kre printf \\n 130 1.1 cgd return 0 131 1.1 cgd } 132