blob: 8bc941d3542bf474716c3b67cde449f7e2e0af0e [file] [log] [blame]
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar9810cfb2019-12-11 21:23:00 +010011 * filepath.c: dealing with file names and paths.
Bram Moolenaarb005cd82019-09-04 15:54:55 +020012 */
13
14#include "vim.h"
15
16#ifdef MSWIN
17/*
18 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19 */
20
21/*
22 * Get the short path (8.3) for the filename in "fnamep".
23 * Only works for a valid file name.
24 * When the path gets longer "fnamep" is changed and the allocated buffer
25 * is put in "bufp".
26 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
27 * Returns OK on success, FAIL on failure.
28 */
29 static int
30get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
31{
32 int l, len;
Bram Moolenaar3f396972019-10-30 04:10:06 +010033 WCHAR *newbuf;
34 WCHAR *wfname;
Bram Moolenaarb005cd82019-09-04 15:54:55 +020035
Bram Moolenaar3f396972019-10-30 04:10:06 +010036 len = MAXPATHL;
37 newbuf = malloc(len * sizeof(*newbuf));
38 if (newbuf == NULL)
39 return FAIL;
40
41 wfname = enc_to_utf16(*fnamep, NULL);
42 if (wfname == NULL)
43 {
44 vim_free(newbuf);
45 return FAIL;
46 }
47
48 l = GetShortPathNameW(wfname, newbuf, len);
Bram Moolenaarb005cd82019-09-04 15:54:55 +020049 if (l > len - 1)
50 {
Bram Moolenaar26262f82019-09-04 20:59:15 +020051 // If that doesn't work (not enough space), then save the string
52 // and try again with a new buffer big enough.
Bram Moolenaar3f396972019-10-30 04:10:06 +010053 WCHAR *newbuf_t = newbuf;
54 newbuf = vim_realloc(newbuf, (l + 1) * sizeof(*newbuf));
Bram Moolenaarb005cd82019-09-04 15:54:55 +020055 if (newbuf == NULL)
Bram Moolenaar3f396972019-10-30 04:10:06 +010056 {
57 vim_free(wfname);
58 vim_free(newbuf_t);
Bram Moolenaarb005cd82019-09-04 15:54:55 +020059 return FAIL;
Bram Moolenaar3f396972019-10-30 04:10:06 +010060 }
Bram Moolenaar26262f82019-09-04 20:59:15 +020061 // Really should always succeed, as the buffer is big enough.
Bram Moolenaar3f396972019-10-30 04:10:06 +010062 l = GetShortPathNameW(wfname, newbuf, l+1);
Bram Moolenaarb005cd82019-09-04 15:54:55 +020063 }
Bram Moolenaar3f396972019-10-30 04:10:06 +010064 if (l != 0)
65 {
66 char_u *p = utf16_to_enc(newbuf, NULL);
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +020067
Bram Moolenaar3f396972019-10-30 04:10:06 +010068 if (p != NULL)
69 {
70 vim_free(*bufp);
71 *fnamep = *bufp = p;
72 }
73 else
74 {
75 vim_free(wfname);
76 vim_free(newbuf);
77 return FAIL;
78 }
79 }
80 vim_free(wfname);
81 vim_free(newbuf);
Bram Moolenaarb005cd82019-09-04 15:54:55 +020082
Bram Moolenaar2ade7142019-11-04 20:36:50 +010083 *fnamelen = l == 0 ? l : (int)STRLEN(*bufp);
Bram Moolenaarb005cd82019-09-04 15:54:55 +020084 return OK;
85}
86
87/*
88 * Get the short path (8.3) for the filename in "fname". The converted
89 * path is returned in "bufp".
90 *
91 * Some of the directories specified in "fname" may not exist. This function
92 * will shorten the existing directories at the beginning of the path and then
93 * append the remaining non-existing path.
94 *
95 * fname - Pointer to the filename to shorten. On return, contains the
96 * pointer to the shortened pathname
97 * bufp - Pointer to an allocated buffer for the filename.
98 * fnamelen - Length of the filename pointed to by fname
99 *
100 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
101 */
102 static int
103shortpath_for_invalid_fname(
104 char_u **fname,
105 char_u **bufp,
106 int *fnamelen)
107{
108 char_u *short_fname, *save_fname, *pbuf_unused;
109 char_u *endp, *save_endp;
110 char_u ch;
111 int old_len, len;
112 int new_len, sfx_len;
113 int retval = OK;
114
Bram Moolenaar26262f82019-09-04 20:59:15 +0200115 // Make a copy
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200116 old_len = *fnamelen;
117 save_fname = vim_strnsave(*fname, old_len);
118 pbuf_unused = NULL;
119 short_fname = NULL;
120
Bram Moolenaar26262f82019-09-04 20:59:15 +0200121 endp = save_fname + old_len - 1; // Find the end of the copy
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200122 save_endp = endp;
123
124 /*
125 * Try shortening the supplied path till it succeeds by removing one
126 * directory at a time from the tail of the path.
127 */
128 len = 0;
129 for (;;)
130 {
Bram Moolenaar26262f82019-09-04 20:59:15 +0200131 // go back one path-separator
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200132 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
133 --endp;
134 if (endp <= save_fname)
Bram Moolenaar26262f82019-09-04 20:59:15 +0200135 break; // processed the complete path
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200136
137 /*
138 * Replace the path separator with a NUL and try to shorten the
139 * resulting path.
140 */
141 ch = *endp;
142 *endp = 0;
143 short_fname = save_fname;
144 len = (int)STRLEN(short_fname) + 1;
145 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
146 {
147 retval = FAIL;
148 goto theend;
149 }
Bram Moolenaar26262f82019-09-04 20:59:15 +0200150 *endp = ch; // preserve the string
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200151
152 if (len > 0)
Bram Moolenaar26262f82019-09-04 20:59:15 +0200153 break; // successfully shortened the path
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200154
Bram Moolenaar26262f82019-09-04 20:59:15 +0200155 // failed to shorten the path. Skip the path separator
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200156 --endp;
157 }
158
159 if (len > 0)
160 {
161 /*
162 * Succeeded in shortening the path. Now concatenate the shortened
163 * path with the remaining path at the tail.
164 */
165
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100166 // Compute the length of the new path.
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200167 sfx_len = (int)(save_endp - endp) + 1;
168 new_len = len + sfx_len;
169
170 *fnamelen = new_len;
171 vim_free(*bufp);
172 if (new_len > old_len)
173 {
Bram Moolenaar26262f82019-09-04 20:59:15 +0200174 // There is not enough space in the currently allocated string,
175 // copy it to a buffer big enough.
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200176 *fname = *bufp = vim_strnsave(short_fname, new_len);
177 if (*fname == NULL)
178 {
179 retval = FAIL;
180 goto theend;
181 }
182 }
183 else
184 {
Bram Moolenaar26262f82019-09-04 20:59:15 +0200185 // Transfer short_fname to the main buffer (it's big enough),
186 // unless get_short_pathname() did its work in-place.
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200187 *fname = *bufp = save_fname;
188 if (short_fname != save_fname)
189 vim_strncpy(save_fname, short_fname, len);
190 save_fname = NULL;
191 }
192
Bram Moolenaar26262f82019-09-04 20:59:15 +0200193 // concat the not-shortened part of the path
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200194 vim_strncpy(*fname + len, endp, sfx_len);
195 (*fname)[new_len] = NUL;
196 }
197
198theend:
199 vim_free(pbuf_unused);
200 vim_free(save_fname);
201
202 return retval;
203}
204
205/*
206 * Get a pathname for a partial path.
207 * Returns OK for success, FAIL for failure.
208 */
209 static int
210shortpath_for_partial(
211 char_u **fnamep,
212 char_u **bufp,
213 int *fnamelen)
214{
215 int sepcount, len, tflen;
216 char_u *p;
217 char_u *pbuf, *tfname;
218 int hasTilde;
219
Bram Moolenaar26262f82019-09-04 20:59:15 +0200220 // Count up the path separators from the RHS.. so we know which part
221 // of the path to return.
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200222 sepcount = 0;
223 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
224 if (vim_ispathsep(*p))
225 ++sepcount;
226
Bram Moolenaar26262f82019-09-04 20:59:15 +0200227 // Need full path first (use expand_env() to remove a "~/")
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200228 hasTilde = (**fnamep == '~');
229 if (hasTilde)
230 pbuf = tfname = expand_env_save(*fnamep);
231 else
232 pbuf = tfname = FullName_save(*fnamep, FALSE);
233
234 len = tflen = (int)STRLEN(tfname);
235
236 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
237 return FAIL;
238
239 if (len == 0)
240 {
Bram Moolenaar26262f82019-09-04 20:59:15 +0200241 // Don't have a valid filename, so shorten the rest of the
242 // path if we can. This CAN give us invalid 8.3 filenames, but
243 // there's not a lot of point in guessing what it might be.
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200244 len = tflen;
245 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
246 return FAIL;
247 }
248
Bram Moolenaar26262f82019-09-04 20:59:15 +0200249 // Count the paths backward to find the beginning of the desired string.
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200250 for (p = tfname + len - 1; p >= tfname; --p)
251 {
252 if (has_mbyte)
253 p -= mb_head_off(tfname, p);
254 if (vim_ispathsep(*p))
255 {
256 if (sepcount == 0 || (hasTilde && sepcount == 1))
257 break;
258 else
259 sepcount --;
260 }
261 }
262 if (hasTilde)
263 {
264 --p;
265 if (p >= tfname)
266 *p = '~';
267 else
268 return FAIL;
269 }
270 else
271 ++p;
272
Bram Moolenaar26262f82019-09-04 20:59:15 +0200273 // Copy in the string - p indexes into tfname - allocated at pbuf
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200274 vim_free(*bufp);
275 *fnamelen = (int)STRLEN(p);
276 *bufp = pbuf;
277 *fnamep = p;
278
279 return OK;
280}
281#endif // MSWIN
282
283/*
284 * Adjust a filename, according to a string of modifiers.
285 * *fnamep must be NUL terminated when called. When returning, the length is
286 * determined by *fnamelen.
287 * Returns VALID_ flags or -1 for failure.
288 * When there is an error, *fnamep is set to NULL.
289 */
290 int
291modify_fname(
292 char_u *src, // string with modifiers
293 int tilde_file, // "~" is a file name, not $HOME
294 int *usedlen, // characters after src that are used
295 char_u **fnamep, // file name so far
296 char_u **bufp, // buffer for allocated file name or NULL
297 int *fnamelen) // length of fnamep
298{
299 int valid = 0;
300 char_u *tail;
301 char_u *s, *p, *pbuf;
302 char_u dirname[MAXPATHL];
303 int c;
304 int has_fullname = 0;
Bram Moolenaard816cd92020-02-04 22:23:09 +0100305 int has_homerelative = 0;
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200306#ifdef MSWIN
307 char_u *fname_start = *fnamep;
308 int has_shortname = 0;
309#endif
310
311repeat:
Bram Moolenaar26262f82019-09-04 20:59:15 +0200312 // ":p" - full path/file_name
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200313 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
314 {
315 has_fullname = 1;
316
317 valid |= VALID_PATH;
318 *usedlen += 2;
319
Bram Moolenaar26262f82019-09-04 20:59:15 +0200320 // Expand "~/path" for all systems and "~user/path" for Unix and VMS
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200321 if ((*fnamep)[0] == '~'
322#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
323 && ((*fnamep)[1] == '/'
324# ifdef BACKSLASH_IN_FILENAME
325 || (*fnamep)[1] == '\\'
326# endif
327 || (*fnamep)[1] == NUL)
328#endif
329 && !(tilde_file && (*fnamep)[1] == NUL)
330 )
331 {
332 *fnamep = expand_env_save(*fnamep);
Bram Moolenaar26262f82019-09-04 20:59:15 +0200333 vim_free(*bufp); // free any allocated file name
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200334 *bufp = *fnamep;
335 if (*fnamep == NULL)
336 return -1;
337 }
338
Bram Moolenaar26262f82019-09-04 20:59:15 +0200339 // When "/." or "/.." is used: force expansion to get rid of it.
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200340 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
341 {
342 if (vim_ispathsep(*p)
343 && p[1] == '.'
344 && (p[2] == NUL
345 || vim_ispathsep(p[2])
346 || (p[2] == '.'
347 && (p[3] == NUL || vim_ispathsep(p[3])))))
348 break;
349 }
350
Bram Moolenaar26262f82019-09-04 20:59:15 +0200351 // FullName_save() is slow, don't use it when not needed.
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200352 if (*p != NUL || !vim_isAbsName(*fnamep))
353 {
354 *fnamep = FullName_save(*fnamep, *p != NUL);
Bram Moolenaar26262f82019-09-04 20:59:15 +0200355 vim_free(*bufp); // free any allocated file name
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200356 *bufp = *fnamep;
357 if (*fnamep == NULL)
358 return -1;
359 }
360
361#ifdef MSWIN
362# if _WIN32_WINNT >= 0x0500
363 if (vim_strchr(*fnamep, '~') != NULL)
364 {
365 // Expand 8.3 filename to full path. Needed to make sure the same
366 // file does not have two different names.
367 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
368 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
369 WCHAR buf[_MAX_PATH];
370
371 if (wfname != NULL)
372 {
373 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
374 {
375 char_u *p = utf16_to_enc(buf, NULL);
376
377 if (p != NULL)
378 {
379 vim_free(*bufp); // free any allocated file name
380 *bufp = *fnamep = p;
381 }
382 }
383 vim_free(wfname);
384 }
385 }
386# endif
387#endif
Bram Moolenaar26262f82019-09-04 20:59:15 +0200388 // Append a path separator to a directory.
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200389 if (mch_isdir(*fnamep))
390 {
Bram Moolenaar26262f82019-09-04 20:59:15 +0200391 // Make room for one or two extra characters.
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200392 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
Bram Moolenaar26262f82019-09-04 20:59:15 +0200393 vim_free(*bufp); // free any allocated file name
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200394 *bufp = *fnamep;
395 if (*fnamep == NULL)
396 return -1;
397 add_pathsep(*fnamep);
398 }
399 }
400
Bram Moolenaar26262f82019-09-04 20:59:15 +0200401 // ":." - path relative to the current directory
402 // ":~" - path relative to the home directory
403 // ":8" - shortname path - postponed till after
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200404 while (src[*usedlen] == ':'
405 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
406 {
407 *usedlen += 2;
408 if (c == '8')
409 {
410#ifdef MSWIN
Bram Moolenaar26262f82019-09-04 20:59:15 +0200411 has_shortname = 1; // Postpone this.
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200412#endif
413 continue;
414 }
415 pbuf = NULL;
Bram Moolenaar26262f82019-09-04 20:59:15 +0200416 // Need full path first (use expand_env() to remove a "~/")
Bram Moolenaard816cd92020-02-04 22:23:09 +0100417 if (!has_fullname && !has_homerelative)
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200418 {
419 if (c == '.' && **fnamep == '~')
420 p = pbuf = expand_env_save(*fnamep);
421 else
422 p = pbuf = FullName_save(*fnamep, FALSE);
423 }
424 else
425 p = *fnamep;
426
427 has_fullname = 0;
428
429 if (p != NULL)
430 {
431 if (c == '.')
432 {
Bram Moolenaard816cd92020-02-04 22:23:09 +0100433 size_t namelen;
434
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200435 mch_dirname(dirname, MAXPATHL);
Bram Moolenaard816cd92020-02-04 22:23:09 +0100436 if (has_homerelative)
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200437 {
Bram Moolenaard816cd92020-02-04 22:23:09 +0100438 s = vim_strsave(dirname);
439 if (s != NULL)
440 {
441 home_replace(NULL, s, dirname, MAXPATHL, TRUE);
442 vim_free(s);
443 }
444 }
445 namelen = STRLEN(dirname);
446
447 // Do not call shorten_fname() here since it removes the prefix
448 // even though the path does not have a prefix.
449 if (fnamencmp(p, dirname, namelen) == 0)
450 {
451 p += namelen;
Bram Moolenaara78e9c62020-02-05 21:14:00 +0100452 if (vim_ispathsep(*p))
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200453 {
Bram Moolenaara78e9c62020-02-05 21:14:00 +0100454 while (*p && vim_ispathsep(*p))
455 ++p;
456 *fnamep = p;
457 if (pbuf != NULL)
458 {
459 // free any allocated file name
460 vim_free(*bufp);
461 *bufp = pbuf;
462 pbuf = NULL;
463 }
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200464 }
465 }
466 }
467 else
468 {
469 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
Bram Moolenaar26262f82019-09-04 20:59:15 +0200470 // Only replace it when it starts with '~'
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200471 if (*dirname == '~')
472 {
473 s = vim_strsave(dirname);
474 if (s != NULL)
475 {
476 *fnamep = s;
477 vim_free(*bufp);
478 *bufp = s;
Bram Moolenaard816cd92020-02-04 22:23:09 +0100479 has_homerelative = TRUE;
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200480 }
481 }
482 }
483 vim_free(pbuf);
484 }
485 }
486
487 tail = gettail(*fnamep);
488 *fnamelen = (int)STRLEN(*fnamep);
489
Bram Moolenaar26262f82019-09-04 20:59:15 +0200490 // ":h" - head, remove "/file_name", can be repeated
491 // Don't remove the first "/" or "c:\"
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200492 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
493 {
494 valid |= VALID_HEAD;
495 *usedlen += 2;
496 s = get_past_head(*fnamep);
497 while (tail > s && after_pathsep(s, tail))
498 MB_PTR_BACK(*fnamep, tail);
499 *fnamelen = (int)(tail - *fnamep);
500#ifdef VMS
501 if (*fnamelen > 0)
Bram Moolenaar26262f82019-09-04 20:59:15 +0200502 *fnamelen += 1; // the path separator is part of the path
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200503#endif
504 if (*fnamelen == 0)
505 {
Bram Moolenaar26262f82019-09-04 20:59:15 +0200506 // Result is empty. Turn it into "." to make ":cd %:h" work.
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200507 p = vim_strsave((char_u *)".");
508 if (p == NULL)
509 return -1;
510 vim_free(*bufp);
511 *bufp = *fnamep = tail = p;
512 *fnamelen = 1;
513 }
514 else
515 {
516 while (tail > s && !after_pathsep(s, tail))
517 MB_PTR_BACK(*fnamep, tail);
518 }
519 }
520
Bram Moolenaar26262f82019-09-04 20:59:15 +0200521 // ":8" - shortname
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200522 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
523 {
524 *usedlen += 2;
525#ifdef MSWIN
526 has_shortname = 1;
527#endif
528 }
529
530#ifdef MSWIN
531 /*
532 * Handle ":8" after we have done 'heads' and before we do 'tails'.
533 */
534 if (has_shortname)
535 {
Bram Moolenaar26262f82019-09-04 20:59:15 +0200536 // Copy the string if it is shortened by :h and when it wasn't copied
537 // yet, because we are going to change it in place. Avoids changing
538 // the buffer name for "%:8".
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200539 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
540 {
541 p = vim_strnsave(*fnamep, *fnamelen);
542 if (p == NULL)
543 return -1;
544 vim_free(*bufp);
545 *bufp = *fnamep = p;
546 }
547
Bram Moolenaar26262f82019-09-04 20:59:15 +0200548 // Split into two implementations - makes it easier. First is where
549 // there isn't a full name already, second is where there is.
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200550 if (!has_fullname && !vim_isAbsName(*fnamep))
551 {
552 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
553 return -1;
554 }
555 else
556 {
557 int l = *fnamelen;
558
Bram Moolenaar26262f82019-09-04 20:59:15 +0200559 // Simple case, already have the full-name.
560 // Nearly always shorter, so try first time.
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200561 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
562 return -1;
563
564 if (l == 0)
565 {
Bram Moolenaar26262f82019-09-04 20:59:15 +0200566 // Couldn't find the filename, search the paths.
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200567 l = *fnamelen;
568 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
569 return -1;
570 }
571 *fnamelen = l;
572 }
573 }
574#endif // MSWIN
575
Bram Moolenaar26262f82019-09-04 20:59:15 +0200576 // ":t" - tail, just the basename
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200577 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
578 {
579 *usedlen += 2;
580 *fnamelen -= (int)(tail - *fnamep);
581 *fnamep = tail;
582 }
583
Bram Moolenaar26262f82019-09-04 20:59:15 +0200584 // ":e" - extension, can be repeated
585 // ":r" - root, without extension, can be repeated
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200586 while (src[*usedlen] == ':'
587 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
588 {
Bram Moolenaar26262f82019-09-04 20:59:15 +0200589 // find a '.' in the tail:
590 // - for second :e: before the current fname
591 // - otherwise: The last '.'
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200592 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
593 s = *fnamep - 2;
594 else
595 s = *fnamep + *fnamelen - 1;
596 for ( ; s > tail; --s)
597 if (s[0] == '.')
598 break;
Bram Moolenaar26262f82019-09-04 20:59:15 +0200599 if (src[*usedlen + 1] == 'e') // :e
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200600 {
601 if (s > tail)
602 {
603 *fnamelen += (int)(*fnamep - (s + 1));
604 *fnamep = s + 1;
605#ifdef VMS
Bram Moolenaar26262f82019-09-04 20:59:15 +0200606 // cut version from the extension
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200607 s = *fnamep + *fnamelen - 1;
608 for ( ; s > *fnamep; --s)
609 if (s[0] == ';')
610 break;
611 if (s > *fnamep)
612 *fnamelen = s - *fnamep;
613#endif
614 }
615 else if (*fnamep <= tail)
616 *fnamelen = 0;
617 }
Bram Moolenaar26262f82019-09-04 20:59:15 +0200618 else // :r
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200619 {
Bram Moolenaarb1892952019-10-08 23:26:50 +0200620 char_u *limit = *fnamep;
621
622 if (limit < tail)
623 limit = tail;
624 if (s > limit) // remove one extension
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200625 *fnamelen = (int)(s - *fnamep);
626 }
627 *usedlen += 2;
628 }
629
Bram Moolenaar26262f82019-09-04 20:59:15 +0200630 // ":s?pat?foo?" - substitute
631 // ":gs?pat?foo?" - global substitute
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200632 if (src[*usedlen] == ':'
633 && (src[*usedlen + 1] == 's'
634 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
635 {
636 char_u *str;
637 char_u *pat;
638 char_u *sub;
639 int sep;
640 char_u *flags;
641 int didit = FALSE;
642
643 flags = (char_u *)"";
644 s = src + *usedlen + 2;
645 if (src[*usedlen + 1] == 'g')
646 {
647 flags = (char_u *)"g";
648 ++s;
649 }
650
651 sep = *s++;
652 if (sep)
653 {
Bram Moolenaar26262f82019-09-04 20:59:15 +0200654 // find end of pattern
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200655 p = vim_strchr(s, sep);
656 if (p != NULL)
657 {
658 pat = vim_strnsave(s, (int)(p - s));
659 if (pat != NULL)
660 {
661 s = p + 1;
Bram Moolenaar26262f82019-09-04 20:59:15 +0200662 // find end of substitution
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200663 p = vim_strchr(s, sep);
664 if (p != NULL)
665 {
666 sub = vim_strnsave(s, (int)(p - s));
667 str = vim_strnsave(*fnamep, *fnamelen);
668 if (sub != NULL && str != NULL)
669 {
670 *usedlen = (int)(p + 1 - src);
671 s = do_string_sub(str, pat, sub, NULL, flags);
672 if (s != NULL)
673 {
674 *fnamep = s;
675 *fnamelen = (int)STRLEN(s);
676 vim_free(*bufp);
677 *bufp = s;
678 didit = TRUE;
679 }
680 }
681 vim_free(sub);
682 vim_free(str);
683 }
684 vim_free(pat);
685 }
686 }
Bram Moolenaar26262f82019-09-04 20:59:15 +0200687 // after using ":s", repeat all the modifiers
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200688 if (didit)
689 goto repeat;
690 }
691 }
692
693 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
694 {
Bram Moolenaar26262f82019-09-04 20:59:15 +0200695 // vim_strsave_shellescape() needs a NUL terminated string.
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200696 c = (*fnamep)[*fnamelen];
697 if (c != NUL)
698 (*fnamep)[*fnamelen] = NUL;
699 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
700 if (c != NUL)
701 (*fnamep)[*fnamelen] = c;
702 if (p == NULL)
703 return -1;
704 vim_free(*bufp);
705 *bufp = *fnamep = p;
706 *fnamelen = (int)STRLEN(p);
707 *usedlen += 2;
708 }
709
710 return valid;
711}
712
713#if defined(FEAT_EVAL) || defined(PROTO)
714
715/*
716 * "chdir(dir)" function
717 */
718 void
719f_chdir(typval_T *argvars, typval_T *rettv)
720{
721 char_u *cwd;
722 cdscope_T scope = CDSCOPE_GLOBAL;
723
724 rettv->v_type = VAR_STRING;
725 rettv->vval.v_string = NULL;
726
727 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaard816cd92020-02-04 22:23:09 +0100728 // Returning an empty string means it failed.
Bram Moolenaar002bc792020-06-05 22:33:42 +0200729 // No error message, for historic reasons.
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200730 return;
731
732 // Return the current directory
733 cwd = alloc(MAXPATHL);
734 if (cwd != NULL)
735 {
736 if (mch_dirname(cwd, MAXPATHL) != FAIL)
737 {
738#ifdef BACKSLASH_IN_FILENAME
739 slash_adjust(cwd);
740#endif
741 rettv->vval.v_string = vim_strsave(cwd);
742 }
743 vim_free(cwd);
744 }
745
746 if (curwin->w_localdir != NULL)
747 scope = CDSCOPE_WINDOW;
748 else if (curtab->tp_localdir != NULL)
749 scope = CDSCOPE_TABPAGE;
750
751 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
752 // Directory change failed
753 VIM_CLEAR(rettv->vval.v_string);
754}
755
756/*
757 * "delete()" function
758 */
759 void
760f_delete(typval_T *argvars, typval_T *rettv)
761{
762 char_u nbuf[NUMBUFLEN];
763 char_u *name;
764 char_u *flags;
765
766 rettv->vval.v_number = -1;
767 if (check_restricted() || check_secure())
768 return;
769
770 name = tv_get_string(&argvars[0]);
771 if (name == NULL || *name == NUL)
772 {
773 emsg(_(e_invarg));
774 return;
775 }
776
777 if (argvars[1].v_type != VAR_UNKNOWN)
778 flags = tv_get_string_buf(&argvars[1], nbuf);
779 else
780 flags = (char_u *)"";
781
782 if (*flags == NUL)
Bram Moolenaar26262f82019-09-04 20:59:15 +0200783 // delete a file
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200784 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
785 else if (STRCMP(flags, "d") == 0)
Bram Moolenaar26262f82019-09-04 20:59:15 +0200786 // delete an empty directory
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200787 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
788 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar26262f82019-09-04 20:59:15 +0200789 // delete a directory recursively
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200790 rettv->vval.v_number = delete_recursive(name);
791 else
792 semsg(_(e_invexpr2), flags);
793}
794
795/*
796 * "executable()" function
797 */
798 void
799f_executable(typval_T *argvars, typval_T *rettv)
800{
801 char_u *name = tv_get_string(&argvars[0]);
802
Bram Moolenaar26262f82019-09-04 20:59:15 +0200803 // Check in $PATH and also check directly if there is a directory name.
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200804 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE);
805}
806
807/*
808 * "exepath()" function
809 */
810 void
811f_exepath(typval_T *argvars, typval_T *rettv)
812{
813 char_u *p = NULL;
814
815 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
816 rettv->v_type = VAR_STRING;
817 rettv->vval.v_string = p;
818}
819
820/*
821 * "filereadable()" function
822 */
823 void
824f_filereadable(typval_T *argvars, typval_T *rettv)
825{
826 int fd;
827 char_u *p;
828 int n;
829
830#ifndef O_NONBLOCK
831# define O_NONBLOCK 0
832#endif
833 p = tv_get_string(&argvars[0]);
834 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
835 O_RDONLY | O_NONBLOCK, 0)) >= 0)
836 {
837 n = TRUE;
838 close(fd);
839 }
840 else
841 n = FALSE;
842
843 rettv->vval.v_number = n;
844}
845
846/*
847 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
848 * rights to write into.
849 */
850 void
851f_filewritable(typval_T *argvars, typval_T *rettv)
852{
853 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
854}
855
Bram Moolenaar840d16f2019-09-10 21:27:18 +0200856 static void
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200857findfilendir(
858 typval_T *argvars UNUSED,
859 typval_T *rettv,
860 int find_what UNUSED)
861{
862#ifdef FEAT_SEARCHPATH
863 char_u *fname;
864 char_u *fresult = NULL;
865 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
866 char_u *p;
867 char_u pathbuf[NUMBUFLEN];
868 int count = 1;
869 int first = TRUE;
870 int error = FALSE;
871#endif
872
873 rettv->vval.v_string = NULL;
874 rettv->v_type = VAR_STRING;
875
876#ifdef FEAT_SEARCHPATH
877 fname = tv_get_string(&argvars[0]);
878
879 if (argvars[1].v_type != VAR_UNKNOWN)
880 {
881 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
882 if (p == NULL)
883 error = TRUE;
884 else
885 {
886 if (*p != NUL)
887 path = p;
888
889 if (argvars[2].v_type != VAR_UNKNOWN)
890 count = (int)tv_get_number_chk(&argvars[2], &error);
891 }
892 }
893
894 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
895 error = TRUE;
896
897 if (*fname != NUL && !error)
898 {
899 do
900 {
901 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
902 vim_free(fresult);
903 fresult = find_file_in_path_option(first ? fname : NULL,
904 first ? (int)STRLEN(fname) : 0,
905 0, first, path,
906 find_what,
907 curbuf->b_ffname,
908 find_what == FINDFILE_DIR
909 ? (char_u *)"" : curbuf->b_p_sua);
910 first = FALSE;
911
912 if (fresult != NULL && rettv->v_type == VAR_LIST)
913 list_append_string(rettv->vval.v_list, fresult, -1);
914
915 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
916 }
917
918 if (rettv->v_type == VAR_STRING)
919 rettv->vval.v_string = fresult;
920#endif
921}
922
923/*
924 * "finddir({fname}[, {path}[, {count}]])" function
925 */
926 void
927f_finddir(typval_T *argvars, typval_T *rettv)
928{
929 findfilendir(argvars, rettv, FINDFILE_DIR);
930}
931
932/*
933 * "findfile({fname}[, {path}[, {count}]])" function
934 */
935 void
936f_findfile(typval_T *argvars, typval_T *rettv)
937{
938 findfilendir(argvars, rettv, FINDFILE_FILE);
939}
940
941/*
942 * "fnamemodify({fname}, {mods})" function
943 */
944 void
945f_fnamemodify(typval_T *argvars, typval_T *rettv)
946{
947 char_u *fname;
948 char_u *mods;
949 int usedlen = 0;
950 int len;
951 char_u *fbuf = NULL;
952 char_u buf[NUMBUFLEN];
953
954 fname = tv_get_string_chk(&argvars[0]);
955 mods = tv_get_string_buf_chk(&argvars[1], buf);
956 if (fname == NULL || mods == NULL)
957 fname = NULL;
958 else
959 {
960 len = (int)STRLEN(fname);
961 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
962 }
963
964 rettv->v_type = VAR_STRING;
965 if (fname == NULL)
966 rettv->vval.v_string = NULL;
967 else
968 rettv->vval.v_string = vim_strnsave(fname, len);
969 vim_free(fbuf);
970}
971
972/*
973 * "getcwd()" function
974 *
975 * Return the current working directory of a window in a tab page.
976 * First optional argument 'winnr' is the window number or -1 and the second
977 * optional argument 'tabnr' is the tab page number.
978 *
979 * If no arguments are supplied, then return the directory of the current
980 * window.
981 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
982 * the specified window.
983 * If 'winnr' is 0 then return the directory of the current window.
984 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
985 * directory of the specified tab page. Otherwise return the directory of the
986 * specified window in the specified tab page.
987 * If the window or the tab page doesn't exist then return NULL.
988 */
989 void
990f_getcwd(typval_T *argvars, typval_T *rettv)
991{
992 win_T *wp = NULL;
993 tabpage_T *tp = NULL;
994 char_u *cwd;
995 int global = FALSE;
996
997 rettv->v_type = VAR_STRING;
998 rettv->vval.v_string = NULL;
999
1000 if (argvars[0].v_type == VAR_NUMBER
1001 && argvars[0].vval.v_number == -1
1002 && argvars[1].v_type == VAR_UNKNOWN)
1003 global = TRUE;
1004 else
1005 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
1006
1007 if (wp != NULL && wp->w_localdir != NULL)
1008 rettv->vval.v_string = vim_strsave(wp->w_localdir);
1009 else if (tp != NULL && tp->tp_localdir != NULL)
1010 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
1011 else if (wp != NULL || tp != NULL || global)
1012 {
1013 if (globaldir != NULL)
1014 rettv->vval.v_string = vim_strsave(globaldir);
1015 else
1016 {
1017 cwd = alloc(MAXPATHL);
1018 if (cwd != NULL)
1019 {
1020 if (mch_dirname(cwd, MAXPATHL) != FAIL)
1021 rettv->vval.v_string = vim_strsave(cwd);
1022 vim_free(cwd);
1023 }
1024 }
1025 }
1026#ifdef BACKSLASH_IN_FILENAME
1027 if (rettv->vval.v_string != NULL)
1028 slash_adjust(rettv->vval.v_string);
1029#endif
1030}
1031
1032/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001033 * Convert "st" to file permission string.
1034 */
1035 char_u *
1036getfpermst(stat_T *st, char_u *perm)
1037{
1038 char_u flags[] = "rwx";
1039 int i;
1040
1041 for (i = 0; i < 9; i++)
1042 {
1043 if (st->st_mode & (1 << (8 - i)))
1044 perm[i] = flags[i % 3];
1045 else
1046 perm[i] = '-';
1047 }
1048 return perm;
1049}
1050
1051/*
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001052 * "getfperm({fname})" function
1053 */
1054 void
1055f_getfperm(typval_T *argvars, typval_T *rettv)
1056{
1057 char_u *fname;
1058 stat_T st;
1059 char_u *perm = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001060 char_u permbuf[] = "---------";
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001061
1062 fname = tv_get_string(&argvars[0]);
1063
1064 rettv->v_type = VAR_STRING;
1065 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001066 perm = vim_strsave(getfpermst(&st, permbuf));
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001067 rettv->vval.v_string = perm;
1068}
1069
1070/*
1071 * "getfsize({fname})" function
1072 */
1073 void
1074f_getfsize(typval_T *argvars, typval_T *rettv)
1075{
1076 char_u *fname;
1077 stat_T st;
1078
1079 fname = tv_get_string(&argvars[0]);
1080
1081 rettv->v_type = VAR_NUMBER;
1082
1083 if (mch_stat((char *)fname, &st) >= 0)
1084 {
1085 if (mch_isdir(fname))
1086 rettv->vval.v_number = 0;
1087 else
1088 {
1089 rettv->vval.v_number = (varnumber_T)st.st_size;
1090
Bram Moolenaar26262f82019-09-04 20:59:15 +02001091 // non-perfect check for overflow
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001092 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
1093 rettv->vval.v_number = -2;
1094 }
1095 }
1096 else
1097 rettv->vval.v_number = -1;
1098}
1099
1100/*
1101 * "getftime({fname})" function
1102 */
1103 void
1104f_getftime(typval_T *argvars, typval_T *rettv)
1105{
1106 char_u *fname;
1107 stat_T st;
1108
1109 fname = tv_get_string(&argvars[0]);
1110
1111 if (mch_stat((char *)fname, &st) >= 0)
1112 rettv->vval.v_number = (varnumber_T)st.st_mtime;
1113 else
1114 rettv->vval.v_number = -1;
1115}
1116
1117/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001118 * Convert "st" to file type string.
1119 */
1120 char_u *
1121getftypest(stat_T *st)
1122{
1123 char *t;
1124
1125 if (S_ISREG(st->st_mode))
1126 t = "file";
1127 else if (S_ISDIR(st->st_mode))
1128 t = "dir";
1129 else if (S_ISLNK(st->st_mode))
1130 t = "link";
1131 else if (S_ISBLK(st->st_mode))
1132 t = "bdev";
1133 else if (S_ISCHR(st->st_mode))
1134 t = "cdev";
1135 else if (S_ISFIFO(st->st_mode))
1136 t = "fifo";
1137 else if (S_ISSOCK(st->st_mode))
1138 t = "socket";
1139 else
1140 t = "other";
1141 return (char_u*)t;
1142}
1143
1144/*
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001145 * "getftype({fname})" function
1146 */
1147 void
1148f_getftype(typval_T *argvars, typval_T *rettv)
1149{
1150 char_u *fname;
1151 stat_T st;
1152 char_u *type = NULL;
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001153
1154 fname = tv_get_string(&argvars[0]);
1155
1156 rettv->v_type = VAR_STRING;
1157 if (mch_lstat((char *)fname, &st) >= 0)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001158 type = vim_strsave(getftypest(&st));
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001159 rettv->vval.v_string = type;
1160}
1161
1162/*
1163 * "glob()" function
1164 */
1165 void
1166f_glob(typval_T *argvars, typval_T *rettv)
1167{
1168 int options = WILD_SILENT|WILD_USE_NL;
1169 expand_T xpc;
1170 int error = FALSE;
1171
Bram Moolenaar26262f82019-09-04 20:59:15 +02001172 // When the optional second argument is non-zero, don't remove matches
1173 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001174 rettv->v_type = VAR_STRING;
1175 if (argvars[1].v_type != VAR_UNKNOWN)
1176 {
1177 if (tv_get_number_chk(&argvars[1], &error))
1178 options |= WILD_KEEP_ALL;
1179 if (argvars[2].v_type != VAR_UNKNOWN)
1180 {
1181 if (tv_get_number_chk(&argvars[2], &error))
1182 rettv_list_set(rettv, NULL);
1183 if (argvars[3].v_type != VAR_UNKNOWN
1184 && tv_get_number_chk(&argvars[3], &error))
1185 options |= WILD_ALLLINKS;
1186 }
1187 }
1188 if (!error)
1189 {
1190 ExpandInit(&xpc);
1191 xpc.xp_context = EXPAND_FILES;
1192 if (p_wic)
1193 options += WILD_ICASE;
1194 if (rettv->v_type == VAR_STRING)
1195 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
1196 NULL, options, WILD_ALL);
1197 else if (rettv_list_alloc(rettv) != FAIL)
1198 {
1199 int i;
1200
1201 ExpandOne(&xpc, tv_get_string(&argvars[0]),
1202 NULL, options, WILD_ALL_KEEP);
1203 for (i = 0; i < xpc.xp_numfiles; i++)
1204 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
1205
1206 ExpandCleanup(&xpc);
1207 }
1208 }
1209 else
1210 rettv->vval.v_string = NULL;
1211}
1212
1213/*
1214 * "glob2regpat()" function
1215 */
1216 void
1217f_glob2regpat(typval_T *argvars, typval_T *rettv)
1218{
1219 char_u *pat = tv_get_string_chk(&argvars[0]);
1220
1221 rettv->v_type = VAR_STRING;
1222 rettv->vval.v_string = (pat == NULL)
1223 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
1224}
1225
1226/*
1227 * "globpath()" function
1228 */
1229 void
1230f_globpath(typval_T *argvars, typval_T *rettv)
1231{
1232 int flags = WILD_IGNORE_COMPLETESLASH;
1233 char_u buf1[NUMBUFLEN];
1234 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
1235 int error = FALSE;
1236 garray_T ga;
1237 int i;
1238
1239 // When the optional second argument is non-zero, don't remove matches
1240 // for 'wildignore' and don't put matches for 'suffixes' at the end.
1241 rettv->v_type = VAR_STRING;
1242 if (argvars[2].v_type != VAR_UNKNOWN)
1243 {
1244 if (tv_get_number_chk(&argvars[2], &error))
1245 flags |= WILD_KEEP_ALL;
1246 if (argvars[3].v_type != VAR_UNKNOWN)
1247 {
1248 if (tv_get_number_chk(&argvars[3], &error))
1249 rettv_list_set(rettv, NULL);
1250 if (argvars[4].v_type != VAR_UNKNOWN
1251 && tv_get_number_chk(&argvars[4], &error))
1252 flags |= WILD_ALLLINKS;
1253 }
1254 }
1255 if (file != NULL && !error)
1256 {
1257 ga_init2(&ga, (int)sizeof(char_u *), 10);
1258 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
1259 if (rettv->v_type == VAR_STRING)
1260 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
1261 else if (rettv_list_alloc(rettv) != FAIL)
1262 for (i = 0; i < ga.ga_len; ++i)
1263 list_append_string(rettv->vval.v_list,
1264 ((char_u **)(ga.ga_data))[i], -1);
1265 ga_clear_strings(&ga);
1266 }
1267 else
1268 rettv->vval.v_string = NULL;
1269}
1270
1271/*
1272 * "isdirectory()" function
1273 */
1274 void
1275f_isdirectory(typval_T *argvars, typval_T *rettv)
1276{
1277 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
1278}
1279
1280/*
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001281 * Create the directory in which "dir" is located, and higher levels when
1282 * needed.
1283 * Return OK or FAIL.
1284 */
1285 static int
1286mkdir_recurse(char_u *dir, int prot)
1287{
1288 char_u *p;
1289 char_u *updir;
1290 int r = FAIL;
1291
Bram Moolenaar26262f82019-09-04 20:59:15 +02001292 // Get end of directory name in "dir".
1293 // We're done when it's "/" or "c:/".
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001294 p = gettail_sep(dir);
1295 if (p <= get_past_head(dir))
1296 return OK;
1297
Bram Moolenaar26262f82019-09-04 20:59:15 +02001298 // If the directory exists we're done. Otherwise: create it.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001299 updir = vim_strnsave(dir, (int)(p - dir));
1300 if (updir == NULL)
1301 return FAIL;
1302 if (mch_isdir(updir))
1303 r = OK;
1304 else if (mkdir_recurse(updir, prot) == OK)
1305 r = vim_mkdir_emsg(updir, prot);
1306 vim_free(updir);
1307 return r;
1308}
1309
1310/*
1311 * "mkdir()" function
1312 */
1313 void
1314f_mkdir(typval_T *argvars, typval_T *rettv)
1315{
1316 char_u *dir;
1317 char_u buf[NUMBUFLEN];
1318 int prot = 0755;
1319
1320 rettv->vval.v_number = FAIL;
1321 if (check_restricted() || check_secure())
1322 return;
1323
1324 dir = tv_get_string_buf(&argvars[0], buf);
1325 if (*dir == NUL)
1326 return;
1327
1328 if (*gettail(dir) == NUL)
Bram Moolenaar26262f82019-09-04 20:59:15 +02001329 // remove trailing slashes
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001330 *gettail_sep(dir) = NUL;
1331
1332 if (argvars[1].v_type != VAR_UNKNOWN)
1333 {
1334 if (argvars[2].v_type != VAR_UNKNOWN)
1335 {
1336 prot = (int)tv_get_number_chk(&argvars[2], NULL);
1337 if (prot == -1)
1338 return;
1339 }
1340 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
1341 {
1342 if (mch_isdir(dir))
1343 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02001344 // With the "p" flag it's OK if the dir already exists.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001345 rettv->vval.v_number = OK;
1346 return;
1347 }
1348 mkdir_recurse(dir, prot);
1349 }
1350 }
1351 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
1352}
1353
1354/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02001355 * "pathshorten()" function
1356 */
1357 void
1358f_pathshorten(typval_T *argvars, typval_T *rettv)
1359{
1360 char_u *p;
1361
1362 rettv->v_type = VAR_STRING;
1363 p = tv_get_string_chk(&argvars[0]);
1364 if (p == NULL)
1365 rettv->vval.v_string = NULL;
1366 else
1367 {
1368 p = vim_strsave(p);
1369 rettv->vval.v_string = p;
1370 if (p != NULL)
1371 shorten_dir(p);
1372 }
1373}
1374
1375/*
Bram Moolenaar80147dd2020-02-04 22:32:59 +01001376 * Evaluate "expr" (= "context") for readdir().
1377 */
1378 static int
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001379readdir_checkitem(void *context, void *item)
Bram Moolenaar80147dd2020-02-04 22:32:59 +01001380{
1381 typval_T *expr = (typval_T *)context;
1382 typval_T save_val;
1383 typval_T rettv;
1384 typval_T argv[2];
1385 int retval = 0;
1386 int error = FALSE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001387 char_u *name = (char_u*)item;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01001388
1389 prepare_vimvar(VV_VAL, &save_val);
1390 set_vim_var_string(VV_VAL, name, -1);
1391 argv[0].v_type = VAR_STRING;
1392 argv[0].vval.v_string = name;
1393
1394 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
1395 goto theend;
1396
1397 retval = tv_get_number_chk(&rettv, &error);
1398 if (error)
1399 retval = -1;
1400 clear_tv(&rettv);
1401
1402theend:
1403 set_vim_var_string(VV_VAL, NULL, 0);
1404 restore_vimvar(VV_VAL, &save_val);
1405 return retval;
1406}
1407
1408/*
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001409 * "readdir()" function
1410 */
1411 void
1412f_readdir(typval_T *argvars, typval_T *rettv)
1413{
1414 typval_T *expr;
1415 int ret;
1416 char_u *path;
1417 char_u *p;
1418 garray_T ga;
1419 int i;
1420
1421 if (rettv_list_alloc(rettv) == FAIL)
1422 return;
1423 path = tv_get_string(&argvars[0]);
1424 expr = &argvars[1];
1425
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001426 ret = readdir_core(&ga, path, FALSE, (void *)expr,
1427 (expr->v_type == VAR_UNKNOWN) ? NULL : readdir_checkitem);
1428 if (ret == OK)
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001429 {
1430 for (i = 0; i < ga.ga_len; i++)
1431 {
1432 p = ((char_u **)ga.ga_data)[i];
1433 list_append_string(rettv->vval.v_list, p, -1);
1434 }
1435 }
1436 ga_clear_strings(&ga);
1437}
1438
1439/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001440 * Evaluate "expr" (= "context") for readdirex().
1441 */
1442 static int
1443readdirex_checkitem(void *context, void *item)
1444{
1445 typval_T *expr = (typval_T *)context;
1446 typval_T save_val;
1447 typval_T rettv;
1448 typval_T argv[2];
1449 int retval = 0;
1450 int error = FALSE;
1451 dict_T *dict = (dict_T*)item;
1452
1453 prepare_vimvar(VV_VAL, &save_val);
1454 set_vim_var_dict(VV_VAL, dict);
1455 argv[0].v_type = VAR_DICT;
1456 argv[0].vval.v_dict = dict;
1457
1458 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
1459 goto theend;
1460
1461 retval = tv_get_number_chk(&rettv, &error);
1462 if (error)
1463 retval = -1;
1464 clear_tv(&rettv);
1465
1466theend:
1467 set_vim_var_dict(VV_VAL, NULL);
1468 restore_vimvar(VV_VAL, &save_val);
1469 return retval;
1470}
1471
1472/*
1473 * "readdirex()" function
1474 */
1475 void
1476f_readdirex(typval_T *argvars, typval_T *rettv)
1477{
1478 typval_T *expr;
1479 int ret;
1480 char_u *path;
1481 garray_T ga;
1482 int i;
1483
1484 if (rettv_list_alloc(rettv) == FAIL)
1485 return;
1486 path = tv_get_string(&argvars[0]);
1487 expr = &argvars[1];
1488
1489 ret = readdir_core(&ga, path, TRUE, (void *)expr,
1490 (expr->v_type == VAR_UNKNOWN) ? NULL : readdirex_checkitem);
1491 if (ret == OK)
1492 {
1493 for (i = 0; i < ga.ga_len; i++)
1494 {
1495 dict_T *dict = ((dict_T**)ga.ga_data)[i];
1496 list_append_dict(rettv->vval.v_list, dict);
1497 dict_unref(dict);
1498 }
1499 }
1500 ga_clear(&ga);
1501}
1502
1503/*
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001504 * "readfile()" function
1505 */
1506 void
1507f_readfile(typval_T *argvars, typval_T *rettv)
1508{
1509 int binary = FALSE;
1510 int blob = FALSE;
1511 int failed = FALSE;
1512 char_u *fname;
1513 FILE *fd;
Bram Moolenaar26262f82019-09-04 20:59:15 +02001514 char_u buf[(IOSIZE/256)*256]; // rounded to avoid odd + 1
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001515 int io_size = sizeof(buf);
Bram Moolenaar26262f82019-09-04 20:59:15 +02001516 int readlen; // size of last fread()
1517 char_u *prev = NULL; // previously read bytes, if any
1518 long prevlen = 0; // length of data in prev
1519 long prevsize = 0; // size of prev buffer
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001520 long maxline = MAXLNUM;
1521 long cnt = 0;
Bram Moolenaar26262f82019-09-04 20:59:15 +02001522 char_u *p; // position in buf
1523 char_u *start; // start of current line
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001524
1525 if (argvars[1].v_type != VAR_UNKNOWN)
1526 {
1527 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
1528 binary = TRUE;
1529 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
1530 blob = TRUE;
1531
1532 if (argvars[2].v_type != VAR_UNKNOWN)
1533 maxline = (long)tv_get_number(&argvars[2]);
1534 }
1535
Bram Moolenaar15352dc2020-04-06 21:12:42 +02001536 if ((blob ? rettv_blob_alloc(rettv) : rettv_list_alloc(rettv)) == FAIL)
1537 return;
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001538
Bram Moolenaar26262f82019-09-04 20:59:15 +02001539 // Always open the file in binary mode, library functions have a mind of
1540 // their own about CR-LF conversion.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001541 fname = tv_get_string(&argvars[0]);
Bram Moolenaar15352dc2020-04-06 21:12:42 +02001542
1543 if (mch_isdir(fname))
1544 {
1545 semsg(_(e_isadir2), fname);
1546 return;
1547 }
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001548 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
1549 {
1550 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
1551 return;
1552 }
1553
1554 if (blob)
1555 {
1556 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
1557 {
Bram Moolenaar15352dc2020-04-06 21:12:42 +02001558 semsg(_(e_notread), fname);
1559 // An empty blob is returned on error.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001560 blob_free(rettv->vval.v_blob);
Bram Moolenaar15352dc2020-04-06 21:12:42 +02001561 rettv->vval.v_blob = NULL;
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001562 }
1563 fclose(fd);
1564 return;
1565 }
1566
1567 while (cnt < maxline || maxline < 0)
1568 {
1569 readlen = (int)fread(buf, 1, io_size, fd);
1570
Bram Moolenaar26262f82019-09-04 20:59:15 +02001571 // This for loop processes what was read, but is also entered at end
1572 // of file so that either:
1573 // - an incomplete line gets written
1574 // - a "binary" file gets an empty line at the end if it ends in a
1575 // newline.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001576 for (p = buf, start = buf;
1577 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
1578 ++p)
1579 {
1580 if (*p == '\n' || readlen <= 0)
1581 {
1582 listitem_T *li;
1583 char_u *s = NULL;
1584 long_u len = p - start;
1585
Bram Moolenaar26262f82019-09-04 20:59:15 +02001586 // Finished a line. Remove CRs before NL.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001587 if (readlen > 0 && !binary)
1588 {
1589 while (len > 0 && start[len - 1] == '\r')
1590 --len;
Bram Moolenaar26262f82019-09-04 20:59:15 +02001591 // removal may cross back to the "prev" string
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001592 if (len == 0)
1593 while (prevlen > 0 && prev[prevlen - 1] == '\r')
1594 --prevlen;
1595 }
1596 if (prevlen == 0)
1597 s = vim_strnsave(start, (int)len);
1598 else
1599 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02001600 // Change "prev" buffer to be the right size. This way
1601 // the bytes are only copied once, and very long lines are
1602 // allocated only once.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001603 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
1604 {
1605 mch_memmove(s + prevlen, start, len);
1606 s[prevlen + len] = NUL;
Bram Moolenaar26262f82019-09-04 20:59:15 +02001607 prev = NULL; // the list will own the string
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001608 prevlen = prevsize = 0;
1609 }
1610 }
1611 if (s == NULL)
1612 {
1613 do_outofmem_msg((long_u) prevlen + len + 1);
1614 failed = TRUE;
1615 break;
1616 }
1617
1618 if ((li = listitem_alloc()) == NULL)
1619 {
1620 vim_free(s);
1621 failed = TRUE;
1622 break;
1623 }
1624 li->li_tv.v_type = VAR_STRING;
1625 li->li_tv.v_lock = 0;
1626 li->li_tv.vval.v_string = s;
1627 list_append(rettv->vval.v_list, li);
1628
Bram Moolenaar26262f82019-09-04 20:59:15 +02001629 start = p + 1; // step over newline
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001630 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
1631 break;
1632 }
1633 else if (*p == NUL)
1634 *p = '\n';
Bram Moolenaar26262f82019-09-04 20:59:15 +02001635 // Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
1636 // when finding the BF and check the previous two bytes.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001637 else if (*p == 0xbf && enc_utf8 && !binary)
1638 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02001639 // Find the two bytes before the 0xbf. If p is at buf, or buf
1640 // + 1, these may be in the "prev" string.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001641 char_u back1 = p >= buf + 1 ? p[-1]
1642 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
1643 char_u back2 = p >= buf + 2 ? p[-2]
1644 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
1645 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
1646
1647 if (back2 == 0xef && back1 == 0xbb)
1648 {
1649 char_u *dest = p - 2;
1650
Bram Moolenaar26262f82019-09-04 20:59:15 +02001651 // Usually a BOM is at the beginning of a file, and so at
1652 // the beginning of a line; then we can just step over it.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001653 if (start == dest)
1654 start = p + 1;
1655 else
1656 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02001657 // have to shuffle buf to close gap
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001658 int adjust_prevlen = 0;
1659
1660 if (dest < buf)
1661 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02001662 adjust_prevlen = (int)(buf - dest); // must be 1 or 2
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001663 dest = buf;
1664 }
1665 if (readlen > p - buf + 1)
1666 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
1667 readlen -= 3 - adjust_prevlen;
1668 prevlen -= adjust_prevlen;
1669 p = dest - 1;
1670 }
1671 }
1672 }
Bram Moolenaar26262f82019-09-04 20:59:15 +02001673 } // for
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001674
1675 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
1676 break;
1677 if (start < p)
1678 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02001679 // There's part of a line in buf, store it in "prev".
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001680 if (p - start + prevlen >= prevsize)
1681 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02001682 // need bigger "prev" buffer
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001683 char_u *newprev;
1684
Bram Moolenaar26262f82019-09-04 20:59:15 +02001685 // A common use case is ordinary text files and "prev" gets a
1686 // fragment of a line, so the first allocation is made
1687 // small, to avoid repeatedly 'allocing' large and
1688 // 'reallocing' small.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001689 if (prevsize == 0)
1690 prevsize = (long)(p - start);
1691 else
1692 {
1693 long grow50pc = (prevsize * 3) / 2;
1694 long growmin = (long)((p - start) * 2 + prevlen);
1695 prevsize = grow50pc > growmin ? grow50pc : growmin;
1696 }
1697 newprev = vim_realloc(prev, prevsize);
1698 if (newprev == NULL)
1699 {
1700 do_outofmem_msg((long_u)prevsize);
1701 failed = TRUE;
1702 break;
1703 }
1704 prev = newprev;
1705 }
Bram Moolenaar26262f82019-09-04 20:59:15 +02001706 // Add the line part to end of "prev".
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001707 mch_memmove(prev + prevlen, start, p - start);
1708 prevlen += (long)(p - start);
1709 }
Bram Moolenaar26262f82019-09-04 20:59:15 +02001710 } // while
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001711
Bram Moolenaar26262f82019-09-04 20:59:15 +02001712 // For a negative line count use only the lines at the end of the file,
1713 // free the rest.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001714 if (!failed && maxline < 0)
1715 while (cnt > -maxline)
1716 {
1717 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
1718 --cnt;
1719 }
1720
1721 if (failed)
1722 {
1723 // an empty list is returned on error
1724 list_free(rettv->vval.v_list);
1725 rettv_list_alloc(rettv);
1726 }
1727
1728 vim_free(prev);
1729 fclose(fd);
1730}
1731
1732/*
1733 * "resolve()" function
1734 */
1735 void
1736f_resolve(typval_T *argvars, typval_T *rettv)
1737{
1738 char_u *p;
1739#ifdef HAVE_READLINK
1740 char_u *buf = NULL;
1741#endif
1742
1743 p = tv_get_string(&argvars[0]);
1744#ifdef FEAT_SHORTCUT
1745 {
1746 char_u *v = NULL;
1747
1748 v = mch_resolve_path(p, TRUE);
1749 if (v != NULL)
1750 rettv->vval.v_string = v;
1751 else
1752 rettv->vval.v_string = vim_strsave(p);
1753 }
1754#else
1755# ifdef HAVE_READLINK
1756 {
1757 char_u *cpy;
1758 int len;
1759 char_u *remain = NULL;
1760 char_u *q;
1761 int is_relative_to_current = FALSE;
1762 int has_trailing_pathsep = FALSE;
1763 int limit = 100;
1764
1765 p = vim_strsave(p);
Bram Moolenaar70188f52019-12-23 18:18:52 +01001766 if (p == NULL)
1767 goto fail;
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001768 if (p[0] == '.' && (vim_ispathsep(p[1])
1769 || (p[1] == '.' && (vim_ispathsep(p[2])))))
1770 is_relative_to_current = TRUE;
1771
1772 len = STRLEN(p);
1773 if (len > 0 && after_pathsep(p, p + len))
1774 {
1775 has_trailing_pathsep = TRUE;
Bram Moolenaar26262f82019-09-04 20:59:15 +02001776 p[len - 1] = NUL; // the trailing slash breaks readlink()
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001777 }
1778
1779 q = getnextcomp(p);
1780 if (*q != NUL)
1781 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02001782 // Separate the first path component in "p", and keep the
1783 // remainder (beginning with the path separator).
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001784 remain = vim_strsave(q - 1);
1785 q[-1] = NUL;
1786 }
1787
1788 buf = alloc(MAXPATHL + 1);
1789 if (buf == NULL)
Bram Moolenaar70188f52019-12-23 18:18:52 +01001790 {
1791 vim_free(p);
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001792 goto fail;
Bram Moolenaar70188f52019-12-23 18:18:52 +01001793 }
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001794
1795 for (;;)
1796 {
1797 for (;;)
1798 {
1799 len = readlink((char *)p, (char *)buf, MAXPATHL);
1800 if (len <= 0)
1801 break;
1802 buf[len] = NUL;
1803
1804 if (limit-- == 0)
1805 {
1806 vim_free(p);
1807 vim_free(remain);
1808 emsg(_("E655: Too many symbolic links (cycle?)"));
1809 rettv->vval.v_string = NULL;
1810 goto fail;
1811 }
1812
Bram Moolenaar26262f82019-09-04 20:59:15 +02001813 // Ensure that the result will have a trailing path separator
1814 // if the argument has one.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001815 if (remain == NULL && has_trailing_pathsep)
1816 add_pathsep(buf);
1817
Bram Moolenaar26262f82019-09-04 20:59:15 +02001818 // Separate the first path component in the link value and
1819 // concatenate the remainders.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001820 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
1821 if (*q != NUL)
1822 {
1823 if (remain == NULL)
1824 remain = vim_strsave(q - 1);
1825 else
1826 {
1827 cpy = concat_str(q - 1, remain);
1828 if (cpy != NULL)
1829 {
1830 vim_free(remain);
1831 remain = cpy;
1832 }
1833 }
1834 q[-1] = NUL;
1835 }
1836
1837 q = gettail(p);
1838 if (q > p && *q == NUL)
1839 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02001840 // Ignore trailing path separator.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001841 q[-1] = NUL;
1842 q = gettail(p);
1843 }
1844 if (q > p && !mch_isFullName(buf))
1845 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02001846 // symlink is relative to directory of argument
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001847 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
1848 if (cpy != NULL)
1849 {
1850 STRCPY(cpy, p);
1851 STRCPY(gettail(cpy), buf);
1852 vim_free(p);
1853 p = cpy;
1854 }
1855 }
1856 else
1857 {
1858 vim_free(p);
1859 p = vim_strsave(buf);
1860 }
1861 }
1862
1863 if (remain == NULL)
1864 break;
1865
Bram Moolenaar26262f82019-09-04 20:59:15 +02001866 // Append the first path component of "remain" to "p".
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001867 q = getnextcomp(remain + 1);
1868 len = q - remain - (*q != NUL);
1869 cpy = vim_strnsave(p, STRLEN(p) + len);
1870 if (cpy != NULL)
1871 {
1872 STRNCAT(cpy, remain, len);
1873 vim_free(p);
1874 p = cpy;
1875 }
Bram Moolenaar26262f82019-09-04 20:59:15 +02001876 // Shorten "remain".
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001877 if (*q != NUL)
1878 STRMOVE(remain, q - 1);
1879 else
1880 VIM_CLEAR(remain);
1881 }
1882
Bram Moolenaar26262f82019-09-04 20:59:15 +02001883 // If the result is a relative path name, make it explicitly relative to
1884 // the current directory if and only if the argument had this form.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001885 if (!vim_ispathsep(*p))
1886 {
1887 if (is_relative_to_current
1888 && *p != NUL
1889 && !(p[0] == '.'
1890 && (p[1] == NUL
1891 || vim_ispathsep(p[1])
1892 || (p[1] == '.'
1893 && (p[2] == NUL
1894 || vim_ispathsep(p[2]))))))
1895 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02001896 // Prepend "./".
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001897 cpy = concat_str((char_u *)"./", p);
1898 if (cpy != NULL)
1899 {
1900 vim_free(p);
1901 p = cpy;
1902 }
1903 }
1904 else if (!is_relative_to_current)
1905 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02001906 // Strip leading "./".
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001907 q = p;
1908 while (q[0] == '.' && vim_ispathsep(q[1]))
1909 q += 2;
1910 if (q > p)
1911 STRMOVE(p, p + 2);
1912 }
1913 }
1914
Bram Moolenaar26262f82019-09-04 20:59:15 +02001915 // Ensure that the result will have no trailing path separator
1916 // if the argument had none. But keep "/" or "//".
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001917 if (!has_trailing_pathsep)
1918 {
1919 q = p + STRLEN(p);
1920 if (after_pathsep(p, q))
1921 *gettail_sep(p) = NUL;
1922 }
1923
1924 rettv->vval.v_string = p;
1925 }
1926# else
1927 rettv->vval.v_string = vim_strsave(p);
1928# endif
1929#endif
1930
1931 simplify_filename(rettv->vval.v_string);
1932
1933#ifdef HAVE_READLINK
1934fail:
1935 vim_free(buf);
1936#endif
1937 rettv->v_type = VAR_STRING;
1938}
1939
1940/*
1941 * "tempname()" function
1942 */
1943 void
1944f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
1945{
1946 static int x = 'A';
1947
1948 rettv->v_type = VAR_STRING;
1949 rettv->vval.v_string = vim_tempname(x, FALSE);
1950
Bram Moolenaar26262f82019-09-04 20:59:15 +02001951 // Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
1952 // names. Skip 'I' and 'O', they are used for shell redirection.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001953 do
1954 {
1955 if (x == 'Z')
1956 x = '0';
1957 else if (x == '9')
1958 x = 'A';
1959 else
1960 {
1961#ifdef EBCDIC
1962 if (x == 'I')
1963 x = 'J';
1964 else if (x == 'R')
1965 x = 'S';
1966 else
1967#endif
1968 ++x;
1969 }
1970 } while (x == 'I' || x == 'O');
1971}
1972
1973/*
1974 * "writefile()" function
1975 */
1976 void
1977f_writefile(typval_T *argvars, typval_T *rettv)
1978{
1979 int binary = FALSE;
1980 int append = FALSE;
1981#ifdef HAVE_FSYNC
1982 int do_fsync = p_fs;
1983#endif
1984 char_u *fname;
1985 FILE *fd;
1986 int ret = 0;
1987 listitem_T *li;
1988 list_T *list = NULL;
1989 blob_T *blob = NULL;
1990
1991 rettv->vval.v_number = -1;
1992 if (check_secure())
1993 return;
1994
1995 if (argvars[0].v_type == VAR_LIST)
1996 {
1997 list = argvars[0].vval.v_list;
1998 if (list == NULL)
1999 return;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002000 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002001 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002002 if (tv_get_string_chk(&li->li_tv) == NULL)
2003 return;
2004 }
2005 else if (argvars[0].v_type == VAR_BLOB)
2006 {
2007 blob = argvars[0].vval.v_blob;
2008 if (blob == NULL)
2009 return;
2010 }
2011 else
2012 {
Bram Moolenaar18a2b872020-03-19 13:08:45 +01002013 semsg(_(e_invarg2),
2014 _("writefile() first argument must be a List or a Blob"));
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002015 return;
2016 }
2017
2018 if (argvars[2].v_type != VAR_UNKNOWN)
2019 {
2020 char_u *arg2 = tv_get_string_chk(&argvars[2]);
2021
2022 if (arg2 == NULL)
2023 return;
2024 if (vim_strchr(arg2, 'b') != NULL)
2025 binary = TRUE;
2026 if (vim_strchr(arg2, 'a') != NULL)
2027 append = TRUE;
2028#ifdef HAVE_FSYNC
2029 if (vim_strchr(arg2, 's') != NULL)
2030 do_fsync = TRUE;
2031 else if (vim_strchr(arg2, 'S') != NULL)
2032 do_fsync = FALSE;
2033#endif
2034 }
2035
2036 fname = tv_get_string_chk(&argvars[1]);
2037 if (fname == NULL)
2038 return;
2039
Bram Moolenaar26262f82019-09-04 20:59:15 +02002040 // Always open the file in binary mode, library functions have a mind of
2041 // their own about CR-LF conversion.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002042 if (*fname == NUL || (fd = mch_fopen((char *)fname,
2043 append ? APPENDBIN : WRITEBIN)) == NULL)
2044 {
2045 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
2046 ret = -1;
2047 }
2048 else if (blob)
2049 {
2050 if (write_blob(fd, blob) == FAIL)
2051 ret = -1;
2052#ifdef HAVE_FSYNC
2053 else if (do_fsync)
2054 // Ignore the error, the user wouldn't know what to do about it.
2055 // May happen for a device.
2056 vim_ignored = vim_fsync(fileno(fd));
2057#endif
2058 fclose(fd);
2059 }
2060 else
2061 {
2062 if (write_list(fd, list, binary) == FAIL)
2063 ret = -1;
2064#ifdef HAVE_FSYNC
2065 else if (do_fsync)
Bram Moolenaar26262f82019-09-04 20:59:15 +02002066 // Ignore the error, the user wouldn't know what to do about it.
2067 // May happen for a device.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002068 vim_ignored = vim_fsync(fileno(fd));
2069#endif
2070 fclose(fd);
2071 }
2072
2073 rettv->vval.v_number = ret;
2074}
2075
2076#endif // FEAT_EVAL
2077
2078#if defined(FEAT_BROWSE) || defined(PROTO)
2079/*
2080 * Generic browse function. Calls gui_mch_browse() when possible.
2081 * Later this may pop-up a non-GUI file selector (external command?).
2082 */
2083 char_u *
2084do_browse(
Bram Moolenaar26262f82019-09-04 20:59:15 +02002085 int flags, // BROWSE_SAVE and BROWSE_DIR
2086 char_u *title, // title for the window
2087 char_u *dflt, // default file name (may include directory)
2088 char_u *ext, // extension added
2089 char_u *initdir, // initial directory, NULL for current dir or
2090 // when using path from "dflt"
2091 char_u *filter, // file name filter
2092 buf_T *buf) // buffer to read/write for
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002093{
2094 char_u *fname;
Bram Moolenaar26262f82019-09-04 20:59:15 +02002095 static char_u *last_dir = NULL; // last used directory
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002096 char_u *tofree = NULL;
2097 int save_browse = cmdmod.browse;
2098
Bram Moolenaar26262f82019-09-04 20:59:15 +02002099 // Must turn off browse to avoid that autocommands will get the
2100 // flag too!
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002101 cmdmod.browse = FALSE;
2102
2103 if (title == NULL || *title == NUL)
2104 {
2105 if (flags & BROWSE_DIR)
2106 title = (char_u *)_("Select Directory dialog");
2107 else if (flags & BROWSE_SAVE)
2108 title = (char_u *)_("Save File dialog");
2109 else
2110 title = (char_u *)_("Open File dialog");
2111 }
2112
Bram Moolenaar26262f82019-09-04 20:59:15 +02002113 // When no directory specified, use default file name, default dir, buffer
2114 // dir, last dir or current dir
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002115 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
2116 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02002117 if (mch_isdir(dflt)) // default file name is a directory
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002118 {
2119 initdir = dflt;
2120 dflt = NULL;
2121 }
Bram Moolenaar26262f82019-09-04 20:59:15 +02002122 else if (gettail(dflt) != dflt) // default file name includes a path
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002123 {
2124 tofree = vim_strsave(dflt);
2125 if (tofree != NULL)
2126 {
2127 initdir = tofree;
2128 *gettail(initdir) = NUL;
2129 dflt = gettail(dflt);
2130 }
2131 }
2132 }
2133
2134 if (initdir == NULL || *initdir == NUL)
2135 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02002136 // When 'browsedir' is a directory, use it
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002137 if (STRCMP(p_bsdir, "last") != 0
2138 && STRCMP(p_bsdir, "buffer") != 0
2139 && STRCMP(p_bsdir, "current") != 0
2140 && mch_isdir(p_bsdir))
2141 initdir = p_bsdir;
Bram Moolenaar26262f82019-09-04 20:59:15 +02002142 // When saving or 'browsedir' is "buffer", use buffer fname
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002143 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
2144 && buf != NULL && buf->b_ffname != NULL)
2145 {
2146 if (dflt == NULL || *dflt == NUL)
2147 dflt = gettail(curbuf->b_ffname);
2148 tofree = vim_strsave(curbuf->b_ffname);
2149 if (tofree != NULL)
2150 {
2151 initdir = tofree;
2152 *gettail(initdir) = NUL;
2153 }
2154 }
Bram Moolenaar26262f82019-09-04 20:59:15 +02002155 // When 'browsedir' is "last", use dir from last browse
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002156 else if (*p_bsdir == 'l')
2157 initdir = last_dir;
Bram Moolenaar26262f82019-09-04 20:59:15 +02002158 // When 'browsedir is "current", use current directory. This is the
2159 // default already, leave initdir empty.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002160 }
2161
2162# ifdef FEAT_GUI
Bram Moolenaar26262f82019-09-04 20:59:15 +02002163 if (gui.in_use) // when this changes, also adjust f_has()!
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002164 {
2165 if (filter == NULL
2166# ifdef FEAT_EVAL
2167 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
2168 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
2169# endif
2170 )
2171 filter = BROWSE_FILTER_DEFAULT;
2172 if (flags & BROWSE_DIR)
2173 {
2174# if defined(FEAT_GUI_GTK) || defined(MSWIN)
Bram Moolenaar26262f82019-09-04 20:59:15 +02002175 // For systems that have a directory dialog.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002176 fname = gui_mch_browsedir(title, initdir);
2177# else
Bram Moolenaar26262f82019-09-04 20:59:15 +02002178 // Generic solution for selecting a directory: select a file and
2179 // remove the file name.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002180 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
2181# endif
2182# if !defined(FEAT_GUI_GTK)
Bram Moolenaar26262f82019-09-04 20:59:15 +02002183 // Win32 adds a dummy file name, others return an arbitrary file
2184 // name. GTK+ 2 returns only the directory,
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002185 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
2186 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02002187 // Remove the file name.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002188 char_u *tail = gettail_sep(fname);
2189
2190 if (tail == fname)
Bram Moolenaar26262f82019-09-04 20:59:15 +02002191 *tail++ = '.'; // use current dir
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002192 *tail = NUL;
2193 }
2194# endif
2195 }
2196 else
2197 fname = gui_mch_browse(flags & BROWSE_SAVE,
2198 title, dflt, ext, initdir, (char_u *)_(filter));
2199
Bram Moolenaar26262f82019-09-04 20:59:15 +02002200 // We hang around in the dialog for a while, the user might do some
2201 // things to our files. The Win32 dialog allows deleting or renaming
2202 // a file, check timestamps.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002203 need_check_timestamps = TRUE;
2204 did_check_timestamps = FALSE;
2205 }
2206 else
2207# endif
2208 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02002209 // TODO: non-GUI file selector here
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002210 emsg(_("E338: Sorry, no file browser in console mode"));
2211 fname = NULL;
2212 }
2213
Bram Moolenaar26262f82019-09-04 20:59:15 +02002214 // keep the directory for next time
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002215 if (fname != NULL)
2216 {
2217 vim_free(last_dir);
2218 last_dir = vim_strsave(fname);
2219 if (last_dir != NULL && !(flags & BROWSE_DIR))
2220 {
2221 *gettail(last_dir) = NUL;
2222 if (*last_dir == NUL)
2223 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02002224 // filename only returned, must be in current dir
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002225 vim_free(last_dir);
2226 last_dir = alloc(MAXPATHL);
2227 if (last_dir != NULL)
2228 mch_dirname(last_dir, MAXPATHL);
2229 }
2230 }
2231 }
2232
2233 vim_free(tofree);
2234 cmdmod.browse = save_browse;
2235
2236 return fname;
2237}
2238#endif
2239
2240#if defined(FEAT_EVAL) || defined(PROTO)
2241
2242/*
2243 * "browse(save, title, initdir, default)" function
2244 */
2245 void
2246f_browse(typval_T *argvars UNUSED, typval_T *rettv)
2247{
2248# ifdef FEAT_BROWSE
2249 int save;
2250 char_u *title;
2251 char_u *initdir;
2252 char_u *defname;
2253 char_u buf[NUMBUFLEN];
2254 char_u buf2[NUMBUFLEN];
2255 int error = FALSE;
2256
2257 save = (int)tv_get_number_chk(&argvars[0], &error);
2258 title = tv_get_string_chk(&argvars[1]);
2259 initdir = tv_get_string_buf_chk(&argvars[2], buf);
2260 defname = tv_get_string_buf_chk(&argvars[3], buf2);
2261
2262 if (error || title == NULL || initdir == NULL || defname == NULL)
2263 rettv->vval.v_string = NULL;
2264 else
2265 rettv->vval.v_string =
2266 do_browse(save ? BROWSE_SAVE : 0,
2267 title, defname, NULL, initdir, NULL, curbuf);
2268# else
2269 rettv->vval.v_string = NULL;
2270# endif
2271 rettv->v_type = VAR_STRING;
2272}
2273
2274/*
2275 * "browsedir(title, initdir)" function
2276 */
2277 void
2278f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
2279{
2280# ifdef FEAT_BROWSE
2281 char_u *title;
2282 char_u *initdir;
2283 char_u buf[NUMBUFLEN];
2284
2285 title = tv_get_string_chk(&argvars[0]);
2286 initdir = tv_get_string_buf_chk(&argvars[1], buf);
2287
2288 if (title == NULL || initdir == NULL)
2289 rettv->vval.v_string = NULL;
2290 else
2291 rettv->vval.v_string = do_browse(BROWSE_DIR,
2292 title, NULL, NULL, initdir, NULL, curbuf);
2293# else
2294 rettv->vval.v_string = NULL;
2295# endif
2296 rettv->v_type = VAR_STRING;
2297}
2298
2299#endif // FEAT_EVAL
Bram Moolenaar26262f82019-09-04 20:59:15 +02002300
2301/*
2302 * Replace home directory by "~" in each space or comma separated file name in
2303 * 'src'.
2304 * If anything fails (except when out of space) dst equals src.
2305 */
2306 void
2307home_replace(
2308 buf_T *buf, // when not NULL, check for help files
2309 char_u *src, // input file name
2310 char_u *dst, // where to put the result
2311 int dstlen, // maximum length of the result
2312 int one) // if TRUE, only replace one file name, include
2313 // spaces and commas in the file name.
2314{
2315 size_t dirlen = 0, envlen = 0;
2316 size_t len;
2317 char_u *homedir_env, *homedir_env_orig;
2318 char_u *p;
2319
2320 if (src == NULL)
2321 {
2322 *dst = NUL;
2323 return;
2324 }
2325
2326 /*
2327 * If the file is a help file, remove the path completely.
2328 */
2329 if (buf != NULL && buf->b_help)
2330 {
2331 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
2332 return;
2333 }
2334
2335 /*
2336 * We check both the value of the $HOME environment variable and the
2337 * "real" home directory.
2338 */
2339 if (homedir != NULL)
2340 dirlen = STRLEN(homedir);
2341
2342#ifdef VMS
2343 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
2344#else
2345 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
2346#endif
2347#ifdef MSWIN
2348 if (homedir_env == NULL)
2349 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
2350#endif
2351 // Empty is the same as not set.
2352 if (homedir_env != NULL && *homedir_env == NUL)
2353 homedir_env = NULL;
2354
2355 if (homedir_env != NULL && *homedir_env == '~')
2356 {
2357 int usedlen = 0;
2358 int flen;
2359 char_u *fbuf = NULL;
2360
2361 flen = (int)STRLEN(homedir_env);
2362 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
2363 &homedir_env, &fbuf, &flen);
2364 flen = (int)STRLEN(homedir_env);
2365 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
2366 // Remove the trailing / that is added to a directory.
2367 homedir_env[flen - 1] = NUL;
2368 }
2369
2370 if (homedir_env != NULL)
2371 envlen = STRLEN(homedir_env);
2372
2373 if (!one)
2374 src = skipwhite(src);
2375 while (*src && dstlen > 0)
2376 {
2377 /*
2378 * Here we are at the beginning of a file name.
2379 * First, check to see if the beginning of the file name matches
2380 * $HOME or the "real" home directory. Check that there is a '/'
2381 * after the match (so that if e.g. the file is "/home/pieter/bla",
2382 * and the home directory is "/home/piet", the file does not end up
2383 * as "~er/bla" (which would seem to indicate the file "bla" in user
2384 * er's home directory)).
2385 */
2386 p = homedir;
2387 len = dirlen;
2388 for (;;)
2389 {
2390 if ( len
2391 && fnamencmp(src, p, len) == 0
2392 && (vim_ispathsep(src[len])
2393 || (!one && (src[len] == ',' || src[len] == ' '))
2394 || src[len] == NUL))
2395 {
2396 src += len;
2397 if (--dstlen > 0)
2398 *dst++ = '~';
2399
2400 /*
2401 * If it's just the home directory, add "/".
2402 */
2403 if (!vim_ispathsep(src[0]) && --dstlen > 0)
2404 *dst++ = '/';
2405 break;
2406 }
2407 if (p == homedir_env)
2408 break;
2409 p = homedir_env;
2410 len = envlen;
2411 }
2412
2413 // if (!one) skip to separator: space or comma
2414 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
2415 *dst++ = *src++;
2416 // skip separator
2417 while ((*src == ' ' || *src == ',') && --dstlen > 0)
2418 *dst++ = *src++;
2419 }
2420 // if (dstlen == 0) out of space, what to do???
2421
2422 *dst = NUL;
2423
2424 if (homedir_env != homedir_env_orig)
2425 vim_free(homedir_env);
2426}
2427
2428/*
2429 * Like home_replace, store the replaced string in allocated memory.
2430 * When something fails, NULL is returned.
2431 */
2432 char_u *
2433home_replace_save(
2434 buf_T *buf, // when not NULL, check for help files
2435 char_u *src) // input file name
2436{
2437 char_u *dst;
2438 unsigned len;
2439
2440 len = 3; // space for "~/" and trailing NUL
2441 if (src != NULL) // just in case
2442 len += (unsigned)STRLEN(src);
2443 dst = alloc(len);
2444 if (dst != NULL)
2445 home_replace(buf, src, dst, len, TRUE);
2446 return dst;
2447}
2448
2449/*
2450 * Compare two file names and return:
2451 * FPC_SAME if they both exist and are the same file.
2452 * FPC_SAMEX if they both don't exist and have the same file name.
2453 * FPC_DIFF if they both exist and are different files.
2454 * FPC_NOTX if they both don't exist.
2455 * FPC_DIFFX if one of them doesn't exist.
2456 * For the first name environment variables are expanded if "expandenv" is
2457 * TRUE.
2458 */
2459 int
2460fullpathcmp(
2461 char_u *s1,
2462 char_u *s2,
2463 int checkname, // when both don't exist, check file names
2464 int expandenv)
2465{
2466#ifdef UNIX
2467 char_u exp1[MAXPATHL];
2468 char_u full1[MAXPATHL];
2469 char_u full2[MAXPATHL];
2470 stat_T st1, st2;
2471 int r1, r2;
2472
2473 if (expandenv)
2474 expand_env(s1, exp1, MAXPATHL);
2475 else
2476 vim_strncpy(exp1, s1, MAXPATHL - 1);
2477 r1 = mch_stat((char *)exp1, &st1);
2478 r2 = mch_stat((char *)s2, &st2);
2479 if (r1 != 0 && r2 != 0)
2480 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002481 // if mch_stat() doesn't work, may compare the names
Bram Moolenaar26262f82019-09-04 20:59:15 +02002482 if (checkname)
2483 {
2484 if (fnamecmp(exp1, s2) == 0)
2485 return FPC_SAMEX;
2486 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
2487 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
2488 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
2489 return FPC_SAMEX;
2490 }
2491 return FPC_NOTX;
2492 }
2493 if (r1 != 0 || r2 != 0)
2494 return FPC_DIFFX;
2495 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
2496 return FPC_SAME;
2497 return FPC_DIFF;
2498#else
2499 char_u *exp1; // expanded s1
2500 char_u *full1; // full path of s1
2501 char_u *full2; // full path of s2
2502 int retval = FPC_DIFF;
2503 int r1, r2;
2504
2505 // allocate one buffer to store three paths (alloc()/free() is slow!)
2506 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
2507 {
2508 full1 = exp1 + MAXPATHL;
2509 full2 = full1 + MAXPATHL;
2510
2511 if (expandenv)
2512 expand_env(s1, exp1, MAXPATHL);
2513 else
2514 vim_strncpy(exp1, s1, MAXPATHL - 1);
2515 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
2516 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
2517
2518 // If vim_FullName() fails, the file probably doesn't exist.
2519 if (r1 != OK && r2 != OK)
2520 {
2521 if (checkname && fnamecmp(exp1, s2) == 0)
2522 retval = FPC_SAMEX;
2523 else
2524 retval = FPC_NOTX;
2525 }
2526 else if (r1 != OK || r2 != OK)
2527 retval = FPC_DIFFX;
2528 else if (fnamecmp(full1, full2))
2529 retval = FPC_DIFF;
2530 else
2531 retval = FPC_SAME;
2532 vim_free(exp1);
2533 }
2534 return retval;
2535#endif
2536}
2537
2538/*
2539 * Get the tail of a path: the file name.
2540 * When the path ends in a path separator the tail is the NUL after it.
2541 * Fail safe: never returns NULL.
2542 */
2543 char_u *
2544gettail(char_u *fname)
2545{
2546 char_u *p1, *p2;
2547
2548 if (fname == NULL)
2549 return (char_u *)"";
2550 for (p1 = p2 = get_past_head(fname); *p2; ) // find last part of path
2551 {
2552 if (vim_ispathsep_nocolon(*p2))
2553 p1 = p2 + 1;
2554 MB_PTR_ADV(p2);
2555 }
2556 return p1;
2557}
2558
2559/*
2560 * Get pointer to tail of "fname", including path separators. Putting a NUL
2561 * here leaves the directory name. Takes care of "c:/" and "//".
2562 * Always returns a valid pointer.
2563 */
2564 char_u *
2565gettail_sep(char_u *fname)
2566{
2567 char_u *p;
2568 char_u *t;
2569
2570 p = get_past_head(fname); // don't remove the '/' from "c:/file"
2571 t = gettail(fname);
2572 while (t > p && after_pathsep(fname, t))
2573 --t;
2574#ifdef VMS
2575 // path separator is part of the path
2576 ++t;
2577#endif
2578 return t;
2579}
2580
2581/*
2582 * get the next path component (just after the next path separator).
2583 */
2584 char_u *
2585getnextcomp(char_u *fname)
2586{
2587 while (*fname && !vim_ispathsep(*fname))
2588 MB_PTR_ADV(fname);
2589 if (*fname)
2590 ++fname;
2591 return fname;
2592}
2593
2594/*
2595 * Get a pointer to one character past the head of a path name.
2596 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
2597 * If there is no head, path is returned.
2598 */
2599 char_u *
2600get_past_head(char_u *path)
2601{
2602 char_u *retval;
2603
2604#if defined(MSWIN)
2605 // may skip "c:"
2606 if (isalpha(path[0]) && path[1] == ':')
2607 retval = path + 2;
2608 else
2609 retval = path;
2610#else
2611# if defined(AMIGA)
2612 // may skip "label:"
2613 retval = vim_strchr(path, ':');
2614 if (retval == NULL)
2615 retval = path;
2616# else // Unix
2617 retval = path;
2618# endif
2619#endif
2620
2621 while (vim_ispathsep(*retval))
2622 ++retval;
2623
2624 return retval;
2625}
2626
2627/*
2628 * Return TRUE if 'c' is a path separator.
2629 * Note that for MS-Windows this includes the colon.
2630 */
2631 int
2632vim_ispathsep(int c)
2633{
2634#ifdef UNIX
2635 return (c == '/'); // UNIX has ':' inside file names
2636#else
2637# ifdef BACKSLASH_IN_FILENAME
2638 return (c == ':' || c == '/' || c == '\\');
2639# else
2640# ifdef VMS
2641 // server"user passwd"::device:[full.path.name]fname.extension;version"
2642 return (c == ':' || c == '[' || c == ']' || c == '/'
2643 || c == '<' || c == '>' || c == '"' );
2644# else
2645 return (c == ':' || c == '/');
2646# endif // VMS
2647# endif
2648#endif
2649}
2650
2651/*
2652 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
2653 */
2654 int
2655vim_ispathsep_nocolon(int c)
2656{
2657 return vim_ispathsep(c)
2658#ifdef BACKSLASH_IN_FILENAME
2659 && c != ':'
2660#endif
2661 ;
2662}
2663
2664/*
2665 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
2666 * It's done in-place.
2667 */
2668 void
2669shorten_dir(char_u *str)
2670{
2671 char_u *tail, *s, *d;
2672 int skip = FALSE;
2673
2674 tail = gettail(str);
2675 d = str;
2676 for (s = str; ; ++s)
2677 {
2678 if (s >= tail) // copy the whole tail
2679 {
2680 *d++ = *s;
2681 if (*s == NUL)
2682 break;
2683 }
2684 else if (vim_ispathsep(*s)) // copy '/' and next char
2685 {
2686 *d++ = *s;
2687 skip = FALSE;
2688 }
2689 else if (!skip)
2690 {
2691 *d++ = *s; // copy next char
2692 if (*s != '~' && *s != '.') // and leading "~" and "."
2693 skip = TRUE;
2694 if (has_mbyte)
2695 {
2696 int l = mb_ptr2len(s);
2697
2698 while (--l > 0)
2699 *d++ = *++s;
2700 }
2701 }
2702 }
2703}
2704
2705/*
2706 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
2707 * Also returns TRUE if there is no directory name.
2708 * "fname" must be writable!.
2709 */
2710 int
2711dir_of_file_exists(char_u *fname)
2712{
2713 char_u *p;
2714 int c;
2715 int retval;
2716
2717 p = gettail_sep(fname);
2718 if (p == fname)
2719 return TRUE;
2720 c = *p;
2721 *p = NUL;
2722 retval = mch_isdir(fname);
2723 *p = c;
2724 return retval;
2725}
2726
2727/*
2728 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
2729 * and deal with 'fileignorecase'.
2730 */
2731 int
2732vim_fnamecmp(char_u *x, char_u *y)
2733{
2734#ifdef BACKSLASH_IN_FILENAME
2735 return vim_fnamencmp(x, y, MAXPATHL);
2736#else
2737 if (p_fic)
2738 return MB_STRICMP(x, y);
2739 return STRCMP(x, y);
2740#endif
2741}
2742
2743 int
2744vim_fnamencmp(char_u *x, char_u *y, size_t len)
2745{
2746#ifdef BACKSLASH_IN_FILENAME
2747 char_u *px = x;
2748 char_u *py = y;
2749 int cx = NUL;
2750 int cy = NUL;
2751
2752 while (len > 0)
2753 {
2754 cx = PTR2CHAR(px);
2755 cy = PTR2CHAR(py);
2756 if (cx == NUL || cy == NUL
2757 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
2758 && !(cx == '/' && cy == '\\')
2759 && !(cx == '\\' && cy == '/')))
2760 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002761 len -= mb_ptr2len(px);
2762 px += mb_ptr2len(px);
2763 py += mb_ptr2len(py);
Bram Moolenaar26262f82019-09-04 20:59:15 +02002764 }
2765 if (len == 0)
2766 return 0;
2767 return (cx - cy);
2768#else
2769 if (p_fic)
2770 return MB_STRNICMP(x, y, len);
2771 return STRNCMP(x, y, len);
2772#endif
2773}
2774
2775/*
2776 * Concatenate file names fname1 and fname2 into allocated memory.
2777 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
2778 */
2779 char_u *
2780concat_fnames(char_u *fname1, char_u *fname2, int sep)
2781{
2782 char_u *dest;
2783
2784 dest = alloc(STRLEN(fname1) + STRLEN(fname2) + 3);
2785 if (dest != NULL)
2786 {
2787 STRCPY(dest, fname1);
2788 if (sep)
2789 add_pathsep(dest);
2790 STRCAT(dest, fname2);
2791 }
2792 return dest;
2793}
2794
2795/*
2796 * Add a path separator to a file name, unless it already ends in a path
2797 * separator.
2798 */
2799 void
2800add_pathsep(char_u *p)
2801{
2802 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
2803 STRCAT(p, PATHSEPSTR);
2804}
2805
2806/*
2807 * FullName_save - Make an allocated copy of a full file name.
2808 * Returns NULL when out of memory.
2809 */
2810 char_u *
2811FullName_save(
2812 char_u *fname,
2813 int force) // force expansion, even when it already looks
2814 // like a full path name
2815{
2816 char_u *buf;
2817 char_u *new_fname = NULL;
2818
2819 if (fname == NULL)
2820 return NULL;
2821
2822 buf = alloc(MAXPATHL);
2823 if (buf != NULL)
2824 {
2825 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
2826 new_fname = vim_strsave(buf);
2827 else
2828 new_fname = vim_strsave(fname);
2829 vim_free(buf);
2830 }
2831 return new_fname;
2832}
2833
2834/*
2835 * return TRUE if "fname" exists.
2836 */
2837 int
2838vim_fexists(char_u *fname)
2839{
2840 stat_T st;
2841
2842 if (mch_stat((char *)fname, &st))
2843 return FALSE;
2844 return TRUE;
2845}
2846
2847/*
2848 * Invoke expand_wildcards() for one pattern.
2849 * Expand items like "%:h" before the expansion.
2850 * Returns OK or FAIL.
2851 */
2852 int
2853expand_wildcards_eval(
2854 char_u **pat, // pointer to input pattern
2855 int *num_file, // resulting number of files
2856 char_u ***file, // array of resulting files
2857 int flags) // EW_DIR, etc.
2858{
2859 int ret = FAIL;
2860 char_u *eval_pat = NULL;
2861 char_u *exp_pat = *pat;
2862 char *ignored_msg;
2863 int usedlen;
2864
2865 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
2866 {
2867 ++emsg_off;
2868 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
2869 NULL, &ignored_msg, NULL);
2870 --emsg_off;
2871 if (eval_pat != NULL)
2872 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
2873 }
2874
2875 if (exp_pat != NULL)
2876 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
2877
2878 if (eval_pat != NULL)
2879 {
2880 vim_free(exp_pat);
2881 vim_free(eval_pat);
2882 }
2883
2884 return ret;
2885}
2886
2887/*
2888 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
2889 * 'wildignore'.
2890 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
2891 */
2892 int
2893expand_wildcards(
2894 int num_pat, // number of input patterns
2895 char_u **pat, // array of input patterns
2896 int *num_files, // resulting number of files
2897 char_u ***files, // array of resulting files
2898 int flags) // EW_DIR, etc.
2899{
2900 int retval;
2901 int i, j;
2902 char_u *p;
2903 int non_suf_match; // number without matching suffix
2904
2905 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
2906
2907 // When keeping all matches, return here
2908 if ((flags & EW_KEEPALL) || retval == FAIL)
2909 return retval;
2910
2911#ifdef FEAT_WILDIGN
2912 /*
2913 * Remove names that match 'wildignore'.
2914 */
2915 if (*p_wig)
2916 {
2917 char_u *ffname;
2918
2919 // check all files in (*files)[]
2920 for (i = 0; i < *num_files; ++i)
2921 {
2922 ffname = FullName_save((*files)[i], FALSE);
2923 if (ffname == NULL) // out of memory
2924 break;
2925# ifdef VMS
2926 vms_remove_version(ffname);
2927# endif
2928 if (match_file_list(p_wig, (*files)[i], ffname))
2929 {
2930 // remove this matching file from the list
2931 vim_free((*files)[i]);
2932 for (j = i; j + 1 < *num_files; ++j)
2933 (*files)[j] = (*files)[j + 1];
2934 --*num_files;
2935 --i;
2936 }
2937 vim_free(ffname);
2938 }
2939
2940 // If the number of matches is now zero, we fail.
2941 if (*num_files == 0)
2942 {
2943 VIM_CLEAR(*files);
2944 return FAIL;
2945 }
2946 }
2947#endif
2948
2949 /*
2950 * Move the names where 'suffixes' match to the end.
2951 */
2952 if (*num_files > 1)
2953 {
2954 non_suf_match = 0;
2955 for (i = 0; i < *num_files; ++i)
2956 {
2957 if (!match_suffix((*files)[i]))
2958 {
2959 /*
2960 * Move the name without matching suffix to the front
2961 * of the list.
2962 */
2963 p = (*files)[i];
2964 for (j = i; j > non_suf_match; --j)
2965 (*files)[j] = (*files)[j - 1];
2966 (*files)[non_suf_match++] = p;
2967 }
2968 }
2969 }
2970
2971 return retval;
2972}
2973
2974/*
2975 * Return TRUE if "fname" matches with an entry in 'suffixes'.
2976 */
2977 int
2978match_suffix(char_u *fname)
2979{
2980 int fnamelen, setsuflen;
2981 char_u *setsuf;
2982#define MAXSUFLEN 30 // maximum length of a file suffix
2983 char_u suf_buf[MAXSUFLEN];
2984
2985 fnamelen = (int)STRLEN(fname);
2986 setsuflen = 0;
2987 for (setsuf = p_su; *setsuf; )
2988 {
2989 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
2990 if (setsuflen == 0)
2991 {
2992 char_u *tail = gettail(fname);
2993
2994 // empty entry: match name without a '.'
2995 if (vim_strchr(tail, '.') == NULL)
2996 {
2997 setsuflen = 1;
2998 break;
2999 }
3000 }
3001 else
3002 {
3003 if (fnamelen >= setsuflen
3004 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
3005 (size_t)setsuflen) == 0)
3006 break;
3007 setsuflen = 0;
3008 }
3009 }
3010 return (setsuflen != 0);
3011}
3012
3013#ifdef VIM_BACKTICK
3014
3015/*
3016 * Return TRUE if we can expand this backtick thing here.
3017 */
3018 static int
3019vim_backtick(char_u *p)
3020{
3021 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
3022}
3023
3024/*
3025 * Expand an item in `backticks` by executing it as a command.
3026 * Currently only works when pat[] starts and ends with a `.
3027 * Returns number of file names found, -1 if an error is encountered.
3028 */
3029 static int
3030expand_backtick(
3031 garray_T *gap,
3032 char_u *pat,
3033 int flags) // EW_* flags
3034{
3035 char_u *p;
3036 char_u *cmd;
3037 char_u *buffer;
3038 int cnt = 0;
3039 int i;
3040
3041 // Create the command: lop off the backticks.
3042 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
3043 if (cmd == NULL)
3044 return -1;
3045
3046#ifdef FEAT_EVAL
3047 if (*cmd == '=') // `={expr}`: Expand expression
3048 buffer = eval_to_string(cmd + 1, &p, TRUE);
3049 else
3050#endif
3051 buffer = get_cmd_output(cmd, NULL,
3052 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
3053 vim_free(cmd);
3054 if (buffer == NULL)
3055 return -1;
3056
3057 cmd = buffer;
3058 while (*cmd != NUL)
3059 {
3060 cmd = skipwhite(cmd); // skip over white space
3061 p = cmd;
3062 while (*p != NUL && *p != '\r' && *p != '\n') // skip over entry
3063 ++p;
3064 // add an entry if it is not empty
3065 if (p > cmd)
3066 {
3067 i = *p;
3068 *p = NUL;
3069 addfile(gap, cmd, flags);
3070 *p = i;
3071 ++cnt;
3072 }
3073 cmd = p;
3074 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
3075 ++cmd;
3076 }
3077
3078 vim_free(buffer);
3079 return cnt;
3080}
3081#endif // VIM_BACKTICK
3082
3083#if defined(MSWIN)
3084/*
3085 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
3086 * it's shared between these systems.
3087 */
3088
3089/*
3090 * comparison function for qsort in dos_expandpath()
3091 */
3092 static int
3093pstrcmp(const void *a, const void *b)
3094{
3095 return (pathcmp(*(char **)a, *(char **)b, -1));
3096}
3097
3098/*
3099 * Recursively expand one path component into all matching files and/or
3100 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
3101 * Return the number of matches found.
3102 * "path" has backslashes before chars that are not to be expanded, starting
3103 * at "path[wildoff]".
3104 * Return the number of matches found.
3105 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
3106 */
3107 static int
3108dos_expandpath(
3109 garray_T *gap,
3110 char_u *path,
3111 int wildoff,
3112 int flags, // EW_* flags
3113 int didstar) // expanded "**" once already
3114{
3115 char_u *buf;
3116 char_u *path_end;
3117 char_u *p, *s, *e;
3118 int start_len = gap->ga_len;
3119 char_u *pat;
3120 regmatch_T regmatch;
3121 int starts_with_dot;
3122 int matches;
3123 int len;
3124 int starstar = FALSE;
3125 static int stardepth = 0; // depth for "**" expansion
3126 HANDLE hFind = INVALID_HANDLE_VALUE;
3127 WIN32_FIND_DATAW wfb;
3128 WCHAR *wn = NULL; // UCS-2 name, NULL when not used.
3129 char_u *matchname;
3130 int ok;
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +02003131 char_u *p_alt;
Bram Moolenaar26262f82019-09-04 20:59:15 +02003132
3133 // Expanding "**" may take a long time, check for CTRL-C.
3134 if (stardepth > 0)
3135 {
3136 ui_breakcheck();
3137 if (got_int)
3138 return 0;
3139 }
3140
3141 // Make room for file name. When doing encoding conversion the actual
3142 // length may be quite a bit longer, thus use the maximum possible length.
3143 buf = alloc(MAXPATHL);
3144 if (buf == NULL)
3145 return 0;
3146
3147 /*
3148 * Find the first part in the path name that contains a wildcard or a ~1.
3149 * Copy it into buf, including the preceding characters.
3150 */
3151 p = buf;
3152 s = buf;
3153 e = NULL;
3154 path_end = path;
3155 while (*path_end != NUL)
3156 {
3157 // May ignore a wildcard that has a backslash before it; it will
3158 // be removed by rem_backslash() or file_pat_to_reg_pat() below.
3159 if (path_end >= path + wildoff && rem_backslash(path_end))
3160 *p++ = *path_end++;
3161 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
3162 {
3163 if (e != NULL)
3164 break;
3165 s = p + 1;
3166 }
3167 else if (path_end >= path + wildoff
3168 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
3169 e = p;
3170 if (has_mbyte)
3171 {
3172 len = (*mb_ptr2len)(path_end);
3173 STRNCPY(p, path_end, len);
3174 p += len;
3175 path_end += len;
3176 }
3177 else
3178 *p++ = *path_end++;
3179 }
3180 e = p;
3181 *e = NUL;
3182
3183 // now we have one wildcard component between s and e
3184 // Remove backslashes between "wildoff" and the start of the wildcard
3185 // component.
3186 for (p = buf + wildoff; p < s; ++p)
3187 if (rem_backslash(p))
3188 {
3189 STRMOVE(p, p + 1);
3190 --e;
3191 --s;
3192 }
3193
3194 // Check for "**" between "s" and "e".
3195 for (p = s; p < e; ++p)
3196 if (p[0] == '*' && p[1] == '*')
3197 starstar = TRUE;
3198
3199 starts_with_dot = *s == '.';
3200 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
3201 if (pat == NULL)
3202 {
3203 vim_free(buf);
3204 return 0;
3205 }
3206
3207 // compile the regexp into a program
3208 if (flags & (EW_NOERROR | EW_NOTWILD))
3209 ++emsg_silent;
3210 regmatch.rm_ic = TRUE; // Always ignore case
3211 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
3212 if (flags & (EW_NOERROR | EW_NOTWILD))
3213 --emsg_silent;
3214 vim_free(pat);
3215
3216 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
3217 {
3218 vim_free(buf);
3219 return 0;
3220 }
3221
3222 // remember the pattern or file name being looked for
3223 matchname = vim_strsave(s);
3224
3225 // If "**" is by itself, this is the first time we encounter it and more
3226 // is following then find matches without any directory.
3227 if (!didstar && stardepth < 100 && starstar && e - s == 2
3228 && *path_end == '/')
3229 {
3230 STRCPY(s, path_end + 1);
3231 ++stardepth;
3232 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
3233 --stardepth;
3234 }
3235
3236 // Scan all files in the directory with "dir/ *.*"
3237 STRCPY(s, "*.*");
3238 wn = enc_to_utf16(buf, NULL);
3239 if (wn != NULL)
3240 hFind = FindFirstFileW(wn, &wfb);
3241 ok = (hFind != INVALID_HANDLE_VALUE);
3242
3243 while (ok)
3244 {
3245 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +02003246
Bram Moolenaar26262f82019-09-04 20:59:15 +02003247 if (p == NULL)
3248 break; // out of memory
3249
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +02003250 if (*wfb.cAlternateFileName == NUL)
Bram Moolenaar40655d52020-04-06 23:49:50 +02003251 p_alt = NULL;
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +02003252 else
3253 p_alt = utf16_to_enc(wfb.cAlternateFileName, NULL);
3254
Bram Moolenaar26262f82019-09-04 20:59:15 +02003255 // Ignore entries starting with a dot, unless when asked for. Accept
3256 // all entries found with "matchname".
3257 if ((p[0] != '.' || starts_with_dot
3258 || ((flags & EW_DODOT)
3259 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
3260 && (matchname == NULL
3261 || (regmatch.regprog != NULL
Bram Moolenaar40655d52020-04-06 23:49:50 +02003262 && (vim_regexec(&regmatch, p, (colnr_T)0)
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +02003263 || (p_alt != NULL
Bram Moolenaar40655d52020-04-06 23:49:50 +02003264 && vim_regexec(&regmatch, p_alt, (colnr_T)0))))
Bram Moolenaar26262f82019-09-04 20:59:15 +02003265 || ((flags & EW_NOTWILD)
3266 && fnamencmp(path + (s - buf), p, e - s) == 0)))
3267 {
3268 STRCPY(s, p);
3269 len = (int)STRLEN(buf);
3270
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +02003271 if (starstar && stardepth < 100
3272 && (wfb.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
Bram Moolenaar26262f82019-09-04 20:59:15 +02003273 {
3274 // For "**" in the pattern first go deeper in the tree to
3275 // find matches.
3276 STRCPY(buf + len, "/**");
3277 STRCPY(buf + len + 3, path_end);
3278 ++stardepth;
3279 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
3280 --stardepth;
3281 }
3282
3283 STRCPY(buf + len, path_end);
3284 if (mch_has_exp_wildcard(path_end))
3285 {
3286 // need to expand another component of the path
3287 // remove backslashes for the remaining components only
3288 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
3289 }
3290 else
3291 {
3292 // no more wildcards, check if there is a match
3293 // remove backslashes for the remaining components only
3294 if (*path_end != 0)
3295 backslash_halve(buf + len + 1);
3296 if (mch_getperm(buf) >= 0) // add existing file
3297 addfile(gap, buf, flags);
3298 }
3299 }
3300
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +02003301 vim_free(p_alt);
Bram Moolenaar26262f82019-09-04 20:59:15 +02003302 vim_free(p);
3303 ok = FindNextFileW(hFind, &wfb);
Bram Moolenaar26262f82019-09-04 20:59:15 +02003304 }
3305
3306 FindClose(hFind);
3307 vim_free(wn);
3308 vim_free(buf);
3309 vim_regfree(regmatch.regprog);
3310 vim_free(matchname);
3311
3312 matches = gap->ga_len - start_len;
3313 if (matches > 0)
3314 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
3315 sizeof(char_u *), pstrcmp);
3316 return matches;
3317}
3318
3319 int
3320mch_expandpath(
3321 garray_T *gap,
3322 char_u *path,
3323 int flags) // EW_* flags
3324{
3325 return dos_expandpath(gap, path, 0, flags, FALSE);
3326}
3327#endif // MSWIN
3328
3329#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
3330 || defined(PROTO)
3331/*
3332 * Unix style wildcard expansion code.
3333 * It's here because it's used both for Unix and Mac.
3334 */
3335 static int
3336pstrcmp(const void *a, const void *b)
3337{
3338 return (pathcmp(*(char **)a, *(char **)b, -1));
3339}
3340
3341/*
3342 * Recursively expand one path component into all matching files and/or
3343 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
3344 * "path" has backslashes before chars that are not to be expanded, starting
3345 * at "path + wildoff".
3346 * Return the number of matches found.
3347 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
3348 */
3349 int
3350unix_expandpath(
3351 garray_T *gap,
3352 char_u *path,
3353 int wildoff,
3354 int flags, // EW_* flags
3355 int didstar) // expanded "**" once already
3356{
3357 char_u *buf;
3358 char_u *path_end;
3359 char_u *p, *s, *e;
3360 int start_len = gap->ga_len;
3361 char_u *pat;
3362 regmatch_T regmatch;
3363 int starts_with_dot;
3364 int matches;
3365 int len;
3366 int starstar = FALSE;
3367 static int stardepth = 0; // depth for "**" expansion
3368
3369 DIR *dirp;
3370 struct dirent *dp;
3371
3372 // Expanding "**" may take a long time, check for CTRL-C.
3373 if (stardepth > 0)
3374 {
3375 ui_breakcheck();
3376 if (got_int)
3377 return 0;
3378 }
3379
3380 // make room for file name
3381 buf = alloc(STRLEN(path) + BASENAMELEN + 5);
3382 if (buf == NULL)
3383 return 0;
3384
3385 /*
3386 * Find the first part in the path name that contains a wildcard.
3387 * When EW_ICASE is set every letter is considered to be a wildcard.
3388 * Copy it into "buf", including the preceding characters.
3389 */
3390 p = buf;
3391 s = buf;
3392 e = NULL;
3393 path_end = path;
3394 while (*path_end != NUL)
3395 {
3396 // May ignore a wildcard that has a backslash before it; it will
3397 // be removed by rem_backslash() or file_pat_to_reg_pat() below.
3398 if (path_end >= path + wildoff && rem_backslash(path_end))
3399 *p++ = *path_end++;
3400 else if (*path_end == '/')
3401 {
3402 if (e != NULL)
3403 break;
3404 s = p + 1;
3405 }
3406 else if (path_end >= path + wildoff
3407 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
3408 || (!p_fic && (flags & EW_ICASE)
3409 && isalpha(PTR2CHAR(path_end)))))
3410 e = p;
3411 if (has_mbyte)
3412 {
3413 len = (*mb_ptr2len)(path_end);
3414 STRNCPY(p, path_end, len);
3415 p += len;
3416 path_end += len;
3417 }
3418 else
3419 *p++ = *path_end++;
3420 }
3421 e = p;
3422 *e = NUL;
3423
3424 // Now we have one wildcard component between "s" and "e".
3425 // Remove backslashes between "wildoff" and the start of the wildcard
3426 // component.
3427 for (p = buf + wildoff; p < s; ++p)
3428 if (rem_backslash(p))
3429 {
3430 STRMOVE(p, p + 1);
3431 --e;
3432 --s;
3433 }
3434
3435 // Check for "**" between "s" and "e".
3436 for (p = s; p < e; ++p)
3437 if (p[0] == '*' && p[1] == '*')
3438 starstar = TRUE;
3439
3440 // convert the file pattern to a regexp pattern
3441 starts_with_dot = *s == '.';
3442 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
3443 if (pat == NULL)
3444 {
3445 vim_free(buf);
3446 return 0;
3447 }
3448
3449 // compile the regexp into a program
3450 if (flags & EW_ICASE)
3451 regmatch.rm_ic = TRUE; // 'wildignorecase' set
3452 else
3453 regmatch.rm_ic = p_fic; // ignore case when 'fileignorecase' is set
3454 if (flags & (EW_NOERROR | EW_NOTWILD))
3455 ++emsg_silent;
3456 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
3457 if (flags & (EW_NOERROR | EW_NOTWILD))
3458 --emsg_silent;
3459 vim_free(pat);
3460
3461 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
3462 {
3463 vim_free(buf);
3464 return 0;
3465 }
3466
3467 // If "**" is by itself, this is the first time we encounter it and more
3468 // is following then find matches without any directory.
3469 if (!didstar && stardepth < 100 && starstar && e - s == 2
3470 && *path_end == '/')
3471 {
3472 STRCPY(s, path_end + 1);
3473 ++stardepth;
3474 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
3475 --stardepth;
3476 }
3477
3478 // open the directory for scanning
3479 *s = NUL;
3480 dirp = opendir(*buf == NUL ? "." : (char *)buf);
3481
3482 // Find all matching entries
3483 if (dirp != NULL)
3484 {
3485 for (;;)
3486 {
3487 dp = readdir(dirp);
3488 if (dp == NULL)
3489 break;
3490 if ((dp->d_name[0] != '.' || starts_with_dot
3491 || ((flags & EW_DODOT)
3492 && dp->d_name[1] != NUL
3493 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
3494 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
3495 (char_u *)dp->d_name, (colnr_T)0))
3496 || ((flags & EW_NOTWILD)
3497 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
3498 {
3499 STRCPY(s, dp->d_name);
3500 len = STRLEN(buf);
3501
3502 if (starstar && stardepth < 100)
3503 {
3504 // For "**" in the pattern first go deeper in the tree to
3505 // find matches.
3506 STRCPY(buf + len, "/**");
3507 STRCPY(buf + len + 3, path_end);
3508 ++stardepth;
3509 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
3510 --stardepth;
3511 }
3512
3513 STRCPY(buf + len, path_end);
3514 if (mch_has_exp_wildcard(path_end)) // handle more wildcards
3515 {
3516 // need to expand another component of the path
3517 // remove backslashes for the remaining components only
3518 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
3519 }
3520 else
3521 {
3522 stat_T sb;
3523
3524 // no more wildcards, check if there is a match
3525 // remove backslashes for the remaining components only
3526 if (*path_end != NUL)
3527 backslash_halve(buf + len + 1);
3528 // add existing file or symbolic link
3529 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
3530 : mch_getperm(buf) >= 0)
3531 {
3532#ifdef MACOS_CONVERT
3533 size_t precomp_len = STRLEN(buf)+1;
3534 char_u *precomp_buf =
3535 mac_precompose_path(buf, precomp_len, &precomp_len);
3536
3537 if (precomp_buf)
3538 {
3539 mch_memmove(buf, precomp_buf, precomp_len);
3540 vim_free(precomp_buf);
3541 }
3542#endif
3543 addfile(gap, buf, flags);
3544 }
3545 }
3546 }
3547 }
3548
3549 closedir(dirp);
3550 }
3551
3552 vim_free(buf);
3553 vim_regfree(regmatch.regprog);
3554
3555 matches = gap->ga_len - start_len;
3556 if (matches > 0)
3557 qsort(((char_u **)gap->ga_data) + start_len, matches,
3558 sizeof(char_u *), pstrcmp);
3559 return matches;
3560}
3561#endif
3562
3563/*
3564 * Return TRUE if "p" contains what looks like an environment variable.
3565 * Allowing for escaping.
3566 */
3567 static int
3568has_env_var(char_u *p)
3569{
3570 for ( ; *p; MB_PTR_ADV(p))
3571 {
3572 if (*p == '\\' && p[1] != NUL)
3573 ++p;
3574 else if (vim_strchr((char_u *)
3575#if defined(MSWIN)
3576 "$%"
3577#else
3578 "$"
3579#endif
3580 , *p) != NULL)
3581 return TRUE;
3582 }
3583 return FALSE;
3584}
3585
3586#ifdef SPECIAL_WILDCHAR
3587/*
3588 * Return TRUE if "p" contains a special wildcard character, one that Vim
3589 * cannot expand, requires using a shell.
3590 */
3591 static int
3592has_special_wildchar(char_u *p)
3593{
3594 for ( ; *p; MB_PTR_ADV(p))
3595 {
3596 // Disallow line break characters.
3597 if (*p == '\r' || *p == '\n')
3598 break;
3599 // Allow for escaping.
3600 if (*p == '\\' && p[1] != NUL && p[1] != '\r' && p[1] != '\n')
3601 ++p;
3602 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
3603 {
3604 // A { must be followed by a matching }.
3605 if (*p == '{' && vim_strchr(p, '}') == NULL)
3606 continue;
3607 // A quote and backtick must be followed by another one.
3608 if ((*p == '`' || *p == '\'') && vim_strchr(p, *p) == NULL)
3609 continue;
3610 return TRUE;
3611 }
3612 }
3613 return FALSE;
3614}
3615#endif
3616
3617/*
3618 * Generic wildcard expansion code.
3619 *
3620 * Characters in "pat" that should not be expanded must be preceded with a
3621 * backslash. E.g., "/path\ with\ spaces/my\*star*"
3622 *
3623 * Return FAIL when no single file was found. In this case "num_file" is not
3624 * set, and "file" may contain an error message.
3625 * Return OK when some files found. "num_file" is set to the number of
3626 * matches, "file" to the array of matches. Call FreeWild() later.
3627 */
3628 int
3629gen_expand_wildcards(
3630 int num_pat, // number of input patterns
3631 char_u **pat, // array of input patterns
3632 int *num_file, // resulting number of files
3633 char_u ***file, // array of resulting files
3634 int flags) // EW_* flags
3635{
3636 int i;
3637 garray_T ga;
3638 char_u *p;
3639 static int recursive = FALSE;
3640 int add_pat;
3641 int retval = OK;
3642#if defined(FEAT_SEARCHPATH)
3643 int did_expand_in_path = FALSE;
3644#endif
3645
3646 /*
3647 * expand_env() is called to expand things like "~user". If this fails,
3648 * it calls ExpandOne(), which brings us back here. In this case, always
3649 * call the machine specific expansion function, if possible. Otherwise,
3650 * return FAIL.
3651 */
3652 if (recursive)
3653#ifdef SPECIAL_WILDCHAR
3654 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
3655#else
3656 return FAIL;
3657#endif
3658
3659#ifdef SPECIAL_WILDCHAR
3660 /*
3661 * If there are any special wildcard characters which we cannot handle
3662 * here, call machine specific function for all the expansion. This
3663 * avoids starting the shell for each argument separately.
3664 * For `=expr` do use the internal function.
3665 */
3666 for (i = 0; i < num_pat; i++)
3667 {
3668 if (has_special_wildchar(pat[i])
3669# ifdef VIM_BACKTICK
3670 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
3671# endif
3672 )
3673 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
3674 }
3675#endif
3676
3677 recursive = TRUE;
3678
3679 /*
3680 * The matching file names are stored in a growarray. Init it empty.
3681 */
3682 ga_init2(&ga, (int)sizeof(char_u *), 30);
3683
3684 for (i = 0; i < num_pat; ++i)
3685 {
3686 add_pat = -1;
3687 p = pat[i];
3688
3689#ifdef VIM_BACKTICK
3690 if (vim_backtick(p))
3691 {
3692 add_pat = expand_backtick(&ga, p, flags);
3693 if (add_pat == -1)
3694 retval = FAIL;
3695 }
3696 else
3697#endif
3698 {
3699 /*
3700 * First expand environment variables, "~/" and "~user/".
3701 */
3702 if ((has_env_var(p) && !(flags & EW_NOTENV)) || *p == '~')
3703 {
3704 p = expand_env_save_opt(p, TRUE);
3705 if (p == NULL)
3706 p = pat[i];
3707#ifdef UNIX
3708 /*
3709 * On Unix, if expand_env() can't expand an environment
3710 * variable, use the shell to do that. Discard previously
3711 * found file names and start all over again.
3712 */
3713 else if (has_env_var(p) || *p == '~')
3714 {
3715 vim_free(p);
3716 ga_clear_strings(&ga);
3717 i = mch_expand_wildcards(num_pat, pat, num_file, file,
3718 flags|EW_KEEPDOLLAR);
3719 recursive = FALSE;
3720 return i;
3721 }
3722#endif
3723 }
3724
3725 /*
3726 * If there are wildcards: Expand file names and add each match to
3727 * the list. If there is no match, and EW_NOTFOUND is given, add
3728 * the pattern.
3729 * If there are no wildcards: Add the file name if it exists or
3730 * when EW_NOTFOUND is given.
3731 */
3732 if (mch_has_exp_wildcard(p))
3733 {
3734#if defined(FEAT_SEARCHPATH)
3735 if ((flags & EW_PATH)
3736 && !mch_isFullName(p)
3737 && !(p[0] == '.'
3738 && (vim_ispathsep(p[1])
3739 || (p[1] == '.' && vim_ispathsep(p[2]))))
3740 )
3741 {
3742 // :find completion where 'path' is used.
3743 // Recursiveness is OK here.
3744 recursive = FALSE;
3745 add_pat = expand_in_path(&ga, p, flags);
3746 recursive = TRUE;
3747 did_expand_in_path = TRUE;
3748 }
3749 else
3750#endif
3751 add_pat = mch_expandpath(&ga, p, flags);
3752 }
3753 }
3754
3755 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
3756 {
3757 char_u *t = backslash_halve_save(p);
3758
3759 // When EW_NOTFOUND is used, always add files and dirs. Makes
3760 // "vim c:/" work.
3761 if (flags & EW_NOTFOUND)
3762 addfile(&ga, t, flags | EW_DIR | EW_FILE);
3763 else
3764 addfile(&ga, t, flags);
3765
3766 if (t != p)
3767 vim_free(t);
3768 }
3769
3770#if defined(FEAT_SEARCHPATH)
3771 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
3772 uniquefy_paths(&ga, p);
3773#endif
3774 if (p != pat[i])
3775 vim_free(p);
3776 }
3777
3778 *num_file = ga.ga_len;
3779 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
3780
3781 recursive = FALSE;
3782
3783 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
3784}
3785
3786/*
3787 * Add a file to a file list. Accepted flags:
3788 * EW_DIR add directories
3789 * EW_FILE add files
3790 * EW_EXEC add executable files
3791 * EW_NOTFOUND add even when it doesn't exist
3792 * EW_ADDSLASH add slash after directory name
3793 * EW_ALLLINKS add symlink also when the referred file does not exist
3794 */
3795 void
3796addfile(
3797 garray_T *gap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003798 char_u *f, // filename
Bram Moolenaar26262f82019-09-04 20:59:15 +02003799 int flags)
3800{
3801 char_u *p;
3802 int isdir;
3803 stat_T sb;
3804
3805 // if the file/dir/link doesn't exist, may not add it
3806 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
3807 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
3808 return;
3809
3810#ifdef FNAME_ILLEGAL
3811 // if the file/dir contains illegal characters, don't add it
3812 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
3813 return;
3814#endif
3815
3816 isdir = mch_isdir(f);
3817 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
3818 return;
3819
3820 // If the file isn't executable, may not add it. Do accept directories.
3821 // When invoked from expand_shellcmd() do not use $PATH.
3822 if (!isdir && (flags & EW_EXEC)
3823 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
3824 return;
3825
3826 // Make room for another item in the file list.
3827 if (ga_grow(gap, 1) == FAIL)
3828 return;
3829
3830 p = alloc(STRLEN(f) + 1 + isdir);
3831 if (p == NULL)
3832 return;
3833
3834 STRCPY(p, f);
3835#ifdef BACKSLASH_IN_FILENAME
3836 slash_adjust(p);
3837#endif
3838 /*
3839 * Append a slash or backslash after directory names if none is present.
3840 */
3841#ifndef DONT_ADD_PATHSEP_TO_DIR
3842 if (isdir && (flags & EW_ADDSLASH))
3843 add_pathsep(p);
3844#endif
3845 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
3846}
3847
3848/*
3849 * Free the list of files returned by expand_wildcards() or other expansion
3850 * functions.
3851 */
3852 void
3853FreeWild(int count, char_u **files)
3854{
3855 if (count <= 0 || files == NULL)
3856 return;
3857 while (count--)
3858 vim_free(files[count]);
3859 vim_free(files);
3860}
3861
3862/*
3863 * Compare path "p[]" to "q[]".
3864 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]"
3865 * Return value like strcmp(p, q), but consider path separators.
3866 */
3867 int
3868pathcmp(const char *p, const char *q, int maxlen)
3869{
3870 int i, j;
3871 int c1, c2;
3872 const char *s = NULL;
3873
3874 for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);)
3875 {
3876 c1 = PTR2CHAR((char_u *)p + i);
3877 c2 = PTR2CHAR((char_u *)q + j);
3878
3879 // End of "p": check if "q" also ends or just has a slash.
3880 if (c1 == NUL)
3881 {
3882 if (c2 == NUL) // full match
3883 return 0;
3884 s = q;
3885 i = j;
3886 break;
3887 }
3888
3889 // End of "q": check if "p" just has a slash.
3890 if (c2 == NUL)
3891 {
3892 s = p;
3893 break;
3894 }
3895
3896 if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2)
3897#ifdef BACKSLASH_IN_FILENAME
3898 // consider '/' and '\\' to be equal
3899 && !((c1 == '/' && c2 == '\\')
3900 || (c1 == '\\' && c2 == '/'))
3901#endif
3902 )
3903 {
3904 if (vim_ispathsep(c1))
3905 return -1;
3906 if (vim_ispathsep(c2))
3907 return 1;
3908 return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2)
3909 : c1 - c2; // no match
3910 }
3911
Bram Moolenaar1614a142019-10-06 22:00:13 +02003912 i += mb_ptr2len((char_u *)p + i);
3913 j += mb_ptr2len((char_u *)q + j);
Bram Moolenaar26262f82019-09-04 20:59:15 +02003914 }
3915 if (s == NULL) // "i" or "j" ran into "maxlen"
3916 return 0;
3917
3918 c1 = PTR2CHAR((char_u *)s + i);
Bram Moolenaar1614a142019-10-06 22:00:13 +02003919 c2 = PTR2CHAR((char_u *)s + i + mb_ptr2len((char_u *)s + i));
Bram Moolenaar26262f82019-09-04 20:59:15 +02003920 // ignore a trailing slash, but not "//" or ":/"
3921 if (c2 == NUL
3922 && i > 0
3923 && !after_pathsep((char_u *)s, (char_u *)s + i)
3924#ifdef BACKSLASH_IN_FILENAME
3925 && (c1 == '/' || c1 == '\\')
3926#else
3927 && c1 == '/'
3928#endif
3929 )
3930 return 0; // match with trailing slash
3931 if (s == q)
3932 return -1; // no match
3933 return 1;
3934}
3935
3936/*
3937 * Return TRUE if "name" is a full (absolute) path name or URL.
3938 */
3939 int
3940vim_isAbsName(char_u *name)
3941{
3942 return (path_with_url(name) != 0 || mch_isFullName(name));
3943}
3944
3945/*
3946 * Get absolute file name into buffer "buf[len]".
3947 *
3948 * return FAIL for failure, OK otherwise
3949 */
3950 int
3951vim_FullName(
3952 char_u *fname,
3953 char_u *buf,
3954 int len,
3955 int force) // force expansion even when already absolute
3956{
3957 int retval = OK;
3958 int url;
3959
3960 *buf = NUL;
3961 if (fname == NULL)
3962 return FAIL;
3963
3964 url = path_with_url(fname);
3965 if (!url)
3966 retval = mch_FullName(fname, buf, len, force);
3967 if (url || retval == FAIL)
3968 {
3969 // something failed; use the file name (truncate when too long)
3970 vim_strncpy(buf, fname, len - 1);
3971 }
3972#if defined(MSWIN)
3973 slash_adjust(buf);
3974#endif
3975 return retval;
3976}