tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / nvi / dist / ex / ex_cd.c
blobf3e648f9d6ff9903dadc36f9804d9bfd196f8c2a
1 /* $NetBSD: ex_cd.c,v 1.2 2013/11/22 15:52:05 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.
8 * See the LICENSE file for redistribution information.
9 */
11 #include "config.h"
13 #ifndef lint
14 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 ";
15 #endif /* not lint */
17 #include <sys/param.h>
18 #include <sys/queue.h>
20 #include <bitstring.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <pwd.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "../common/common.h"
32 * ex_cd -- :cd[!] [directory]
33 * Change directories.
35 * PUBLIC: int ex_cd __P((SCR *, EXCMD *));
37 int
38 ex_cd(SCR *sp, EXCMD *cmdp)
40 struct passwd *pw;
41 ARGS *ap;
42 const char *p, *t; /* XXX: END OF THE STACK, DON'T TRUST GETCWD. */
43 const char *dir;
44 char buf[MAXPATHLEN * 2];
45 size_t dlen;
48 * !!!
49 * Historic practice is that the cd isn't attempted if the file has
50 * been modified, unless its name begins with a leading '/' or the
51 * force flag is set.
53 if (F_ISSET(sp->ep, F_MODIFIED) &&
54 !FL_ISSET(cmdp->iflags, E_C_FORCE) && sp->frp->name[0] != '/') {
55 msgq(sp, M_ERR,
56 "120|File modified since last complete write; write or use ! to override");
57 return (1);
60 switch (cmdp->argc) {
61 case 0:
62 /* If no argument, change to the user's home directory. */
63 if ((dir = getenv("HOME")) == NULL) {
64 if ((pw = getpwuid(getuid())) == NULL ||
65 pw->pw_dir == NULL || pw->pw_dir[0] == '\0') {
66 msgq(sp, M_ERR,
67 "121|Unable to find home directory location");
68 return (1);
70 dir = pw->pw_dir;
72 break;
73 case 1:
74 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1,
75 dir, dlen);
76 break;
77 default:
78 abort();
82 * Try the current directory first. If this succeeds, don't display
83 * a message, vi didn't historically, and it should be obvious to the
84 * user where they are.
86 if (!chdir(dir))
87 return (0);
90 * If moving to the user's home directory, or, the path begins with
91 * "/", "./" or "../", it's the only place we try.
93 if (cmdp->argc == 0 ||
94 (ap = cmdp->argv[0])->bp[0] == '/' ||
95 (ap->len == 1 && ap->bp[0] == '.') ||
96 (ap->len >= 2 && ap->bp[0] == '.' && ap->bp[1] == '.' &&
97 (ap->bp[2] == '/' || ap->bp[2] == '\0')))
98 goto err;
100 /* Try the O_CDPATH option values. */
101 for (p = t = O_STR(sp, O_CDPATH);; ++p)
102 if (*p == '\0' || *p == ':') {
104 * Empty strings specify ".". The only way to get an
105 * empty string is a leading colon, colons in a row,
106 * or a trailing colon. Or, to put it the other way,
107 * if the length is 1 or less, then we're dealing with
108 * ":XXX", "XXX::XXXX" , "XXX:", or "". Since we've
109 * already tried dot, we ignore tham all.
111 if (t < p - 1) {
112 (void)snprintf(buf,
113 sizeof(buf), "%.*s/%s", (int)(p - t),
114 t, dir);
115 if (!chdir(buf)) {
116 if (getcwd(buf, sizeof(buf)) != NULL)
117 msgq_str(sp, M_INFO, buf, "122|New current directory: %s");
118 return (0);
121 t = p + 1;
122 if (*p == '\0')
123 break;
126 err: msgq_str(sp, M_SYSERR, dir, "%s");
127 return (1);