head	1.13;
access;
symbols
	perseant-exfatfs-base-20250801:1.13
	perseant-exfatfs-base-20240630:1.13
	perseant-exfatfs:1.13.0.36
	perseant-exfatfs-base:1.13
	cjep_sun2x:1.13.0.34
	cjep_sun2x-base:1.13
	cjep_staticlib_x-base1:1.13
	cjep_staticlib_x:1.13.0.32
	cjep_staticlib_x-base:1.13
	phil-wifi-20200421:1.13
	phil-wifi-20200411:1.13
	phil-wifi-20200406:1.13
	pgoyette-compat-merge-20190127:1.13
	pgoyette-compat-20190127:1.13
	pgoyette-compat-20190118:1.13
	pgoyette-compat-1226:1.13
	pgoyette-compat-1126:1.13
	pgoyette-compat-1020:1.13
	pgoyette-compat-0930:1.13
	pgoyette-compat-0906:1.13
	pgoyette-compat-0728:1.13
	pgoyette-compat-0625:1.13
	pgoyette-compat-0521:1.13
	pgoyette-compat-0502:1.13
	pgoyette-compat-0422:1.13
	pgoyette-compat-0415:1.13
	pgoyette-compat-0407:1.13
	pgoyette-compat-0330:1.13
	pgoyette-compat-0322:1.13
	pgoyette-compat-0315:1.13
	pgoyette-compat:1.13.0.30
	pgoyette-compat-base:1.13
	perseant-stdc-iso10646:1.13.0.28
	perseant-stdc-iso10646-base:1.13
	prg-localcount2-base3:1.13
	prg-localcount2-base2:1.13
	prg-localcount2-base1:1.13
	prg-localcount2:1.13.0.26
	prg-localcount2-base:1.13
	pgoyette-localcount-20170426:1.13
	bouyer-socketcan-base1:1.13
	pgoyette-localcount-20170320:1.13
	bouyer-socketcan:1.13.0.24
	bouyer-socketcan-base:1.13
	pgoyette-localcount-20170107:1.13
	pgoyette-localcount-20161104:1.13
	localcount-20160914:1.13
	pgoyette-localcount-20160806:1.13
	pgoyette-localcount-20160726:1.13
	pgoyette-localcount:1.13.0.22
	pgoyette-localcount-base:1.13
	netbsd-5-2-3-RELEASE:1.13
	netbsd-5-1-5-RELEASE:1.13
	yamt-pagecache-base9:1.13
	yamt-pagecache-tag8:1.13
	tls-earlyentropy:1.13.0.18
	tls-earlyentropy-base:1.13
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.13
	riastradh-drm2-base3:1.13
	netbsd-5-2-2-RELEASE:1.13
	netbsd-5-1-4-RELEASE:1.13
	netbsd-5-2-1-RELEASE:1.13
	netbsd-5-1-3-RELEASE:1.13
	agc-symver:1.13.0.20
	agc-symver-base:1.13
	tls-maxphys-base:1.13
	yamt-pagecache-base8:1.13
	netbsd-5-2:1.13.0.16
	yamt-pagecache-base7:1.13
	netbsd-5-2-RELEASE:1.13
	netbsd-5-2-RC1:1.13
	yamt-pagecache-base6:1.13
	yamt-pagecache-base5:1.13
	yamt-pagecache-base4:1.13
	netbsd-5-1-2-RELEASE:1.13
	netbsd-5-1-1-RELEASE:1.13
	yamt-pagecache-base3:1.13
	yamt-pagecache-base2:1.13
	yamt-pagecache:1.13.0.14
	yamt-pagecache-base:1.13
	bouyer-quota2-nbase:1.13
	bouyer-quota2:1.13.0.12
	bouyer-quota2-base:1.13
	matt-nb5-pq3:1.13.0.10
	matt-nb5-pq3-base:1.13
	netbsd-5-1:1.13.0.8
	netbsd-5-1-RELEASE:1.13
	netbsd-5-1-RC4:1.13
	netbsd-5-1-RC3:1.13
	netbsd-5-1-RC2:1.13
	netbsd-5-1-RC1:1.13
	netbsd-5-0-2-RELEASE:1.13
	netbsd-5-0-1-RELEASE:1.13
	jym-xensuspend-nbase:1.13
	netbsd-5-0:1.13.0.6
	netbsd-5-0-RELEASE:1.13
	netbsd-5-0-RC4:1.13
	netbsd-5-0-RC3:1.13
	netbsd-5-0-RC2:1.13
	jym-xensuspend:1.13.0.4
	jym-xensuspend-base:1.13
	netbsd-5-0-RC1:1.13
	netbsd-5:1.13.0.2
	netbsd-5-base:1.13
	matt-mips64-base2:1.12
	matt-mips64:1.12.0.32
	mjf-devfs2:1.12.0.30
	mjf-devfs2-base:1.12
	netbsd-4-0-1-RELEASE:1.12
	wrstuden-revivesa-base-3:1.12
	wrstuden-revivesa-base-2:1.12
	wrstuden-fixsa-newbase:1.12
	wrstuden-revivesa-base-1:1.12
	yamt-pf42-base4:1.12
	yamt-pf42-base3:1.12
	hpcarm-cleanup-nbase:1.12
	yamt-pf42-baseX:1.12
	yamt-pf42-base2:1.12
	wrstuden-revivesa:1.12.0.28
	wrstuden-revivesa-base:1.12
	yamt-pf42:1.12.0.26
	yamt-pf42-base:1.12
	keiichi-mipv6-nbase:1.12
	keiichi-mipv6:1.12.0.24
	keiichi-mipv6-base:1.12
	matt-armv6-nbase:1.12
	matt-armv6-prevmlocking:1.12
	wrstuden-fixsa-base-1:1.12
	netbsd-4-0:1.12.0.22
	netbsd-4-0-RELEASE:1.12
	cube-autoconf:1.12.0.20
	cube-autoconf-base:1.12
	netbsd-4-0-RC5:1.12
	netbsd-4-0-RC4:1.12
	netbsd-4-0-RC3:1.12
	netbsd-4-0-RC2:1.12
	netbsd-4-0-RC1:1.12
	matt-armv6:1.12.0.18
	matt-armv6-base:1.12
	matt-mips64-base:1.12
	hpcarm-cleanup:1.12.0.16
	hpcarm-cleanup-base:1.12
	netbsd-3-1-1-RELEASE:1.12
	netbsd-3-0-3-RELEASE:1.12
	wrstuden-fixsa:1.12.0.14
	wrstuden-fixsa-base:1.12
	abandoned-netbsd-4-base:1.12
	abandoned-netbsd-4:1.12.0.8
	netbsd-3-1:1.12.0.10
	netbsd-3-1-RELEASE:1.12
	netbsd-3-0-2-RELEASE:1.12
	netbsd-3-1-RC4:1.12
	netbsd-3-1-RC3:1.12
	netbsd-3-1-RC2:1.12
	netbsd-3-1-RC1:1.12
	netbsd-4:1.12.0.12
	netbsd-4-base:1.12
	chap-midi-nbase:1.12
	netbsd-3-0-1-RELEASE:1.12
	chap-midi:1.12.0.6
	chap-midi-base:1.12
	netbsd-3-0:1.12.0.4
	netbsd-3-0-RELEASE:1.12
	netbsd-3-0-RC6:1.12
	netbsd-3-0-RC5:1.12
	netbsd-3-0-RC4:1.12
	netbsd-3-0-RC3:1.12
	netbsd-3-0-RC2:1.12
	netbsd-3-0-RC1:1.12
	netbsd-2-0-3-RELEASE:1.11.4.2
	netbsd-2-1:1.11.6.1.0.2
	netbsd-2-1-RELEASE:1.11.6.1
	netbsd-2-1-RC6:1.11.6.1
	netbsd-2-1-RC5:1.11.6.1
	netbsd-2-1-RC4:1.11.6.1
	netbsd-2-1-RC3:1.11.6.1
	netbsd-2-1-RC2:1.11.6.1
	netbsd-2-1-RC1:1.11.6.1
	netbsd-2-0-2-RELEASE:1.11.4.2
	netbsd-3:1.12.0.2
	netbsd-3-base:1.12
	netbsd-2-0-1-RELEASE:1.11
	netbsd-2:1.11.0.6
	netbsd-2-base:1.11
	netbsd-2-0-RELEASE:1.11
	netbsd-2-0-RC5:1.11
	netbsd-2-0-RC4:1.11
	netbsd-2-0-RC3:1.11
	netbsd-2-0-RC2:1.11
	netbsd-2-0-RC1:1.11
	netbsd-2-0:1.11.0.4
	netbsd-2-0-base:1.11
	netbsd-1-6-PATCH002-RELEASE:1.11
	netbsd-1-6-PATCH002:1.11
	netbsd-1-6-PATCH002-RC4:1.11
	netbsd-1-6-PATCH002-RC3:1.11
	netbsd-1-6-PATCH002-RC2:1.11
	netbsd-1-6-PATCH002-RC1:1.11
	netbsd-1-6-PATCH001:1.11
	netbsd-1-6-PATCH001-RELEASE:1.11
	netbsd-1-6-PATCH001-RC3:1.11
	netbsd-1-6-PATCH001-RC2:1.11
	netbsd-1-6-PATCH001-RC1:1.11
	fvdl_fs64_base:1.11
	netbsd-1-6-RELEASE:1.11
	netbsd-1-6-RC3:1.11
	netbsd-1-6-RC2:1.11
	netbsd-1-6-RC1:1.11
	netbsd-1-6:1.11.0.2
	netbsd-1-6-base:1.11
	netbsd-1-5-PATCH003:1.9
	netbsd-1-5-PATCH002:1.9
	netbsd-1-5-PATCH001:1.9
	nvi_1_79:1.1.1.6
	netbsd-1-5-RELEASE:1.9
	netbsd-1-5-BETA2:1.9
	netbsd-1-5-BETA:1.9
	netbsd-1-4-PATCH003:1.8
	netbsd-1-5-ALPHA2:1.9
	netbsd-1-5:1.9.0.4
	netbsd-1-5-base:1.9
	minoura-xpg4dl-base:1.9
	minoura-xpg4dl:1.9.0.2
	netbsd-1-4-PATCH002:1.8
	wrstuden-devbsize-19991221:1.8
	wrstuden-devbsize:1.8.0.6
	wrstuden-devbsize-base:1.8
	comdex-fall-1999:1.8.0.4
	comdex-fall-1999-base:1.8
	netbsd-1-4-PATCH001:1.8
	netbsd-1-4-RELEASE:1.8
	netbsd-1-4:1.8.0.2
	netbsd-1-4-base:1.8
	netbsd-1-3-PATCH003:1.6
	netbsd-1-3-PATCH003-CANDIDATE2:1.6
	netbsd-1-3-PATCH003-CANDIDATE1:1.6
	netbsd-1-3-PATCH003-CANDIDATE0:1.6
	netbsd-1-3-PATCH002:1.6
	netbsd-1-3-PATCH001:1.6
	netbsd-1-3-RELEASE:1.6
	netbsd-1-3-BETA:1.6
	netbsd-1-3:1.6.0.2
	netbsd-1-3-base:1.6
	netbsd-1-2-PATCH001:1.6
	netbsd-1-2-RELEASE:1.6
	netbsd-1-2-BETA:1.6
	netbsd-1-2:1.6.0.4
	netbsd-1-2-base:1.6
	nvi_1_66:1.1.1.5
	BOSTIC:1.1.1
	netbsd-1-1-PATCH001:1.5
	netbsd-1-1-RELEASE:1.5
	netbsd-1-1:1.5.0.4
	netbsd-1-1-base:1.5
	netbsd-1-0-PATCH06:1.5
	netbsd-1-0-PATCH05:1.5
	netbsd-1-0-PATCH04:1.5
	netbsd-1-0-PATCH03:1.5
	netbsd-1-0-PATCH02:1.5
	netbsd-1-0-PATCH1:1.5
	netbsd-1-0-PATCH0:1.5
	netbsd-1-0-RELEASE:1.5
	netbsd-1-0:1.5.0.2
	nvi-1-34b:1.1.1.4
	nvi-1-33b:1.1.1.3
	netbsd-1-0-base:1.3
	nvi-1-11b:1.1.1.2
	nvi-1-03:1.1.1.1
	bostic-nvi:1.1.1;
locks; strict;
comment	@ * @;


1.13
date	2008.10.29.16.49.37;	author christos;	state dead;
branches;
next	1.12;

1.12
date	2005.02.12.12.53.22;	author aymeric;	state Exp;
branches;
next	1.11;

1.11
date	2002.04.09.01.47.33;	author thorpej;	state Exp;
branches
	1.11.4.1
	1.11.6.1;
next	1.10;

1.10
date	2001.03.31.11.37.50;	author aymeric;	state Exp;
branches;
next	1.9;

1.9
date	2000.03.13.23.22.52;	author soren;	state Exp;
branches;
next	1.8;

1.8
date	98.07.26.23.14.40;	author mycroft;	state Exp;
branches;
next	1.7;

1.7
date	98.01.09.08.07.43;	author perry;	state Exp;
branches;
next	1.6;

1.6
date	96.05.20.03.47.28;	author mrg;	state Exp;
branches;
next	1.5;

1.5
date	94.08.17.20.12.16;	author cgd;	state Exp;
branches
	1.5.2.1;
next	1.4;

1.4
date	94.08.17.16.35.47;	author cgd;	state Exp;
branches;
next	1.3;

1.3
date	94.03.28.04.29.10;	author cgd;	state Exp;
branches;
next	1.2;

1.2
date	94.01.24.06.40.11;	author cgd;	state Exp;
branches;
next	1.1;

1.1
date	94.01.24.05.53.02;	author cgd;	state Exp;
branches
	1.1.1.1;
next	;

1.11.4.1
date	2005.02.12.12.24.13;	author aymeric;	state Exp;
branches;
next	1.11.4.2;

1.11.4.2
date	2005.02.12.12.46.26;	author aymeric;	state Exp;
branches;
next	;

1.11.6.1
date	2005.05.06.14.42.07;	author riz;	state Exp;
branches;
next	;

1.5.2.1
date	94.08.17.20.12.16;	author cgd;	state dead;
branches;
next	1.5.2.2;

1.5.2.2
date	94.08.17.20.12.17;	author cgd;	state Exp;
branches;
next	;

1.1.1.1
date	94.01.24.05.53.03;	author cgd;	state Exp;
branches;
next	1.1.1.2;

1.1.1.2
date	94.03.28.02.53.27;	author cgd;	state Exp;
branches;
next	1.1.1.3;

1.1.1.3
date	94.08.17.16.16.23;	author cgd;	state Exp;
branches;
next	1.1.1.4;

1.1.1.4
date	94.08.17.19.17.18;	author cgd;	state Exp;
branches;
next	1.1.1.5;

1.1.1.5
date	96.05.20.01.54.16;	author mrg;	state Exp;
branches;
next	1.1.1.6;

1.1.1.6
date	2001.03.31.11.29.46;	author aymeric;	state Exp;
branches;
next	;


desc
@@


1.13
log
@bye old vi!
@
text
@/*	$NetBSD: ex_cd.c,v 1.12 2005/02/12 12:53:22 aymeric Exp $	*/

/*-
 * Copyright (c) 1992, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 * Copyright (c) 1992, 1993, 1994, 1995, 1996
 *	Keith Bostic.  All rights reserved.
 *
 * See the LICENSE file for redistribution information.
 */

#include "config.h"

#include <sys/cdefs.h>
#ifndef lint
#if 0
static const char sccsid[] = "@@(#)ex_cd.c	10.10 (Berkeley) 8/12/96";
#else
__RCSID("$NetBSD: ex_cd.c,v 1.12 2005/02/12 12:53:22 aymeric Exp $");
#endif
#endif /* not lint */

#include <sys/param.h>
#include <sys/queue.h>

#include <bitstring.h>
#include <errno.h>
#include <limits.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "../common/common.h"

/*
 * ex_cd -- :cd[!] [directory]
 *	Change directories.
 *
 * PUBLIC: int ex_cd __P((SCR *, EXCMD *));
 */
int
ex_cd(sp, cmdp)
	SCR *sp;
	EXCMD *cmdp;
{
	struct passwd *pw;
	ARGS *ap;
	CHAR_T savech;
	const char *dir, *t;	/* XXX: END OF THE STACK, DON'T TRUST GETCWD. */
	char *p, buf[MAXPATHLEN * 2];

	/*
	 * !!!
	 * 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.
	 */
	if (F_ISSET(sp->ep, F_MODIFIED) &&
	    !FL_ISSET(cmdp->iflags, E_C_FORCE) && sp->frp->name[0] != '/') {
		msgq(sp, M_ERR,
    "120|File modified since last complete write; write or use ! to override");
		return (1);
	}

	switch (cmdp->argc) {
	case 0:
		/* If no argument, change to the user's home directory. */
		if ((dir = getenv("HOME")) == NULL) {
			if ((pw = getpwuid(getuid())) == NULL ||
			    pw->pw_dir == NULL || pw->pw_dir[0] == '\0') {
				msgq(sp, M_ERR,
			   "121|Unable to find home directory location");
				return (1);
			}
			dir = pw->pw_dir;
		}
		break;
	case 1:
		dir = cmdp->argv[0]->bp;
		break;
	default:
		abort();
	}

	/*
	 * 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.
	 */
	if (!chdir(dir))
		return (0);

	/*
	 * If moving to the user's home directory, or, the path begins with
	 * "/", "./" or "../", it's the only place we try.
	 */
	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;

	/* Try the O_CDPATH option values. */
	for (t = p = O_STR(sp, O_CDPATH);; ++p)
		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,
			 * 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.
			 */
			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);
				}
			}
			t = p + 1;
			if (*p == '\0')
				break;
		}

err:	msgq_str(sp, M_SYSERR, dir, "%s");
	return (1);
}
@


1.12
log
@Fix the RCSID's to be $NetBSD$ instead of $NetBSD
@
text
@d1 1
a1 1
/*	$NetBSD: ex_cd.c,v 1.11 2002/04/09 01:47:33 thorpej Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD$");
@


1.11
log
@Use __RCSID() and __COPYRIGHT().
@
text
@d1 1
a1 1
/*	$NetBSD: ex_cd.c,v 1.10 2001/03/31 11:37:50 aymeric Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD");
@


1.11.6.1
log
@Pull up revision 1.12 (requested by aymeric in ticket #1195):
Fix the RCSID's to be $NetBSD$ instead of $NetBSD
@
text
@d1 1
a1 1
/*	$NetBSD: ex_cd.c,v 1.11 2002/04/09 01:47:33 thorpej Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD$");
@


1.11.4.1
log
@Fix the RCSID's to be $NetBSD$ instead of $NetBSD
@
text
@d1 1
a1 1
/*	$NetBSD: ex_cd.c,v 1.11 2002/04/09 01:47:33 thorpej Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD$");
@


1.11.4.2
log
@Backout previous. Sorry.
@
text
@d19 1
a19 1
__RCSID("$NetBSD");
@


1.10
log
@merge changes after import of nvi 1.79
@
text
@d1 1
a1 1
/*	$NetBSD: ex_cd.c,v 1.9 2000/03/13 23:22:52 soren Exp $	*/
d14 1
d16 1
d18 3
@


1.9
log
@Fix doubled 'the's.
@
text
@d1 1
a1 1
/*	$NetBSD: ex_cd.c,v 1.8 1998/07/26 23:14:40 mycroft Exp $	*/
d15 1
a15 1
static const char sccsid[] = "@@(#)ex_cd.c	10.9 (Berkeley) 3/30/96";
d45 3
a47 3
	CDPATH *cdp;
	const char *dir;	/* XXX END OF THE STACK, DON'T TRUST GETCWD. */
	char buf[MAXPATHLEN * 2];
d83 3
a85 3
	 * Try the current directory first.  If this succeeds, don't
	 * display a message, vi didn't historically, and it's real
	 * obvious to the user where they are.
d101 2
a102 47
	/* If the user has a CDPATH variable, try its elements. */
	for (cdp = EXP(sp)->cdq.tqh_first; cdp != NULL; cdp = cdp->q.tqe_next) {
		(void)snprintf(buf, sizeof(buf), "%s/%s", cdp->path, dir);
		if (!chdir(buf)) {
			if (getcwd(buf, sizeof(buf)) != NULL)
				msgq_str(sp, M_INFO, buf,
				    "122|New current directory: %s");
			return (0);
		}
	}
err:	msgq_str(sp, M_SYSERR, dir, "%s");
	return (1);
}

#define	FREE_CDPATH(cdp) {						\
	TAILQ_REMOVE(&exp->cdq, (cdp), q);				\
	free((cdp)->path);						\
	free(cdp);							\
}

/*
 * ex_cdalloc --
 *	Create a new list of cd paths.
 *
 * PUBLIC: int ex_cdalloc __P((SCR *, char *));
 */
int
ex_cdalloc(sp, str)
	SCR *sp;
	char *str;
{
	EX_PRIVATE *exp;
	CDPATH *cdp;
	size_t len;
	int founddot;
	char *p, *t;

	/* Free current queue. */
	exp = EXP(sp);
	while ((cdp = exp->cdq.tqh_first) != NULL)
		FREE_CDPATH(cdp);

	/*
	 * Create new queue.  The CDPATH environmental variable (and the
	 * user's manual entry) are delimited by colon characters.
	 */
	for (p = t = str, founddot = 0;; ++p) {
d108 3
a110 5
			 * if the length is zero, then it's either ":XXX",
			 * "XXX::XXXX" , "XXX:", or "", and the only failure
			 * mode is the last one.  Note, the string ":" gives
			 * us two entries of '.', so we only include one of
			 * them.
d112 10
a121 7
			if ((len = p - t) == 0) {
				if (p == str && *p == '\0')
					break;
				if (founddot) {
					if (*p == '\0')
						break;
					continue;
a122 9
				len = 1;
				t = ".";
				founddot = 1;
			}
			MALLOC_RET(sp, cdp, CDPATH *, sizeof(CDPATH));
			MALLOC(sp, cdp->path, char *, len + 1);
			if (cdp->path == NULL) {
				free(cdp);
				return (1);
a123 3
			memmove(cdp->path, t, len);
			cdp->path[len] = '\0';
			TAILQ_INSERT_TAIL(&exp->cdq, cdp, q);
d125 2
a127 18
		if (*p == '\0')
			 break;
	}
	return (0);
}
						/* Free previous queue. */
/*
 * ex_cdfree --
 *	Free the cd path list.
 *
 * PUBLIC: int ex_cdfree __P((SCR *));
 */
int
ex_cdfree(sp)
	SCR *sp;
{
	EX_PRIVATE *exp;
	CDPATH *cdp;
d129 2
a130 5
	/* Free up cd path information. */
	exp = EXP(sp);
	while ((cdp = exp->cdq.tqh_first) != NULL)
		FREE_CDPATH(cdp);
	return (0);
@


1.8
log
@const poisoning.
@
text
@d1 1
a1 1
/*	$NetBSD: ex_cd.c,v 1.7 1998/01/09 08:07:43 perry Exp $	*/
d153 1
a153 1
			 * if the the length is zero, then it's either ":XXX",
@


1.7
log
@RCS Id Police.
@
text
@d1 1
a1 1
/*	$NetBSD$	*/
d46 1
a46 1
	char *dir;		/* XXX END OF THE STACK, DON'T TRUST GETCWD. */
@


1.6
log
@merge in nvi 1.66
@
text
@d1 2
@


1.5
log
@clean up import.
@
text
@d4 2
d7 1
a7 27
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
d10 2
d13 1
a13 1
static char sccsid[] = "@@(#)ex_cd.c	8.18 (Berkeley) 8/17/94";
a17 1
#include <sys/time.h>
a22 1
#include <signal.h>
a25 1
#include <termios.h>
d28 1
a28 6
#include "compat.h"
#include <db.h>
#include <regex.h>

#include "vi.h"
#include "excmd.h"
d33 2
d37 1
a37 1
ex_cd(sp, ep, cmdp)
d39 1
a39 2
	EXF *ep;
	EXCMDARG *cmdp;
d53 2
a54 2
	if (F_ISSET(ep, F_MODIFIED) &&
	    !F_ISSET(cmdp, E_FORCE) && sp->frp->name[0] != '/') {
d56 1
a56 1
    "File modified since last complete write; write or use ! to override");
d67 1
a67 1
			   "Unable to find home directory location");
d80 5
a84 1
	/* Try the current directory first. */
d86 1
a86 1
		goto ret;
d103 3
a105 2
ret:			if (getcwd(buf, sizeof(buf)) != NULL)
				msgq(sp, M_INFO, "New directory: %s", buf);
d109 1
a109 1
err:	msgq(sp, M_SYSERR, "%s", dir);
d116 1
a116 1
	FREE((cdp), sizeof(CDPATH));					\
d118 1
d122 2
d189 2
@


1.5.2.1
log
@file ex_cd.c was added on branch netbsd-1-0 on 1994-08-17 20:12:17 +0000
@
text
@d1 223
@


1.5.2.2
log
@clean up import.
@
text
@a0 223
/*-
 * Copyright (c) 1992, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
static char sccsid[] = "@@(#)ex_cd.c	8.18 (Berkeley) 8/17/94";
#endif /* not lint */

#include <sys/param.h>
#include <sys/queue.h>
#include <sys/time.h>

#include <bitstring.h>
#include <errno.h>
#include <limits.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#include "compat.h"
#include <db.h>
#include <regex.h>

#include "vi.h"
#include "excmd.h"

/*
 * ex_cd -- :cd[!] [directory]
 *	Change directories.
 */
int
ex_cd(sp, ep, cmdp)
	SCR *sp;
	EXF *ep;
	EXCMDARG *cmdp;
{
	struct passwd *pw;
	ARGS *ap;
	CDPATH *cdp;
	char *dir;		/* XXX END OF THE STACK, DON'T TRUST GETCWD. */
	char buf[MAXPATHLEN * 2];

	/*
	 * !!!
	 * 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.
	 */
	if (F_ISSET(ep, F_MODIFIED) &&
	    !F_ISSET(cmdp, E_FORCE) && sp->frp->name[0] != '/') {
		msgq(sp, M_ERR,
    "File modified since last complete write; write or use ! to override");
		return (1);
	}

	switch (cmdp->argc) {
	case 0:
		/* If no argument, change to the user's home directory. */
		if ((dir = getenv("HOME")) == NULL) {
			if ((pw = getpwuid(getuid())) == NULL ||
			    pw->pw_dir == NULL || pw->pw_dir[0] == '\0') {
				msgq(sp, M_ERR,
			   "Unable to find home directory location");
				return (1);
			}
			dir = pw->pw_dir;
		}
		break;
	case 1:
		dir = cmdp->argv[0]->bp;
		break;
	default:
		abort();
	}

	/* Try the current directory first. */
	if (!chdir(dir))
		goto ret;

	/*
	 * If moving to the user's home directory, or, the path begins with
	 * "/", "./" or "../", it's the only place we try.
	 */
	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;

	/* If the user has a CDPATH variable, try its elements. */
	for (cdp = EXP(sp)->cdq.tqh_first; cdp != NULL; cdp = cdp->q.tqe_next) {
		(void)snprintf(buf, sizeof(buf), "%s/%s", cdp->path, dir);
		if (!chdir(buf)) {
ret:			if (getcwd(buf, sizeof(buf)) != NULL)
				msgq(sp, M_INFO, "New directory: %s", buf);
			return (0);
		}
	}
err:	msgq(sp, M_SYSERR, "%s", dir);
	return (1);
}

#define	FREE_CDPATH(cdp) {						\
	TAILQ_REMOVE(&exp->cdq, (cdp), q);				\
	free((cdp)->path);						\
	FREE((cdp), sizeof(CDPATH));					\
}
/*
 * ex_cdalloc --
 *	Create a new list of cd paths.
 */
int
ex_cdalloc(sp, str)
	SCR *sp;
	char *str;
{
	EX_PRIVATE *exp;
	CDPATH *cdp;
	size_t len;
	int founddot;
	char *p, *t;

	/* Free current queue. */
	exp = EXP(sp);
	while ((cdp = exp->cdq.tqh_first) != NULL)
		FREE_CDPATH(cdp);

	/*
	 * Create new queue.  The CDPATH environmental variable (and the
	 * user's manual entry) are delimited by colon characters.
	 */
	for (p = t = str, founddot = 0;; ++p) {
		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,
			 * if the the length is zero, then it's either ":XXX",
			 * "XXX::XXXX" , "XXX:", or "", and the only failure
			 * mode is the last one.  Note, the string ":" gives
			 * us two entries of '.', so we only include one of
			 * them.
			 */
			if ((len = p - t) == 0) {
				if (p == str && *p == '\0')
					break;
				if (founddot) {
					if (*p == '\0')
						break;
					continue;
				}
				len = 1;
				t = ".";
				founddot = 1;
			}
			MALLOC_RET(sp, cdp, CDPATH *, sizeof(CDPATH));
			MALLOC(sp, cdp->path, char *, len + 1);
			if (cdp->path == NULL) {
				free(cdp);
				return (1);
			}
			memmove(cdp->path, t, len);
			cdp->path[len] = '\0';
			TAILQ_INSERT_TAIL(&exp->cdq, cdp, q);
			t = p + 1;
		}
		if (*p == '\0')
			 break;
	}
	return (0);
}
						/* Free previous queue. */
/*
 * ex_cdfree --
 *	Free the cd path list.
 */
int
ex_cdfree(sp)
	SCR *sp;
{
	EX_PRIVATE *exp;
	CDPATH *cdp;

	/* Free up cd path information. */
	exp = EXP(sp);
	while ((cdp = exp->cdq.tqh_first) != NULL)
		FREE_CDPATH(cdp);
	return (0);
}
@


1.4
log
@clean up import.  still have to hack some things.
@
text
@d35 1
a35 1
static const char sccsid[] = "@@(#)ex_cd.c	8.17 (Berkeley) 8/17/94";
@


1.3
log
@nvi 1.11(beta) from bostic.  reconcile conflicts/kill rcsids.
@
text
@d35 1
a35 1
static char sccsid[] = "@@(#)ex_cd.c	8.10 (Berkeley) 3/19/94";
d45 1
d70 2
d73 15
a87 2
	EX_PRIVATE *exp;
	char *dir, buf[MAXPATHLEN];
d91 1
d93 7
a99 3
			msgq(sp, M_ERR,
			    "Environment variable HOME not set.");
			return (1);
d109 4
d114 2
a115 8
	 * If the user has a CDPATH variable, we use it, otherwise
	 * we use the current directory.
	 *
	 * !!!
	 * This violates historic practice.  Historic vi didn't consider
	 * CDPATH, and therefore always used the current directory. This
	 * is probably correct; if user's have set CDPATH to not include
	 * the current directory, they probably had a reason.
d117 14
a130 15
	exp = EXP(sp);
	if (dir[0] == '/') {
		if (chdir(dir) < 0)
			goto err;
	} else {
		for (cdp = exp->cdq.tqh_first;
		    cdp != NULL; cdp = cdp->q.tqe_next) {
			(void)snprintf(buf,
			    sizeof(buf), "%s/%s", cdp->path, dir);
			if (!chdir(buf))
				break;
		}
		if (cdp == NULL) {
err:			msgq(sp, M_SYSERR, dir);
			return (1);
d133 2
a134 3
	if (getcwd(buf, sizeof(buf)) != NULL)
		msgq(sp, M_INFO, "New directory: %s", buf);
	return (0);
@


1.2
log
@more Ids than you'll ever want.
@
text
@d2 1
a2 1
 * Copyright (c) 1992, 1993
d35 1
a35 2
/* from: static char sccsid[] = "@@(#)ex_cd.c	8.3 (Berkeley) 12/2/93"; */
static char *rcsid = "$Id$";
d39 2
d42 1
d44 3
d49 1
d52 4
d69 2
d88 26
a113 3
	if (chdir(dir) < 0) {
		msgq(sp, M_SYSERR, dir);
		return (1);
d117 88
@


1.1
log
@Initial revision
@
text
@d35 2
a36 1
static char sccsid[] = "@@(#)ex_cd.c	8.3 (Berkeley) 12/2/93";
@


1.1.1.1
log
@nvi 1.03, from ftp.cs.berkeley.edu, per keith bostic's permission.
@
text
@@


1.1.1.2
log
@nvi/nex 1.11beta from bostic.
@
text
@d2 1
a2 1
 * Copyright (c) 1992, 1993, 1994
d35 1
a35 1
static char sccsid[] = "@@(#)ex_cd.c	8.10 (Berkeley) 3/19/94";
a38 2
#include <sys/queue.h>
#include <sys/time.h>
a39 1
#include <bitstring.h>
a40 3
#include <limits.h>
#include <signal.h>
#include <stdio.h>
a42 1
#include <termios.h>
a44 4
#include "compat.h"
#include <db.h>
#include <regex.h>

a57 2
	CDPATH *cdp;
	EX_PRIVATE *exp;
d75 3
a77 26
	/*
	 * If the user has a CDPATH variable, we use it, otherwise
	 * we use the current directory.
	 *
	 * !!!
	 * This violates historic practice.  Historic vi didn't consider
	 * CDPATH, and therefore always used the current directory. This
	 * is probably correct; if user's have set CDPATH to not include
	 * the current directory, they probably had a reason.
	 */
	exp = EXP(sp);
	if (dir[0] == '/') {
		if (chdir(dir) < 0)
			goto err;
	} else {
		for (cdp = exp->cdq.tqh_first;
		    cdp != NULL; cdp = cdp->q.tqe_next) {
			(void)snprintf(buf,
			    sizeof(buf), "%s/%s", cdp->path, dir);
			if (!chdir(buf))
				break;
		}
		if (cdp == NULL) {
err:			msgq(sp, M_SYSERR, dir);
			return (1);
		}
a80 88
	return (0);
}

#define	FREE_CDPATH(cdp) {						\
	TAILQ_REMOVE(&exp->cdq, (cdp), q);				\
	free((cdp)->path);						\
	FREE((cdp), sizeof(CDPATH));					\
}
/*
 * ex_cdalloc --
 *	Create a new list of cd paths.
 */
int
ex_cdalloc(sp, str)
	SCR *sp;
	char *str;
{
	EX_PRIVATE *exp;
	CDPATH *cdp;
	size_t len;
	int founddot;
	char *p, *t;

	/* Free current queue. */
	exp = EXP(sp);
	while ((cdp = exp->cdq.tqh_first) != NULL)
		FREE_CDPATH(cdp);

	/*
	 * Create new queue.  The CDPATH environmental variable (and the
	 * user's manual entry) are delimited by colon characters.
	 */
	for (p = t = str, founddot = 0;; ++p) {
		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,
			 * if the the length is zero, then it's either ":XXX",
			 * "XXX::XXXX" , "XXX:", or "", and the only failure
			 * mode is the last one.  Note, the string ":" gives
			 * us two entries of '.', so we only include one of
			 * them.
			 */
			if ((len = p - t) == 0) {
				if (p == str && *p == '\0')
					break;
				if (founddot) {
					if (*p == '\0')
						break;
					continue;
				}
				len = 1;
				t = ".";
				founddot = 1;
			}
			MALLOC_RET(sp, cdp, CDPATH *, sizeof(CDPATH));
			MALLOC(sp, cdp->path, char *, len + 1);
			if (cdp->path == NULL) {
				free(cdp);
				return (1);
			}
			memmove(cdp->path, t, len);
			cdp->path[len] = '\0';
			TAILQ_INSERT_TAIL(&exp->cdq, cdp, q);
			t = p + 1;
		}
		if (*p == '\0')
			 break;
	}
	return (0);
}
						/* Free previous queue. */
/*
 * ex_cdfree --
 *	Free the cd path list.
 */
int
ex_cdfree(sp)
	SCR *sp;
{
	EX_PRIVATE *exp;
	CDPATH *cdp;

	/* Free up cd path information. */
	exp = EXP(sp);
	while ((cdp = exp->cdq.tqh_first) != NULL)
		FREE_CDPATH(cdp);
@


1.1.1.3
log
@new public version of nvi
@
text
@d35 1
a35 1
static const char sccsid[] = "@@(#)ex_cd.c	8.17 (Berkeley) 8/17/94";
a44 1
#include <pwd.h>
a68 2
	struct passwd *pw;
	ARGS *ap;
d70 2
a71 15
	char *dir;		/* XXX END OF THE STACK, DON'T TRUST GETCWD. */
	char buf[MAXPATHLEN * 2];

	/*
	 * !!!
	 * 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.
	 */
	if (F_ISSET(ep, F_MODIFIED) &&
	    !F_ISSET(cmdp, E_FORCE) && sp->frp->name[0] != '/') {
		msgq(sp, M_ERR,
    "File modified since last complete write; write or use ! to override");
		return (1);
	}
a74 1
		/* If no argument, change to the user's home directory. */
d76 3
a78 7
			if ((pw = getpwuid(getuid())) == NULL ||
			    pw->pw_dir == NULL || pw->pw_dir[0] == '\0') {
				msgq(sp, M_ERR,
			   "Unable to find home directory location");
				return (1);
			}
			dir = pw->pw_dir;
a87 4
	/* Try the current directory first. */
	if (!chdir(dir))
		goto ret;

d89 8
a96 2
	 * If moving to the user's home directory, or, the path begins with
	 * "/", "./" or "../", it's the only place we try.
d98 15
a112 14
	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;

	/* If the user has a CDPATH variable, try its elements. */
	for (cdp = EXP(sp)->cdq.tqh_first; cdp != NULL; cdp = cdp->q.tqe_next) {
		(void)snprintf(buf, sizeof(buf), "%s/%s", cdp->path, dir);
		if (!chdir(buf)) {
ret:			if (getcwd(buf, sizeof(buf)) != NULL)
				msgq(sp, M_INFO, "New directory: %s", buf);
			return (0);
d115 3
a117 2
err:	msgq(sp, M_SYSERR, "%s", dir);
	return (1);
@


1.1.1.4
log
@new public version of nvi
@
text
@d35 1
a35 1
static char sccsid[] = "@@(#)ex_cd.c	8.18 (Berkeley) 8/17/94";
@


1.1.1.5
log
@import of nvi 1.66
@
text
@a3 2
 * Copyright (c) 1992, 1993, 1994, 1995, 1996
 *	Keith Bostic.  All rights reserved.
d5 27
a31 1
 * See the LICENSE file for redistribution information.
a33 2
#include "config.h"

d35 1
a35 1
static const char sccsid[] = "@@(#)ex_cd.c	10.9 (Berkeley) 3/30/96";
d40 1
d46 1
d50 1
d53 6
a58 1
#include "../common/common.h"
a62 2
 *
 * PUBLIC: int ex_cd __P((SCR *, EXCMD *));
d65 1
a65 1
ex_cd(sp, cmdp)
d67 2
a68 1
	EXCMD *cmdp;
d82 2
a83 2
	if (F_ISSET(sp->ep, F_MODIFIED) &&
	    !FL_ISSET(cmdp->iflags, E_C_FORCE) && sp->frp->name[0] != '/') {
d85 1
a85 1
    "120|File modified since last complete write; write or use ! to override");
d96 1
a96 1
			   "121|Unable to find home directory location");
d109 1
a109 5
	/*
	 * Try the current directory first.  If this succeeds, don't
	 * display a message, vi didn't historically, and it's real
	 * obvious to the user where they are.
	 */
d111 1
a111 1
		return (0);
d128 2
a129 3
			if (getcwd(buf, sizeof(buf)) != NULL)
				msgq_str(sp, M_INFO, buf,
				    "122|New current directory: %s");
d133 1
a133 1
err:	msgq_str(sp, M_SYSERR, dir, "%s");
d140 1
a140 1
	free(cdp);							\
a141 1

a144 2
 *
 * PUBLIC: int ex_cdalloc __P((SCR *, char *));
a209 2
 *
 * PUBLIC: int ex_cdfree __P((SCR *));
@


1.1.1.6
log
@import of nvi 1.79
@
text
@d13 1
a13 1
static const char sccsid[] = "@@(#)ex_cd.c	10.10 (Berkeley) 8/12/96";
d43 2
a44 2
	CHAR_T savech;
	char *dir, *p, *t;	/* XXX: END OF THE STACK, DON'T TRUST GETCWD. */
d81 3
a83 3
	 * 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.
d99 47
a145 2
	/* Try the O_CDPATH option values. */
	for (p = t = O_STR(sp, O_CDPATH);; ++p)
d151 5
a155 3
			 * 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.
d157 7
a163 10
			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);
d165 9
d175 3
a178 2
			if (*p == '\0')
				break;
d180 18
d199 5
a203 2
err:	msgq_str(sp, M_SYSERR, dir, "%s");
	return (1);
@

