2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
12 #include <sys/queue.h>
15 #include <bitstring.h>
24 #include "../common/common.h"
27 * ex_cd -- :cd[!] [directory]
30 * PUBLIC: int ex_cd(SCR *, EXCMD *);
33 ex_cd(SCR
*sp
, EXCMD
*cmdp
)
44 * Historic practice is that the cd isn't attempted if the file has
45 * been modified, unless its name begins with a leading '/' or the
48 if (F_ISSET(sp
->ep
, F_MODIFIED
) &&
49 !FL_ISSET(cmdp
->iflags
, E_C_FORCE
) && sp
->frp
->name
[0] != '/') {
51 "120|File modified since last complete write; write or use ! to override");
57 /* If no argument, change to the user's home directory. */
58 if ((dir
= getenv("HOME")) == NULL
) {
59 if ((pw
= getpwuid(getuid())) == NULL
||
60 pw
->pw_dir
== NULL
|| pw
->pw_dir
[0] == '\0') {
62 "121|Unable to find home directory location");
69 INT2CHAR(sp
, cmdp
->argv
[0]->bp
, cmdp
->argv
[0]->len
+ 1,
77 * Try the current directory first. If this succeeds, don't display
78 * a message, vi didn't historically, and it should be obvious to the
79 * user where they are.
85 * If moving to the user's home directory, or, the path begins with
86 * "/", "./" or "../", it's the only place we try.
88 if (cmdp
->argc
== 0 ||
89 (ap
= cmdp
->argv
[0])->bp
[0] == '/' ||
90 (ap
->len
== 1 && ap
->bp
[0] == '.') ||
91 (ap
->len
>= 2 && ap
->bp
[0] == '.' && ap
->bp
[1] == '.' &&
92 (ap
->bp
[2] == '/' || ap
->bp
[2] == '\0')))
95 /* Try the O_CDPATH option values. */
96 for (p
= t
= O_STR(sp
, O_CDPATH
);; ++p
)
97 if (*p
== '\0' || *p
== ':') {
99 * Ignore the empty strings and ".", since we've already
100 * tried the current directory.
102 if (t
< p
&& (p
- t
!= 1 || *t
!= '.')) {
105 if ((buf
= join(t
, dir
)) == NULL
) {
106 msgq(sp
, M_SYSERR
, NULL
);
112 if ((buf
= getcwd(NULL
, 0)) != NULL
) {
113 msgq_str(sp
, M_INFO
, buf
, "122|New current directory: %s");
125 err
: msgq_str(sp
, M_SYSERR
, dir
, "%s");