* Copyright (c) 1992, 1993, 1994
* The Regents of the University of California. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* 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
static char sccsid
[] = "@(#)ex_cd.c 8.10 (Berkeley) 3/19/94";
* ex_cd -- :cd[!] [directory]
char *dir
, buf
[MAXPATHLEN
];
if ((dir
= getenv("HOME")) == NULL
) {
"Environment variable HOME not set.");
* If the user has a CDPATH variable, we use it, otherwise
* we use the current directory.
* This violates historic practice. Historic vi didn't consider
* CDPATH, and therefore always used the current directory. This
* is probably correct; if user's have set CDPATH to not include
* the current directory, they probably had a reason.
for (cdp
= exp
->cdq
.tqh_first
;
cdp
!= NULL
; cdp
= cdp
->q
.tqe_next
) {
sizeof(buf
), "%s/%s", cdp
->path
, dir
);
err
: msgq(sp
, M_SYSERR
, dir
);
if (getcwd(buf
, sizeof(buf
)) != NULL
)
msgq(sp
, M_INFO
, "New directory: %s", buf
);
#define FREE_CDPATH(cdp) { \
TAILQ_REMOVE(&exp->cdq, (cdp), q); \
FREE((cdp), sizeof(CDPATH)); \
* Create a new list of cd paths.
/* Free current queue. */
while ((cdp
= exp
->cdq
.tqh_first
) != NULL
)
* Create new queue. The CDPATH environmental variable (and the
* user's manual entry) are delimited by colon characters.
for (p
= t
= str
, founddot
= 0;; ++p
) {
if (*p
== '\0' || *p
== ':') {
* Empty strings specify ".". The only way to get an
* empty string is a leading colon, colons in a row,
* or a trailing colon. Or, to put it the other way,
* if the the length is zero, then it's either ":XXX",
* "XXX::XXXX" , "XXX:", or "", and the only failure
* mode is the last one. Note, the string ":" gives
* us two entries of '.', so we only include one of
if ((len
= p
- t
) == 0) {
if (p
== str
&& *p
== '\0')
MALLOC_RET(sp
, cdp
, CDPATH
*, sizeof(CDPATH
));
MALLOC(sp
, cdp
->path
, char *, len
+ 1);
memmove(cdp
->path
, t
, len
);
TAILQ_INSERT_TAIL(&exp
->cdq
, cdp
, q
);
/* Free previous queue. */
/* Free up cd path information. */
while ((cdp
= exp
->cdq
.tqh_first
) != NULL
)