159243Sobrien/*- 259243Sobrien * Copyright (c) 1992, 1993, 1994 3231990Smp * The Regents of the University of California. All rights reserved. 4231990Smp * Copyright (c) 1992, 1993, 1994, 1995, 1996 5231990Smp * Keith Bostic. All rights reserved. 659243Sobrien * 7231990Smp * See the LICENSE file for redistribution information. 859243Sobrien */ 9231990Smp 10231990Smp#include "config.h" 11231990Smp 12231990Smp#ifndef lint 13231990Smpstatic const char sccsid[] = "$Id: ex_cd.c,v 10.13 2012/04/12 06:28:27 zy Exp $"; 14231990Smp#endif /* not lint */ 15231990Smp 16231990Smp#include <sys/queue.h> 17231990Smp#include <sys/time.h> 1859243Sobrien 1959243Sobrien#include <bitstring.h> 2059243Sobrien#include <errno.h> 21231990Smp#include <limits.h> 22231990Smp#include <pwd.h> 23231990Smp#include <stdio.h> 24231990Smp#include <stdlib.h> 25231990Smp#include <string.h> 26231990Smp#include <unistd.h> 27231990Smp 28231990Smp#include "../common/common.h" 29231990Smp 30231990Smp/* 31 * ex_cd -- :cd[!] [directory] 32 * Change directories. 33 * 34 * PUBLIC: int ex_cd __P((SCR *, EXCMD *)); 35 */ 36int 37ex_cd(SCR *sp, EXCMD *cmdp) 38{ 39 struct passwd *pw; 40 ARGS *ap; 41 int savech; 42 char *dir, *p, *t; 43 char *buf; 44 size_t dlen; 45 46 /* 47 * !!! 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 50 * force flag is set. 51 */ 52 if (F_ISSET(sp->ep, F_MODIFIED) && 53 !FL_ISSET(cmdp->iflags, E_C_FORCE) && sp->frp->name[0] != '/') { 54 msgq(sp, M_ERR, 55 "120|File modified since last complete write; write or use ! to override"); 56 return (1); 57 } 58 59 switch (cmdp->argc) { 60 case 0: 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') { 65 msgq(sp, M_ERR, 66 "121|Unable to find home directory location"); 67 return (1); 68 } 69 dir = pw->pw_dir; 70 } 71 break; 72 case 1: 73 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1, 74 dir, dlen); 75 break; 76 default: 77 abort(); 78 } 79 80 /* 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. 84 */ 85 if (!chdir(dir)) 86 return (0); 87 88 /* 89 * If moving to the user's home directory, or, the path begins with 90 * "/", "./" or "../", it's the only place we try. 91 */ 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'))) 97 goto err; 98 99 /* Try the O_CDPATH option values. */ 100 for (p = t = O_STR(sp, O_CDPATH);; ++p) 101 if (*p == '\0' || *p == ':') { 102 /* 103 * Ignore the empty strings and ".", since we've already 104 * tried the current directory. 105 */ 106 if (t < p && (p - t != 1 || *t != '.')) { 107 savech = *p; 108 *p = '\0'; 109 if ((buf = join(t, dir)) == NULL) { 110 msgq(sp, M_SYSERR, NULL); 111 return (1); 112 } 113 *p = savech; 114 if (!chdir(buf)) { 115 free(buf); 116 if ((buf = getcwd(NULL, 0)) != NULL) { 117 msgq_str(sp, M_INFO, buf, "122|New current directory: %s"); 118 free(buf); 119 } 120 return (0); 121 } 122 free(buf); 123 } 124 t = p + 1; 125 if (*p == '\0') 126 break; 127 } 128 129err: msgq_str(sp, M_SYSERR, dir, "%s"); 130 return (1); 131} 132