xref: /openbsd/usr.bin/vi/ex/ex_cd.c (revision 891d7ab6)
1 /*	$OpenBSD: ex_cd.c,v 1.9 2009/10/27 23:59:47 deraadt 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(sp, cmdp)
36 	SCR *sp;
37 	EXCMD *cmdp;
38 {
39 	struct passwd *pw;
40 	ARGS *ap;
41 	CHAR_T savech;
42 	char *dir, *p, *t;	/* XXX: END OF THE STACK, DON'T TRUST GETCWD. */
43 	char buf[MAXPATHLEN * 2];
44 
45 	/*
46 	 * !!!
47 	 * Historic practice is that the cd isn't attempted if the file has
48 	 * been modified, unless its name begins with a leading '/' or the
49 	 * force flag is set.
50 	 */
51 	if (F_ISSET(sp->ep, F_MODIFIED) &&
52 	    !FL_ISSET(cmdp->iflags, E_C_FORCE) && sp->frp->name[0] != '/') {
53 		msgq(sp, M_ERR,
54     "120|File modified since last complete write; write or use ! to override");
55 		return (1);
56 	}
57 
58 	switch (cmdp->argc) {
59 	case 0:
60 		/* If no argument, change to the user's home directory. */
61 		if ((dir = getenv("HOME")) == NULL || *dir == '\0') {
62 			if ((pw = getpwuid(getuid())) == NULL ||
63 			    pw->pw_dir == NULL || pw->pw_dir[0] == '\0') {
64 				msgq(sp, M_ERR,
65 			   "121|Unable to find home directory location");
66 				return (1);
67 			}
68 			dir = pw->pw_dir;
69 		}
70 		break;
71 	case 1:
72 		dir = cmdp->argv[0]->bp;
73 		break;
74 	default:
75 		abort();
76 	}
77 
78 	/*
79 	 * Try the current directory first.  If this succeeds, don't display
80 	 * a message, vi didn't historically, and it should be obvious to the
81 	 * user where they are.
82 	 */
83 	if (!chdir(dir))
84 		return (0);
85 
86 	/*
87 	 * If moving to the user's home directory, or, the path begins with
88 	 * "/", "./" or "../", it's the only place we try.
89 	 */
90 	if (cmdp->argc == 0 ||
91 	    (ap = cmdp->argv[0])->bp[0] == '/' ||
92 	    (ap->len == 1 && ap->bp[0] == '.') ||
93 	    (ap->len >= 2 && ap->bp[0] == '.' && ap->bp[1] == '.' &&
94 	    (ap->bp[2] == '/' || ap->bp[2] == '\0')))
95 		goto err;
96 
97 	/* Try the O_CDPATH option values. */
98 	for (p = t = O_STR(sp, O_CDPATH);; ++p)
99 		if (*p == '\0' || *p == ':') {
100 			/*
101 			 * Empty strings specify ".".  The only way to get an
102 			 * empty string is a leading colon, colons in a row,
103 			 * or a trailing colon.  Or, to put it the other way,
104 			 * if the length is 1 or less, then we're dealing with
105 			 * ":XXX", "XXX::XXXX" , "XXX:", or "".  Since we've
106 			 * already tried dot, we ignore them all.
107 			 */
108 			if (t < p - 1) {
109 				savech = *p;
110 				*p = '\0';
111 				(void)snprintf(buf,
112 				    sizeof(buf), "%s/%s", t, dir);
113 				*p = savech;
114 				if (!chdir(buf)) {
115 					if (getcwd(buf, sizeof(buf)) != NULL)
116 		msgq_str(sp, M_INFO, buf, "122|New current directory: %s");
117 					return (0);
118 				}
119 			}
120 			t = p + 1;
121 			if (*p == '\0')
122 				break;
123 		}
124 
125 err:	msgq_str(sp, M_SYSERR, dir, "%s");
126 	return (1);
127 }
128