1/*-
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.
6 *
7 * See the LICENSE file for redistribution information.
8 */
9
10#include "config.h"
11
12#include <sys/queue.h>
13#include <sys/time.h>
14
15#include <bitstring.h>
16#include <errno.h>
17#include <limits.h>
18#include <pwd.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23
24#include "../common/common.h"
25
26/*
27 * ex_cd -- :cd[!] [directory]
28 *	Change directories.
29 *
30 * PUBLIC: int ex_cd(SCR *, EXCMD *);
31 */
32int
33ex_cd(SCR *sp, EXCMD *cmdp)
34{
35	struct passwd *pw;
36	ARGS *ap;
37	int savech;
38	char *dir, *p, *t;
39	char *buf;
40	size_t dlen;
41
42	/*
43	 * !!!
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
46	 * force flag is set.
47	 */
48	if (F_ISSET(sp->ep, F_MODIFIED) &&
49	    !FL_ISSET(cmdp->iflags, E_C_FORCE) && sp->frp->name[0] != '/') {
50		msgq(sp, M_ERR,
51    "120|File modified since last complete write; write or use ! to override");
52		return (1);
53	}
54
55	switch (cmdp->argc) {
56	case 0:
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') {
61				msgq(sp, M_ERR,
62			   "121|Unable to find home directory location");
63				return (1);
64			}
65			dir = pw->pw_dir;
66		}
67		break;
68	case 1:
69		INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1,
70			 dir, dlen);
71		break;
72	default:
73		abort();
74	}
75
76	/*
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.
80	 */
81	if (!chdir(dir))
82		return (0);
83
84	/*
85	 * If moving to the user's home directory, or, the path begins with
86	 * "/", "./" or "../", it's the only place we try.
87	 */
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')))
93		goto err;
94
95	/* Try the O_CDPATH option values. */
96	for (p = t = O_STR(sp, O_CDPATH);; ++p)
97		if (*p == '\0' || *p == ':') {
98			/*
99			 * Ignore the empty strings and ".", since we've already
100			 * tried the current directory.
101			 */
102			if (t < p && (p - t != 1 || *t != '.')) {
103				savech = *p;
104				*p = '\0';
105				if ((buf = join(t, dir)) == NULL) {
106					msgq(sp, M_SYSERR, NULL);
107					return (1);
108				}
109				*p = savech;
110				if (!chdir(buf)) {
111					free(buf);
112					if ((buf = getcwd(NULL, 0)) != NULL) {
113		msgq_str(sp, M_INFO, buf, "122|New current directory: %s");
114						free(buf);
115					}
116					return (0);
117				}
118				free(buf);
119			}
120			t = p + 1;
121			if (*p == '\0')
122				break;
123		}
124
125err:	msgq_str(sp, M_SYSERR, dir, "%s");
126	return (1);
127}
128