cd.c revision 20886
1258307Sgjb/*- 2258307Sgjb * Copyright (c) 1991, 1993 3258307Sgjb * The Regents of the University of California. All rights reserved. 4258307Sgjb * 5258307Sgjb * This code is derived from software contributed to Berkeley by 6258307Sgjb * Kenneth Almquist. 7258307Sgjb * 8266553Sgjb * Redistribution and use in source and binary forms, with or without 9260772Sgjb * modification, are permitted provided that the following conditions 10260772Sgjb * are met: 11260772Sgjb * 1. Redistributions of source code must retain the above copyright 12260772Sgjb * notice, this list of conditions and the following disclaimer. 13258307Sgjb * 2. Redistributions in binary form must reproduce the above copyright 14276827Sgjb * notice, this list of conditions and the following disclaimer in the 15260772Sgjb * documentation and/or other materials provided with the distribution. 16260772Sgjb * 3. All advertising materials mentioning features or use of this software 17260772Sgjb * must display the following acknowledgement: 18260772Sgjb * This product includes software developed by the University of 19260772Sgjb * California, Berkeley and its contributors. 20260772Sgjb * 4. Neither the name of the University nor the names of its contributors 21260772Sgjb * may be used to endorse or promote products derived from this software 22260772Sgjb * without specific prior written permission. 23260772Sgjb * 24260772Sgjb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25260772Sgjb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26260772Sgjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27260772Sgjb * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28260772Sgjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29260772Sgjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30260772Sgjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31260772Sgjb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32260772Sgjb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33258307Sgjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34259246Sgjb * SUCH DAMAGE. 35259246Sgjb * 36276820Sgjb * $Id: cd.c,v 1.9 1996/12/23 05:31:48 steve Exp $ 37276820Sgjb */ 38276820Sgjb 39259246Sgjb#ifndef lint 40259246Sgjbstatic char const sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95"; 41259246Sgjb#endif /* not lint */ 42258307Sgjb 43276822Sgjb#include <sys/types.h> 44258847Sgjb#include <sys/stat.h> 45258307Sgjb#include <stdlib.h> 46258307Sgjb#include <string.h> 47276765Sgjb#include <unistd.h> 48271491Sgjb#include <errno.h> 49276765Sgjb#include <limits.h> 50276765Sgjb 51260772Sgjb/* 52271491Sgjb * The cd and pwd commands. 53276765Sgjb */ 54276766Sgjb 55276765Sgjb#include "shell.h" 56258307Sgjb#include "var.h" 57276827Sgjb#include "nodes.h" /* for jobs.h */ 58276827Sgjb#include "jobs.h" 59276827Sgjb#include "options.h" 60276827Sgjb#include "output.h" 61276827Sgjb#include "memalloc.h" 62276827Sgjb#include "error.h" 63276827Sgjb#include "exec.h" 64276827Sgjb#include "redir.h" 65276827Sgjb#include "mystring.h" 66276827Sgjb#include "show.h" 67276827Sgjb#include "cd.h" 68276827Sgjb 69276827SgjbSTATIC int docd __P((char *, int)); 70276827SgjbSTATIC char *getcomponent __P((void)); 71276827SgjbSTATIC void updatepwd __P((char *)); 72276827Sgjb 73276827Sgjbchar *curdir = NULL; /* current working directory */ 74276827Sgjbchar *prevdir; /* previous working directory */ 75276827SgjbSTATIC char *cdcomppath; 76260772Sgjb 77260772Sgjbint 78258307Sgjbcdcmd(argc, argv) 79271491Sgjb int argc; 80258307Sgjb char **argv; 81271876Sgjb{ 82271876Sgjb char *dest; 83271876Sgjb char *path; 84271876Sgjb char *p; 85271876Sgjb struct stat statb; 86271876Sgjb int print = 0; 87271480Sgjb 88258307Sgjb nextopt(nullstr); 89258307Sgjb if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL) 90258307Sgjb 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 (getcwd(NULL, 0)); 241} 242