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.
13 static const char sccsid[] = "$Id: ex_cd.c,v 10.13 2012/04/12 06:28:27 zy Exp $";
16 #include <sys/queue.h>
19 #include <bitstring.h>
28 #include "../common/common.h"
31 * ex_cd -- :cd[!] [directory]
34 * PUBLIC: int ex_cd(SCR *, EXCMD *);
37 ex_cd(SCR *sp, EXCMD *cmdp)
48 * Historic practice is that the cd isn't attempted if the file has
49 * been modified, unless its name begins with a leading '/' or the
52 if (F_ISSET(sp->ep, F_MODIFIED) &&
53 !FL_ISSET(cmdp->iflags, E_C_FORCE) && sp->frp->name[0] != '/') {
55 "120|File modified since last complete write; write or use ! to override");
61 /* If no argument, change to the user's home directory. */
62 if ((dir = getenv("HOME")) == NULL) {
63 if ((pw = getpwuid(getuid())) == NULL ||
64 pw->pw_dir == NULL || pw->pw_dir[0] == '\0') {
66 "121|Unable to find home directory location");
73 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1,
81 * Try the current directory first. If this succeeds, don't display
82 * a message, vi didn't historically, and it should be obvious to the
83 * user where they are.
89 * If moving to the user's home directory, or, the path begins with
90 * "/", "./" or "../", it's the only place we try.
92 if (cmdp->argc == 0 ||
93 (ap = cmdp->argv[0])->bp[0] == '/' ||
94 (ap->len == 1 && ap->bp[0] == '.') ||
95 (ap->len >= 2 && ap->bp[0] == '.' && ap->bp[1] == '.' &&
96 (ap->bp[2] == '/' || ap->bp[2] == '\0')))
99 /* Try the O_CDPATH option values. */
100 for (p = t = O_STR(sp, O_CDPATH);; ++p)
101 if (*p == '\0' || *p == ':') {
103 * Ignore the empty strings and ".", since we've already
104 * tried the current directory.
106 if (t < p && (p - t != 1 || *t != '.')) {
109 if ((buf = join(t, dir)) == NULL) {
110 msgq(sp, M_SYSERR, NULL);
116 if ((buf = getcwd(NULL, 0)) != NULL) {
117 msgq_str(sp, M_INFO, buf, "122|New current directory: %s");
129 err: msgq_str(sp, M_SYSERR, dir, "%s");