1 /* $OpenBSD: ex_cd.c,v 1.10 2014/11/12 04:28:41 bentley Exp $ */ 2 3 /*- 4 * Copyright (c) 1992, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * Copyright (c) 1992, 1993, 1994, 1995, 1996 7 * Keith Bostic. All rights reserved. 8 * 9 * See the LICENSE file for redistribution information. 10 */ 11 12 #include "config.h" 13 14 #include <sys/param.h> 15 #include <sys/queue.h> 16 17 #include <bitstring.h> 18 #include <errno.h> 19 #include <limits.h> 20 #include <pwd.h> 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <string.h> 24 #include <unistd.h> 25 26 #include "../common/common.h" 27 28 /* 29 * ex_cd -- :cd[!] [directory] 30 * Change directories. 31 * 32 * PUBLIC: int ex_cd(SCR *, EXCMD *); 33 */ 34 int 35 ex_cd(SCR *sp, EXCMD *cmdp) 36 { 37 struct passwd *pw; 38 ARGS *ap; 39 CHAR_T savech; 40 char *dir, *p, *t; /* XXX: END OF THE STACK, DON'T TRUST GETCWD. */ 41 char buf[MAXPATHLEN * 2]; 42 43 /* 44 * !!! 45 * Historic practice is that the cd isn't attempted if the file has 46 * been modified, unless its name begins with a leading '/' or the 47 * force flag is set. 48 */ 49 if (F_ISSET(sp->ep, F_MODIFIED) && 50 !FL_ISSET(cmdp->iflags, E_C_FORCE) && sp->frp->name[0] != '/') { 51 msgq(sp, M_ERR, 52 "120|File modified since last complete write; write or use ! to override"); 53 return (1); 54 } 55 56 switch (cmdp->argc) { 57 case 0: 58 /* If no argument, change to the user's home directory. */ 59 if ((dir = getenv("HOME")) == NULL || *dir == '\0') { 60 if ((pw = getpwuid(getuid())) == NULL || 61 pw->pw_dir == NULL || pw->pw_dir[0] == '\0') { 62 msgq(sp, M_ERR, 63 "121|Unable to find home directory location"); 64 return (1); 65 } 66 dir = pw->pw_dir; 67 } 68 break; 69 case 1: 70 dir = cmdp->argv[0]->bp; 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 * Empty strings specify ".". The only way to get an 100 * empty string is a leading colon, colons in a row, 101 * or a trailing colon. Or, to put it the other way, 102 * if the length is 1 or less, then we're dealing with 103 * ":XXX", "XXX::XXXX" , "XXX:", or "". Since we've 104 * already tried dot, we ignore them all. 105 */ 106 if (t < p - 1) { 107 savech = *p; 108 *p = '\0'; 109 (void)snprintf(buf, 110 sizeof(buf), "%s/%s", t, dir); 111 *p = savech; 112 if (!chdir(buf)) { 113 if (getcwd(buf, sizeof(buf)) != NULL) 114 msgq_str(sp, M_INFO, buf, "122|New current directory: %s"); 115 return (0); 116 } 117 } 118 t = p + 1; 119 if (*p == '\0') 120 break; 121 } 122 123 err: msgq_str(sp, M_SYSERR, dir, "%s"); 124 return (1); 125 } 126