2002-04-09 05:47:30 +04:00
|
|
|
/* $NetBSD: ex_cd.c,v 1.11 2002/04/09 01:47:33 thorpej Exp $ */
|
1998-01-09 11:03:16 +03:00
|
|
|
|
1994-01-24 08:52:58 +03:00
|
|
|
/*-
|
1994-03-28 08:28:20 +04:00
|
|
|
* Copyright (c) 1992, 1993, 1994
|
1994-01-24 08:52:58 +03:00
|
|
|
* The Regents of the University of California. All rights reserved.
|
1996-05-20 07:47:00 +04:00
|
|
|
* Copyright (c) 1992, 1993, 1994, 1995, 1996
|
|
|
|
* Keith Bostic. All rights reserved.
|
1994-01-24 08:52:58 +03:00
|
|
|
*
|
1996-05-20 07:47:00 +04:00
|
|
|
* See the LICENSE file for redistribution information.
|
1994-01-24 08:52:58 +03:00
|
|
|
*/
|
|
|
|
|
1996-05-20 07:47:00 +04:00
|
|
|
#include "config.h"
|
|
|
|
|
2002-04-09 05:47:30 +04:00
|
|
|
#include <sys/cdefs.h>
|
1994-01-24 08:52:58 +03:00
|
|
|
#ifndef lint
|
2002-04-09 05:47:30 +04:00
|
|
|
#if 0
|
2001-03-31 15:37:44 +04:00
|
|
|
static const char sccsid[] = "@(#)ex_cd.c 10.10 (Berkeley) 8/12/96";
|
2002-04-09 05:47:30 +04:00
|
|
|
#else
|
|
|
|
__RCSID("$NetBSD");
|
|
|
|
#endif
|
1994-01-24 08:52:58 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
1994-03-28 08:28:20 +04:00
|
|
|
#include <sys/queue.h>
|
1994-01-24 08:52:58 +03:00
|
|
|
|
1994-03-28 08:28:20 +04:00
|
|
|
#include <bitstring.h>
|
1994-01-24 08:52:58 +03:00
|
|
|
#include <errno.h>
|
1994-03-28 08:28:20 +04:00
|
|
|
#include <limits.h>
|
1994-08-17 20:35:35 +04:00
|
|
|
#include <pwd.h>
|
1994-03-28 08:28:20 +04:00
|
|
|
#include <stdio.h>
|
1994-01-24 08:52:58 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
1996-05-20 07:47:00 +04:00
|
|
|
#include "../common/common.h"
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ex_cd -- :cd[!] [directory]
|
|
|
|
* Change directories.
|
1996-05-20 07:47:00 +04:00
|
|
|
*
|
|
|
|
* PUBLIC: int ex_cd __P((SCR *, EXCMD *));
|
1994-01-24 08:52:58 +03:00
|
|
|
*/
|
|
|
|
int
|
1996-05-20 07:47:00 +04:00
|
|
|
ex_cd(sp, cmdp)
|
1994-01-24 08:52:58 +03:00
|
|
|
SCR *sp;
|
1996-05-20 07:47:00 +04:00
|
|
|
EXCMD *cmdp;
|
1994-01-24 08:52:58 +03:00
|
|
|
{
|
1994-08-17 20:35:35 +04:00
|
|
|
struct passwd *pw;
|
|
|
|
ARGS *ap;
|
2001-03-31 15:37:44 +04:00
|
|
|
CHAR_T savech;
|
|
|
|
const char *dir, *t; /* XXX: END OF THE STACK, DON'T TRUST GETCWD. */
|
|
|
|
char *p, buf[MAXPATHLEN * 2];
|
1994-08-17 20:35:35 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* !!!
|
|
|
|
* Historic practice is that the cd isn't attempted if the file has
|
|
|
|
* been modified, unless its name begins with a leading '/' or the
|
|
|
|
* force flag is set.
|
|
|
|
*/
|
1996-05-20 07:47:00 +04:00
|
|
|
if (F_ISSET(sp->ep, F_MODIFIED) &&
|
|
|
|
!FL_ISSET(cmdp->iflags, E_C_FORCE) && sp->frp->name[0] != '/') {
|
1994-08-17 20:35:35 +04:00
|
|
|
msgq(sp, M_ERR,
|
1996-05-20 07:47:00 +04:00
|
|
|
"120|File modified since last complete write; write or use ! to override");
|
1994-08-17 20:35:35 +04:00
|
|
|
return (1);
|
|
|
|
}
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
switch (cmdp->argc) {
|
|
|
|
case 0:
|
1994-08-17 20:35:35 +04:00
|
|
|
/* If no argument, change to the user's home directory. */
|
1994-01-24 08:52:58 +03:00
|
|
|
if ((dir = getenv("HOME")) == NULL) {
|
1994-08-17 20:35:35 +04:00
|
|
|
if ((pw = getpwuid(getuid())) == NULL ||
|
|
|
|
pw->pw_dir == NULL || pw->pw_dir[0] == '\0') {
|
|
|
|
msgq(sp, M_ERR,
|
1996-05-20 07:47:00 +04:00
|
|
|
"121|Unable to find home directory location");
|
1994-08-17 20:35:35 +04:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
dir = pw->pw_dir;
|
1994-01-24 08:52:58 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
dir = cmdp->argv[0]->bp;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
1996-05-20 07:47:00 +04:00
|
|
|
/*
|
2001-03-31 15:37:44 +04:00
|
|
|
* Try the current directory first. If this succeeds, don't display
|
|
|
|
* a message, vi didn't historically, and it should be obvious to the
|
|
|
|
* user where they are.
|
1996-05-20 07:47:00 +04:00
|
|
|
*/
|
1994-08-17 20:35:35 +04:00
|
|
|
if (!chdir(dir))
|
1996-05-20 07:47:00 +04:00
|
|
|
return (0);
|
1994-08-17 20:35:35 +04:00
|
|
|
|
1994-03-28 08:28:20 +04:00
|
|
|
/*
|
1994-08-17 20:35:35 +04:00
|
|
|
* If moving to the user's home directory, or, the path begins with
|
|
|
|
* "/", "./" or "../", it's the only place we try.
|
1994-03-28 08:28:20 +04:00
|
|
|
*/
|
1994-08-17 20:35:35 +04:00
|
|
|
if (cmdp->argc == 0 ||
|
|
|
|
(ap = cmdp->argv[0])->bp[0] == '/' ||
|
|
|
|
ap->len == 1 && ap->bp[0] == '.' ||
|
|
|
|
ap->len >= 2 && ap->bp[0] == '.' && ap->bp[1] == '.' &&
|
|
|
|
(ap->bp[2] == '/' || ap->bp[2] == '\0'))
|
|
|
|
goto err;
|
|
|
|
|
2001-03-31 15:37:44 +04:00
|
|
|
/* Try the O_CDPATH option values. */
|
|
|
|
for (t = p = O_STR(sp, O_CDPATH);; ++p)
|
1994-03-28 08:28:20 +04:00
|
|
|
if (*p == '\0' || *p == ':') {
|
|
|
|
/*
|
|
|
|
* Empty strings specify ".". The only way to get an
|
|
|
|
* empty string is a leading colon, colons in a row,
|
|
|
|
* or a trailing colon. Or, to put it the other way,
|
2001-03-31 15:37:44 +04:00
|
|
|
* if the length is 1 or less, then we're dealing with
|
|
|
|
* ":XXX", "XXX::XXXX" , "XXX:", or "". Since we've
|
|
|
|
* already tried dot, we ignore tham all.
|
1994-03-28 08:28:20 +04:00
|
|
|
*/
|
2001-03-31 15:37:44 +04:00
|
|
|
if (t < p - 1) {
|
|
|
|
savech = *p;
|
|
|
|
*p = '\0';
|
|
|
|
(void)snprintf(buf,
|
|
|
|
sizeof(buf), "%s/%s", t, dir);
|
|
|
|
*p = savech;
|
|
|
|
if (!chdir(buf)) {
|
|
|
|
if (getcwd(buf, sizeof(buf)) != NULL)
|
|
|
|
msgq_str(sp, M_INFO, buf, "122|New current directory: %s");
|
|
|
|
return (0);
|
1994-03-28 08:28:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
t = p + 1;
|
2001-03-31 15:37:44 +04:00
|
|
|
if (*p == '\0')
|
|
|
|
break;
|
1994-03-28 08:28:20 +04:00
|
|
|
}
|
|
|
|
|
2001-03-31 15:37:44 +04:00
|
|
|
err: msgq_str(sp, M_SYSERR, dir, "%s");
|
|
|
|
return (1);
|
1994-03-28 08:28:20 +04:00
|
|
|
}
|