Deleted Added
full compact
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

--- 19 unchanged lines hidden (view full) ---

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 $
36 * $Id: cd.c,v 1.11 1996/12/23 22:29:03 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>
49
50/*
51 * The cd and pwd commands.
52 */
53
54#include "shell.h"
55#include "var.h"
56#include "nodes.h" /* for jobs.h */

--- 69 unchanged lines hidden (view full) ---

126STATIC int
127docd(dest, print)
128 char *dest;
129 int print;
130{
131
132 TRACE(("docd(\"%s\", %d) called\n", dest, print));
133 INTOFF;
135 if (chdir(dest) < 0) {
134 updatepwd(dest);
135 if (chdir(stackblock()) < 0) {
136 INTON;
137 return -1;
138 }
139 updatepwd(dest);
139 hashcd(); /* update command hash table */
140 if (prevdir)
141 ckfree(prevdir);
142 prevdir = curdir;
143 curdir = savestr(stackblock());
144 INTON;
145 if (print && iflag)
146 out1fmt("%s\n", stackblock());
147 return 0;
148}
149
150
151/*

--- 17 unchanged lines hidden (view full) ---

169 *p++ = '\0';
170 cdcomppath = p;
171 }
172 return start;
173}
174
175
176/*
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.
177 * Determine the new working directory, but don't actually enforce
178 * any changes.
179 */
180STATIC void
181updatepwd(dir)
182 char *dir;
183{
184 char *new;
185 char *p;
186
184 hashcd(); /* update command hash table */
187 cdcomppath = stalloc(strlen(dir) + 1);
188 scopy(dir, cdcomppath);
189 STARTSTACKSTR(new);
190 if (*dir != '/') {
191 if (curdir == NULL)
192 return;
193 p = curdir;
194 while (*p)

--- 8 unchanged lines hidden (view full) ---

203 STPUTC('/', new);
204 while (*p)
205 STPUTC(*p++, new);
206 }
207 }
208 if (new == stackblock())
209 STPUTC('/', new);
210 STACKSTRNUL(new);
209 INTOFF;
210 if (prevdir)
211 ckfree(prevdir);
212 prevdir = curdir;
213 curdir = savestr(stackblock());
214 INTON;
211}
212
213
214int
215pwdcmd(argc, argv)
216 int argc;
217 char **argv;
218{
223 if(!getpwd())
219 if (!getpwd())
220 error("getcwd() failed: %s", strerror(errno));
221 out1str(curdir);
222 out1c('\n');
223 return 0;
224}
225
226
227/*
228 * Find out what the current directory is. If we already know the current
229 * directory, this routine returns immediately.
230 */
231char *
232getpwd()
233{
234 if (curdir)
235 return (curdir);
236 return ((curdir = getcwd(NULL, 0)));
237}