cd.c revision 20887
1/*-
2 * Copyright (c) 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	$Id: cd.c,v 1.10 1996/12/23 22:16:35 steve Exp $
37 */
38
39#ifndef lint
40static char const sccsid[] = "@(#)cd.c	8.2 (Berkeley) 5/4/95";
41#endif /* not lint */
42
43#include <sys/types.h>
44#include <sys/stat.h>
45#include <stdlib.h>
46#include <string.h>
47#include <unistd.h>
48#include <errno.h>
49#include <limits.h>
50
51/*
52 * The cd and pwd commands.
53 */
54
55#include "shell.h"
56#include "var.h"
57#include "nodes.h"	/* for jobs.h */
58#include "jobs.h"
59#include "options.h"
60#include "output.h"
61#include "memalloc.h"
62#include "error.h"
63#include "exec.h"
64#include "redir.h"
65#include "mystring.h"
66#include "show.h"
67#include "cd.h"
68
69STATIC int docd __P((char *, int));
70STATIC char *getcomponent __P((void));
71STATIC void updatepwd __P((char *));
72
73char *curdir = NULL;		/* current working directory */
74char *prevdir;			/* previous working directory */
75STATIC char *cdcomppath;
76
77int
78cdcmd(argc, argv)
79	int argc;
80	char **argv;
81{
82	char *dest;
83	char *path;
84	char *p;
85	struct stat statb;
86	int print = 0;
87
88	nextopt(nullstr);
89	if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
90		error("HOME not set");
91	if (*dest == '\0')
92		dest = ".";
93	if (dest[0] == '-' && dest[1] == '\0') {
94		dest = prevdir ? prevdir : curdir;
95		if (dest)
96			print = 1;
97		else
98			dest = ".";
99	}
100	if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL)
101		path = nullstr;
102	while ((p = padvance(&path, dest)) != NULL) {
103		if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
104			if (!print) {
105				/*
106				 * XXX - rethink
107				 */
108				if (p[0] == '.' && p[1] == '/')
109					p += 2;
110				print = strcmp(p, dest);
111			}
112			if (docd(p, print) >= 0)
113				return 0;
114
115		}
116	}
117	error("can't cd to %s", dest);
118	/*NOTREACHED*/
119	return 0;
120}
121
122
123/*
124 * Actually do the chdir.  In an interactive shell, print the
125 * directory name if "print" is nonzero.
126 */
127STATIC int
128docd(dest, print)
129	char *dest;
130	int print;
131{
132
133	TRACE(("docd(\"%s\", %d) called\n", dest, print));
134	INTOFF;
135	if (chdir(dest) < 0) {
136		INTON;
137		return -1;
138	}
139	updatepwd(dest);
140	INTON;
141	if (print && iflag)
142		out1fmt("%s\n", stackblock());
143	return 0;
144}
145
146
147/*
148 * Get the next component of the path name pointed to by cdcomppath.
149 * This routine overwrites the string pointed to by cdcomppath.
150 */
151STATIC char *
152getcomponent()
153{
154	register char *p;
155	char *start;
156
157	if ((p = cdcomppath) == NULL)
158		return NULL;
159	start = cdcomppath;
160	while (*p != '/' && *p != '\0')
161		p++;
162	if (*p == '\0') {
163		cdcomppath = NULL;
164	} else {
165		*p++ = '\0';
166		cdcomppath = p;
167	}
168	return start;
169}
170
171
172/*
173 * Update curdir (the name of the current directory) in response to a
174 * cd command.  We also call hashcd to let the routines in exec.c know
175 * that the current directory has changed.
176 */
177STATIC void
178updatepwd(dir)
179	char *dir;
180{
181	char *new;
182	char *p;
183
184	hashcd();				/* update command hash table */
185	cdcomppath = stalloc(strlen(dir) + 1);
186	scopy(dir, cdcomppath);
187	STARTSTACKSTR(new);
188	if (*dir != '/') {
189		if (curdir == NULL)
190			return;
191		p = curdir;
192		while (*p)
193			STPUTC(*p++, new);
194		if (p[-1] == '/')
195			STUNPUTC(new);
196	}
197	while ((p = getcomponent()) != NULL) {
198		if (equal(p, "..")) {
199			while (new > stackblock() && (STUNPUTC(new), *new) != '/');
200		} else if (*p != '\0' && ! equal(p, ".")) {
201			STPUTC('/', new);
202			while (*p)
203				STPUTC(*p++, new);
204		}
205	}
206	if (new == stackblock())
207		STPUTC('/', new);
208	STACKSTRNUL(new);
209	INTOFF;
210	if (prevdir)
211		ckfree(prevdir);
212	prevdir = curdir;
213	curdir = savestr(stackblock());
214	INTON;
215}
216
217
218int
219pwdcmd(argc, argv)
220	int argc;
221	char **argv;
222{
223	if(!getpwd())
224		error("getcwd() failed: %s", strerror(errno));
225	out1str(curdir);
226	out1c('\n');
227	return 0;
228}
229
230
231/*
232 * Find out what the current directory is. If we already know the current
233 * directory, this routine returns immediately.
234 */
235char *
236getpwd()
237{
238	if (curdir)
239		return (curdir);
240	return ((curdir = getcwd(NULL, 0)));
241}
242