xref: /minix3/external/bsd/nvi/dist/ex/ex_cd.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ex_cd.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994
484d9c625SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
584d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994, 1995, 1996
684d9c625SLionel Sambuc  *	Keith Bostic.  All rights reserved.
784d9c625SLionel Sambuc  *
884d9c625SLionel Sambuc  * See the LICENSE file for redistribution information.
984d9c625SLionel Sambuc  */
1084d9c625SLionel Sambuc 
1184d9c625SLionel Sambuc #include "config.h"
1284d9c625SLionel Sambuc 
13*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
14*0a6a1f1dSLionel Sambuc #if 0
1584d9c625SLionel Sambuc #ifndef lint
1684d9c625SLionel Sambuc 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 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ex_cd.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20*0a6a1f1dSLionel Sambuc #endif
2184d9c625SLionel Sambuc 
2284d9c625SLionel Sambuc #include <sys/param.h>
2384d9c625SLionel Sambuc #include <sys/queue.h>
2484d9c625SLionel Sambuc 
2584d9c625SLionel Sambuc #include <bitstring.h>
2684d9c625SLionel Sambuc #include <errno.h>
2784d9c625SLionel Sambuc #include <limits.h>
2884d9c625SLionel Sambuc #include <pwd.h>
2984d9c625SLionel Sambuc #include <stdio.h>
3084d9c625SLionel Sambuc #include <stdlib.h>
3184d9c625SLionel Sambuc #include <string.h>
3284d9c625SLionel Sambuc #include <unistd.h>
3384d9c625SLionel Sambuc 
3484d9c625SLionel Sambuc #include "../common/common.h"
3584d9c625SLionel Sambuc 
3684d9c625SLionel Sambuc /*
3784d9c625SLionel Sambuc  * ex_cd -- :cd[!] [directory]
3884d9c625SLionel Sambuc  *	Change directories.
3984d9c625SLionel Sambuc  *
4084d9c625SLionel Sambuc  * PUBLIC: int ex_cd __P((SCR *, EXCMD *));
4184d9c625SLionel Sambuc  */
4284d9c625SLionel Sambuc int
ex_cd(SCR * sp,EXCMD * cmdp)4384d9c625SLionel Sambuc ex_cd(SCR *sp, EXCMD *cmdp)
4484d9c625SLionel Sambuc {
4584d9c625SLionel Sambuc 	struct passwd *pw;
4684d9c625SLionel Sambuc 	ARGS *ap;
4784d9c625SLionel Sambuc 	const char *p, *t;	/* XXX: END OF THE STACK, DON'T TRUST GETCWD. */
4884d9c625SLionel Sambuc 	const char *dir;
4984d9c625SLionel Sambuc 	char buf[MAXPATHLEN * 2];
5084d9c625SLionel Sambuc 	size_t dlen;
5184d9c625SLionel Sambuc 
5284d9c625SLionel Sambuc 	/*
5384d9c625SLionel Sambuc 	 * !!!
5484d9c625SLionel Sambuc 	 * Historic practice is that the cd isn't attempted if the file has
5584d9c625SLionel Sambuc 	 * been modified, unless its name begins with a leading '/' or the
5684d9c625SLionel Sambuc 	 * force flag is set.
5784d9c625SLionel Sambuc 	 */
5884d9c625SLionel Sambuc 	if (F_ISSET(sp->ep, F_MODIFIED) &&
5984d9c625SLionel Sambuc 	    !FL_ISSET(cmdp->iflags, E_C_FORCE) && sp->frp->name[0] != '/') {
6084d9c625SLionel Sambuc 		msgq(sp, M_ERR,
6184d9c625SLionel Sambuc     "120|File modified since last complete write; write or use ! to override");
6284d9c625SLionel Sambuc 		return (1);
6384d9c625SLionel Sambuc 	}
6484d9c625SLionel Sambuc 
6584d9c625SLionel Sambuc 	switch (cmdp->argc) {
6684d9c625SLionel Sambuc 	case 0:
6784d9c625SLionel Sambuc 		/* If no argument, change to the user's home directory. */
6884d9c625SLionel Sambuc 		if ((dir = getenv("HOME")) == NULL) {
6984d9c625SLionel Sambuc 			if ((pw = getpwuid(getuid())) == NULL ||
7084d9c625SLionel Sambuc 			    pw->pw_dir == NULL || pw->pw_dir[0] == '\0') {
7184d9c625SLionel Sambuc 				msgq(sp, M_ERR,
7284d9c625SLionel Sambuc 			   "121|Unable to find home directory location");
7384d9c625SLionel Sambuc 				return (1);
7484d9c625SLionel Sambuc 			}
7584d9c625SLionel Sambuc 			dir = pw->pw_dir;
7684d9c625SLionel Sambuc 		}
7784d9c625SLionel Sambuc 		break;
7884d9c625SLionel Sambuc 	case 1:
7984d9c625SLionel Sambuc 		INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1,
8084d9c625SLionel Sambuc 			 dir, dlen);
8184d9c625SLionel Sambuc 		break;
8284d9c625SLionel Sambuc 	default:
8384d9c625SLionel Sambuc 		abort();
8484d9c625SLionel Sambuc 	}
8584d9c625SLionel Sambuc 
8684d9c625SLionel Sambuc 	/*
8784d9c625SLionel Sambuc 	 * Try the current directory first.  If this succeeds, don't display
8884d9c625SLionel Sambuc 	 * a message, vi didn't historically, and it should be obvious to the
8984d9c625SLionel Sambuc 	 * user where they are.
9084d9c625SLionel Sambuc 	 */
9184d9c625SLionel Sambuc 	if (!chdir(dir))
9284d9c625SLionel Sambuc 		return (0);
9384d9c625SLionel Sambuc 
9484d9c625SLionel Sambuc 	/*
9584d9c625SLionel Sambuc 	 * If moving to the user's home directory, or, the path begins with
9684d9c625SLionel Sambuc 	 * "/", "./" or "../", it's the only place we try.
9784d9c625SLionel Sambuc 	 */
9884d9c625SLionel Sambuc 	if (cmdp->argc == 0 ||
9984d9c625SLionel Sambuc 	    (ap = cmdp->argv[0])->bp[0] == '/' ||
10084d9c625SLionel Sambuc 	    (ap->len == 1 && ap->bp[0] == '.') ||
10184d9c625SLionel Sambuc 	    (ap->len >= 2 && ap->bp[0] == '.' && ap->bp[1] == '.' &&
10284d9c625SLionel Sambuc 	    (ap->bp[2] == '/' || ap->bp[2] == '\0')))
10384d9c625SLionel Sambuc 		goto err;
10484d9c625SLionel Sambuc 
10584d9c625SLionel Sambuc 	/* Try the O_CDPATH option values. */
10684d9c625SLionel Sambuc 	for (p = t = O_STR(sp, O_CDPATH);; ++p)
10784d9c625SLionel Sambuc 		if (*p == '\0' || *p == ':') {
10884d9c625SLionel Sambuc 			/*
10984d9c625SLionel Sambuc 			 * Empty strings specify ".".  The only way to get an
11084d9c625SLionel Sambuc 			 * empty string is a leading colon, colons in a row,
11184d9c625SLionel Sambuc 			 * or a trailing colon.  Or, to put it the other way,
11284d9c625SLionel Sambuc 			 * if the length is 1 or less, then we're dealing with
11384d9c625SLionel Sambuc 			 * ":XXX", "XXX::XXXX" , "XXX:", or "".  Since we've
11484d9c625SLionel Sambuc 			 * already tried dot, we ignore tham all.
11584d9c625SLionel Sambuc 			 */
11684d9c625SLionel Sambuc 			if (t < p - 1) {
11784d9c625SLionel Sambuc 				(void)snprintf(buf,
11884d9c625SLionel Sambuc 				    sizeof(buf), "%.*s/%s", (int)(p - t),
11984d9c625SLionel Sambuc 					t, dir);
12084d9c625SLionel Sambuc 				if (!chdir(buf)) {
12184d9c625SLionel Sambuc 					if (getcwd(buf, sizeof(buf)) != NULL)
12284d9c625SLionel Sambuc 		msgq_str(sp, M_INFO, buf, "122|New current directory: %s");
12384d9c625SLionel Sambuc 					return (0);
12484d9c625SLionel Sambuc 				}
12584d9c625SLionel Sambuc 			}
12684d9c625SLionel Sambuc 			t = p + 1;
12784d9c625SLionel Sambuc 			if (*p == '\0')
12884d9c625SLionel Sambuc 				break;
12984d9c625SLionel Sambuc 		}
13084d9c625SLionel Sambuc 
13184d9c625SLionel Sambuc err:	msgq_str(sp, M_SYSERR, dir, "%s");
13284d9c625SLionel Sambuc 	return (1);
13384d9c625SLionel Sambuc }
134