xref: /minix/external/bsd/nvi/dist/ex/ex_cd.c (revision 0a6a1f1d)
1 /*	$NetBSD: ex_cd.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
2 /*-
3  * Copyright (c) 1992, 1993, 1994
4  *	The Regents of the University of California.  All rights reserved.
5  * Copyright (c) 1992, 1993, 1994, 1995, 1996
6  *	Keith Bostic.  All rights reserved.
7  *
8  * See the LICENSE file for redistribution information.
9  */
10 
11 #include "config.h"
12 
13 #include <sys/cdefs.h>
14 #if 0
15 #ifndef lint
16 static const char sccsid[] = "Id: ex_cd.c,v 10.12 2001/06/25 15:19:14 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:14 ";
17 #endif /* not lint */
18 #else
19 __RCSID("$NetBSD: ex_cd.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20 #endif
21 
22 #include <sys/param.h>
23 #include <sys/queue.h>
24 
25 #include <bitstring.h>
26 #include <errno.h>
27 #include <limits.h>
28 #include <pwd.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 
34 #include "../common/common.h"
35 
36 /*
37  * ex_cd -- :cd[!] [directory]
38  *	Change directories.
39  *
40  * PUBLIC: int ex_cd __P((SCR *, EXCMD *));
41  */
42 int
ex_cd(SCR * sp,EXCMD * cmdp)43 ex_cd(SCR *sp, EXCMD *cmdp)
44 {
45 	struct passwd *pw;
46 	ARGS *ap;
47 	const char *p, *t;	/* XXX: END OF THE STACK, DON'T TRUST GETCWD. */
48 	const char *dir;
49 	char buf[MAXPATHLEN * 2];
50 	size_t dlen;
51 
52 	/*
53 	 * !!!
54 	 * Historic practice is that the cd isn't attempted if the file has
55 	 * been modified, unless its name begins with a leading '/' or the
56 	 * force flag is set.
57 	 */
58 	if (F_ISSET(sp->ep, F_MODIFIED) &&
59 	    !FL_ISSET(cmdp->iflags, E_C_FORCE) && sp->frp->name[0] != '/') {
60 		msgq(sp, M_ERR,
61     "120|File modified since last complete write; write or use ! to override");
62 		return (1);
63 	}
64 
65 	switch (cmdp->argc) {
66 	case 0:
67 		/* If no argument, change to the user's home directory. */
68 		if ((dir = getenv("HOME")) == NULL) {
69 			if ((pw = getpwuid(getuid())) == NULL ||
70 			    pw->pw_dir == NULL || pw->pw_dir[0] == '\0') {
71 				msgq(sp, M_ERR,
72 			   "121|Unable to find home directory location");
73 				return (1);
74 			}
75 			dir = pw->pw_dir;
76 		}
77 		break;
78 	case 1:
79 		INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1,
80 			 dir, dlen);
81 		break;
82 	default:
83 		abort();
84 	}
85 
86 	/*
87 	 * Try the current directory first.  If this succeeds, don't display
88 	 * a message, vi didn't historically, and it should be obvious to the
89 	 * user where they are.
90 	 */
91 	if (!chdir(dir))
92 		return (0);
93 
94 	/*
95 	 * If moving to the user's home directory, or, the path begins with
96 	 * "/", "./" or "../", it's the only place we try.
97 	 */
98 	if (cmdp->argc == 0 ||
99 	    (ap = cmdp->argv[0])->bp[0] == '/' ||
100 	    (ap->len == 1 && ap->bp[0] == '.') ||
101 	    (ap->len >= 2 && ap->bp[0] == '.' && ap->bp[1] == '.' &&
102 	    (ap->bp[2] == '/' || ap->bp[2] == '\0')))
103 		goto err;
104 
105 	/* Try the O_CDPATH option values. */
106 	for (p = t = O_STR(sp, O_CDPATH);; ++p)
107 		if (*p == '\0' || *p == ':') {
108 			/*
109 			 * Empty strings specify ".".  The only way to get an
110 			 * empty string is a leading colon, colons in a row,
111 			 * or a trailing colon.  Or, to put it the other way,
112 			 * if the length is 1 or less, then we're dealing with
113 			 * ":XXX", "XXX::XXXX" , "XXX:", or "".  Since we've
114 			 * already tried dot, we ignore tham all.
115 			 */
116 			if (t < p - 1) {
117 				(void)snprintf(buf,
118 				    sizeof(buf), "%.*s/%s", (int)(p - t),
119 					t, dir);
120 				if (!chdir(buf)) {
121 					if (getcwd(buf, sizeof(buf)) != NULL)
122 		msgq_str(sp, M_INFO, buf, "122|New current directory: %s");
123 					return (0);
124 				}
125 			}
126 			t = p + 1;
127 			if (*p == '\0')
128 				break;
129 		}
130 
131 err:	msgq_str(sp, M_SYSERR, dir, "%s");
132 	return (1);
133 }
134