diff options
Diffstat (limited to 'src/nvim/path.c')
-rw-r--r-- | src/nvim/path.c | 88 |
1 files changed, 52 insertions, 36 deletions
diff --git a/src/nvim/path.c b/src/nvim/path.c index 5ac3d07f67..5cd93ab811 100644 --- a/src/nvim/path.c +++ b/src/nvim/path.c @@ -556,8 +556,9 @@ static size_t do_path_expand(garray_T *gap, const char_u *path, return 0; } - /* make room for file name */ - buf = xmalloc(STRLEN(path) + BASENAMELEN + 5); + // Make room for file name. When doing encoding conversion the actual + // length may be quite a bit longer, thus use the maximum possible length. + buf = xmalloc(MAXPATHL); /* * Find the first part in the path name that contains a wildcard. @@ -681,11 +682,16 @@ static size_t do_path_expand(garray_T *gap, const char_u *path, /* remove backslashes for the remaining components only */ (void)do_path_expand(gap, buf, len + 1, flags, false); } else { - /* no more wildcards, check if there is a match */ - /* remove backslashes for the remaining components only */ - if (*path_end != NUL) + FileInfo file_info; + + // no more wildcards, check if there is a match + // remove backslashes for the remaining components only + if (*path_end != NUL) { backslash_halve(buf + len + 1); - if (os_file_exists(buf)) { /* add existing file */ + } + // add existing file or symbolic link + if ((flags & EW_ALLLINKS) ? os_fileinfo_link((char *)buf, &file_info) + : os_file_exists(buf)) { addfile(gap, buf, flags); } } @@ -1153,12 +1159,17 @@ int gen_expand_wildcards(int num_pat, char_u **pat, int *num_file, add_pat = -1; p = pat[i]; - if (vim_backtick(p)) + if (vim_backtick(p)) { add_pat = expand_backtick(&ga, p, flags); - else { - /* - * First expand environment variables, "~/" and "~user/". - */ + if (add_pat == -1) { + recursive = false; + FreeWild(ga.ga_len, (char_u **)ga.ga_data); + *num_file = 0; + *file = NULL; + return FAIL; + } + } else { + // First expand environment variables, "~/" and "~user/". if (has_env_var(p) || *p == '~') { p = expand_env_save_opt(p, true); if (p == NULL) @@ -1241,13 +1252,10 @@ static int vim_backtick(char_u *p) return *p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`'; } -/* - * Expand an item in `backticks` by executing it as a command. - * Currently only works when pat[] starts and ends with a `. - * Returns number of file names found. - */ -static int -expand_backtick ( +// Expand an item in `backticks` by executing it as a command. +// Currently only works when pat[] starts and ends with a `. +// Returns number of file names found, -1 if an error is encountered. +static int expand_backtick( garray_T *gap, char_u *pat, int flags /* EW_* flags */ @@ -1268,8 +1276,9 @@ expand_backtick ( buffer = get_cmd_output(cmd, NULL, (flags & EW_SILENT) ? kShellOptSilent : 0, NULL); xfree(cmd); - if (buffer == NULL) - return 0; + if (buffer == NULL) { + return -1; + } cmd = buffer; while (*cmd != NUL) { @@ -1294,26 +1303,28 @@ expand_backtick ( return cnt; } -/* - * Add a file to a file list. Accepted flags: - * EW_DIR add directories - * EW_FILE add files - * EW_EXEC add executable files - * EW_NOTFOUND add even when it doesn't exist - * EW_ADDSLASH add slash after directory name - */ -void -addfile ( +// Add a file to a file list. Accepted flags: +// EW_DIR add directories +// EW_FILE add files +// EW_EXEC add executable files +// EW_NOTFOUND add even when it doesn't exist +// EW_ADDSLASH add slash after directory name +// EW_ALLLINKS add symlink also when the referred file does not exist +void addfile( garray_T *gap, char_u *f, /* filename */ int flags ) { bool isdir; + FileInfo file_info; - /* if the file/dir doesn't exist, may not add it */ - if (!(flags & EW_NOTFOUND) && !os_file_exists(f)) + // if the file/dir/link doesn't exist, may not add it + if (!(flags & EW_NOTFOUND) && + ((flags & EW_ALLLINKS) ? + !os_fileinfo_link((char *)f, &file_info) : !os_file_exists(f))) { return; + } #ifdef FNAME_ILLEGAL /* if the file/dir contains illegal characters, don't add it */ @@ -1768,19 +1779,20 @@ bool same_directory(char_u *f1, char_u *f2) */ int pathcmp(const char *p, const char *q, int maxlen) { - int i; + int i, j; int c1, c2; const char *s = NULL; - for (i = 0; maxlen < 0 || i < maxlen; i += MB_PTR2LEN((char_u *)p + i)) { + for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);) { c1 = PTR2CHAR((char_u *)p + i); - c2 = PTR2CHAR((char_u *)q + i); + c2 = PTR2CHAR((char_u *)q + j); /* End of "p": check if "q" also ends or just has a slash. */ if (c1 == NUL) { if (c2 == NUL) /* full match */ return 0; s = q; + i = j; break; } @@ -1804,9 +1816,13 @@ int pathcmp(const char *p, const char *q, int maxlen) return p_fic ? vim_toupper(c1) - vim_toupper(c2) : c1 - c2; /* no match */ } + + i += MB_PTR2LEN((char_u *)p + i); + j += MB_PTR2LEN((char_u *)q + j); } - if (s == NULL) /* "i" ran into "maxlen" */ + if (s == NULL) { // "i" or "j" ran into "maxlen" return 0; + } c1 = PTR2CHAR((char_u *)s + i); c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i)); |