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