1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
/* vi:set ts=2 sts=2 sw=2:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* fs.c -- filesystem access
*/
#include <uv.h>
#include "os.h"
#include "../message.h"
#include "../misc2.h"
int mch_chdir(char *path) {
if (p_verbose >= 5) {
verbose_enter();
smsg((char_u *)"chdir(%s)", path);
verbose_leave();
}
return uv_chdir(path);
}
/*
* Get name of current directory into buffer 'buf' of length 'len' bytes.
* Return OK for success, FAIL for failure.
*/
int mch_dirname(char_u *buf, int len)
{
int errno;
if ((errno = uv_cwd((char *) buf, len)) != 0) {
STRCPY(buf, uv_strerror(errno));
return FAIL;
}
return OK;
}
/*
* Get the absolute name of the given relative directory.
*
* parameter directory: Directory name, relative to current directory.
* return FAIL for failure, OK for success
*/
int mch_full_dir_name(char *directory, char *buffer, int len)
{
int retval = OK;
if(0 == STRLEN(directory)) {
return mch_dirname((char_u *) buffer, len);
}
char old_dir[MAXPATHL];
/* Get current directory name. */
if (FAIL == mch_dirname((char_u *) old_dir, MAXPATHL)) {
return FAIL;
}
/* We have to get back to the current dir at the end, check if that works. */
if (0 != mch_chdir(old_dir)) {
return FAIL;
}
if (0 != mch_chdir(directory)) {
retval = FAIL;
}
if ((FAIL == retval) || (FAIL == mch_dirname((char_u *) buffer, len))) {
retval = FAIL;
}
if (0 != mch_chdir(old_dir)) {
/* That shouldn't happen, since we've tested if it works. */
retval = FAIL;
EMSG(_(e_prev_dir));
}
return retval;
}
/*
* Append to_append to path with a slash in between.
*/
int append_path(char *path, char *to_append, int max_len)
{
int current_length = STRLEN(path);
int to_append_length = STRLEN(to_append);
/* Do not append empty strings. */
if (0 == to_append_length)
return OK;
/* Do not append a dot. */
if (STRCMP(to_append, ".") == 0)
return OK;
/* Glue both paths with a slash. */
if (current_length > 0 && path[current_length-1] != '/') {
current_length += 1; /* Count the trailing slash. */
if (current_length > max_len)
return FAIL;
STRCAT(path, "/");
}
/* +1 for the NUL at the end. */
if (current_length + to_append_length +1 > max_len) {
return FAIL;
}
STRCAT(path, to_append);
return OK;
}
/*
* Get absolute file name into "buf[len]".
*
* parameter force: Also expand when the given path in fname is already
* absolute.
*
* return FAIL for failure, OK for success
*/
int mch_full_name(char_u *fname, char_u *buf, int len, int force)
{
char_u *p;
*buf = NUL;
char relative_directory[len];
char *end_of_path = (char *) fname;
/* expand it if forced or not an absolute path */
if (force || !mch_is_full_name(fname)) {
if ((p = vim_strrchr(fname, '/')) != NULL) {
STRNCPY(relative_directory, fname, p-fname);
relative_directory[p-fname] = NUL;
end_of_path = (char *) (p + 1);
} else {
relative_directory[0] = NUL;
end_of_path = (char *) fname;
}
if (FAIL == mch_full_dir_name(relative_directory, (char *) buf, len)) {
return FAIL;
}
}
return append_path((char *) buf, (char *) end_of_path, len);
}
/*
* Return TRUE if "fname" does not depend on the current directory.
*/
int mch_is_full_name(char_u *fname)
{
return *fname == '/' || *fname == '~';
}
|