blob: f0da60f4527c850c08c75d2132e885769615ed8b [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)
Christian Brabandt81da16b2022-03-10 12:24:02 +0000189 STRNCPY(save_fname, short_fname, len);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200190 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 {
K.Takata54119102022-02-03 13:33:03 +0000375 char_u *q = utf16_to_enc(buf, NULL);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200376
K.Takata54119102022-02-03 13:33:03 +0000377 if (q != NULL)
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200378 {
379 vim_free(*bufp); // free any allocated file name
K.Takata54119102022-02-03 13:33:03 +0000380 *bufp = *fnamep = q;
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200381 }
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 {
=?UTF-8?q?Dundar=20G=C3=B6c?=78a84042022-02-09 15:20:39 +0000419 if (**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
Bram Moolenaar273af492020-09-25 23:49:01 +0200713/*
714 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
715 * "trim_len" specifies how many characters to keep for each directory.
716 * Must be 1 or more.
717 * It's done in-place.
718 */
719 static void
720shorten_dir_len(char_u *str, int trim_len)
721{
722 char_u *tail, *s, *d;
723 int skip = FALSE;
724 int dirchunk_len = 0;
725
726 tail = gettail(str);
727 d = str;
728 for (s = str; ; ++s)
729 {
730 if (s >= tail) // copy the whole tail
731 {
732 *d++ = *s;
733 if (*s == NUL)
734 break;
735 }
736 else if (vim_ispathsep(*s)) // copy '/' and next char
737 {
738 *d++ = *s;
739 skip = FALSE;
740 dirchunk_len = 0;
741 }
742 else if (!skip)
743 {
744 *d++ = *s; // copy next char
745 if (*s != '~' && *s != '.') // and leading "~" and "."
746 {
747 ++dirchunk_len; // only count word chars for the size
748
749 // keep copying chars until we have our preferred length (or
750 // until the above if/else branches move us along)
751 if (dirchunk_len >= trim_len)
752 skip = TRUE;
753 }
754
755 if (has_mbyte)
756 {
757 int l = mb_ptr2len(s);
758
759 while (--l > 0)
760 *d++ = *++s;
761 }
762 }
763 }
764}
765
766/*
767 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
768 * It's done in-place.
769 */
770 void
771shorten_dir(char_u *str)
772{
773 shorten_dir_len(str, 1);
774}
775
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200776#if defined(FEAT_EVAL) || defined(PROTO)
777
778/*
779 * "chdir(dir)" function
780 */
781 void
782f_chdir(typval_T *argvars, typval_T *rettv)
783{
784 char_u *cwd;
785 cdscope_T scope = CDSCOPE_GLOBAL;
786
787 rettv->v_type = VAR_STRING;
788 rettv->vval.v_string = NULL;
789
790 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarc5809432021-03-27 21:23:30 +0100791 {
Bram Moolenaard816cd92020-02-04 22:23:09 +0100792 // Returning an empty string means it failed.
Bram Moolenaar002bc792020-06-05 22:33:42 +0200793 // No error message, for historic reasons.
Bram Moolenaarc5809432021-03-27 21:23:30 +0100794 if (in_vim9script())
795 (void) check_for_string_arg(argvars, 0);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200796 return;
Bram Moolenaarc5809432021-03-27 21:23:30 +0100797 }
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200798
799 // Return the current directory
800 cwd = alloc(MAXPATHL);
801 if (cwd != NULL)
802 {
803 if (mch_dirname(cwd, MAXPATHL) != FAIL)
804 {
805#ifdef BACKSLASH_IN_FILENAME
806 slash_adjust(cwd);
807#endif
808 rettv->vval.v_string = vim_strsave(cwd);
809 }
810 vim_free(cwd);
811 }
812
813 if (curwin->w_localdir != NULL)
814 scope = CDSCOPE_WINDOW;
815 else if (curtab->tp_localdir != NULL)
816 scope = CDSCOPE_TABPAGE;
817
818 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
819 // Directory change failed
820 VIM_CLEAR(rettv->vval.v_string);
821}
822
823/*
824 * "delete()" function
825 */
826 void
827f_delete(typval_T *argvars, typval_T *rettv)
828{
829 char_u nbuf[NUMBUFLEN];
830 char_u *name;
831 char_u *flags;
832
833 rettv->vval.v_number = -1;
834 if (check_restricted() || check_secure())
835 return;
836
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200837 if (in_vim9script()
838 && (check_for_string_arg(argvars, 0) == FAIL
839 || check_for_opt_string_arg(argvars, 1) == FAIL))
840 return;
841
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200842 name = tv_get_string(&argvars[0]);
843 if (name == NULL || *name == NUL)
844 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000845 emsg(_(e_invalid_argument));
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200846 return;
847 }
848
849 if (argvars[1].v_type != VAR_UNKNOWN)
850 flags = tv_get_string_buf(&argvars[1], nbuf);
851 else
852 flags = (char_u *)"";
853
854 if (*flags == NUL)
Bram Moolenaar26262f82019-09-04 20:59:15 +0200855 // delete a file
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200856 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
857 else if (STRCMP(flags, "d") == 0)
Bram Moolenaar26262f82019-09-04 20:59:15 +0200858 // delete an empty directory
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200859 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
860 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar26262f82019-09-04 20:59:15 +0200861 // delete a directory recursively
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200862 rettv->vval.v_number = delete_recursive(name);
863 else
Bram Moolenaar108010a2021-06-27 22:03:33 +0200864 semsg(_(e_invalid_expression_str), flags);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200865}
866
867/*
868 * "executable()" function
869 */
870 void
871f_executable(typval_T *argvars, typval_T *rettv)
872{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100873 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100874 return;
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200875
Bram Moolenaar26262f82019-09-04 20:59:15 +0200876 // Check in $PATH and also check directly if there is a directory name.
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100877 rettv->vval.v_number = mch_can_exe(tv_get_string(&argvars[0]), NULL, TRUE);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200878}
879
880/*
881 * "exepath()" function
882 */
883 void
884f_exepath(typval_T *argvars, typval_T *rettv)
885{
886 char_u *p = NULL;
887
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100888 if (in_vim9script() && check_for_nonempty_string_arg(argvars, 0) == FAIL)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100889 return;
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200890 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
891 rettv->v_type = VAR_STRING;
892 rettv->vval.v_string = p;
893}
894
895/*
896 * "filereadable()" function
897 */
898 void
899f_filereadable(typval_T *argvars, typval_T *rettv)
900{
901 int fd;
902 char_u *p;
903 int n;
904
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100905 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100906 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200907
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200908#ifndef O_NONBLOCK
909# define O_NONBLOCK 0
910#endif
911 p = tv_get_string(&argvars[0]);
912 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
913 O_RDONLY | O_NONBLOCK, 0)) >= 0)
914 {
915 n = TRUE;
916 close(fd);
917 }
918 else
919 n = FALSE;
920
921 rettv->vval.v_number = n;
922}
923
924/*
925 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
926 * rights to write into.
927 */
928 void
929f_filewritable(typval_T *argvars, typval_T *rettv)
930{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100931 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100932 return;
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200933 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
934}
935
Bram Moolenaar840d16f2019-09-10 21:27:18 +0200936 static void
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200937findfilendir(
938 typval_T *argvars UNUSED,
939 typval_T *rettv,
940 int find_what UNUSED)
941{
942#ifdef FEAT_SEARCHPATH
943 char_u *fname;
944 char_u *fresult = NULL;
945 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
946 char_u *p;
947 char_u pathbuf[NUMBUFLEN];
948 int count = 1;
949 int first = TRUE;
950 int error = FALSE;
951#endif
952
953 rettv->vval.v_string = NULL;
954 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200955 if (in_vim9script()
956 && (check_for_nonempty_string_arg(argvars, 0) == FAIL
957 || check_for_opt_string_arg(argvars, 1) == FAIL
958 || (argvars[1].v_type != VAR_UNKNOWN
959 && check_for_opt_number_arg(argvars, 2) == FAIL)))
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100960 return;
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200961
962#ifdef FEAT_SEARCHPATH
963 fname = tv_get_string(&argvars[0]);
964
965 if (argvars[1].v_type != VAR_UNKNOWN)
966 {
967 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
968 if (p == NULL)
969 error = TRUE;
970 else
971 {
972 if (*p != NUL)
973 path = p;
974
975 if (argvars[2].v_type != VAR_UNKNOWN)
976 count = (int)tv_get_number_chk(&argvars[2], &error);
977 }
978 }
979
980 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
981 error = TRUE;
982
983 if (*fname != NUL && !error)
984 {
985 do
986 {
987 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
988 vim_free(fresult);
989 fresult = find_file_in_path_option(first ? fname : NULL,
990 first ? (int)STRLEN(fname) : 0,
991 0, first, path,
992 find_what,
993 curbuf->b_ffname,
994 find_what == FINDFILE_DIR
995 ? (char_u *)"" : curbuf->b_p_sua);
996 first = FALSE;
997
998 if (fresult != NULL && rettv->v_type == VAR_LIST)
999 list_append_string(rettv->vval.v_list, fresult, -1);
1000
1001 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
1002 }
1003
1004 if (rettv->v_type == VAR_STRING)
1005 rettv->vval.v_string = fresult;
1006#endif
1007}
1008
1009/*
1010 * "finddir({fname}[, {path}[, {count}]])" function
1011 */
1012 void
1013f_finddir(typval_T *argvars, typval_T *rettv)
1014{
1015 findfilendir(argvars, rettv, FINDFILE_DIR);
1016}
1017
1018/*
1019 * "findfile({fname}[, {path}[, {count}]])" function
1020 */
1021 void
1022f_findfile(typval_T *argvars, typval_T *rettv)
1023{
1024 findfilendir(argvars, rettv, FINDFILE_FILE);
1025}
1026
1027/*
1028 * "fnamemodify({fname}, {mods})" function
1029 */
1030 void
1031f_fnamemodify(typval_T *argvars, typval_T *rettv)
1032{
1033 char_u *fname;
1034 char_u *mods;
1035 int usedlen = 0;
Bram Moolenaarc5308522020-12-13 12:25:35 +01001036 int len = 0;
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001037 char_u *fbuf = NULL;
1038 char_u buf[NUMBUFLEN];
1039
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001040 if (in_vim9script()
1041 && (check_for_string_arg(argvars, 0) == FAIL
1042 || check_for_string_arg(argvars, 1) == FAIL))
Bram Moolenaar7bb4e742020-12-09 12:41:50 +01001043 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001044
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001045 fname = tv_get_string_chk(&argvars[0]);
1046 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaarc5308522020-12-13 12:25:35 +01001047 if (mods == NULL || fname == NULL)
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001048 fname = NULL;
Bram Moolenaarc5308522020-12-13 12:25:35 +01001049 else
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001050 {
1051 len = (int)STRLEN(fname);
Bram Moolenaarc5308522020-12-13 12:25:35 +01001052 if (mods != NULL && *mods != NUL)
1053 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001054 }
1055
1056 rettv->v_type = VAR_STRING;
1057 if (fname == NULL)
1058 rettv->vval.v_string = NULL;
1059 else
1060 rettv->vval.v_string = vim_strnsave(fname, len);
1061 vim_free(fbuf);
1062}
1063
1064/*
1065 * "getcwd()" function
1066 *
1067 * Return the current working directory of a window in a tab page.
1068 * First optional argument 'winnr' is the window number or -1 and the second
1069 * optional argument 'tabnr' is the tab page number.
1070 *
1071 * If no arguments are supplied, then return the directory of the current
1072 * window.
1073 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
1074 * the specified window.
1075 * If 'winnr' is 0 then return the directory of the current window.
1076 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
1077 * directory of the specified tab page. Otherwise return the directory of the
1078 * specified window in the specified tab page.
1079 * If the window or the tab page doesn't exist then return NULL.
1080 */
1081 void
1082f_getcwd(typval_T *argvars, typval_T *rettv)
1083{
1084 win_T *wp = NULL;
1085 tabpage_T *tp = NULL;
1086 char_u *cwd;
1087 int global = FALSE;
1088
1089 rettv->v_type = VAR_STRING;
1090 rettv->vval.v_string = NULL;
1091
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001092 if (in_vim9script()
1093 && (check_for_opt_number_arg(argvars, 0) == FAIL
1094 || (argvars[0].v_type != VAR_UNKNOWN
1095 && check_for_opt_number_arg(argvars, 1) == FAIL)))
1096 return;
1097
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001098 if (argvars[0].v_type == VAR_NUMBER
1099 && argvars[0].vval.v_number == -1
1100 && argvars[1].v_type == VAR_UNKNOWN)
1101 global = TRUE;
1102 else
1103 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
1104
Bram Moolenaar851c7a62021-11-18 20:47:31 +00001105 if (wp != NULL && wp->w_localdir != NULL
1106 && argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001107 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar851c7a62021-11-18 20:47:31 +00001108 else if (tp != NULL && tp->tp_localdir != NULL
1109 && argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001110 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
1111 else if (wp != NULL || tp != NULL || global)
1112 {
Bram Moolenaar851c7a62021-11-18 20:47:31 +00001113 if (globaldir != NULL && argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001114 rettv->vval.v_string = vim_strsave(globaldir);
1115 else
1116 {
1117 cwd = alloc(MAXPATHL);
1118 if (cwd != NULL)
1119 {
1120 if (mch_dirname(cwd, MAXPATHL) != FAIL)
1121 rettv->vval.v_string = vim_strsave(cwd);
1122 vim_free(cwd);
1123 }
1124 }
1125 }
1126#ifdef BACKSLASH_IN_FILENAME
1127 if (rettv->vval.v_string != NULL)
1128 slash_adjust(rettv->vval.v_string);
1129#endif
1130}
1131
1132/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001133 * Convert "st" to file permission string.
1134 */
1135 char_u *
1136getfpermst(stat_T *st, char_u *perm)
1137{
1138 char_u flags[] = "rwx";
1139 int i;
1140
1141 for (i = 0; i < 9; i++)
1142 {
1143 if (st->st_mode & (1 << (8 - i)))
1144 perm[i] = flags[i % 3];
1145 else
1146 perm[i] = '-';
1147 }
1148 return perm;
1149}
1150
1151/*
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001152 * "getfperm({fname})" function
1153 */
1154 void
1155f_getfperm(typval_T *argvars, typval_T *rettv)
1156{
1157 char_u *fname;
1158 stat_T st;
1159 char_u *perm = NULL;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001160 char_u permbuf[] = "---------";
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001161
Bram Moolenaar32105ae2021-03-27 18:59:25 +01001162 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +01001163 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001164
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001165 fname = tv_get_string(&argvars[0]);
1166
1167 rettv->v_type = VAR_STRING;
1168 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001169 perm = vim_strsave(getfpermst(&st, permbuf));
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001170 rettv->vval.v_string = perm;
1171}
1172
1173/*
1174 * "getfsize({fname})" function
1175 */
1176 void
1177f_getfsize(typval_T *argvars, typval_T *rettv)
1178{
1179 char_u *fname;
1180 stat_T st;
1181
Bram Moolenaar32105ae2021-03-27 18:59:25 +01001182 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +01001183 return;
1184
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001185 fname = tv_get_string(&argvars[0]);
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001186 if (mch_stat((char *)fname, &st) >= 0)
1187 {
1188 if (mch_isdir(fname))
1189 rettv->vval.v_number = 0;
1190 else
1191 {
1192 rettv->vval.v_number = (varnumber_T)st.st_size;
1193
Bram Moolenaar26262f82019-09-04 20:59:15 +02001194 // non-perfect check for overflow
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001195 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
1196 rettv->vval.v_number = -2;
1197 }
1198 }
1199 else
1200 rettv->vval.v_number = -1;
1201}
1202
1203/*
1204 * "getftime({fname})" function
1205 */
1206 void
1207f_getftime(typval_T *argvars, typval_T *rettv)
1208{
1209 char_u *fname;
1210 stat_T st;
1211
Bram Moolenaar32105ae2021-03-27 18:59:25 +01001212 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +01001213 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001214
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001215 fname = tv_get_string(&argvars[0]);
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001216 if (mch_stat((char *)fname, &st) >= 0)
1217 rettv->vval.v_number = (varnumber_T)st.st_mtime;
1218 else
1219 rettv->vval.v_number = -1;
1220}
1221
1222/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001223 * Convert "st" to file type string.
1224 */
1225 char_u *
1226getftypest(stat_T *st)
1227{
1228 char *t;
1229
1230 if (S_ISREG(st->st_mode))
1231 t = "file";
1232 else if (S_ISDIR(st->st_mode))
1233 t = "dir";
1234 else if (S_ISLNK(st->st_mode))
1235 t = "link";
1236 else if (S_ISBLK(st->st_mode))
1237 t = "bdev";
1238 else if (S_ISCHR(st->st_mode))
1239 t = "cdev";
1240 else if (S_ISFIFO(st->st_mode))
1241 t = "fifo";
1242 else if (S_ISSOCK(st->st_mode))
1243 t = "socket";
1244 else
1245 t = "other";
1246 return (char_u*)t;
1247}
1248
1249/*
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001250 * "getftype({fname})" function
1251 */
1252 void
1253f_getftype(typval_T *argvars, typval_T *rettv)
1254{
1255 char_u *fname;
1256 stat_T st;
1257 char_u *type = NULL;
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001258
Bram Moolenaar32105ae2021-03-27 18:59:25 +01001259 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +01001260 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001261
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001262 fname = tv_get_string(&argvars[0]);
1263
1264 rettv->v_type = VAR_STRING;
1265 if (mch_lstat((char *)fname, &st) >= 0)
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001266 type = vim_strsave(getftypest(&st));
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001267 rettv->vval.v_string = type;
1268}
1269
1270/*
1271 * "glob()" function
1272 */
1273 void
1274f_glob(typval_T *argvars, typval_T *rettv)
1275{
1276 int options = WILD_SILENT|WILD_USE_NL;
1277 expand_T xpc;
1278 int error = FALSE;
1279
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001280 if (in_vim9script()
1281 && (check_for_string_arg(argvars, 0) == FAIL
1282 || check_for_opt_bool_arg(argvars, 1) == FAIL
1283 || (argvars[1].v_type != VAR_UNKNOWN
1284 && (check_for_opt_bool_arg(argvars, 2) == FAIL
1285 || (argvars[2].v_type != VAR_UNKNOWN
1286 && check_for_opt_bool_arg(argvars, 3) == FAIL)))))
1287 return;
1288
Bram Moolenaar26262f82019-09-04 20:59:15 +02001289 // When the optional second argument is non-zero, don't remove matches
1290 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001291 rettv->v_type = VAR_STRING;
1292 if (argvars[1].v_type != VAR_UNKNOWN)
1293 {
Bram Moolenaar5892ea12020-09-02 21:53:11 +02001294 if (tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001295 options |= WILD_KEEP_ALL;
1296 if (argvars[2].v_type != VAR_UNKNOWN)
1297 {
Bram Moolenaar5892ea12020-09-02 21:53:11 +02001298 if (tv_get_bool_chk(&argvars[2], &error))
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001299 rettv_list_set(rettv, NULL);
1300 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaar5892ea12020-09-02 21:53:11 +02001301 && tv_get_bool_chk(&argvars[3], &error))
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001302 options |= WILD_ALLLINKS;
1303 }
1304 }
1305 if (!error)
1306 {
1307 ExpandInit(&xpc);
1308 xpc.xp_context = EXPAND_FILES;
1309 if (p_wic)
1310 options += WILD_ICASE;
1311 if (rettv->v_type == VAR_STRING)
1312 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
1313 NULL, options, WILD_ALL);
1314 else if (rettv_list_alloc(rettv) != FAIL)
1315 {
1316 int i;
1317
1318 ExpandOne(&xpc, tv_get_string(&argvars[0]),
1319 NULL, options, WILD_ALL_KEEP);
1320 for (i = 0; i < xpc.xp_numfiles; i++)
1321 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
1322
1323 ExpandCleanup(&xpc);
1324 }
1325 }
1326 else
1327 rettv->vval.v_string = NULL;
1328}
1329
1330/*
1331 * "glob2regpat()" function
1332 */
1333 void
1334f_glob2regpat(typval_T *argvars, typval_T *rettv)
1335{
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02001336 char_u buf[NUMBUFLEN];
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001337 char_u *pat;
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001338
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001339 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1340 return;
1341
1342 pat = tv_get_string_buf_chk_strict(&argvars[0], buf, in_vim9script());
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001343 rettv->v_type = VAR_STRING;
1344 rettv->vval.v_string = (pat == NULL)
1345 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
1346}
1347
1348/*
1349 * "globpath()" function
1350 */
1351 void
1352f_globpath(typval_T *argvars, typval_T *rettv)
1353{
1354 int flags = WILD_IGNORE_COMPLETESLASH;
1355 char_u buf1[NUMBUFLEN];
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001356 char_u *file;
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001357 int error = FALSE;
1358 garray_T ga;
1359 int i;
1360
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001361 if (in_vim9script()
1362 && (check_for_string_arg(argvars, 0) == FAIL
1363 || check_for_string_arg(argvars, 1) == FAIL
1364 || check_for_opt_bool_arg(argvars, 2) == FAIL
1365 || (argvars[2].v_type != VAR_UNKNOWN
1366 && (check_for_opt_bool_arg(argvars, 3) == FAIL
1367 || (argvars[3].v_type != VAR_UNKNOWN
1368 && check_for_opt_bool_arg(argvars, 4) == FAIL)))))
1369 return;
1370
1371 file = tv_get_string_buf_chk(&argvars[1], buf1);
1372
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001373 // When the optional second argument is non-zero, don't remove matches
1374 // for 'wildignore' and don't put matches for 'suffixes' at the end.
1375 rettv->v_type = VAR_STRING;
1376 if (argvars[2].v_type != VAR_UNKNOWN)
1377 {
Bram Moolenaarf966ce52020-09-02 21:57:07 +02001378 if (tv_get_bool_chk(&argvars[2], &error))
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001379 flags |= WILD_KEEP_ALL;
1380 if (argvars[3].v_type != VAR_UNKNOWN)
1381 {
Bram Moolenaarf966ce52020-09-02 21:57:07 +02001382 if (tv_get_bool_chk(&argvars[3], &error))
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001383 rettv_list_set(rettv, NULL);
1384 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaarf966ce52020-09-02 21:57:07 +02001385 && tv_get_bool_chk(&argvars[4], &error))
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001386 flags |= WILD_ALLLINKS;
1387 }
1388 }
1389 if (file != NULL && !error)
1390 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001391 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001392 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
1393 if (rettv->v_type == VAR_STRING)
1394 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
1395 else if (rettv_list_alloc(rettv) != FAIL)
1396 for (i = 0; i < ga.ga_len; ++i)
1397 list_append_string(rettv->vval.v_list,
1398 ((char_u **)(ga.ga_data))[i], -1);
1399 ga_clear_strings(&ga);
1400 }
1401 else
1402 rettv->vval.v_string = NULL;
1403}
1404
1405/*
1406 * "isdirectory()" function
1407 */
1408 void
1409f_isdirectory(typval_T *argvars, typval_T *rettv)
1410{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001411 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1412 return;
1413
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001414 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
1415}
1416
1417/*
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001418 * Create the directory in which "dir" is located, and higher levels when
1419 * needed.
1420 * Return OK or FAIL.
1421 */
1422 static int
1423mkdir_recurse(char_u *dir, int prot)
1424{
1425 char_u *p;
1426 char_u *updir;
1427 int r = FAIL;
1428
Bram Moolenaar26262f82019-09-04 20:59:15 +02001429 // Get end of directory name in "dir".
1430 // We're done when it's "/" or "c:/".
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001431 p = gettail_sep(dir);
1432 if (p <= get_past_head(dir))
1433 return OK;
1434
Bram Moolenaar26262f82019-09-04 20:59:15 +02001435 // If the directory exists we're done. Otherwise: create it.
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001436 updir = vim_strnsave(dir, p - dir);
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001437 if (updir == NULL)
1438 return FAIL;
1439 if (mch_isdir(updir))
1440 r = OK;
1441 else if (mkdir_recurse(updir, prot) == OK)
1442 r = vim_mkdir_emsg(updir, prot);
1443 vim_free(updir);
1444 return r;
1445}
1446
1447/*
1448 * "mkdir()" function
1449 */
1450 void
1451f_mkdir(typval_T *argvars, typval_T *rettv)
1452{
1453 char_u *dir;
1454 char_u buf[NUMBUFLEN];
1455 int prot = 0755;
1456
1457 rettv->vval.v_number = FAIL;
1458 if (check_restricted() || check_secure())
1459 return;
1460
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001461 if (in_vim9script()
1462 && (check_for_nonempty_string_arg(argvars, 0) == FAIL
1463 || check_for_opt_string_arg(argvars, 1) == FAIL
1464 || (argvars[1].v_type != VAR_UNKNOWN
1465 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1466 return;
1467
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001468 dir = tv_get_string_buf(&argvars[0], buf);
1469 if (*dir == NUL)
1470 return;
1471
1472 if (*gettail(dir) == NUL)
Bram Moolenaar26262f82019-09-04 20:59:15 +02001473 // remove trailing slashes
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001474 *gettail_sep(dir) = NUL;
1475
1476 if (argvars[1].v_type != VAR_UNKNOWN)
1477 {
1478 if (argvars[2].v_type != VAR_UNKNOWN)
1479 {
1480 prot = (int)tv_get_number_chk(&argvars[2], NULL);
1481 if (prot == -1)
1482 return;
1483 }
1484 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
1485 {
1486 if (mch_isdir(dir))
1487 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02001488 // With the "p" flag it's OK if the dir already exists.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001489 rettv->vval.v_number = OK;
1490 return;
1491 }
1492 mkdir_recurse(dir, prot);
1493 }
1494 }
1495 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
1496}
1497
1498/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02001499 * "pathshorten()" function
1500 */
1501 void
1502f_pathshorten(typval_T *argvars, typval_T *rettv)
1503{
1504 char_u *p;
Bram Moolenaar6a33ef02020-09-25 22:42:48 +02001505 int trim_len = 1;
1506
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001507 if (in_vim9script()
1508 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001509 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001510 return;
1511
Bram Moolenaar6a33ef02020-09-25 22:42:48 +02001512 if (argvars[1].v_type != VAR_UNKNOWN)
1513 {
1514 trim_len = (int)tv_get_number(&argvars[1]);
1515 if (trim_len < 1)
1516 trim_len = 1;
1517 }
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02001518
1519 rettv->v_type = VAR_STRING;
1520 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar6a33ef02020-09-25 22:42:48 +02001521
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02001522 if (p == NULL)
1523 rettv->vval.v_string = NULL;
1524 else
1525 {
1526 p = vim_strsave(p);
1527 rettv->vval.v_string = p;
1528 if (p != NULL)
Bram Moolenaar6a33ef02020-09-25 22:42:48 +02001529 shorten_dir_len(p, trim_len);
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02001530 }
1531}
1532
1533/*
Bram Moolenaarf8abbf32020-08-19 16:00:06 +02001534 * Common code for readdir_checkitem() and readdirex_checkitem().
1535 * Either "name" or "dict" is NULL.
Bram Moolenaar80147dd2020-02-04 22:32:59 +01001536 */
1537 static int
Bram Moolenaarf8abbf32020-08-19 16:00:06 +02001538checkitem_common(void *context, char_u *name, dict_T *dict)
Bram Moolenaar80147dd2020-02-04 22:32:59 +01001539{
1540 typval_T *expr = (typval_T *)context;
1541 typval_T save_val;
1542 typval_T rettv;
1543 typval_T argv[2];
1544 int retval = 0;
1545 int error = FALSE;
Bram Moolenaar80147dd2020-02-04 22:32:59 +01001546
1547 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaarf8abbf32020-08-19 16:00:06 +02001548 if (name != NULL)
1549 {
1550 set_vim_var_string(VV_VAL, name, -1);
1551 argv[0].v_type = VAR_STRING;
1552 argv[0].vval.v_string = name;
1553 }
1554 else
1555 {
1556 set_vim_var_dict(VV_VAL, dict);
1557 argv[0].v_type = VAR_DICT;
1558 argv[0].vval.v_dict = dict;
1559 }
Bram Moolenaar80147dd2020-02-04 22:32:59 +01001560
1561 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
1562 goto theend;
1563
Bram Moolenaarf8abbf32020-08-19 16:00:06 +02001564 // We want to use -1, but also true/false should be allowed.
1565 if (rettv.v_type == VAR_SPECIAL || rettv.v_type == VAR_BOOL)
1566 {
1567 rettv.v_type = VAR_NUMBER;
1568 rettv.vval.v_number = rettv.vval.v_number == VVAL_TRUE;
1569 }
Bram Moolenaar80147dd2020-02-04 22:32:59 +01001570 retval = tv_get_number_chk(&rettv, &error);
1571 if (error)
1572 retval = -1;
1573 clear_tv(&rettv);
1574
1575theend:
Bram Moolenaarf8abbf32020-08-19 16:00:06 +02001576 if (name != NULL)
1577 set_vim_var_string(VV_VAL, NULL, 0);
1578 else
1579 set_vim_var_dict(VV_VAL, NULL);
Bram Moolenaar80147dd2020-02-04 22:32:59 +01001580 restore_vimvar(VV_VAL, &save_val);
1581 return retval;
1582}
1583
Bram Moolenaarf8abbf32020-08-19 16:00:06 +02001584/*
1585 * Evaluate "expr" (= "context") for readdir().
1586 */
1587 static int
1588readdir_checkitem(void *context, void *item)
1589{
1590 char_u *name = (char_u *)item;
1591
1592 return checkitem_common(context, name, NULL);
1593}
1594
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001595 static int
1596readdirex_dict_arg(typval_T *tv, int *cmp)
1597{
1598 char_u *compare;
1599
1600 if (tv->v_type != VAR_DICT)
1601 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001602 emsg(_(e_dictionary_required));
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001603 return FAIL;
1604 }
1605
1606 if (dict_find(tv->vval.v_dict, (char_u *)"sort", -1) != NULL)
1607 compare = dict_get_string(tv->vval.v_dict, (char_u *)"sort", FALSE);
1608 else
1609 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001610 semsg(_(e_dictionary_key_str_required), "sort");
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001611 return FAIL;
1612 }
1613
1614 if (STRCMP(compare, (char_u *) "none") == 0)
1615 *cmp = READDIR_SORT_NONE;
1616 else if (STRCMP(compare, (char_u *) "case") == 0)
1617 *cmp = READDIR_SORT_BYTE;
1618 else if (STRCMP(compare, (char_u *) "icase") == 0)
1619 *cmp = READDIR_SORT_IC;
1620 else if (STRCMP(compare, (char_u *) "collate") == 0)
1621 *cmp = READDIR_SORT_COLLATE;
1622 return OK;
1623}
1624
Bram Moolenaar80147dd2020-02-04 22:32:59 +01001625/*
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001626 * "readdir()" function
1627 */
1628 void
1629f_readdir(typval_T *argvars, typval_T *rettv)
1630{
1631 typval_T *expr;
1632 int ret;
1633 char_u *path;
1634 char_u *p;
1635 garray_T ga;
1636 int i;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001637 int sort = READDIR_SORT_BYTE;
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001638
1639 if (rettv_list_alloc(rettv) == FAIL)
1640 return;
Yegappan Lakshmanan5bca9062021-07-24 21:33:26 +02001641
1642 if (in_vim9script()
1643 && (check_for_string_arg(argvars, 0) == FAIL
1644 || (argvars[1].v_type != VAR_UNKNOWN
1645 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
1646 return;
1647
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001648 path = tv_get_string(&argvars[0]);
1649 expr = &argvars[1];
1650
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001651 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN &&
1652 readdirex_dict_arg(&argvars[2], &sort) == FAIL)
1653 return;
1654
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001655 ret = readdir_core(&ga, path, FALSE, (void *)expr,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001656 (expr->v_type == VAR_UNKNOWN) ? NULL : readdir_checkitem, sort);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001657 if (ret == OK)
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001658 {
1659 for (i = 0; i < ga.ga_len; i++)
1660 {
1661 p = ((char_u **)ga.ga_data)[i];
1662 list_append_string(rettv->vval.v_list, p, -1);
1663 }
1664 }
1665 ga_clear_strings(&ga);
1666}
1667
1668/*
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001669 * Evaluate "expr" (= "context") for readdirex().
1670 */
1671 static int
1672readdirex_checkitem(void *context, void *item)
1673{
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001674 dict_T *dict = (dict_T*)item;
1675
Bram Moolenaarf8abbf32020-08-19 16:00:06 +02001676 return checkitem_common(context, NULL, dict);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001677}
1678
1679/*
1680 * "readdirex()" function
1681 */
1682 void
1683f_readdirex(typval_T *argvars, typval_T *rettv)
1684{
1685 typval_T *expr;
1686 int ret;
1687 char_u *path;
1688 garray_T ga;
1689 int i;
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001690 int sort = READDIR_SORT_BYTE;
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001691
1692 if (rettv_list_alloc(rettv) == FAIL)
1693 return;
Yegappan Lakshmanan5bca9062021-07-24 21:33:26 +02001694
1695 if (in_vim9script()
1696 && (check_for_string_arg(argvars, 0) == FAIL
1697 || (argvars[1].v_type != VAR_UNKNOWN
1698 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
1699 return;
1700
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001701 path = tv_get_string(&argvars[0]);
1702 expr = &argvars[1];
1703
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001704 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN &&
1705 readdirex_dict_arg(&argvars[2], &sort) == FAIL)
1706 return;
1707
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001708 ret = readdir_core(&ga, path, TRUE, (void *)expr,
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001709 (expr->v_type == VAR_UNKNOWN) ? NULL : readdirex_checkitem, sort);
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001710 if (ret == OK)
1711 {
1712 for (i = 0; i < ga.ga_len; i++)
1713 {
1714 dict_T *dict = ((dict_T**)ga.ga_data)[i];
1715 list_append_dict(rettv->vval.v_list, dict);
1716 dict_unref(dict);
1717 }
1718 }
1719 ga_clear(&ga);
1720}
1721
1722/*
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001723 * "readfile()" function
1724 */
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001725 static void
1726read_file_or_blob(typval_T *argvars, typval_T *rettv, int always_blob)
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001727{
1728 int binary = FALSE;
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001729 int blob = always_blob;
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001730 int failed = FALSE;
1731 char_u *fname;
1732 FILE *fd;
Bram Moolenaar26262f82019-09-04 20:59:15 +02001733 char_u buf[(IOSIZE/256)*256]; // rounded to avoid odd + 1
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001734 int io_size = sizeof(buf);
Bram Moolenaar26262f82019-09-04 20:59:15 +02001735 int readlen; // size of last fread()
1736 char_u *prev = NULL; // previously read bytes, if any
1737 long prevlen = 0; // length of data in prev
1738 long prevsize = 0; // size of prev buffer
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001739 long maxline = MAXLNUM;
1740 long cnt = 0;
Bram Moolenaar26262f82019-09-04 20:59:15 +02001741 char_u *p; // position in buf
1742 char_u *start; // start of current line
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001743
1744 if (argvars[1].v_type != VAR_UNKNOWN)
1745 {
1746 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
1747 binary = TRUE;
1748 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
1749 blob = TRUE;
1750
1751 if (argvars[2].v_type != VAR_UNKNOWN)
1752 maxline = (long)tv_get_number(&argvars[2]);
1753 }
1754
Bram Moolenaar15352dc2020-04-06 21:12:42 +02001755 if ((blob ? rettv_blob_alloc(rettv) : rettv_list_alloc(rettv)) == FAIL)
1756 return;
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001757
Bram Moolenaar26262f82019-09-04 20:59:15 +02001758 // Always open the file in binary mode, library functions have a mind of
1759 // their own about CR-LF conversion.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001760 fname = tv_get_string(&argvars[0]);
Bram Moolenaar15352dc2020-04-06 21:12:42 +02001761
1762 if (mch_isdir(fname))
1763 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001764 semsg(_(e_src_is_directory), fname);
Bram Moolenaar15352dc2020-04-06 21:12:42 +02001765 return;
1766 }
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001767 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
1768 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001769 semsg(_(e_cant_open_file_str), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001770 return;
1771 }
1772
1773 if (blob)
1774 {
1775 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
1776 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001777 semsg(_(e_cant_read_file_str), fname);
Bram Moolenaar15352dc2020-04-06 21:12:42 +02001778 // An empty blob is returned on error.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001779 blob_free(rettv->vval.v_blob);
Bram Moolenaar15352dc2020-04-06 21:12:42 +02001780 rettv->vval.v_blob = NULL;
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001781 }
1782 fclose(fd);
1783 return;
1784 }
1785
1786 while (cnt < maxline || maxline < 0)
1787 {
1788 readlen = (int)fread(buf, 1, io_size, fd);
1789
Bram Moolenaar26262f82019-09-04 20:59:15 +02001790 // This for loop processes what was read, but is also entered at end
1791 // of file so that either:
1792 // - an incomplete line gets written
1793 // - a "binary" file gets an empty line at the end if it ends in a
1794 // newline.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001795 for (p = buf, start = buf;
1796 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
1797 ++p)
1798 {
Dominique Pellef5d639a2022-01-12 15:24:40 +00001799 if (readlen <= 0 || *p == '\n')
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001800 {
1801 listitem_T *li;
1802 char_u *s = NULL;
1803 long_u len = p - start;
1804
Bram Moolenaar26262f82019-09-04 20:59:15 +02001805 // Finished a line. Remove CRs before NL.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001806 if (readlen > 0 && !binary)
1807 {
1808 while (len > 0 && start[len - 1] == '\r')
1809 --len;
Bram Moolenaar26262f82019-09-04 20:59:15 +02001810 // removal may cross back to the "prev" string
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001811 if (len == 0)
1812 while (prevlen > 0 && prev[prevlen - 1] == '\r')
1813 --prevlen;
1814 }
1815 if (prevlen == 0)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001816 s = vim_strnsave(start, len);
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001817 else
1818 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02001819 // Change "prev" buffer to be the right size. This way
1820 // the bytes are only copied once, and very long lines are
1821 // allocated only once.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001822 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
1823 {
1824 mch_memmove(s + prevlen, start, len);
1825 s[prevlen + len] = NUL;
Bram Moolenaar26262f82019-09-04 20:59:15 +02001826 prev = NULL; // the list will own the string
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001827 prevlen = prevsize = 0;
1828 }
1829 }
1830 if (s == NULL)
1831 {
1832 do_outofmem_msg((long_u) prevlen + len + 1);
1833 failed = TRUE;
1834 break;
1835 }
1836
1837 if ((li = listitem_alloc()) == NULL)
1838 {
1839 vim_free(s);
1840 failed = TRUE;
1841 break;
1842 }
1843 li->li_tv.v_type = VAR_STRING;
1844 li->li_tv.v_lock = 0;
1845 li->li_tv.vval.v_string = s;
1846 list_append(rettv->vval.v_list, li);
1847
Bram Moolenaar26262f82019-09-04 20:59:15 +02001848 start = p + 1; // step over newline
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001849 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
1850 break;
1851 }
1852 else if (*p == NUL)
1853 *p = '\n';
Bram Moolenaar26262f82019-09-04 20:59:15 +02001854 // Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
1855 // when finding the BF and check the previous two bytes.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001856 else if (*p == 0xbf && enc_utf8 && !binary)
1857 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02001858 // Find the two bytes before the 0xbf. If p is at buf, or buf
1859 // + 1, these may be in the "prev" string.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001860 char_u back1 = p >= buf + 1 ? p[-1]
1861 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
1862 char_u back2 = p >= buf + 2 ? p[-2]
1863 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
1864 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
1865
1866 if (back2 == 0xef && back1 == 0xbb)
1867 {
1868 char_u *dest = p - 2;
1869
Bram Moolenaar26262f82019-09-04 20:59:15 +02001870 // Usually a BOM is at the beginning of a file, and so at
1871 // the beginning of a line; then we can just step over it.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001872 if (start == dest)
1873 start = p + 1;
1874 else
1875 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02001876 // have to shuffle buf to close gap
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001877 int adjust_prevlen = 0;
1878
1879 if (dest < buf)
1880 {
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001881 // must be 1 or 2
1882 adjust_prevlen = (int)(buf - dest);
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001883 dest = buf;
1884 }
1885 if (readlen > p - buf + 1)
1886 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
1887 readlen -= 3 - adjust_prevlen;
1888 prevlen -= adjust_prevlen;
1889 p = dest - 1;
1890 }
1891 }
1892 }
Bram Moolenaar26262f82019-09-04 20:59:15 +02001893 } // for
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001894
1895 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
1896 break;
1897 if (start < p)
1898 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02001899 // There's part of a line in buf, store it in "prev".
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001900 if (p - start + prevlen >= prevsize)
1901 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02001902 // need bigger "prev" buffer
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001903 char_u *newprev;
1904
Bram Moolenaar26262f82019-09-04 20:59:15 +02001905 // A common use case is ordinary text files and "prev" gets a
1906 // fragment of a line, so the first allocation is made
1907 // small, to avoid repeatedly 'allocing' large and
1908 // 'reallocing' small.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001909 if (prevsize == 0)
1910 prevsize = (long)(p - start);
1911 else
1912 {
1913 long grow50pc = (prevsize * 3) / 2;
1914 long growmin = (long)((p - start) * 2 + prevlen);
1915 prevsize = grow50pc > growmin ? grow50pc : growmin;
1916 }
1917 newprev = vim_realloc(prev, prevsize);
1918 if (newprev == NULL)
1919 {
1920 do_outofmem_msg((long_u)prevsize);
1921 failed = TRUE;
1922 break;
1923 }
1924 prev = newprev;
1925 }
Bram Moolenaar26262f82019-09-04 20:59:15 +02001926 // Add the line part to end of "prev".
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001927 mch_memmove(prev + prevlen, start, p - start);
1928 prevlen += (long)(p - start);
1929 }
Bram Moolenaar26262f82019-09-04 20:59:15 +02001930 } // while
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001931
Bram Moolenaar26262f82019-09-04 20:59:15 +02001932 // For a negative line count use only the lines at the end of the file,
1933 // free the rest.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001934 if (!failed && maxline < 0)
1935 while (cnt > -maxline)
1936 {
1937 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
1938 --cnt;
1939 }
1940
1941 if (failed)
1942 {
1943 // an empty list is returned on error
1944 list_free(rettv->vval.v_list);
1945 rettv_list_alloc(rettv);
1946 }
1947
1948 vim_free(prev);
1949 fclose(fd);
1950}
1951
1952/*
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001953 * "readblob()" function
1954 */
1955 void
1956f_readblob(typval_T *argvars, typval_T *rettv)
1957{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001958 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1959 return;
1960
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001961 read_file_or_blob(argvars, rettv, TRUE);
1962}
1963
1964/*
1965 * "readfile()" function
1966 */
1967 void
1968f_readfile(typval_T *argvars, typval_T *rettv)
1969{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001970 if (in_vim9script()
1971 && (check_for_nonempty_string_arg(argvars, 0) == FAIL
1972 || check_for_opt_string_arg(argvars, 1) == FAIL
1973 || (argvars[1].v_type != VAR_UNKNOWN
1974 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1975 return;
1976
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001977 read_file_or_blob(argvars, rettv, FALSE);
1978}
1979
1980/*
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001981 * "resolve()" function
1982 */
1983 void
1984f_resolve(typval_T *argvars, typval_T *rettv)
1985{
1986 char_u *p;
1987#ifdef HAVE_READLINK
1988 char_u *buf = NULL;
1989#endif
1990
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001991 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1992 return;
1993
Bram Moolenaarb005cd82019-09-04 15:54:55 +02001994 p = tv_get_string(&argvars[0]);
1995#ifdef FEAT_SHORTCUT
1996 {
1997 char_u *v = NULL;
1998
1999 v = mch_resolve_path(p, TRUE);
2000 if (v != NULL)
2001 rettv->vval.v_string = v;
2002 else
2003 rettv->vval.v_string = vim_strsave(p);
2004 }
2005#else
2006# ifdef HAVE_READLINK
2007 {
2008 char_u *cpy;
2009 int len;
2010 char_u *remain = NULL;
2011 char_u *q;
2012 int is_relative_to_current = FALSE;
2013 int has_trailing_pathsep = FALSE;
2014 int limit = 100;
2015
2016 p = vim_strsave(p);
Bram Moolenaar70188f52019-12-23 18:18:52 +01002017 if (p == NULL)
2018 goto fail;
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002019 if (p[0] == '.' && (vim_ispathsep(p[1])
2020 || (p[1] == '.' && (vim_ispathsep(p[2])))))
2021 is_relative_to_current = TRUE;
2022
2023 len = STRLEN(p);
Bram Moolenaar50c4e9e2020-10-05 20:38:06 +02002024 if (len > 1 && after_pathsep(p, p + len))
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002025 {
2026 has_trailing_pathsep = TRUE;
Bram Moolenaar26262f82019-09-04 20:59:15 +02002027 p[len - 1] = NUL; // the trailing slash breaks readlink()
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002028 }
2029
2030 q = getnextcomp(p);
2031 if (*q != NUL)
2032 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02002033 // Separate the first path component in "p", and keep the
2034 // remainder (beginning with the path separator).
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002035 remain = vim_strsave(q - 1);
2036 q[-1] = NUL;
2037 }
2038
2039 buf = alloc(MAXPATHL + 1);
2040 if (buf == NULL)
Bram Moolenaar70188f52019-12-23 18:18:52 +01002041 {
2042 vim_free(p);
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002043 goto fail;
Bram Moolenaar70188f52019-12-23 18:18:52 +01002044 }
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002045
2046 for (;;)
2047 {
2048 for (;;)
2049 {
2050 len = readlink((char *)p, (char *)buf, MAXPATHL);
2051 if (len <= 0)
2052 break;
2053 buf[len] = NUL;
2054
2055 if (limit-- == 0)
2056 {
2057 vim_free(p);
2058 vim_free(remain);
Bram Moolenaara6f79292022-01-04 21:30:47 +00002059 emsg(_(e_too_many_symbolic_links_cycle));
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002060 rettv->vval.v_string = NULL;
2061 goto fail;
2062 }
2063
Bram Moolenaar26262f82019-09-04 20:59:15 +02002064 // Ensure that the result will have a trailing path separator
2065 // if the argument has one.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002066 if (remain == NULL && has_trailing_pathsep)
2067 add_pathsep(buf);
2068
Bram Moolenaar26262f82019-09-04 20:59:15 +02002069 // Separate the first path component in the link value and
2070 // concatenate the remainders.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002071 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
2072 if (*q != NUL)
2073 {
2074 if (remain == NULL)
2075 remain = vim_strsave(q - 1);
2076 else
2077 {
2078 cpy = concat_str(q - 1, remain);
2079 if (cpy != NULL)
2080 {
2081 vim_free(remain);
2082 remain = cpy;
2083 }
2084 }
2085 q[-1] = NUL;
2086 }
2087
2088 q = gettail(p);
2089 if (q > p && *q == NUL)
2090 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02002091 // Ignore trailing path separator.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002092 q[-1] = NUL;
2093 q = gettail(p);
2094 }
2095 if (q > p && !mch_isFullName(buf))
2096 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02002097 // symlink is relative to directory of argument
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002098 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
2099 if (cpy != NULL)
2100 {
2101 STRCPY(cpy, p);
2102 STRCPY(gettail(cpy), buf);
2103 vim_free(p);
2104 p = cpy;
2105 }
2106 }
2107 else
2108 {
2109 vim_free(p);
2110 p = vim_strsave(buf);
2111 }
2112 }
2113
2114 if (remain == NULL)
2115 break;
2116
Bram Moolenaar26262f82019-09-04 20:59:15 +02002117 // Append the first path component of "remain" to "p".
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002118 q = getnextcomp(remain + 1);
2119 len = q - remain - (*q != NUL);
2120 cpy = vim_strnsave(p, STRLEN(p) + len);
2121 if (cpy != NULL)
2122 {
2123 STRNCAT(cpy, remain, len);
2124 vim_free(p);
2125 p = cpy;
2126 }
Bram Moolenaar26262f82019-09-04 20:59:15 +02002127 // Shorten "remain".
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002128 if (*q != NUL)
2129 STRMOVE(remain, q - 1);
2130 else
2131 VIM_CLEAR(remain);
2132 }
2133
Bram Moolenaar26262f82019-09-04 20:59:15 +02002134 // If the result is a relative path name, make it explicitly relative to
2135 // the current directory if and only if the argument had this form.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002136 if (!vim_ispathsep(*p))
2137 {
2138 if (is_relative_to_current
2139 && *p != NUL
2140 && !(p[0] == '.'
2141 && (p[1] == NUL
2142 || vim_ispathsep(p[1])
2143 || (p[1] == '.'
2144 && (p[2] == NUL
2145 || vim_ispathsep(p[2]))))))
2146 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02002147 // Prepend "./".
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002148 cpy = concat_str((char_u *)"./", p);
2149 if (cpy != NULL)
2150 {
2151 vim_free(p);
2152 p = cpy;
2153 }
2154 }
2155 else if (!is_relative_to_current)
2156 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02002157 // Strip leading "./".
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002158 q = p;
2159 while (q[0] == '.' && vim_ispathsep(q[1]))
2160 q += 2;
2161 if (q > p)
2162 STRMOVE(p, p + 2);
2163 }
2164 }
2165
Bram Moolenaar26262f82019-09-04 20:59:15 +02002166 // Ensure that the result will have no trailing path separator
2167 // if the argument had none. But keep "/" or "//".
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002168 if (!has_trailing_pathsep)
2169 {
2170 q = p + STRLEN(p);
2171 if (after_pathsep(p, q))
2172 *gettail_sep(p) = NUL;
2173 }
2174
2175 rettv->vval.v_string = p;
2176 }
2177# else
2178 rettv->vval.v_string = vim_strsave(p);
2179# endif
2180#endif
2181
2182 simplify_filename(rettv->vval.v_string);
2183
2184#ifdef HAVE_READLINK
2185fail:
2186 vim_free(buf);
2187#endif
2188 rettv->v_type = VAR_STRING;
2189}
2190
2191/*
2192 * "tempname()" function
2193 */
2194 void
2195f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
2196{
2197 static int x = 'A';
2198
2199 rettv->v_type = VAR_STRING;
2200 rettv->vval.v_string = vim_tempname(x, FALSE);
2201
Bram Moolenaar26262f82019-09-04 20:59:15 +02002202 // Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
2203 // names. Skip 'I' and 'O', they are used for shell redirection.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002204 do
2205 {
2206 if (x == 'Z')
2207 x = '0';
2208 else if (x == '9')
2209 x = 'A';
2210 else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002211 ++x;
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002212 } while (x == 'I' || x == 'O');
2213}
2214
2215/*
2216 * "writefile()" function
2217 */
2218 void
2219f_writefile(typval_T *argvars, typval_T *rettv)
2220{
2221 int binary = FALSE;
2222 int append = FALSE;
2223#ifdef HAVE_FSYNC
2224 int do_fsync = p_fs;
2225#endif
2226 char_u *fname;
2227 FILE *fd;
2228 int ret = 0;
2229 listitem_T *li;
2230 list_T *list = NULL;
2231 blob_T *blob = NULL;
2232
2233 rettv->vval.v_number = -1;
2234 if (check_secure())
2235 return;
2236
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002237 if (in_vim9script()
2238 && (check_for_list_or_blob_arg(argvars, 0) == FAIL
2239 || check_for_string_arg(argvars, 1) == FAIL
2240 || check_for_opt_string_arg(argvars, 2) == FAIL))
2241 return;
2242
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002243 if (argvars[0].v_type == VAR_LIST)
2244 {
2245 list = argvars[0].vval.v_list;
2246 if (list == NULL)
2247 return;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002248 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002249 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002250 if (tv_get_string_chk(&li->li_tv) == NULL)
2251 return;
2252 }
2253 else if (argvars[0].v_type == VAR_BLOB)
2254 {
2255 blob = argvars[0].vval.v_blob;
2256 if (blob == NULL)
2257 return;
2258 }
2259 else
2260 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002261 semsg(_(e_invalid_argument_str),
Bram Moolenaar18a2b872020-03-19 13:08:45 +01002262 _("writefile() first argument must be a List or a Blob"));
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002263 return;
2264 }
2265
2266 if (argvars[2].v_type != VAR_UNKNOWN)
2267 {
2268 char_u *arg2 = tv_get_string_chk(&argvars[2]);
2269
2270 if (arg2 == NULL)
2271 return;
2272 if (vim_strchr(arg2, 'b') != NULL)
2273 binary = TRUE;
2274 if (vim_strchr(arg2, 'a') != NULL)
2275 append = TRUE;
2276#ifdef HAVE_FSYNC
2277 if (vim_strchr(arg2, 's') != NULL)
2278 do_fsync = TRUE;
2279 else if (vim_strchr(arg2, 'S') != NULL)
2280 do_fsync = FALSE;
2281#endif
2282 }
2283
2284 fname = tv_get_string_chk(&argvars[1]);
2285 if (fname == NULL)
2286 return;
2287
Bram Moolenaar26262f82019-09-04 20:59:15 +02002288 // Always open the file in binary mode, library functions have a mind of
2289 // their own about CR-LF conversion.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002290 if (*fname == NUL || (fd = mch_fopen((char *)fname,
2291 append ? APPENDBIN : WRITEBIN)) == NULL)
2292 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002293 semsg(_(e_cant_create_file_str), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002294 ret = -1;
2295 }
2296 else if (blob)
2297 {
2298 if (write_blob(fd, blob) == FAIL)
2299 ret = -1;
2300#ifdef HAVE_FSYNC
2301 else if (do_fsync)
2302 // Ignore the error, the user wouldn't know what to do about it.
2303 // May happen for a device.
2304 vim_ignored = vim_fsync(fileno(fd));
2305#endif
2306 fclose(fd);
2307 }
2308 else
2309 {
2310 if (write_list(fd, list, binary) == FAIL)
2311 ret = -1;
2312#ifdef HAVE_FSYNC
2313 else if (do_fsync)
Bram Moolenaar26262f82019-09-04 20:59:15 +02002314 // Ignore the error, the user wouldn't know what to do about it.
2315 // May happen for a device.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002316 vim_ignored = vim_fsync(fileno(fd));
2317#endif
2318 fclose(fd);
2319 }
2320
2321 rettv->vval.v_number = ret;
2322}
2323
2324#endif // FEAT_EVAL
2325
2326#if defined(FEAT_BROWSE) || defined(PROTO)
2327/*
2328 * Generic browse function. Calls gui_mch_browse() when possible.
2329 * Later this may pop-up a non-GUI file selector (external command?).
2330 */
2331 char_u *
2332do_browse(
Bram Moolenaar26262f82019-09-04 20:59:15 +02002333 int flags, // BROWSE_SAVE and BROWSE_DIR
2334 char_u *title, // title for the window
2335 char_u *dflt, // default file name (may include directory)
2336 char_u *ext, // extension added
2337 char_u *initdir, // initial directory, NULL for current dir or
2338 // when using path from "dflt"
2339 char_u *filter, // file name filter
2340 buf_T *buf) // buffer to read/write for
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002341{
2342 char_u *fname;
Bram Moolenaar26262f82019-09-04 20:59:15 +02002343 static char_u *last_dir = NULL; // last used directory
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002344 char_u *tofree = NULL;
Bram Moolenaare1004402020-10-24 20:49:43 +02002345 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002346
Bram Moolenaar26262f82019-09-04 20:59:15 +02002347 // Must turn off browse to avoid that autocommands will get the
2348 // flag too!
Bram Moolenaare1004402020-10-24 20:49:43 +02002349 cmdmod.cmod_flags &= ~CMOD_BROWSE;
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002350
2351 if (title == NULL || *title == NUL)
2352 {
2353 if (flags & BROWSE_DIR)
2354 title = (char_u *)_("Select Directory dialog");
2355 else if (flags & BROWSE_SAVE)
2356 title = (char_u *)_("Save File dialog");
2357 else
2358 title = (char_u *)_("Open File dialog");
2359 }
2360
Bram Moolenaar26262f82019-09-04 20:59:15 +02002361 // When no directory specified, use default file name, default dir, buffer
2362 // dir, last dir or current dir
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002363 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
2364 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02002365 if (mch_isdir(dflt)) // default file name is a directory
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002366 {
2367 initdir = dflt;
2368 dflt = NULL;
2369 }
Bram Moolenaar26262f82019-09-04 20:59:15 +02002370 else if (gettail(dflt) != dflt) // default file name includes a path
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002371 {
2372 tofree = vim_strsave(dflt);
2373 if (tofree != NULL)
2374 {
2375 initdir = tofree;
2376 *gettail(initdir) = NUL;
2377 dflt = gettail(dflt);
2378 }
2379 }
2380 }
2381
2382 if (initdir == NULL || *initdir == NUL)
2383 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02002384 // When 'browsedir' is a directory, use it
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002385 if (STRCMP(p_bsdir, "last") != 0
2386 && STRCMP(p_bsdir, "buffer") != 0
2387 && STRCMP(p_bsdir, "current") != 0
2388 && mch_isdir(p_bsdir))
2389 initdir = p_bsdir;
Bram Moolenaar26262f82019-09-04 20:59:15 +02002390 // When saving or 'browsedir' is "buffer", use buffer fname
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002391 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
2392 && buf != NULL && buf->b_ffname != NULL)
2393 {
2394 if (dflt == NULL || *dflt == NUL)
2395 dflt = gettail(curbuf->b_ffname);
2396 tofree = vim_strsave(curbuf->b_ffname);
2397 if (tofree != NULL)
2398 {
2399 initdir = tofree;
2400 *gettail(initdir) = NUL;
2401 }
2402 }
Bram Moolenaar26262f82019-09-04 20:59:15 +02002403 // When 'browsedir' is "last", use dir from last browse
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002404 else if (*p_bsdir == 'l')
2405 initdir = last_dir;
Bram Moolenaar26262f82019-09-04 20:59:15 +02002406 // When 'browsedir is "current", use current directory. This is the
2407 // default already, leave initdir empty.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002408 }
2409
2410# ifdef FEAT_GUI
Bram Moolenaar26262f82019-09-04 20:59:15 +02002411 if (gui.in_use) // when this changes, also adjust f_has()!
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002412 {
2413 if (filter == NULL
2414# ifdef FEAT_EVAL
2415 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
2416 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
2417# endif
2418 )
2419 filter = BROWSE_FILTER_DEFAULT;
2420 if (flags & BROWSE_DIR)
2421 {
2422# if defined(FEAT_GUI_GTK) || defined(MSWIN)
Bram Moolenaar26262f82019-09-04 20:59:15 +02002423 // For systems that have a directory dialog.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002424 fname = gui_mch_browsedir(title, initdir);
2425# else
Bram Moolenaar26262f82019-09-04 20:59:15 +02002426 // Generic solution for selecting a directory: select a file and
2427 // remove the file name.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002428 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
2429# endif
2430# if !defined(FEAT_GUI_GTK)
Bram Moolenaar26262f82019-09-04 20:59:15 +02002431 // Win32 adds a dummy file name, others return an arbitrary file
2432 // name. GTK+ 2 returns only the directory,
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002433 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
2434 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02002435 // Remove the file name.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002436 char_u *tail = gettail_sep(fname);
2437
2438 if (tail == fname)
Bram Moolenaar26262f82019-09-04 20:59:15 +02002439 *tail++ = '.'; // use current dir
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002440 *tail = NUL;
2441 }
2442# endif
2443 }
2444 else
2445 fname = gui_mch_browse(flags & BROWSE_SAVE,
2446 title, dflt, ext, initdir, (char_u *)_(filter));
2447
Bram Moolenaar26262f82019-09-04 20:59:15 +02002448 // We hang around in the dialog for a while, the user might do some
2449 // things to our files. The Win32 dialog allows deleting or renaming
2450 // a file, check timestamps.
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002451 need_check_timestamps = TRUE;
2452 did_check_timestamps = FALSE;
2453 }
2454 else
2455# endif
2456 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02002457 // TODO: non-GUI file selector here
Bram Moolenaareaaac012022-01-02 17:00:40 +00002458 emsg(_(e_sorry_no_file_browser_in_console_mode));
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002459 fname = NULL;
2460 }
2461
Bram Moolenaar26262f82019-09-04 20:59:15 +02002462 // keep the directory for next time
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002463 if (fname != NULL)
2464 {
2465 vim_free(last_dir);
2466 last_dir = vim_strsave(fname);
2467 if (last_dir != NULL && !(flags & BROWSE_DIR))
2468 {
2469 *gettail(last_dir) = NUL;
2470 if (*last_dir == NUL)
2471 {
Bram Moolenaar26262f82019-09-04 20:59:15 +02002472 // filename only returned, must be in current dir
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002473 vim_free(last_dir);
2474 last_dir = alloc(MAXPATHL);
2475 if (last_dir != NULL)
2476 mch_dirname(last_dir, MAXPATHL);
2477 }
2478 }
2479 }
2480
2481 vim_free(tofree);
Bram Moolenaare1004402020-10-24 20:49:43 +02002482 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002483
2484 return fname;
2485}
2486#endif
2487
2488#if defined(FEAT_EVAL) || defined(PROTO)
2489
2490/*
2491 * "browse(save, title, initdir, default)" function
2492 */
2493 void
2494f_browse(typval_T *argvars UNUSED, typval_T *rettv)
2495{
2496# ifdef FEAT_BROWSE
2497 int save;
2498 char_u *title;
2499 char_u *initdir;
2500 char_u *defname;
2501 char_u buf[NUMBUFLEN];
2502 char_u buf2[NUMBUFLEN];
2503 int error = FALSE;
2504
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +01002505 if (in_vim9script()
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002506 && (check_for_bool_arg(argvars, 0) == FAIL
2507 || check_for_string_arg(argvars, 1) == FAIL
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002508 || check_for_string_arg(argvars, 2) == FAIL
2509 || check_for_string_arg(argvars, 3) == FAIL))
Bram Moolenaarf28f2ac2021-03-22 22:21:26 +01002510 return;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002511
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002512 save = (int)tv_get_number_chk(&argvars[0], &error);
2513 title = tv_get_string_chk(&argvars[1]);
2514 initdir = tv_get_string_buf_chk(&argvars[2], buf);
2515 defname = tv_get_string_buf_chk(&argvars[3], buf2);
2516
2517 if (error || title == NULL || initdir == NULL || defname == NULL)
2518 rettv->vval.v_string = NULL;
2519 else
2520 rettv->vval.v_string =
2521 do_browse(save ? BROWSE_SAVE : 0,
2522 title, defname, NULL, initdir, NULL, curbuf);
2523# else
2524 rettv->vval.v_string = NULL;
2525# endif
2526 rettv->v_type = VAR_STRING;
2527}
2528
2529/*
2530 * "browsedir(title, initdir)" function
2531 */
2532 void
2533f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
2534{
2535# ifdef FEAT_BROWSE
2536 char_u *title;
2537 char_u *initdir;
2538 char_u buf[NUMBUFLEN];
2539
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002540 if (in_vim9script()
2541 && (check_for_string_arg(argvars, 0) == FAIL
2542 || check_for_string_arg(argvars, 1) == FAIL))
2543 return;
2544
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002545 title = tv_get_string_chk(&argvars[0]);
2546 initdir = tv_get_string_buf_chk(&argvars[1], buf);
2547
2548 if (title == NULL || initdir == NULL)
2549 rettv->vval.v_string = NULL;
2550 else
2551 rettv->vval.v_string = do_browse(BROWSE_DIR,
2552 title, NULL, NULL, initdir, NULL, curbuf);
2553# else
2554 rettv->vval.v_string = NULL;
2555# endif
2556 rettv->v_type = VAR_STRING;
2557}
2558
2559#endif // FEAT_EVAL
Bram Moolenaar26262f82019-09-04 20:59:15 +02002560
2561/*
2562 * Replace home directory by "~" in each space or comma separated file name in
2563 * 'src'.
2564 * If anything fails (except when out of space) dst equals src.
2565 */
2566 void
2567home_replace(
2568 buf_T *buf, // when not NULL, check for help files
2569 char_u *src, // input file name
2570 char_u *dst, // where to put the result
2571 int dstlen, // maximum length of the result
2572 int one) // if TRUE, only replace one file name, include
2573 // spaces and commas in the file name.
2574{
2575 size_t dirlen = 0, envlen = 0;
2576 size_t len;
2577 char_u *homedir_env, *homedir_env_orig;
2578 char_u *p;
2579
2580 if (src == NULL)
2581 {
2582 *dst = NUL;
2583 return;
2584 }
2585
2586 /*
2587 * If the file is a help file, remove the path completely.
2588 */
2589 if (buf != NULL && buf->b_help)
2590 {
2591 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
2592 return;
2593 }
2594
2595 /*
2596 * We check both the value of the $HOME environment variable and the
2597 * "real" home directory.
2598 */
2599 if (homedir != NULL)
2600 dirlen = STRLEN(homedir);
2601
2602#ifdef VMS
2603 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
2604#else
2605 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
2606#endif
2607#ifdef MSWIN
2608 if (homedir_env == NULL)
2609 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
2610#endif
2611 // Empty is the same as not set.
2612 if (homedir_env != NULL && *homedir_env == NUL)
2613 homedir_env = NULL;
2614
2615 if (homedir_env != NULL && *homedir_env == '~')
2616 {
2617 int usedlen = 0;
2618 int flen;
2619 char_u *fbuf = NULL;
2620
2621 flen = (int)STRLEN(homedir_env);
2622 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
2623 &homedir_env, &fbuf, &flen);
2624 flen = (int)STRLEN(homedir_env);
2625 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
2626 // Remove the trailing / that is added to a directory.
2627 homedir_env[flen - 1] = NUL;
2628 }
2629
2630 if (homedir_env != NULL)
2631 envlen = STRLEN(homedir_env);
2632
2633 if (!one)
2634 src = skipwhite(src);
2635 while (*src && dstlen > 0)
2636 {
2637 /*
2638 * Here we are at the beginning of a file name.
2639 * First, check to see if the beginning of the file name matches
2640 * $HOME or the "real" home directory. Check that there is a '/'
2641 * after the match (so that if e.g. the file is "/home/pieter/bla",
2642 * and the home directory is "/home/piet", the file does not end up
2643 * as "~er/bla" (which would seem to indicate the file "bla" in user
2644 * er's home directory)).
2645 */
2646 p = homedir;
2647 len = dirlen;
2648 for (;;)
2649 {
2650 if ( len
2651 && fnamencmp(src, p, len) == 0
2652 && (vim_ispathsep(src[len])
2653 || (!one && (src[len] == ',' || src[len] == ' '))
2654 || src[len] == NUL))
2655 {
2656 src += len;
2657 if (--dstlen > 0)
2658 *dst++ = '~';
2659
Bram Moolenaar0e390f42020-06-10 13:12:28 +02002660 // Do not add directory separator into dst, because dst is
2661 // expected to just return the directory name without the
2662 // directory separator '/'.
Bram Moolenaar26262f82019-09-04 20:59:15 +02002663 break;
2664 }
2665 if (p == homedir_env)
2666 break;
2667 p = homedir_env;
2668 len = envlen;
2669 }
2670
2671 // if (!one) skip to separator: space or comma
2672 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
2673 *dst++ = *src++;
2674 // skip separator
2675 while ((*src == ' ' || *src == ',') && --dstlen > 0)
2676 *dst++ = *src++;
2677 }
2678 // if (dstlen == 0) out of space, what to do???
2679
2680 *dst = NUL;
2681
2682 if (homedir_env != homedir_env_orig)
2683 vim_free(homedir_env);
2684}
2685
2686/*
2687 * Like home_replace, store the replaced string in allocated memory.
2688 * When something fails, NULL is returned.
2689 */
2690 char_u *
2691home_replace_save(
2692 buf_T *buf, // when not NULL, check for help files
2693 char_u *src) // input file name
2694{
2695 char_u *dst;
2696 unsigned len;
2697
2698 len = 3; // space for "~/" and trailing NUL
2699 if (src != NULL) // just in case
2700 len += (unsigned)STRLEN(src);
2701 dst = alloc(len);
2702 if (dst != NULL)
2703 home_replace(buf, src, dst, len, TRUE);
2704 return dst;
2705}
2706
2707/*
2708 * Compare two file names and return:
2709 * FPC_SAME if they both exist and are the same file.
2710 * FPC_SAMEX if they both don't exist and have the same file name.
2711 * FPC_DIFF if they both exist and are different files.
2712 * FPC_NOTX if they both don't exist.
2713 * FPC_DIFFX if one of them doesn't exist.
2714 * For the first name environment variables are expanded if "expandenv" is
2715 * TRUE.
2716 */
2717 int
2718fullpathcmp(
2719 char_u *s1,
2720 char_u *s2,
2721 int checkname, // when both don't exist, check file names
2722 int expandenv)
2723{
2724#ifdef UNIX
2725 char_u exp1[MAXPATHL];
2726 char_u full1[MAXPATHL];
2727 char_u full2[MAXPATHL];
2728 stat_T st1, st2;
2729 int r1, r2;
2730
2731 if (expandenv)
2732 expand_env(s1, exp1, MAXPATHL);
2733 else
2734 vim_strncpy(exp1, s1, MAXPATHL - 1);
2735 r1 = mch_stat((char *)exp1, &st1);
2736 r2 = mch_stat((char *)s2, &st2);
2737 if (r1 != 0 && r2 != 0)
2738 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002739 // if mch_stat() doesn't work, may compare the names
Bram Moolenaar26262f82019-09-04 20:59:15 +02002740 if (checkname)
2741 {
2742 if (fnamecmp(exp1, s2) == 0)
2743 return FPC_SAMEX;
2744 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
2745 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
2746 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
2747 return FPC_SAMEX;
2748 }
2749 return FPC_NOTX;
2750 }
2751 if (r1 != 0 || r2 != 0)
2752 return FPC_DIFFX;
2753 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
2754 return FPC_SAME;
2755 return FPC_DIFF;
2756#else
2757 char_u *exp1; // expanded s1
2758 char_u *full1; // full path of s1
2759 char_u *full2; // full path of s2
2760 int retval = FPC_DIFF;
2761 int r1, r2;
2762
2763 // allocate one buffer to store three paths (alloc()/free() is slow!)
2764 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
2765 {
2766 full1 = exp1 + MAXPATHL;
2767 full2 = full1 + MAXPATHL;
2768
2769 if (expandenv)
2770 expand_env(s1, exp1, MAXPATHL);
2771 else
2772 vim_strncpy(exp1, s1, MAXPATHL - 1);
2773 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
2774 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
2775
2776 // If vim_FullName() fails, the file probably doesn't exist.
2777 if (r1 != OK && r2 != OK)
2778 {
2779 if (checkname && fnamecmp(exp1, s2) == 0)
2780 retval = FPC_SAMEX;
2781 else
2782 retval = FPC_NOTX;
2783 }
2784 else if (r1 != OK || r2 != OK)
2785 retval = FPC_DIFFX;
2786 else if (fnamecmp(full1, full2))
2787 retval = FPC_DIFF;
2788 else
2789 retval = FPC_SAME;
2790 vim_free(exp1);
2791 }
2792 return retval;
2793#endif
2794}
2795
2796/*
2797 * Get the tail of a path: the file name.
2798 * When the path ends in a path separator the tail is the NUL after it.
2799 * Fail safe: never returns NULL.
2800 */
2801 char_u *
2802gettail(char_u *fname)
2803{
2804 char_u *p1, *p2;
2805
2806 if (fname == NULL)
2807 return (char_u *)"";
2808 for (p1 = p2 = get_past_head(fname); *p2; ) // find last part of path
2809 {
2810 if (vim_ispathsep_nocolon(*p2))
2811 p1 = p2 + 1;
2812 MB_PTR_ADV(p2);
2813 }
2814 return p1;
2815}
2816
2817/*
2818 * Get pointer to tail of "fname", including path separators. Putting a NUL
2819 * here leaves the directory name. Takes care of "c:/" and "//".
2820 * Always returns a valid pointer.
2821 */
2822 char_u *
2823gettail_sep(char_u *fname)
2824{
2825 char_u *p;
2826 char_u *t;
2827
2828 p = get_past_head(fname); // don't remove the '/' from "c:/file"
2829 t = gettail(fname);
2830 while (t > p && after_pathsep(fname, t))
2831 --t;
2832#ifdef VMS
2833 // path separator is part of the path
2834 ++t;
2835#endif
2836 return t;
2837}
2838
2839/*
2840 * get the next path component (just after the next path separator).
2841 */
2842 char_u *
2843getnextcomp(char_u *fname)
2844{
2845 while (*fname && !vim_ispathsep(*fname))
2846 MB_PTR_ADV(fname);
2847 if (*fname)
2848 ++fname;
2849 return fname;
2850}
2851
2852/*
2853 * Get a pointer to one character past the head of a path name.
2854 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
2855 * If there is no head, path is returned.
2856 */
2857 char_u *
2858get_past_head(char_u *path)
2859{
2860 char_u *retval;
2861
2862#if defined(MSWIN)
2863 // may skip "c:"
2864 if (isalpha(path[0]) && path[1] == ':')
2865 retval = path + 2;
2866 else
2867 retval = path;
2868#else
2869# if defined(AMIGA)
2870 // may skip "label:"
2871 retval = vim_strchr(path, ':');
2872 if (retval == NULL)
2873 retval = path;
2874# else // Unix
2875 retval = path;
2876# endif
2877#endif
2878
2879 while (vim_ispathsep(*retval))
2880 ++retval;
2881
2882 return retval;
2883}
2884
2885/*
2886 * Return TRUE if 'c' is a path separator.
2887 * Note that for MS-Windows this includes the colon.
2888 */
2889 int
2890vim_ispathsep(int c)
2891{
2892#ifdef UNIX
2893 return (c == '/'); // UNIX has ':' inside file names
2894#else
2895# ifdef BACKSLASH_IN_FILENAME
2896 return (c == ':' || c == '/' || c == '\\');
2897# else
2898# ifdef VMS
2899 // server"user passwd"::device:[full.path.name]fname.extension;version"
2900 return (c == ':' || c == '[' || c == ']' || c == '/'
2901 || c == '<' || c == '>' || c == '"' );
2902# else
2903 return (c == ':' || c == '/');
2904# endif // VMS
2905# endif
2906#endif
2907}
2908
2909/*
2910 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
2911 */
2912 int
2913vim_ispathsep_nocolon(int c)
2914{
2915 return vim_ispathsep(c)
2916#ifdef BACKSLASH_IN_FILENAME
2917 && c != ':'
2918#endif
2919 ;
2920}
2921
2922/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002923 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
2924 * Also returns TRUE if there is no directory name.
2925 * "fname" must be writable!.
2926 */
2927 int
2928dir_of_file_exists(char_u *fname)
2929{
2930 char_u *p;
2931 int c;
2932 int retval;
2933
2934 p = gettail_sep(fname);
2935 if (p == fname)
2936 return TRUE;
2937 c = *p;
2938 *p = NUL;
2939 retval = mch_isdir(fname);
2940 *p = c;
2941 return retval;
2942}
2943
2944/*
2945 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
2946 * and deal with 'fileignorecase'.
2947 */
2948 int
2949vim_fnamecmp(char_u *x, char_u *y)
2950{
2951#ifdef BACKSLASH_IN_FILENAME
2952 return vim_fnamencmp(x, y, MAXPATHL);
2953#else
2954 if (p_fic)
2955 return MB_STRICMP(x, y);
2956 return STRCMP(x, y);
2957#endif
2958}
2959
2960 int
2961vim_fnamencmp(char_u *x, char_u *y, size_t len)
2962{
2963#ifdef BACKSLASH_IN_FILENAME
2964 char_u *px = x;
2965 char_u *py = y;
2966 int cx = NUL;
2967 int cy = NUL;
2968
2969 while (len > 0)
2970 {
2971 cx = PTR2CHAR(px);
2972 cy = PTR2CHAR(py);
2973 if (cx == NUL || cy == NUL
2974 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
2975 && !(cx == '/' && cy == '\\')
2976 && !(cx == '\\' && cy == '/')))
2977 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002978 len -= mb_ptr2len(px);
2979 px += mb_ptr2len(px);
2980 py += mb_ptr2len(py);
Bram Moolenaar26262f82019-09-04 20:59:15 +02002981 }
2982 if (len == 0)
2983 return 0;
2984 return (cx - cy);
2985#else
2986 if (p_fic)
2987 return MB_STRNICMP(x, y, len);
2988 return STRNCMP(x, y, len);
2989#endif
2990}
2991
2992/*
2993 * Concatenate file names fname1 and fname2 into allocated memory.
2994 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
2995 */
2996 char_u *
2997concat_fnames(char_u *fname1, char_u *fname2, int sep)
2998{
2999 char_u *dest;
3000
3001 dest = alloc(STRLEN(fname1) + STRLEN(fname2) + 3);
3002 if (dest != NULL)
3003 {
3004 STRCPY(dest, fname1);
3005 if (sep)
3006 add_pathsep(dest);
3007 STRCAT(dest, fname2);
3008 }
3009 return dest;
3010}
3011
3012/*
3013 * Add a path separator to a file name, unless it already ends in a path
3014 * separator.
3015 */
3016 void
3017add_pathsep(char_u *p)
3018{
3019 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
3020 STRCAT(p, PATHSEPSTR);
3021}
3022
3023/*
3024 * FullName_save - Make an allocated copy of a full file name.
3025 * Returns NULL when out of memory.
3026 */
3027 char_u *
3028FullName_save(
3029 char_u *fname,
3030 int force) // force expansion, even when it already looks
3031 // like a full path name
3032{
3033 char_u *buf;
3034 char_u *new_fname = NULL;
3035
3036 if (fname == NULL)
3037 return NULL;
3038
3039 buf = alloc(MAXPATHL);
3040 if (buf != NULL)
3041 {
3042 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
3043 new_fname = vim_strsave(buf);
3044 else
3045 new_fname = vim_strsave(fname);
3046 vim_free(buf);
3047 }
3048 return new_fname;
3049}
3050
3051/*
3052 * return TRUE if "fname" exists.
3053 */
3054 int
3055vim_fexists(char_u *fname)
3056{
3057 stat_T st;
3058
3059 if (mch_stat((char *)fname, &st))
3060 return FALSE;
3061 return TRUE;
3062}
3063
3064/*
3065 * Invoke expand_wildcards() for one pattern.
3066 * Expand items like "%:h" before the expansion.
3067 * Returns OK or FAIL.
3068 */
3069 int
3070expand_wildcards_eval(
3071 char_u **pat, // pointer to input pattern
3072 int *num_file, // resulting number of files
3073 char_u ***file, // array of resulting files
3074 int flags) // EW_DIR, etc.
3075{
3076 int ret = FAIL;
3077 char_u *eval_pat = NULL;
3078 char_u *exp_pat = *pat;
3079 char *ignored_msg;
3080 int usedlen;
3081
3082 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
3083 {
3084 ++emsg_off;
3085 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
3086 NULL, &ignored_msg, NULL);
3087 --emsg_off;
3088 if (eval_pat != NULL)
3089 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
3090 }
3091
3092 if (exp_pat != NULL)
3093 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
3094
3095 if (eval_pat != NULL)
3096 {
3097 vim_free(exp_pat);
3098 vim_free(eval_pat);
3099 }
3100
3101 return ret;
3102}
3103
3104/*
3105 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
3106 * 'wildignore'.
3107 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
3108 */
3109 int
3110expand_wildcards(
3111 int num_pat, // number of input patterns
3112 char_u **pat, // array of input patterns
3113 int *num_files, // resulting number of files
3114 char_u ***files, // array of resulting files
3115 int flags) // EW_DIR, etc.
3116{
3117 int retval;
3118 int i, j;
3119 char_u *p;
3120 int non_suf_match; // number without matching suffix
3121
3122 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
3123
3124 // When keeping all matches, return here
3125 if ((flags & EW_KEEPALL) || retval == FAIL)
3126 return retval;
3127
3128#ifdef FEAT_WILDIGN
3129 /*
3130 * Remove names that match 'wildignore'.
3131 */
3132 if (*p_wig)
3133 {
3134 char_u *ffname;
3135
3136 // check all files in (*files)[]
3137 for (i = 0; i < *num_files; ++i)
3138 {
3139 ffname = FullName_save((*files)[i], FALSE);
3140 if (ffname == NULL) // out of memory
3141 break;
3142# ifdef VMS
3143 vms_remove_version(ffname);
3144# endif
3145 if (match_file_list(p_wig, (*files)[i], ffname))
3146 {
3147 // remove this matching file from the list
3148 vim_free((*files)[i]);
3149 for (j = i; j + 1 < *num_files; ++j)
3150 (*files)[j] = (*files)[j + 1];
3151 --*num_files;
3152 --i;
3153 }
3154 vim_free(ffname);
3155 }
3156
3157 // If the number of matches is now zero, we fail.
3158 if (*num_files == 0)
3159 {
3160 VIM_CLEAR(*files);
3161 return FAIL;
3162 }
3163 }
3164#endif
3165
3166 /*
3167 * Move the names where 'suffixes' match to the end.
3168 */
3169 if (*num_files > 1)
3170 {
3171 non_suf_match = 0;
3172 for (i = 0; i < *num_files; ++i)
3173 {
3174 if (!match_suffix((*files)[i]))
3175 {
3176 /*
3177 * Move the name without matching suffix to the front
3178 * of the list.
3179 */
3180 p = (*files)[i];
3181 for (j = i; j > non_suf_match; --j)
3182 (*files)[j] = (*files)[j - 1];
3183 (*files)[non_suf_match++] = p;
3184 }
3185 }
3186 }
3187
3188 return retval;
3189}
3190
3191/*
3192 * Return TRUE if "fname" matches with an entry in 'suffixes'.
3193 */
3194 int
3195match_suffix(char_u *fname)
3196{
3197 int fnamelen, setsuflen;
3198 char_u *setsuf;
3199#define MAXSUFLEN 30 // maximum length of a file suffix
3200 char_u suf_buf[MAXSUFLEN];
3201
3202 fnamelen = (int)STRLEN(fname);
3203 setsuflen = 0;
3204 for (setsuf = p_su; *setsuf; )
3205 {
3206 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
3207 if (setsuflen == 0)
3208 {
3209 char_u *tail = gettail(fname);
3210
3211 // empty entry: match name without a '.'
3212 if (vim_strchr(tail, '.') == NULL)
3213 {
3214 setsuflen = 1;
3215 break;
3216 }
3217 }
3218 else
3219 {
3220 if (fnamelen >= setsuflen
3221 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
3222 (size_t)setsuflen) == 0)
3223 break;
3224 setsuflen = 0;
3225 }
3226 }
3227 return (setsuflen != 0);
3228}
3229
3230#ifdef VIM_BACKTICK
3231
3232/*
3233 * Return TRUE if we can expand this backtick thing here.
3234 */
3235 static int
3236vim_backtick(char_u *p)
3237{
3238 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
3239}
3240
3241/*
3242 * Expand an item in `backticks` by executing it as a command.
3243 * Currently only works when pat[] starts and ends with a `.
3244 * Returns number of file names found, -1 if an error is encountered.
3245 */
3246 static int
3247expand_backtick(
3248 garray_T *gap,
3249 char_u *pat,
3250 int flags) // EW_* flags
3251{
3252 char_u *p;
3253 char_u *cmd;
3254 char_u *buffer;
3255 int cnt = 0;
3256 int i;
3257
3258 // Create the command: lop off the backticks.
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003259 cmd = vim_strnsave(pat + 1, STRLEN(pat) - 2);
Bram Moolenaar26262f82019-09-04 20:59:15 +02003260 if (cmd == NULL)
3261 return -1;
3262
3263#ifdef FEAT_EVAL
3264 if (*cmd == '=') // `={expr}`: Expand expression
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003265 buffer = eval_to_string(cmd + 1, TRUE);
Bram Moolenaar26262f82019-09-04 20:59:15 +02003266 else
3267#endif
3268 buffer = get_cmd_output(cmd, NULL,
3269 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
3270 vim_free(cmd);
3271 if (buffer == NULL)
3272 return -1;
3273
3274 cmd = buffer;
3275 while (*cmd != NUL)
3276 {
3277 cmd = skipwhite(cmd); // skip over white space
3278 p = cmd;
3279 while (*p != NUL && *p != '\r' && *p != '\n') // skip over entry
3280 ++p;
3281 // add an entry if it is not empty
3282 if (p > cmd)
3283 {
3284 i = *p;
3285 *p = NUL;
3286 addfile(gap, cmd, flags);
3287 *p = i;
3288 ++cnt;
3289 }
3290 cmd = p;
3291 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
3292 ++cmd;
3293 }
3294
3295 vim_free(buffer);
3296 return cnt;
3297}
3298#endif // VIM_BACKTICK
3299
3300#if defined(MSWIN)
3301/*
3302 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
3303 * it's shared between these systems.
3304 */
3305
3306/*
3307 * comparison function for qsort in dos_expandpath()
3308 */
3309 static int
3310pstrcmp(const void *a, const void *b)
3311{
3312 return (pathcmp(*(char **)a, *(char **)b, -1));
3313}
3314
3315/*
3316 * Recursively expand one path component into all matching files and/or
3317 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
3318 * Return the number of matches found.
3319 * "path" has backslashes before chars that are not to be expanded, starting
3320 * at "path[wildoff]".
3321 * Return the number of matches found.
3322 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
3323 */
3324 static int
3325dos_expandpath(
3326 garray_T *gap,
3327 char_u *path,
3328 int wildoff,
3329 int flags, // EW_* flags
3330 int didstar) // expanded "**" once already
3331{
3332 char_u *buf;
3333 char_u *path_end;
3334 char_u *p, *s, *e;
3335 int start_len = gap->ga_len;
3336 char_u *pat;
3337 regmatch_T regmatch;
3338 int starts_with_dot;
3339 int matches;
3340 int len;
3341 int starstar = FALSE;
3342 static int stardepth = 0; // depth for "**" expansion
3343 HANDLE hFind = INVALID_HANDLE_VALUE;
3344 WIN32_FIND_DATAW wfb;
3345 WCHAR *wn = NULL; // UCS-2 name, NULL when not used.
3346 char_u *matchname;
3347 int ok;
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +02003348 char_u *p_alt;
Bram Moolenaar26262f82019-09-04 20:59:15 +02003349
3350 // Expanding "**" may take a long time, check for CTRL-C.
3351 if (stardepth > 0)
3352 {
3353 ui_breakcheck();
3354 if (got_int)
3355 return 0;
3356 }
3357
3358 // Make room for file name. When doing encoding conversion the actual
3359 // length may be quite a bit longer, thus use the maximum possible length.
3360 buf = alloc(MAXPATHL);
3361 if (buf == NULL)
3362 return 0;
3363
3364 /*
3365 * Find the first part in the path name that contains a wildcard or a ~1.
3366 * Copy it into buf, including the preceding characters.
3367 */
3368 p = buf;
3369 s = buf;
3370 e = NULL;
3371 path_end = path;
3372 while (*path_end != NUL)
3373 {
3374 // May ignore a wildcard that has a backslash before it; it will
3375 // be removed by rem_backslash() or file_pat_to_reg_pat() below.
3376 if (path_end >= path + wildoff && rem_backslash(path_end))
3377 *p++ = *path_end++;
3378 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
3379 {
3380 if (e != NULL)
3381 break;
3382 s = p + 1;
3383 }
3384 else if (path_end >= path + wildoff
3385 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
3386 e = p;
3387 if (has_mbyte)
3388 {
3389 len = (*mb_ptr2len)(path_end);
3390 STRNCPY(p, path_end, len);
3391 p += len;
3392 path_end += len;
3393 }
3394 else
3395 *p++ = *path_end++;
3396 }
3397 e = p;
3398 *e = NUL;
3399
3400 // now we have one wildcard component between s and e
3401 // Remove backslashes between "wildoff" and the start of the wildcard
3402 // component.
3403 for (p = buf + wildoff; p < s; ++p)
3404 if (rem_backslash(p))
3405 {
3406 STRMOVE(p, p + 1);
3407 --e;
3408 --s;
3409 }
3410
3411 // Check for "**" between "s" and "e".
3412 for (p = s; p < e; ++p)
3413 if (p[0] == '*' && p[1] == '*')
3414 starstar = TRUE;
3415
3416 starts_with_dot = *s == '.';
3417 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
3418 if (pat == NULL)
3419 {
3420 vim_free(buf);
3421 return 0;
3422 }
3423
3424 // compile the regexp into a program
3425 if (flags & (EW_NOERROR | EW_NOTWILD))
3426 ++emsg_silent;
3427 regmatch.rm_ic = TRUE; // Always ignore case
3428 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
3429 if (flags & (EW_NOERROR | EW_NOTWILD))
3430 --emsg_silent;
3431 vim_free(pat);
3432
3433 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
3434 {
3435 vim_free(buf);
3436 return 0;
3437 }
3438
3439 // remember the pattern or file name being looked for
3440 matchname = vim_strsave(s);
3441
3442 // If "**" is by itself, this is the first time we encounter it and more
3443 // is following then find matches without any directory.
3444 if (!didstar && stardepth < 100 && starstar && e - s == 2
3445 && *path_end == '/')
3446 {
3447 STRCPY(s, path_end + 1);
3448 ++stardepth;
3449 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
3450 --stardepth;
3451 }
3452
3453 // Scan all files in the directory with "dir/ *.*"
3454 STRCPY(s, "*.*");
3455 wn = enc_to_utf16(buf, NULL);
3456 if (wn != NULL)
3457 hFind = FindFirstFileW(wn, &wfb);
3458 ok = (hFind != INVALID_HANDLE_VALUE);
3459
3460 while (ok)
3461 {
3462 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +02003463
Bram Moolenaar26262f82019-09-04 20:59:15 +02003464 if (p == NULL)
3465 break; // out of memory
3466
Bram Moolenaar0dc5f602021-02-07 14:01:35 +01003467 // Do not use the alternate filename when the file name ends in '~',
3468 // because it picks up backup files: short name for "foo.vim~" is
3469 // "foo~1.vim", which matches "*.vim".
3470 if (*wfb.cAlternateFileName == NUL || p[STRLEN(p) - 1] == '~')
Bram Moolenaar40655d52020-04-06 23:49:50 +02003471 p_alt = NULL;
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +02003472 else
3473 p_alt = utf16_to_enc(wfb.cAlternateFileName, NULL);
3474
Bram Moolenaar26262f82019-09-04 20:59:15 +02003475 // Ignore entries starting with a dot, unless when asked for. Accept
3476 // all entries found with "matchname".
3477 if ((p[0] != '.' || starts_with_dot
3478 || ((flags & EW_DODOT)
3479 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
3480 && (matchname == NULL
3481 || (regmatch.regprog != NULL
Bram Moolenaar40655d52020-04-06 23:49:50 +02003482 && (vim_regexec(&regmatch, p, (colnr_T)0)
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +02003483 || (p_alt != NULL
Bram Moolenaar40655d52020-04-06 23:49:50 +02003484 && vim_regexec(&regmatch, p_alt, (colnr_T)0))))
Bram Moolenaar26262f82019-09-04 20:59:15 +02003485 || ((flags & EW_NOTWILD)
3486 && fnamencmp(path + (s - buf), p, e - s) == 0)))
3487 {
3488 STRCPY(s, p);
3489 len = (int)STRLEN(buf);
3490
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +02003491 if (starstar && stardepth < 100
3492 && (wfb.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
Bram Moolenaar26262f82019-09-04 20:59:15 +02003493 {
3494 // For "**" in the pattern first go deeper in the tree to
3495 // find matches.
3496 STRCPY(buf + len, "/**");
3497 STRCPY(buf + len + 3, path_end);
3498 ++stardepth;
3499 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
3500 --stardepth;
3501 }
3502
3503 STRCPY(buf + len, path_end);
3504 if (mch_has_exp_wildcard(path_end))
3505 {
3506 // need to expand another component of the path
3507 // remove backslashes for the remaining components only
3508 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
3509 }
3510 else
3511 {
3512 // no more wildcards, check if there is a match
3513 // remove backslashes for the remaining components only
3514 if (*path_end != 0)
3515 backslash_halve(buf + len + 1);
3516 if (mch_getperm(buf) >= 0) // add existing file
3517 addfile(gap, buf, flags);
3518 }
3519 }
3520
Bram Moolenaarc74fbfe2020-04-06 22:56:28 +02003521 vim_free(p_alt);
Bram Moolenaar26262f82019-09-04 20:59:15 +02003522 vim_free(p);
3523 ok = FindNextFileW(hFind, &wfb);
Bram Moolenaar26262f82019-09-04 20:59:15 +02003524 }
3525
3526 FindClose(hFind);
3527 vim_free(wn);
3528 vim_free(buf);
3529 vim_regfree(regmatch.regprog);
3530 vim_free(matchname);
3531
3532 matches = gap->ga_len - start_len;
3533 if (matches > 0)
3534 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
3535 sizeof(char_u *), pstrcmp);
3536 return matches;
3537}
3538
3539 int
3540mch_expandpath(
3541 garray_T *gap,
3542 char_u *path,
3543 int flags) // EW_* flags
3544{
3545 return dos_expandpath(gap, path, 0, flags, FALSE);
3546}
3547#endif // MSWIN
3548
3549#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
3550 || defined(PROTO)
3551/*
3552 * Unix style wildcard expansion code.
3553 * It's here because it's used both for Unix and Mac.
3554 */
3555 static int
3556pstrcmp(const void *a, const void *b)
3557{
3558 return (pathcmp(*(char **)a, *(char **)b, -1));
3559}
3560
3561/*
3562 * Recursively expand one path component into all matching files and/or
3563 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
3564 * "path" has backslashes before chars that are not to be expanded, starting
3565 * at "path + wildoff".
3566 * Return the number of matches found.
3567 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
3568 */
3569 int
3570unix_expandpath(
3571 garray_T *gap,
3572 char_u *path,
3573 int wildoff,
3574 int flags, // EW_* flags
3575 int didstar) // expanded "**" once already
3576{
3577 char_u *buf;
3578 char_u *path_end;
3579 char_u *p, *s, *e;
3580 int start_len = gap->ga_len;
3581 char_u *pat;
3582 regmatch_T regmatch;
3583 int starts_with_dot;
3584 int matches;
3585 int len;
3586 int starstar = FALSE;
3587 static int stardepth = 0; // depth for "**" expansion
3588
3589 DIR *dirp;
3590 struct dirent *dp;
3591
3592 // Expanding "**" may take a long time, check for CTRL-C.
3593 if (stardepth > 0)
3594 {
3595 ui_breakcheck();
3596 if (got_int)
3597 return 0;
3598 }
3599
3600 // make room for file name
3601 buf = alloc(STRLEN(path) + BASENAMELEN + 5);
3602 if (buf == NULL)
3603 return 0;
3604
3605 /*
3606 * Find the first part in the path name that contains a wildcard.
3607 * When EW_ICASE is set every letter is considered to be a wildcard.
3608 * Copy it into "buf", including the preceding characters.
3609 */
3610 p = buf;
3611 s = buf;
3612 e = NULL;
3613 path_end = path;
3614 while (*path_end != NUL)
3615 {
3616 // May ignore a wildcard that has a backslash before it; it will
3617 // be removed by rem_backslash() or file_pat_to_reg_pat() below.
3618 if (path_end >= path + wildoff && rem_backslash(path_end))
3619 *p++ = *path_end++;
3620 else if (*path_end == '/')
3621 {
3622 if (e != NULL)
3623 break;
3624 s = p + 1;
3625 }
3626 else if (path_end >= path + wildoff
3627 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
3628 || (!p_fic && (flags & EW_ICASE)
Bram Moolenaar5921aeb2022-02-19 11:20:12 +00003629 && vim_isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar26262f82019-09-04 20:59:15 +02003630 e = p;
3631 if (has_mbyte)
3632 {
3633 len = (*mb_ptr2len)(path_end);
3634 STRNCPY(p, path_end, len);
3635 p += len;
3636 path_end += len;
3637 }
3638 else
3639 *p++ = *path_end++;
3640 }
3641 e = p;
3642 *e = NUL;
3643
3644 // Now we have one wildcard component between "s" and "e".
3645 // Remove backslashes between "wildoff" and the start of the wildcard
3646 // component.
3647 for (p = buf + wildoff; p < s; ++p)
3648 if (rem_backslash(p))
3649 {
3650 STRMOVE(p, p + 1);
3651 --e;
3652 --s;
3653 }
3654
3655 // Check for "**" between "s" and "e".
3656 for (p = s; p < e; ++p)
3657 if (p[0] == '*' && p[1] == '*')
3658 starstar = TRUE;
3659
3660 // convert the file pattern to a regexp pattern
3661 starts_with_dot = *s == '.';
3662 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
3663 if (pat == NULL)
3664 {
3665 vim_free(buf);
3666 return 0;
3667 }
3668
3669 // compile the regexp into a program
3670 if (flags & EW_ICASE)
3671 regmatch.rm_ic = TRUE; // 'wildignorecase' set
3672 else
3673 regmatch.rm_ic = p_fic; // ignore case when 'fileignorecase' is set
3674 if (flags & (EW_NOERROR | EW_NOTWILD))
3675 ++emsg_silent;
3676 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
3677 if (flags & (EW_NOERROR | EW_NOTWILD))
3678 --emsg_silent;
3679 vim_free(pat);
3680
3681 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
3682 {
3683 vim_free(buf);
3684 return 0;
3685 }
3686
3687 // If "**" is by itself, this is the first time we encounter it and more
3688 // is following then find matches without any directory.
3689 if (!didstar && stardepth < 100 && starstar && e - s == 2
3690 && *path_end == '/')
3691 {
3692 STRCPY(s, path_end + 1);
3693 ++stardepth;
3694 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
3695 --stardepth;
3696 }
3697
3698 // open the directory for scanning
3699 *s = NUL;
3700 dirp = opendir(*buf == NUL ? "." : (char *)buf);
3701
3702 // Find all matching entries
3703 if (dirp != NULL)
3704 {
3705 for (;;)
3706 {
3707 dp = readdir(dirp);
3708 if (dp == NULL)
3709 break;
3710 if ((dp->d_name[0] != '.' || starts_with_dot
3711 || ((flags & EW_DODOT)
3712 && dp->d_name[1] != NUL
3713 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
3714 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
3715 (char_u *)dp->d_name, (colnr_T)0))
3716 || ((flags & EW_NOTWILD)
3717 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
3718 {
3719 STRCPY(s, dp->d_name);
3720 len = STRLEN(buf);
3721
3722 if (starstar && stardepth < 100)
3723 {
3724 // For "**" in the pattern first go deeper in the tree to
3725 // find matches.
3726 STRCPY(buf + len, "/**");
3727 STRCPY(buf + len + 3, path_end);
3728 ++stardepth;
3729 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
3730 --stardepth;
3731 }
3732
3733 STRCPY(buf + len, path_end);
3734 if (mch_has_exp_wildcard(path_end)) // handle more wildcards
3735 {
3736 // need to expand another component of the path
3737 // remove backslashes for the remaining components only
3738 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
3739 }
3740 else
3741 {
3742 stat_T sb;
3743
3744 // no more wildcards, check if there is a match
3745 // remove backslashes for the remaining components only
3746 if (*path_end != NUL)
3747 backslash_halve(buf + len + 1);
3748 // add existing file or symbolic link
3749 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
3750 : mch_getperm(buf) >= 0)
3751 {
3752#ifdef MACOS_CONVERT
3753 size_t precomp_len = STRLEN(buf)+1;
3754 char_u *precomp_buf =
3755 mac_precompose_path(buf, precomp_len, &precomp_len);
3756
3757 if (precomp_buf)
3758 {
3759 mch_memmove(buf, precomp_buf, precomp_len);
3760 vim_free(precomp_buf);
3761 }
3762#endif
3763 addfile(gap, buf, flags);
3764 }
3765 }
3766 }
3767 }
3768
3769 closedir(dirp);
3770 }
3771
3772 vim_free(buf);
3773 vim_regfree(regmatch.regprog);
3774
3775 matches = gap->ga_len - start_len;
3776 if (matches > 0)
3777 qsort(((char_u **)gap->ga_data) + start_len, matches,
3778 sizeof(char_u *), pstrcmp);
3779 return matches;
3780}
3781#endif
3782
3783/*
3784 * Return TRUE if "p" contains what looks like an environment variable.
3785 * Allowing for escaping.
3786 */
3787 static int
3788has_env_var(char_u *p)
3789{
3790 for ( ; *p; MB_PTR_ADV(p))
3791 {
3792 if (*p == '\\' && p[1] != NUL)
3793 ++p;
3794 else if (vim_strchr((char_u *)
3795#if defined(MSWIN)
3796 "$%"
3797#else
3798 "$"
3799#endif
3800 , *p) != NULL)
3801 return TRUE;
3802 }
3803 return FALSE;
3804}
3805
3806#ifdef SPECIAL_WILDCHAR
3807/*
3808 * Return TRUE if "p" contains a special wildcard character, one that Vim
3809 * cannot expand, requires using a shell.
3810 */
3811 static int
3812has_special_wildchar(char_u *p)
3813{
3814 for ( ; *p; MB_PTR_ADV(p))
3815 {
3816 // Disallow line break characters.
3817 if (*p == '\r' || *p == '\n')
3818 break;
3819 // Allow for escaping.
3820 if (*p == '\\' && p[1] != NUL && p[1] != '\r' && p[1] != '\n')
3821 ++p;
3822 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
3823 {
3824 // A { must be followed by a matching }.
3825 if (*p == '{' && vim_strchr(p, '}') == NULL)
3826 continue;
3827 // A quote and backtick must be followed by another one.
3828 if ((*p == '`' || *p == '\'') && vim_strchr(p, *p) == NULL)
3829 continue;
3830 return TRUE;
3831 }
3832 }
3833 return FALSE;
3834}
3835#endif
3836
3837/*
3838 * Generic wildcard expansion code.
3839 *
3840 * Characters in "pat" that should not be expanded must be preceded with a
3841 * backslash. E.g., "/path\ with\ spaces/my\*star*"
3842 *
3843 * Return FAIL when no single file was found. In this case "num_file" is not
3844 * set, and "file" may contain an error message.
3845 * Return OK when some files found. "num_file" is set to the number of
3846 * matches, "file" to the array of matches. Call FreeWild() later.
3847 */
3848 int
3849gen_expand_wildcards(
3850 int num_pat, // number of input patterns
3851 char_u **pat, // array of input patterns
3852 int *num_file, // resulting number of files
3853 char_u ***file, // array of resulting files
3854 int flags) // EW_* flags
3855{
3856 int i;
3857 garray_T ga;
3858 char_u *p;
3859 static int recursive = FALSE;
3860 int add_pat;
3861 int retval = OK;
3862#if defined(FEAT_SEARCHPATH)
3863 int did_expand_in_path = FALSE;
3864#endif
3865
3866 /*
3867 * expand_env() is called to expand things like "~user". If this fails,
3868 * it calls ExpandOne(), which brings us back here. In this case, always
3869 * call the machine specific expansion function, if possible. Otherwise,
3870 * return FAIL.
3871 */
3872 if (recursive)
3873#ifdef SPECIAL_WILDCHAR
3874 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
3875#else
3876 return FAIL;
3877#endif
3878
3879#ifdef SPECIAL_WILDCHAR
3880 /*
3881 * If there are any special wildcard characters which we cannot handle
3882 * here, call machine specific function for all the expansion. This
3883 * avoids starting the shell for each argument separately.
3884 * For `=expr` do use the internal function.
3885 */
3886 for (i = 0; i < num_pat; i++)
3887 {
3888 if (has_special_wildchar(pat[i])
3889# ifdef VIM_BACKTICK
3890 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
3891# endif
3892 )
3893 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
3894 }
3895#endif
3896
3897 recursive = TRUE;
3898
3899 /*
3900 * The matching file names are stored in a growarray. Init it empty.
3901 */
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003902 ga_init2(&ga, sizeof(char_u *), 30);
Bram Moolenaar26262f82019-09-04 20:59:15 +02003903
3904 for (i = 0; i < num_pat; ++i)
3905 {
3906 add_pat = -1;
3907 p = pat[i];
3908
3909#ifdef VIM_BACKTICK
3910 if (vim_backtick(p))
3911 {
3912 add_pat = expand_backtick(&ga, p, flags);
3913 if (add_pat == -1)
3914 retval = FAIL;
3915 }
3916 else
3917#endif
3918 {
3919 /*
3920 * First expand environment variables, "~/" and "~user/".
3921 */
3922 if ((has_env_var(p) && !(flags & EW_NOTENV)) || *p == '~')
3923 {
3924 p = expand_env_save_opt(p, TRUE);
3925 if (p == NULL)
3926 p = pat[i];
3927#ifdef UNIX
3928 /*
3929 * On Unix, if expand_env() can't expand an environment
3930 * variable, use the shell to do that. Discard previously
3931 * found file names and start all over again.
3932 */
3933 else if (has_env_var(p) || *p == '~')
3934 {
3935 vim_free(p);
3936 ga_clear_strings(&ga);
3937 i = mch_expand_wildcards(num_pat, pat, num_file, file,
3938 flags|EW_KEEPDOLLAR);
3939 recursive = FALSE;
3940 return i;
3941 }
3942#endif
3943 }
3944
3945 /*
3946 * If there are wildcards: Expand file names and add each match to
3947 * the list. If there is no match, and EW_NOTFOUND is given, add
3948 * the pattern.
3949 * If there are no wildcards: Add the file name if it exists or
3950 * when EW_NOTFOUND is given.
3951 */
3952 if (mch_has_exp_wildcard(p))
3953 {
3954#if defined(FEAT_SEARCHPATH)
3955 if ((flags & EW_PATH)
3956 && !mch_isFullName(p)
3957 && !(p[0] == '.'
3958 && (vim_ispathsep(p[1])
3959 || (p[1] == '.' && vim_ispathsep(p[2]))))
3960 )
3961 {
3962 // :find completion where 'path' is used.
3963 // Recursiveness is OK here.
3964 recursive = FALSE;
3965 add_pat = expand_in_path(&ga, p, flags);
3966 recursive = TRUE;
3967 did_expand_in_path = TRUE;
3968 }
3969 else
3970#endif
3971 add_pat = mch_expandpath(&ga, p, flags);
3972 }
3973 }
3974
3975 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
3976 {
3977 char_u *t = backslash_halve_save(p);
3978
3979 // When EW_NOTFOUND is used, always add files and dirs. Makes
3980 // "vim c:/" work.
3981 if (flags & EW_NOTFOUND)
3982 addfile(&ga, t, flags | EW_DIR | EW_FILE);
3983 else
3984 addfile(&ga, t, flags);
3985
3986 if (t != p)
3987 vim_free(t);
3988 }
3989
3990#if defined(FEAT_SEARCHPATH)
3991 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
3992 uniquefy_paths(&ga, p);
3993#endif
3994 if (p != pat[i])
3995 vim_free(p);
3996 }
3997
Bram Moolenaar566cc8c2020-06-29 21:14:51 +02003998 // When returning FAIL the array must be freed here.
3999 if (retval == FAIL)
4000 ga_clear(&ga);
4001
Bram Moolenaar26262f82019-09-04 20:59:15 +02004002 *num_file = ga.ga_len;
Bram Moolenaar566cc8c2020-06-29 21:14:51 +02004003 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data
4004 : (char_u **)_("no matches");
Bram Moolenaar26262f82019-09-04 20:59:15 +02004005
4006 recursive = FALSE;
4007
4008 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
4009}
4010
4011/*
4012 * Add a file to a file list. Accepted flags:
4013 * EW_DIR add directories
4014 * EW_FILE add files
4015 * EW_EXEC add executable files
4016 * EW_NOTFOUND add even when it doesn't exist
4017 * EW_ADDSLASH add slash after directory name
4018 * EW_ALLLINKS add symlink also when the referred file does not exist
4019 */
4020 void
4021addfile(
4022 garray_T *gap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004023 char_u *f, // filename
Bram Moolenaar26262f82019-09-04 20:59:15 +02004024 int flags)
4025{
4026 char_u *p;
4027 int isdir;
4028 stat_T sb;
4029
4030 // if the file/dir/link doesn't exist, may not add it
4031 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
4032 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
4033 return;
4034
4035#ifdef FNAME_ILLEGAL
4036 // if the file/dir contains illegal characters, don't add it
4037 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
4038 return;
4039#endif
4040
4041 isdir = mch_isdir(f);
4042 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
4043 return;
4044
4045 // If the file isn't executable, may not add it. Do accept directories.
4046 // When invoked from expand_shellcmd() do not use $PATH.
4047 if (!isdir && (flags & EW_EXEC)
4048 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
4049 return;
4050
4051 // Make room for another item in the file list.
4052 if (ga_grow(gap, 1) == FAIL)
4053 return;
4054
4055 p = alloc(STRLEN(f) + 1 + isdir);
4056 if (p == NULL)
4057 return;
4058
4059 STRCPY(p, f);
4060#ifdef BACKSLASH_IN_FILENAME
4061 slash_adjust(p);
4062#endif
4063 /*
4064 * Append a slash or backslash after directory names if none is present.
4065 */
4066#ifndef DONT_ADD_PATHSEP_TO_DIR
4067 if (isdir && (flags & EW_ADDSLASH))
4068 add_pathsep(p);
4069#endif
4070 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
4071}
4072
4073/*
4074 * Free the list of files returned by expand_wildcards() or other expansion
4075 * functions.
4076 */
4077 void
4078FreeWild(int count, char_u **files)
4079{
4080 if (count <= 0 || files == NULL)
4081 return;
4082 while (count--)
4083 vim_free(files[count]);
4084 vim_free(files);
4085}
4086
4087/*
4088 * Compare path "p[]" to "q[]".
4089 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]"
4090 * Return value like strcmp(p, q), but consider path separators.
4091 */
4092 int
4093pathcmp(const char *p, const char *q, int maxlen)
4094{
4095 int i, j;
4096 int c1, c2;
4097 const char *s = NULL;
4098
4099 for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);)
4100 {
4101 c1 = PTR2CHAR((char_u *)p + i);
4102 c2 = PTR2CHAR((char_u *)q + j);
4103
4104 // End of "p": check if "q" also ends or just has a slash.
4105 if (c1 == NUL)
4106 {
4107 if (c2 == NUL) // full match
4108 return 0;
4109 s = q;
4110 i = j;
4111 break;
4112 }
4113
4114 // End of "q": check if "p" just has a slash.
4115 if (c2 == NUL)
4116 {
4117 s = p;
4118 break;
4119 }
4120
4121 if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2)
4122#ifdef BACKSLASH_IN_FILENAME
4123 // consider '/' and '\\' to be equal
4124 && !((c1 == '/' && c2 == '\\')
4125 || (c1 == '\\' && c2 == '/'))
4126#endif
4127 )
4128 {
4129 if (vim_ispathsep(c1))
4130 return -1;
4131 if (vim_ispathsep(c2))
4132 return 1;
4133 return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2)
4134 : c1 - c2; // no match
4135 }
4136
Bram Moolenaar1614a142019-10-06 22:00:13 +02004137 i += mb_ptr2len((char_u *)p + i);
4138 j += mb_ptr2len((char_u *)q + j);
Bram Moolenaar26262f82019-09-04 20:59:15 +02004139 }
4140 if (s == NULL) // "i" or "j" ran into "maxlen"
4141 return 0;
4142
4143 c1 = PTR2CHAR((char_u *)s + i);
Bram Moolenaar1614a142019-10-06 22:00:13 +02004144 c2 = PTR2CHAR((char_u *)s + i + mb_ptr2len((char_u *)s + i));
Bram Moolenaar26262f82019-09-04 20:59:15 +02004145 // ignore a trailing slash, but not "//" or ":/"
4146 if (c2 == NUL
4147 && i > 0
4148 && !after_pathsep((char_u *)s, (char_u *)s + i)
4149#ifdef BACKSLASH_IN_FILENAME
4150 && (c1 == '/' || c1 == '\\')
4151#else
4152 && c1 == '/'
4153#endif
4154 )
4155 return 0; // match with trailing slash
4156 if (s == q)
4157 return -1; // no match
4158 return 1;
4159}
4160
4161/*
4162 * Return TRUE if "name" is a full (absolute) path name or URL.
4163 */
4164 int
4165vim_isAbsName(char_u *name)
4166{
4167 return (path_with_url(name) != 0 || mch_isFullName(name));
4168}
4169
4170/*
4171 * Get absolute file name into buffer "buf[len]".
4172 *
4173 * return FAIL for failure, OK otherwise
4174 */
4175 int
4176vim_FullName(
4177 char_u *fname,
4178 char_u *buf,
4179 int len,
4180 int force) // force expansion even when already absolute
4181{
4182 int retval = OK;
4183 int url;
4184
4185 *buf = NUL;
4186 if (fname == NULL)
4187 return FAIL;
4188
4189 url = path_with_url(fname);
4190 if (!url)
4191 retval = mch_FullName(fname, buf, len, force);
4192 if (url || retval == FAIL)
4193 {
4194 // something failed; use the file name (truncate when too long)
4195 vim_strncpy(buf, fname, len - 1);
4196 }
4197#if defined(MSWIN)
4198 slash_adjust(buf);
4199#endif
4200 return retval;
4201}