Fix UTIME_OMIT handling
[dragonfly.git] / contrib / nvi2 / ex / ex_cd.c
blobd97012a94473bff7c39685b79781d020e1998adf
1 /*-
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #include <sys/queue.h>
13 #include <sys/time.h>
15 #include <bitstring.h>
16 #include <errno.h>
17 #include <limits.h>
18 #include <pwd.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
24 #include "../common/common.h"
27 * ex_cd -- :cd[!] [directory]
28 * Change directories.
30 * PUBLIC: int ex_cd(SCR *, EXCMD *);
32 int
33 ex_cd(SCR *sp, EXCMD *cmdp)
35 struct passwd *pw;
36 ARGS *ap;
37 int savech;
38 char *dir, *p, *t;
39 char *buf;
40 size_t dlen;
43 * !!!
44 * Historic practice is that the cd isn't attempted if the file has
45 * been modified, unless its name begins with a leading '/' or the
46 * force flag is set.
48 if (F_ISSET(sp->ep, F_MODIFIED) &&
49 !FL_ISSET(cmdp->iflags, E_C_FORCE) && sp->frp->name[0] != '/') {
50 msgq(sp, M_ERR,
51 "120|File modified since last complete write; write or use ! to override");
52 return (1);
55 switch (cmdp->argc) {
56 case 0:
57 /* If no argument, change to the user's home directory. */
58 if ((dir = getenv("HOME")) == NULL) {
59 if ((pw = getpwuid(getuid())) == NULL ||
60 pw->pw_dir == NULL || pw->pw_dir[0] == '\0') {
61 msgq(sp, M_ERR,
62 "121|Unable to find home directory location");
63 return (1);
65 dir = pw->pw_dir;
67 break;
68 case 1:
69 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1,
70 dir, dlen);
71 break;
72 default:
73 abort();
77 * Try the current directory first. If this succeeds, don't display
78 * a message, vi didn't historically, and it should be obvious to the
79 * user where they are.
81 if (!chdir(dir))
82 return (0);
85 * If moving to the user's home directory, or, the path begins with
86 * "/", "./" or "../", it's the only place we try.
88 if (cmdp->argc == 0 ||
89 (ap = cmdp->argv[0])->bp[0] == '/' ||
90 (ap->len == 1 && ap->bp[0] == '.') ||
91 (ap->len >= 2 && ap->bp[0] == '.' && ap->bp[1] == '.' &&
92 (ap->bp[2] == '/' || ap->bp[2] == '\0')))
93 goto err;
95 /* Try the O_CDPATH option values. */
96 for (p = t = O_STR(sp, O_CDPATH);; ++p)
97 if (*p == '\0' || *p == ':') {
99 * Ignore the empty strings and ".", since we've already
100 * tried the current directory.
102 if (t < p && (p - t != 1 || *t != '.')) {
103 savech = *p;
104 *p = '\0';
105 if ((buf = join(t, dir)) == NULL) {
106 msgq(sp, M_SYSERR, NULL);
107 return (1);
109 *p = savech;
110 if (!chdir(buf)) {
111 free(buf);
112 if ((buf = getcwd(NULL, 0)) != NULL) {
113 msgq_str(sp, M_INFO, buf, "122|New current directory: %s");
114 free(buf);
116 return (0);
118 free(buf);
120 t = p + 1;
121 if (*p == '\0')
122 break;
125 err: msgq_str(sp, M_SYSERR, dir, "%s");
126 return (1);