blob: 90d04cfcf1f73e40cc488e64b5d615c1741a0dbd [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 Moolenaar71ccd032020-06-12 22:59:11 +0200392 *fnamep = vim_strnsave(*fnamep, 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 {
Bram Moolenaar0e390f42020-06-10 13:12:28 +0200419 if ((c == '.' || c == '~') && **fnamep == '~')
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200420 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 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200658 pat = vim_strnsave(s, p - s);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200659 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 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200666 sub = vim_strnsave(s, p - s);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200667 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 Moolenaar71ccd032020-06-12 22:59:11 +02001299 updir = vim_strnsave(dir, p - dir);
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001300 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)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001597 s = vim_strnsave(start, len);
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001598 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
Bram Moolenaar0e390f42020-06-10 13:12:28 +02002400 // Do not add directory separator into dst, because dst is
2401 // expected to just return the directory name without the
2402 // directory separator '/'.
Bram Moolenaar26262f82019-09-04 20:59:15 +02002403 break;
2404 }
2405 if (p == homedir_env)
2406 break;
2407 p = homedir_env;
2408 len = envlen;
2409 }
2410
2411 // if (!one) skip to separator: space or comma
2412 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
2413 *dst++ = *src++;
2414 // skip separator
2415 while ((*src == ' ' || *src == ',') && --dstlen > 0)
2416 *dst++ = *src++;
2417 }
2418 // if (dstlen == 0) out of space, what to do???
2419
2420 *dst = NUL;
2421
2422 if (homedir_env != homedir_env_orig)
2423 vim_free(homedir_env);
2424}
2425
2426/*
2427 * Like home_replace, store the replaced string in allocated memory.
2428 * When something fails, NULL is returned.
2429 */
2430 char_u *
2431home_replace_save(
2432 buf_T *buf, // when not NULL, check for help files
2433 char_u *src) // input file name
2434{
2435 char_u *dst;
2436 unsigned len;
2437
2438 len = 3; // space for "~/" and trailing NUL
2439 if (src != NULL) // just in case
2440 len += (unsigned)STRLEN(src);
2441 dst = alloc(len);
2442 if (dst != NULL)
2443 home_replace(buf, src, dst, len, TRUE);
2444 return dst;
2445}
2446
2447/*
2448 * Compare two file names and return:
2449 * FPC_SAME if they both exist and are the same file.
2450 * FPC_SAMEX if they both don't exist and have the same file name.
2451 * FPC_DIFF if they both exist and are different files.
2452 * FPC_NOTX if they both don't exist.
2453 * FPC_DIFFX if one of them doesn't exist.
2454 * For the first name environment variables are expanded if "expandenv" is
2455 * TRUE.
2456 */
2457 int
2458fullpathcmp(
2459 char_u *s1,
2460 char_u *s2,
2461 int checkname, // when both don't exist, check file names
2462 int expandenv)
2463{
2464#ifdef UNIX
2465 char_u exp1[MAXPATHL];
2466 char_u full1[MAXPATHL];
2467 char_u full2[MAXPATHL];
2468 stat_T st1, st2;
2469 int r1, r2;
2470
2471 if (expandenv)
2472 expand_env(s1, exp1, MAXPATHL);
2473 else
2474 vim_strncpy(exp1, s1, MAXPATHL - 1);
2475 r1 = mch_stat((char *)exp1, &st1);
2476 r2 = mch_stat((char *)s2, &st2);
2477 if (r1 != 0 && r2 != 0)
2478 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002479 // if mch_stat() doesn't work, may compare the names
Bram Moolenaar26262f82019-09-04 20:59:15 +02002480 if (checkname)
2481 {
2482 if (fnamecmp(exp1, s2) == 0)
2483 return FPC_SAMEX;
2484 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
2485 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
2486 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
2487 return FPC_SAMEX;
2488 }
2489 return FPC_NOTX;
2490 }
2491 if (r1 != 0 || r2 != 0)
2492 return FPC_DIFFX;
2493 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
2494 return FPC_SAME;
2495 return FPC_DIFF;
2496#else
2497 char_u *exp1; // expanded s1
2498 char_u *full1; // full path of s1
2499 char_u *full2; // full path of s2
2500 int retval = FPC_DIFF;
2501 int r1, r2;
2502
2503 // allocate one buffer to store three paths (alloc()/free() is slow!)
2504 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
2505 {
2506 full1 = exp1 + MAXPATHL;
2507 full2 = full1 + MAXPATHL;
2508
2509 if (expandenv)
2510 expand_env(s1, exp1, MAXPATHL);
2511 else
2512 vim_strncpy(exp1, s1, MAXPATHL - 1);
2513 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
2514 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
2515
2516 // If vim_FullName() fails, the file probably doesn't exist.
2517 if (r1 != OK && r2 != OK)
2518 {
2519 if (checkname && fnamecmp(exp1, s2) == 0)
2520 retval = FPC_SAMEX;
2521 else
2522 retval = FPC_NOTX;
2523 }
2524 else if (r1 != OK || r2 != OK)
2525 retval = FPC_DIFFX;
2526 else if (fnamecmp(full1, full2))
2527 retval = FPC_DIFF;
2528 else
2529 retval = FPC_SAME;
2530 vim_free(exp1);
2531 }
2532 return retval;
2533#endif
2534}
2535
2536/*
2537 * Get the tail of a path: the file name.
2538 * When the path ends in a path separator the tail is the NUL after it.
2539 * Fail safe: never returns NULL.
2540 */
2541 char_u *
2542gettail(char_u *fname)
2543{
2544 char_u *p1, *p2;
2545
2546 if (fname == NULL)
2547 return (char_u *)"";
2548 for (p1 = p2 = get_past_head(fname); *p2; ) // find last part of path
2549 {
2550 if (vim_ispathsep_nocolon(*p2))
2551 p1 = p2 + 1;
2552 MB_PTR_ADV(p2);
2553 }
2554 return p1;
2555}
2556
2557/*
2558 * Get pointer to tail of "fname", including path separators. Putting a NUL
2559 * here leaves the directory name. Takes care of "c:/" and "//".
2560 * Always returns a valid pointer.
2561 */
2562 char_u *
2563gettail_sep(char_u *fname)
2564{
2565 char_u *p;
2566 char_u *t;
2567
2568 p = get_past_head(fname); // don't remove the '/' from "c:/file"
2569 t = gettail(fname);
2570 while (t > p && after_pathsep(fname, t))
2571 --t;
2572#ifdef VMS
2573 // path separator is part of the path
2574 ++t;
2575#endif
2576 return t;
2577}
2578
2579/*
2580 * get the next path component (just after the next path separator).
2581 */
2582 char_u *
2583getnextcomp(char_u *fname)
2584{
2585 while (*fname && !vim_ispathsep(*fname))
2586 MB_PTR_ADV(fname);
2587 if (*fname)
2588 ++fname;
2589 return fname;
2590}
2591
2592/*
2593 * Get a pointer to one character past the head of a path name.
2594 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
2595 * If there is no head, path is returned.
2596 */
2597 char_u *
2598get_past_head(char_u *path)
2599{
2600 char_u *retval;
2601
2602#if defined(MSWIN)
2603 // may skip "c:"
2604 if (isalpha(path[0]) && path[1] == ':')
2605 retval = path + 2;
2606 else
2607 retval = path;
2608#else
2609# if defined(AMIGA)
2610 // may skip "label:"
2611 retval = vim_strchr(path, ':');
2612 if (retval == NULL)
2613 retval = path;
2614# else // Unix
2615 retval = path;
2616# endif
2617#endif
2618
2619 while (vim_ispathsep(*retval))
2620 ++retval;
2621
2622 return retval;
2623}
2624
2625/*
2626 * Return TRUE if 'c' is a path separator.
2627 * Note that for MS-Windows this includes the colon.
2628 */
2629 int
2630vim_ispathsep(int c)
2631{
2632#ifdef UNIX
2633 return (c == '/'); // UNIX has ':' inside file names
2634#else
2635# ifdef BACKSLASH_IN_FILENAME
2636 return (c == ':' || c == '/' || c == '\\');
2637# else
2638# ifdef VMS
2639 // server"user passwd"::device:[full.path.name]fname.extension;version"
2640 return (c == ':' || c == '[' || c == ']' || c == '/'
2641 || c == '<' || c == '>' || c == '"' );
2642# else
2643 return (c == ':' || c == '/');
2644# endif // VMS
2645# endif
2646#endif
2647}
2648
2649/*
2650 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
2651 */
2652 int
2653vim_ispathsep_nocolon(int c)
2654{
2655 return vim_ispathsep(c)
2656#ifdef BACKSLASH_IN_FILENAME
2657 && c != ':'
2658#endif
2659 ;
2660}
2661
2662/*
2663 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
2664 * It's done in-place.
2665 */
2666 void
2667shorten_dir(char_u *str)
2668{
2669 char_u *tail, *s, *d;
2670 int skip = FALSE;
2671
2672 tail = gettail(str);
2673 d = str;
2674 for (s = str; ; ++s)
2675 {
2676 if (s >= tail) // copy the whole tail
2677 {
2678 *d++ = *s;
2679 if (*s == NUL)
2680 break;
2681 }
2682 else if (vim_ispathsep(*s)) // copy '/' and next char
2683 {
2684 *d++ = *s;
2685 skip = FALSE;
2686 }
2687 else if (!skip)
2688 {
2689 *d++ = *s; // copy next char
2690 if (*s != '~' && *s != '.') // and leading "~" and "."
2691 skip = TRUE;
2692 if (has_mbyte)
2693 {
2694 int l = mb_ptr2len(s);
2695
2696 while (--l > 0)
2697 *d++ = *++s;
2698 }
2699 }
2700 }
2701}
2702
2703/*
2704 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
2705 * Also returns TRUE if there is no directory name.
2706 * "fname" must be writable!.
2707 */
2708 int
2709dir_of_file_exists(char_u *fname)
2710{
2711 char_u *p;
2712 int c;
2713 int retval;
2714
2715 p = gettail_sep(fname);
2716 if (p == fname)
2717 return TRUE;
2718 c = *p;
2719 *p = NUL;
2720 retval = mch_isdir(fname);
2721 *p = c;
2722 return retval;
2723}
2724
2725/*
2726 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
2727 * and deal with 'fileignorecase'.
2728 */
2729 int
2730vim_fnamecmp(char_u *x, char_u *y)
2731{
2732#ifdef BACKSLASH_IN_FILENAME
2733 return vim_fnamencmp(x, y, MAXPATHL);
2734#else
2735 if (p_fic)
2736 return MB_STRICMP(x, y);
2737 return STRCMP(x, y);
2738#endif
2739}
2740
2741 int
2742vim_fnamencmp(char_u *x, char_u *y, size_t len)
2743{
2744#ifdef BACKSLASH_IN_FILENAME
2745 char_u *px = x;
2746 char_u *py = y;
2747 int cx = NUL;
2748 int cy = NUL;
2749
2750 while (len > 0)
2751 {
2752 cx = PTR2CHAR(px);
2753 cy = PTR2CHAR(py);
2754 if (cx == NUL || cy == NUL
2755 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
2756 && !(cx == '/' && cy == '\\')
2757 && !(cx == '\\' && cy == '/')))
2758 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002759 len -= mb_ptr2len(px);
2760 px += mb_ptr2len(px);
2761 py += mb_ptr2len(py);
Bram Moolenaar26262f82019-09-04 20:59:15 +02002762 }
2763 if (len == 0)
2764 return 0;
2765 return (cx - cy);
2766#else
2767 if (p_fic)
2768 return MB_STRNICMP(x, y, len);
2769 return STRNCMP(x, y, len);
2770#endif
2771}
2772
2773/*
2774 * Concatenate file names fname1 and fname2 into allocated memory.
2775 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
2776 */
2777 char_u *
2778concat_fnames(char_u *fname1, char_u *fname2, int sep)
2779{
2780 char_u *dest;
2781
2782 dest = alloc(STRLEN(fname1) + STRLEN(fname2) + 3);
2783 if (dest != NULL)
2784 {
2785 STRCPY(dest, fname1);
2786 if (sep)
2787 add_pathsep(dest);
2788 STRCAT(dest, fname2);
2789 }
2790 return dest;
2791}
2792
2793/*
2794 * Add a path separator to a file name, unless it already ends in a path
2795 * separator.
2796 */
2797 void
2798add_pathsep(char_u *p)
2799{
2800 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
2801 STRCAT(p, PATHSEPSTR);
2802}
2803
2804/*
2805 * FullName_save - Make an allocated copy of a full file name.
2806 * Returns NULL when out of memory.
2807 */
2808 char_u *
2809FullName_save(
2810 char_u *fname,
2811 int force) // force expansion, even when it already looks
2812 // like a full path name
2813{
2814 char_u *buf;
2815 char_u *new_fname = NULL;
2816
2817 if (fname == NULL)
2818 return NULL;
2819
2820 buf = alloc(MAXPATHL);
2821 if (buf != NULL)
2822 {
2823 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
2824 new_fname = vim_strsave(buf);
2825 else
2826 new_fname = vim_strsave(fname);
2827 vim_free(buf);
2828 }
2829 return new_fname;
2830}
2831
2832/*
2833 * return TRUE if "fname" exists.
2834 */
2835 int
2836vim_fexists(char_u *fname)
2837{
2838 stat_T st;
2839
2840 if (mch_stat((char *)fname, &st))
2841 return FALSE;
2842 return TRUE;
2843}
2844
2845/*
2846 * Invoke expand_wildcards() for one pattern.
2847 * Expand items like "%:h" before the expansion.
2848 * Returns OK or FAIL.
2849 */
2850 int
2851expand_wildcards_eval(
2852 char_u **pat, // pointer to input pattern
2853 int *num_file, // resulting number of files
2854 char_u ***file, // array of resulting files
2855 int flags) // EW_DIR, etc.
2856{
2857 int ret = FAIL;
2858 char_u *eval_pat = NULL;
2859 char_u *exp_pat = *pat;
2860 char *ignored_msg;
2861 int usedlen;
2862
2863 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
2864 {
2865 ++emsg_off;
2866 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
2867 NULL, &ignored_msg, NULL);
2868 --emsg_off;
2869 if (eval_pat != NULL)
2870 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
2871 }
2872
2873 if (exp_pat != NULL)
2874 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
2875
2876 if (eval_pat != NULL)
2877 {
2878 vim_free(exp_pat);
2879 vim_free(eval_pat);
2880 }
2881
2882 return ret;
2883}
2884
2885/*
2886 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
2887 * 'wildignore'.
2888 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
2889 */
2890 int
2891expand_wildcards(
2892 int num_pat, // number of input patterns
2893 char_u **pat, // array of input patterns
2894 int *num_files, // resulting number of files
2895 char_u ***files, // array of resulting files
2896 int flags) // EW_DIR, etc.
2897{
2898 int retval;
2899 int i, j;
2900 char_u *p;
2901 int non_suf_match; // number without matching suffix
2902
2903 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
2904
2905 // When keeping all matches, return here
2906 if ((flags & EW_KEEPALL) || retval == FAIL)
2907 return retval;
2908
2909#ifdef FEAT_WILDIGN
2910 /*
2911 * Remove names that match 'wildignore'.
2912 */
2913 if (*p_wig)
2914 {
2915 char_u *ffname;
2916
2917 // check all files in (*files)[]
2918 for (i = 0; i < *num_files; ++i)
2919 {
2920 ffname = FullName_save((*files)[i], FALSE);
2921 if (ffname == NULL) // out of memory
2922 break;
2923# ifdef VMS
2924 vms_remove_version(ffname);
2925# endif
2926 if (match_file_list(p_wig, (*files)[i], ffname))
2927 {
2928 // remove this matching file from the list
2929 vim_free((*files)[i]);
2930 for (j = i; j + 1 < *num_files; ++j)
2931 (*files)[j] = (*files)[j + 1];
2932 --*num_files;
2933 --i;
2934 }
2935 vim_free(ffname);
2936 }
2937
2938 // If the number of matches is now zero, we fail.
2939 if (*num_files == 0)
2940 {
2941 VIM_CLEAR(*files);
2942 return FAIL;
2943 }
2944 }
2945#endif
2946
2947 /*
2948 * Move the names where 'suffixes' match to the end.
2949 */
2950 if (*num_files > 1)
2951 {
2952 non_suf_match = 0;
2953 for (i = 0; i < *num_files; ++i)
2954 {
2955 if (!match_suffix((*files)[i]))
2956 {
2957 /*
2958 * Move the name without matching suffix to the front
2959 * of the list.
2960 */
2961 p = (*files)[i];
2962 for (j = i; j > non_suf_match; --j)
2963 (*files)[j] = (*files)[j - 1];
2964 (*files)[non_suf_match++] = p;
2965 }
2966 }
2967 }
2968
2969 return retval;
2970}
2971
2972/*
2973 * Return TRUE if "fname" matches with an entry in 'suffixes'.
2974 */
2975 int
2976match_suffix(char_u *fname)
2977{
2978 int fnamelen, setsuflen;
2979 char_u *setsuf;
2980#define MAXSUFLEN 30 // maximum length of a file suffix
2981 char_u suf_buf[MAXSUFLEN];
2982
2983 fnamelen = (int)STRLEN(fname);
2984 setsuflen = 0;
2985 for (setsuf = p_su; *setsuf; )
2986 {
2987 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
2988 if (setsuflen == 0)
2989 {
2990 char_u *tail = gettail(fname);
2991
2992 // empty entry: match name without a '.'
2993 if (vim_strchr(tail, '.') == NULL)
2994 {
2995 setsuflen = 1;
2996 break;
2997 }
2998 }
2999 else
3000 {
3001 if (fnamelen >= setsuflen
3002 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
3003 (size_t)setsuflen) == 0)
3004 break;
3005 setsuflen = 0;
3006 }
3007 }
3008 return (setsuflen != 0);
3009}
3010
3011#ifdef VIM_BACKTICK
3012
3013/*
3014 * Return TRUE if we can expand this backtick thing here.
3015 */
3016 static int
3017vim_backtick(char_u *p)
3018{
3019 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
3020}
3021
3022/*
3023 * Expand an item in `backticks` by executing it as a command.
3024 * Currently only works when pat[] starts and ends with a `.
3025 * Returns number of file names found, -1 if an error is encountered.
3026 */
3027 static int
3028expand_backtick(
3029 garray_T *gap,
3030 char_u *pat,
3031 int flags) // EW_* flags
3032{
3033 char_u *p;
3034 char_u *cmd;
3035 char_u *buffer;
3036 int cnt = 0;
3037 int i;
3038
3039 // Create the command: lop off the backticks.
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003040 cmd = vim_strnsave(pat + 1, STRLEN(pat) - 2);
Bram Moolenaar26262f82019-09-04 20:59:15 +02003041 if (cmd == NULL)
3042 return -1;
3043
3044#ifdef FEAT_EVAL
3045 if (*cmd == '=') // `={expr}`: Expand expression
3046 buffer = eval_to_string(cmd + 1, &p, TRUE);
3047 else
3048#endif
3049 buffer = get_cmd_output(cmd, NULL,
3050 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
3051 vim_free(cmd);
3052 if (buffer == NULL)
3053 return -1;
3054
3055 cmd = buffer;
3056 while (*cmd != NUL)
3057 {
3058 cmd = skipwhite(cmd); // skip over white space
3059 p = cmd;
3060 while (*p != NUL && *p != '\r' && *p != '\n') // skip over entry
3061 ++p;
3062 // add an entry if it is not empty
3063 if (p > cmd)
3064 {
3065 i = *p;
3066 *p = NUL;
3067 addfile(gap, cmd, flags);
3068 *p = i;
3069 ++cnt;
3070 }
3071 cmd = p;
3072 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
3073 ++cmd;
3074 }
3075
3076 vim_free(buffer);
3077 return cnt;
3078}
3079#endif // VIM_BACKTICK
3080
3081#if defined(MSWIN)
3082/*
3083 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
3084 * it's shared between these systems.
3085 */
3086
3087/*
3088 * comparison function for qsort in dos_expandpath()
3089 */
3090 static int
3091pstrcmp(const void *a, const void *b)
3092{
3093 return (pathcmp(*(char **)a, *(char **)b, -1));
3094}
3095
3096/*
3097 * Recursively expand one path component into all matching files and/or
3098 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
3099 * Return the number of matches found.
3100 * "path" has backslashes before chars that are not to be expanded, starting
3101 * at "path[wildoff]".
3102 * Return the number of matches found.
3103 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
3104 */
3105 static int
3106dos_expandpath(
3107 garray_T *gap,
3108 char_u *path,
3109 int wildoff,
3110 int flags, // EW_* flags
3111 int didstar) // expanded "**" once already
3112{
3113 char_u *buf;
3114 char_u *path_end;
3115 char_u *p, *s, *e;
3116 int start_len = gap->ga_len;
3117 char_u *pat;
3118 regmatch_T regmatch;
3119 int starts_with_dot;
3120 int matches;
3121 int len;
3122 int starstar = FALSE;
3123 static int stardepth = 0; // depth for "**" expansion
3124 HANDLE hFind = INVALID_HANDLE_VALUE;
3125 WIN32_FIND_DATAW wfb;
3126 WCHAR *wn = NULL; // UCS-2 name, NULL when not used.
3127 char_u *matchname;
3128 int ok;
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +02003129 char_u *p_alt;
Bram Moolenaar26262f82019-09-04 20:59:15 +02003130
3131 // Expanding "**" may take a long time, check for CTRL-C.
3132 if (stardepth > 0)
3133 {
3134 ui_breakcheck();
3135 if (got_int)
3136 return 0;
3137 }
3138
3139 // Make room for file name. When doing encoding conversion the actual
3140 // length may be quite a bit longer, thus use the maximum possible length.
3141 buf = alloc(MAXPATHL);
3142 if (buf == NULL)
3143 return 0;
3144
3145 /*
3146 * Find the first part in the path name that contains a wildcard or a ~1.
3147 * Copy it into buf, including the preceding characters.
3148 */
3149 p = buf;
3150 s = buf;
3151 e = NULL;
3152 path_end = path;
3153 while (*path_end != NUL)
3154 {
3155 // May ignore a wildcard that has a backslash before it; it will
3156 // be removed by rem_backslash() or file_pat_to_reg_pat() below.
3157 if (path_end >= path + wildoff && rem_backslash(path_end))
3158 *p++ = *path_end++;
3159 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
3160 {
3161 if (e != NULL)
3162 break;
3163 s = p + 1;
3164 }
3165 else if (path_end >= path + wildoff
3166 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
3167 e = p;
3168 if (has_mbyte)
3169 {
3170 len = (*mb_ptr2len)(path_end);
3171 STRNCPY(p, path_end, len);
3172 p += len;
3173 path_end += len;
3174 }
3175 else
3176 *p++ = *path_end++;
3177 }
3178 e = p;
3179 *e = NUL;
3180
3181 // now we have one wildcard component between s and e
3182 // Remove backslashes between "wildoff" and the start of the wildcard
3183 // component.
3184 for (p = buf + wildoff; p < s; ++p)
3185 if (rem_backslash(p))
3186 {
3187 STRMOVE(p, p + 1);
3188 --e;
3189 --s;
3190 }
3191
3192 // Check for "**" between "s" and "e".
3193 for (p = s; p < e; ++p)
3194 if (p[0] == '*' && p[1] == '*')
3195 starstar = TRUE;
3196
3197 starts_with_dot = *s == '.';
3198 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
3199 if (pat == NULL)
3200 {
3201 vim_free(buf);
3202 return 0;
3203 }
3204
3205 // compile the regexp into a program
3206 if (flags & (EW_NOERROR | EW_NOTWILD))
3207 ++emsg_silent;
3208 regmatch.rm_ic = TRUE; // Always ignore case
3209 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
3210 if (flags & (EW_NOERROR | EW_NOTWILD))
3211 --emsg_silent;
3212 vim_free(pat);
3213
3214 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
3215 {
3216 vim_free(buf);
3217 return 0;
3218 }
3219
3220 // remember the pattern or file name being looked for
3221 matchname = vim_strsave(s);
3222
3223 // If "**" is by itself, this is the first time we encounter it and more
3224 // is following then find matches without any directory.
3225 if (!didstar && stardepth < 100 && starstar && e - s == 2
3226 && *path_end == '/')
3227 {
3228 STRCPY(s, path_end + 1);
3229 ++stardepth;
3230 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
3231 --stardepth;
3232 }
3233
3234 // Scan all files in the directory with "dir/ *.*"
3235 STRCPY(s, "*.*");
3236 wn = enc_to_utf16(buf, NULL);
3237 if (wn != NULL)
3238 hFind = FindFirstFileW(wn, &wfb);
3239 ok = (hFind != INVALID_HANDLE_VALUE);
3240
3241 while (ok)
3242 {
3243 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +02003244
Bram Moolenaar26262f82019-09-04 20:59:15 +02003245 if (p == NULL)
3246 break; // out of memory
3247
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +02003248 if (*wfb.cAlternateFileName == NUL)
Bram Moolenaar40655d52020-04-06 23:49:50 +02003249 p_alt = NULL;
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +02003250 else
3251 p_alt = utf16_to_enc(wfb.cAlternateFileName, NULL);
3252
Bram Moolenaar26262f82019-09-04 20:59:15 +02003253 // Ignore entries starting with a dot, unless when asked for. Accept
3254 // all entries found with "matchname".
3255 if ((p[0] != '.' || starts_with_dot
3256 || ((flags & EW_DODOT)
3257 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
3258 && (matchname == NULL
3259 || (regmatch.regprog != NULL
Bram Moolenaar40655d52020-04-06 23:49:50 +02003260 && (vim_regexec(&regmatch, p, (colnr_T)0)
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +02003261 || (p_alt != NULL
Bram Moolenaar40655d52020-04-06 23:49:50 +02003262 && vim_regexec(&regmatch, p_alt, (colnr_T)0))))
Bram Moolenaar26262f82019-09-04 20:59:15 +02003263 || ((flags & EW_NOTWILD)
3264 && fnamencmp(path + (s - buf), p, e - s) == 0)))
3265 {
3266 STRCPY(s, p);
3267 len = (int)STRLEN(buf);
3268
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +02003269 if (starstar && stardepth < 100
3270 && (wfb.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
Bram Moolenaar26262f82019-09-04 20:59:15 +02003271 {
3272 // For "**" in the pattern first go deeper in the tree to
3273 // find matches.
3274 STRCPY(buf + len, "/**");
3275 STRCPY(buf + len + 3, path_end);
3276 ++stardepth;
3277 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
3278 --stardepth;
3279 }
3280
3281 STRCPY(buf + len, path_end);
3282 if (mch_has_exp_wildcard(path_end))
3283 {
3284 // need to expand another component of the path
3285 // remove backslashes for the remaining components only
3286 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
3287 }
3288 else
3289 {
3290 // no more wildcards, check if there is a match
3291 // remove backslashes for the remaining components only
3292 if (*path_end != 0)
3293 backslash_halve(buf + len + 1);
3294 if (mch_getperm(buf) >= 0) // add existing file
3295 addfile(gap, buf, flags);
3296 }
3297 }
3298
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +02003299 vim_free(p_alt);
Bram Moolenaar26262f82019-09-04 20:59:15 +02003300 vim_free(p);
3301 ok = FindNextFileW(hFind, &wfb);
Bram Moolenaar26262f82019-09-04 20:59:15 +02003302 }
3303
3304 FindClose(hFind);
3305 vim_free(wn);
3306 vim_free(buf);
3307 vim_regfree(regmatch.regprog);
3308 vim_free(matchname);
3309
3310 matches = gap->ga_len - start_len;
3311 if (matches > 0)
3312 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
3313 sizeof(char_u *), pstrcmp);
3314 return matches;
3315}
3316
3317 int
3318mch_expandpath(
3319 garray_T *gap,
3320 char_u *path,
3321 int flags) // EW_* flags
3322{
3323 return dos_expandpath(gap, path, 0, flags, FALSE);
3324}
3325#endif // MSWIN
3326
3327#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
3328 || defined(PROTO)
3329/*
3330 * Unix style wildcard expansion code.
3331 * It's here because it's used both for Unix and Mac.
3332 */
3333 static int
3334pstrcmp(const void *a, const void *b)
3335{
3336 return (pathcmp(*(char **)a, *(char **)b, -1));
3337}
3338
3339/*
3340 * Recursively expand one path component into all matching files and/or
3341 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
3342 * "path" has backslashes before chars that are not to be expanded, starting
3343 * at "path + wildoff".
3344 * Return the number of matches found.
3345 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
3346 */
3347 int
3348unix_expandpath(
3349 garray_T *gap,
3350 char_u *path,
3351 int wildoff,
3352 int flags, // EW_* flags
3353 int didstar) // expanded "**" once already
3354{
3355 char_u *buf;
3356 char_u *path_end;
3357 char_u *p, *s, *e;
3358 int start_len = gap->ga_len;
3359 char_u *pat;
3360 regmatch_T regmatch;
3361 int starts_with_dot;
3362 int matches;
3363 int len;
3364 int starstar = FALSE;
3365 static int stardepth = 0; // depth for "**" expansion
3366
3367 DIR *dirp;
3368 struct dirent *dp;
3369
3370 // Expanding "**" may take a long time, check for CTRL-C.
3371 if (stardepth > 0)
3372 {
3373 ui_breakcheck();
3374 if (got_int)
3375 return 0;
3376 }
3377
3378 // make room for file name
3379 buf = alloc(STRLEN(path) + BASENAMELEN + 5);
3380 if (buf == NULL)
3381 return 0;
3382
3383 /*
3384 * Find the first part in the path name that contains a wildcard.
3385 * When EW_ICASE is set every letter is considered to be a wildcard.
3386 * Copy it into "buf", including the preceding characters.
3387 */
3388 p = buf;
3389 s = buf;
3390 e = NULL;
3391 path_end = path;
3392 while (*path_end != NUL)
3393 {
3394 // May ignore a wildcard that has a backslash before it; it will
3395 // be removed by rem_backslash() or file_pat_to_reg_pat() below.
3396 if (path_end >= path + wildoff && rem_backslash(path_end))
3397 *p++ = *path_end++;
3398 else if (*path_end == '/')
3399 {
3400 if (e != NULL)
3401 break;
3402 s = p + 1;
3403 }
3404 else if (path_end >= path + wildoff
3405 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
3406 || (!p_fic && (flags & EW_ICASE)
3407 && isalpha(PTR2CHAR(path_end)))))
3408 e = p;
3409 if (has_mbyte)
3410 {
3411 len = (*mb_ptr2len)(path_end);
3412 STRNCPY(p, path_end, len);
3413 p += len;
3414 path_end += len;
3415 }
3416 else
3417 *p++ = *path_end++;
3418 }
3419 e = p;
3420 *e = NUL;
3421
3422 // Now we have one wildcard component between "s" and "e".
3423 // Remove backslashes between "wildoff" and the start of the wildcard
3424 // component.
3425 for (p = buf + wildoff; p < s; ++p)
3426 if (rem_backslash(p))
3427 {
3428 STRMOVE(p, p + 1);
3429 --e;
3430 --s;
3431 }
3432
3433 // Check for "**" between "s" and "e".
3434 for (p = s; p < e; ++p)
3435 if (p[0] == '*' && p[1] == '*')
3436 starstar = TRUE;
3437
3438 // convert the file pattern to a regexp pattern
3439 starts_with_dot = *s == '.';
3440 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
3441 if (pat == NULL)
3442 {
3443 vim_free(buf);
3444 return 0;
3445 }
3446
3447 // compile the regexp into a program
3448 if (flags & EW_ICASE)
3449 regmatch.rm_ic = TRUE; // 'wildignorecase' set
3450 else
3451 regmatch.rm_ic = p_fic; // ignore case when 'fileignorecase' is set
3452 if (flags & (EW_NOERROR | EW_NOTWILD))
3453 ++emsg_silent;
3454 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
3455 if (flags & (EW_NOERROR | EW_NOTWILD))
3456 --emsg_silent;
3457 vim_free(pat);
3458
3459 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
3460 {
3461 vim_free(buf);
3462 return 0;
3463 }
3464
3465 // If "**" is by itself, this is the first time we encounter it and more
3466 // is following then find matches without any directory.
3467 if (!didstar && stardepth < 100 && starstar && e - s == 2
3468 && *path_end == '/')
3469 {
3470 STRCPY(s, path_end + 1);
3471 ++stardepth;
3472 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
3473 --stardepth;
3474 }
3475
3476 // open the directory for scanning
3477 *s = NUL;
3478 dirp = opendir(*buf == NUL ? "." : (char *)buf);
3479
3480 // Find all matching entries
3481 if (dirp != NULL)
3482 {
3483 for (;;)
3484 {
3485 dp = readdir(dirp);
3486 if (dp == NULL)
3487 break;
3488 if ((dp->d_name[0] != '.' || starts_with_dot
3489 || ((flags & EW_DODOT)
3490 && dp->d_name[1] != NUL
3491 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
3492 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
3493 (char_u *)dp->d_name, (colnr_T)0))
3494 || ((flags & EW_NOTWILD)
3495 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
3496 {
3497 STRCPY(s, dp->d_name);
3498 len = STRLEN(buf);
3499
3500 if (starstar && stardepth < 100)
3501 {
3502 // For "**" in the pattern first go deeper in the tree to
3503 // find matches.
3504 STRCPY(buf + len, "/**");
3505 STRCPY(buf + len + 3, path_end);
3506 ++stardepth;
3507 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
3508 --stardepth;
3509 }
3510
3511 STRCPY(buf + len, path_end);
3512 if (mch_has_exp_wildcard(path_end)) // handle more wildcards
3513 {
3514 // need to expand another component of the path
3515 // remove backslashes for the remaining components only
3516 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
3517 }
3518 else
3519 {
3520 stat_T sb;
3521
3522 // no more wildcards, check if there is a match
3523 // remove backslashes for the remaining components only
3524 if (*path_end != NUL)
3525 backslash_halve(buf + len + 1);
3526 // add existing file or symbolic link
3527 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
3528 : mch_getperm(buf) >= 0)
3529 {
3530#ifdef MACOS_CONVERT
3531 size_t precomp_len = STRLEN(buf)+1;
3532 char_u *precomp_buf =
3533 mac_precompose_path(buf, precomp_len, &precomp_len);
3534
3535 if (precomp_buf)
3536 {
3537 mch_memmove(buf, precomp_buf, precomp_len);
3538 vim_free(precomp_buf);
3539 }
3540#endif
3541 addfile(gap, buf, flags);
3542 }
3543 }
3544 }
3545 }
3546
3547 closedir(dirp);
3548 }
3549
3550 vim_free(buf);
3551 vim_regfree(regmatch.regprog);
3552
3553 matches = gap->ga_len - start_len;
3554 if (matches > 0)
3555 qsort(((char_u **)gap->ga_data) + start_len, matches,
3556 sizeof(char_u *), pstrcmp);
3557 return matches;
3558}
3559#endif
3560
3561/*
3562 * Return TRUE if "p" contains what looks like an environment variable.
3563 * Allowing for escaping.
3564 */
3565 static int
3566has_env_var(char_u *p)
3567{
3568 for ( ; *p; MB_PTR_ADV(p))
3569 {
3570 if (*p == '\\' && p[1] != NUL)
3571 ++p;
3572 else if (vim_strchr((char_u *)
3573#if defined(MSWIN)
3574 "$%"
3575#else
3576 "$"
3577#endif
3578 , *p) != NULL)
3579 return TRUE;
3580 }
3581 return FALSE;
3582}
3583
3584#ifdef SPECIAL_WILDCHAR
3585/*
3586 * Return TRUE if "p" contains a special wildcard character, one that Vim
3587 * cannot expand, requires using a shell.
3588 */
3589 static int
3590has_special_wildchar(char_u *p)
3591{
3592 for ( ; *p; MB_PTR_ADV(p))
3593 {
3594 // Disallow line break characters.
3595 if (*p == '\r' || *p == '\n')
3596 break;
3597 // Allow for escaping.
3598 if (*p == '\\' && p[1] != NUL && p[1] != '\r' && p[1] != '\n')
3599 ++p;
3600 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
3601 {
3602 // A { must be followed by a matching }.
3603 if (*p == '{' && vim_strchr(p, '}') == NULL)
3604 continue;
3605 // A quote and backtick must be followed by another one.
3606 if ((*p == '`' || *p == '\'') && vim_strchr(p, *p) == NULL)
3607 continue;
3608 return TRUE;
3609 }
3610 }
3611 return FALSE;
3612}
3613#endif
3614
3615/*
3616 * Generic wildcard expansion code.
3617 *
3618 * Characters in "pat" that should not be expanded must be preceded with a
3619 * backslash. E.g., "/path\ with\ spaces/my\*star*"
3620 *
3621 * Return FAIL when no single file was found. In this case "num_file" is not
3622 * set, and "file" may contain an error message.
3623 * Return OK when some files found. "num_file" is set to the number of
3624 * matches, "file" to the array of matches. Call FreeWild() later.
3625 */
3626 int
3627gen_expand_wildcards(
3628 int num_pat, // number of input patterns
3629 char_u **pat, // array of input patterns
3630 int *num_file, // resulting number of files
3631 char_u ***file, // array of resulting files
3632 int flags) // EW_* flags
3633{
3634 int i;
3635 garray_T ga;
3636 char_u *p;
3637 static int recursive = FALSE;
3638 int add_pat;
3639 int retval = OK;
3640#if defined(FEAT_SEARCHPATH)
3641 int did_expand_in_path = FALSE;
3642#endif
3643
3644 /*
3645 * expand_env() is called to expand things like "~user". If this fails,
3646 * it calls ExpandOne(), which brings us back here. In this case, always
3647 * call the machine specific expansion function, if possible. Otherwise,
3648 * return FAIL.
3649 */
3650 if (recursive)
3651#ifdef SPECIAL_WILDCHAR
3652 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
3653#else
3654 return FAIL;
3655#endif
3656
3657#ifdef SPECIAL_WILDCHAR
3658 /*
3659 * If there are any special wildcard characters which we cannot handle
3660 * here, call machine specific function for all the expansion. This
3661 * avoids starting the shell for each argument separately.
3662 * For `=expr` do use the internal function.
3663 */
3664 for (i = 0; i < num_pat; i++)
3665 {
3666 if (has_special_wildchar(pat[i])
3667# ifdef VIM_BACKTICK
3668 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
3669# endif
3670 )
3671 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
3672 }
3673#endif
3674
3675 recursive = TRUE;
3676
3677 /*
3678 * The matching file names are stored in a growarray. Init it empty.
3679 */
3680 ga_init2(&ga, (int)sizeof(char_u *), 30);
3681
3682 for (i = 0; i < num_pat; ++i)
3683 {
3684 add_pat = -1;
3685 p = pat[i];
3686
3687#ifdef VIM_BACKTICK
3688 if (vim_backtick(p))
3689 {
3690 add_pat = expand_backtick(&ga, p, flags);
3691 if (add_pat == -1)
3692 retval = FAIL;
3693 }
3694 else
3695#endif
3696 {
3697 /*
3698 * First expand environment variables, "~/" and "~user/".
3699 */
3700 if ((has_env_var(p) && !(flags & EW_NOTENV)) || *p == '~')
3701 {
3702 p = expand_env_save_opt(p, TRUE);
3703 if (p == NULL)
3704 p = pat[i];
3705#ifdef UNIX
3706 /*
3707 * On Unix, if expand_env() can't expand an environment
3708 * variable, use the shell to do that. Discard previously
3709 * found file names and start all over again.
3710 */
3711 else if (has_env_var(p) || *p == '~')
3712 {
3713 vim_free(p);
3714 ga_clear_strings(&ga);
3715 i = mch_expand_wildcards(num_pat, pat, num_file, file,
3716 flags|EW_KEEPDOLLAR);
3717 recursive = FALSE;
3718 return i;
3719 }
3720#endif
3721 }
3722
3723 /*
3724 * If there are wildcards: Expand file names and add each match to
3725 * the list. If there is no match, and EW_NOTFOUND is given, add
3726 * the pattern.
3727 * If there are no wildcards: Add the file name if it exists or
3728 * when EW_NOTFOUND is given.
3729 */
3730 if (mch_has_exp_wildcard(p))
3731 {
3732#if defined(FEAT_SEARCHPATH)
3733 if ((flags & EW_PATH)
3734 && !mch_isFullName(p)
3735 && !(p[0] == '.'
3736 && (vim_ispathsep(p[1])
3737 || (p[1] == '.' && vim_ispathsep(p[2]))))
3738 )
3739 {
3740 // :find completion where 'path' is used.
3741 // Recursiveness is OK here.
3742 recursive = FALSE;
3743 add_pat = expand_in_path(&ga, p, flags);
3744 recursive = TRUE;
3745 did_expand_in_path = TRUE;
3746 }
3747 else
3748#endif
3749 add_pat = mch_expandpath(&ga, p, flags);
3750 }
3751 }
3752
3753 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
3754 {
3755 char_u *t = backslash_halve_save(p);
3756
3757 // When EW_NOTFOUND is used, always add files and dirs. Makes
3758 // "vim c:/" work.
3759 if (flags & EW_NOTFOUND)
3760 addfile(&ga, t, flags | EW_DIR | EW_FILE);
3761 else
3762 addfile(&ga, t, flags);
3763
3764 if (t != p)
3765 vim_free(t);
3766 }
3767
3768#if defined(FEAT_SEARCHPATH)
3769 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
3770 uniquefy_paths(&ga, p);
3771#endif
3772 if (p != pat[i])
3773 vim_free(p);
3774 }
3775
3776 *num_file = ga.ga_len;
3777 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
3778
3779 recursive = FALSE;
3780
3781 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
3782}
3783
3784/*
3785 * Add a file to a file list. Accepted flags:
3786 * EW_DIR add directories
3787 * EW_FILE add files
3788 * EW_EXEC add executable files
3789 * EW_NOTFOUND add even when it doesn't exist
3790 * EW_ADDSLASH add slash after directory name
3791 * EW_ALLLINKS add symlink also when the referred file does not exist
3792 */
3793 void
3794addfile(
3795 garray_T *gap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003796 char_u *f, // filename
Bram Moolenaar26262f82019-09-04 20:59:15 +02003797 int flags)
3798{
3799 char_u *p;
3800 int isdir;
3801 stat_T sb;
3802
3803 // if the file/dir/link doesn't exist, may not add it
3804 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
3805 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
3806 return;
3807
3808#ifdef FNAME_ILLEGAL
3809 // if the file/dir contains illegal characters, don't add it
3810 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
3811 return;
3812#endif
3813
3814 isdir = mch_isdir(f);
3815 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
3816 return;
3817
3818 // If the file isn't executable, may not add it. Do accept directories.
3819 // When invoked from expand_shellcmd() do not use $PATH.
3820 if (!isdir && (flags & EW_EXEC)
3821 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
3822 return;
3823
3824 // Make room for another item in the file list.
3825 if (ga_grow(gap, 1) == FAIL)
3826 return;
3827
3828 p = alloc(STRLEN(f) + 1 + isdir);
3829 if (p == NULL)
3830 return;
3831
3832 STRCPY(p, f);
3833#ifdef BACKSLASH_IN_FILENAME
3834 slash_adjust(p);
3835#endif
3836 /*
3837 * Append a slash or backslash after directory names if none is present.
3838 */
3839#ifndef DONT_ADD_PATHSEP_TO_DIR
3840 if (isdir && (flags & EW_ADDSLASH))
3841 add_pathsep(p);
3842#endif
3843 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
3844}
3845
3846/*
3847 * Free the list of files returned by expand_wildcards() or other expansion
3848 * functions.
3849 */
3850 void
3851FreeWild(int count, char_u **files)
3852{
3853 if (count <= 0 || files == NULL)
3854 return;
3855 while (count--)
3856 vim_free(files[count]);
3857 vim_free(files);
3858}
3859
3860/*
3861 * Compare path "p[]" to "q[]".
3862 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]"
3863 * Return value like strcmp(p, q), but consider path separators.
3864 */
3865 int
3866pathcmp(const char *p, const char *q, int maxlen)
3867{
3868 int i, j;
3869 int c1, c2;
3870 const char *s = NULL;
3871
3872 for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);)
3873 {
3874 c1 = PTR2CHAR((char_u *)p + i);
3875 c2 = PTR2CHAR((char_u *)q + j);
3876
3877 // End of "p": check if "q" also ends or just has a slash.
3878 if (c1 == NUL)
3879 {
3880 if (c2 == NUL) // full match
3881 return 0;
3882 s = q;
3883 i = j;
3884 break;
3885 }
3886
3887 // End of "q": check if "p" just has a slash.
3888 if (c2 == NUL)
3889 {
3890 s = p;
3891 break;
3892 }
3893
3894 if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2)
3895#ifdef BACKSLASH_IN_FILENAME
3896 // consider '/' and '\\' to be equal
3897 && !((c1 == '/' && c2 == '\\')
3898 || (c1 == '\\' && c2 == '/'))
3899#endif
3900 )
3901 {
3902 if (vim_ispathsep(c1))
3903 return -1;
3904 if (vim_ispathsep(c2))
3905 return 1;
3906 return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2)
3907 : c1 - c2; // no match
3908 }
3909
Bram Moolenaar1614a142019-10-06 22:00:13 +02003910 i += mb_ptr2len((char_u *)p + i);
3911 j += mb_ptr2len((char_u *)q + j);
Bram Moolenaar26262f82019-09-04 20:59:15 +02003912 }
3913 if (s == NULL) // "i" or "j" ran into "maxlen"
3914 return 0;
3915
3916 c1 = PTR2CHAR((char_u *)s + i);
Bram Moolenaar1614a142019-10-06 22:00:13 +02003917 c2 = PTR2CHAR((char_u *)s + i + mb_ptr2len((char_u *)s + i));
Bram Moolenaar26262f82019-09-04 20:59:15 +02003918 // ignore a trailing slash, but not "//" or ":/"
3919 if (c2 == NUL
3920 && i > 0
3921 && !after_pathsep((char_u *)s, (char_u *)s + i)
3922#ifdef BACKSLASH_IN_FILENAME
3923 && (c1 == '/' || c1 == '\\')
3924#else
3925 && c1 == '/'
3926#endif
3927 )
3928 return 0; // match with trailing slash
3929 if (s == q)
3930 return -1; // no match
3931 return 1;
3932}
3933
3934/*
3935 * Return TRUE if "name" is a full (absolute) path name or URL.
3936 */
3937 int
3938vim_isAbsName(char_u *name)
3939{
3940 return (path_with_url(name) != 0 || mch_isFullName(name));
3941}
3942
3943/*
3944 * Get absolute file name into buffer "buf[len]".
3945 *
3946 * return FAIL for failure, OK otherwise
3947 */
3948 int
3949vim_FullName(
3950 char_u *fname,
3951 char_u *buf,
3952 int len,
3953 int force) // force expansion even when already absolute
3954{
3955 int retval = OK;
3956 int url;
3957
3958 *buf = NUL;
3959 if (fname == NULL)
3960 return FAIL;
3961
3962 url = path_with_url(fname);
3963 if (!url)
3964 retval = mch_FullName(fname, buf, len, force);
3965 if (url || retval == FAIL)
3966 {
3967 // something failed; use the file name (truncate when too long)
3968 vim_strncpy(buf, fname, len - 1);
3969 }
3970#if defined(MSWIN)
3971 slash_adjust(buf);
3972#endif
3973 return retval;
3974}