diff options
Diffstat (limited to 'src/filepath.c')
-rw-r--r-- | src/filepath.c | 2138 |
1 files changed, 2138 insertions, 0 deletions
diff --git a/src/filepath.c b/src/filepath.c new file mode 100644 index 0000000000..0ba1a439d3 --- /dev/null +++ b/src/filepath.c @@ -0,0 +1,2138 @@ +/* vi:set ts=8 sts=4 sw=4 noet: + * + * VIM - Vi IMproved by Bram Moolenaar + * + * Do ":help uganda" in Vim to read copying and usage conditions. + * Do ":help credits" in Vim to see a list of people who contributed. + * See README.txt for an overview of the Vim source code. + */ + +/* + * filepath.c: dealing with file names ant paths. + */ + +#include "vim.h" + +#ifdef MSWIN +/* + * Functions for ":8" filename modifier: get 8.3 version of a filename. + */ + +/* + * Get the short path (8.3) for the filename in "fnamep". + * Only works for a valid file name. + * When the path gets longer "fnamep" is changed and the allocated buffer + * is put in "bufp". + * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path. + * Returns OK on success, FAIL on failure. + */ + static int +get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen) +{ + int l, len; + char_u *newbuf; + + len = *fnamelen; + l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len); + if (l > len - 1) + { + /* If that doesn't work (not enough space), then save the string + * and try again with a new buffer big enough. */ + newbuf = vim_strnsave(*fnamep, l); + if (newbuf == NULL) + return FAIL; + + vim_free(*bufp); + *fnamep = *bufp = newbuf; + + /* Really should always succeed, as the buffer is big enough. */ + l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1); + } + + *fnamelen = l; + return OK; +} + +/* + * Get the short path (8.3) for the filename in "fname". The converted + * path is returned in "bufp". + * + * Some of the directories specified in "fname" may not exist. This function + * will shorten the existing directories at the beginning of the path and then + * append the remaining non-existing path. + * + * fname - Pointer to the filename to shorten. On return, contains the + * pointer to the shortened pathname + * bufp - Pointer to an allocated buffer for the filename. + * fnamelen - Length of the filename pointed to by fname + * + * Returns OK on success (or nothing done) and FAIL on failure (out of memory). + */ + static int +shortpath_for_invalid_fname( + char_u **fname, + char_u **bufp, + int *fnamelen) +{ + char_u *short_fname, *save_fname, *pbuf_unused; + char_u *endp, *save_endp; + char_u ch; + int old_len, len; + int new_len, sfx_len; + int retval = OK; + + /* Make a copy */ + old_len = *fnamelen; + save_fname = vim_strnsave(*fname, old_len); + pbuf_unused = NULL; + short_fname = NULL; + + endp = save_fname + old_len - 1; /* Find the end of the copy */ + save_endp = endp; + + /* + * Try shortening the supplied path till it succeeds by removing one + * directory at a time from the tail of the path. + */ + len = 0; + for (;;) + { + /* go back one path-separator */ + while (endp > save_fname && !after_pathsep(save_fname, endp + 1)) + --endp; + if (endp <= save_fname) + break; /* processed the complete path */ + + /* + * Replace the path separator with a NUL and try to shorten the + * resulting path. + */ + ch = *endp; + *endp = 0; + short_fname = save_fname; + len = (int)STRLEN(short_fname) + 1; + if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL) + { + retval = FAIL; + goto theend; + } + *endp = ch; /* preserve the string */ + + if (len > 0) + break; /* successfully shortened the path */ + + /* failed to shorten the path. Skip the path separator */ + --endp; + } + + if (len > 0) + { + /* + * Succeeded in shortening the path. Now concatenate the shortened + * path with the remaining path at the tail. + */ + + /* Compute the length of the new path. */ + sfx_len = (int)(save_endp - endp) + 1; + new_len = len + sfx_len; + + *fnamelen = new_len; + vim_free(*bufp); + if (new_len > old_len) + { + /* There is not enough space in the currently allocated string, + * copy it to a buffer big enough. */ + *fname = *bufp = vim_strnsave(short_fname, new_len); + if (*fname == NULL) + { + retval = FAIL; + goto theend; + } + } + else + { + /* Transfer short_fname to the main buffer (it's big enough), + * unless get_short_pathname() did its work in-place. */ + *fname = *bufp = save_fname; + if (short_fname != save_fname) + vim_strncpy(save_fname, short_fname, len); + save_fname = NULL; + } + + /* concat the not-shortened part of the path */ + vim_strncpy(*fname + len, endp, sfx_len); + (*fname)[new_len] = NUL; + } + +theend: + vim_free(pbuf_unused); + vim_free(save_fname); + + return retval; +} + +/* + * Get a pathname for a partial path. + * Returns OK for success, FAIL for failure. + */ + static int +shortpath_for_partial( + char_u **fnamep, + char_u **bufp, + int *fnamelen) +{ + int sepcount, len, tflen; + char_u *p; + char_u *pbuf, *tfname; + int hasTilde; + + /* Count up the path separators from the RHS.. so we know which part + * of the path to return. */ + sepcount = 0; + for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p)) + if (vim_ispathsep(*p)) + ++sepcount; + + /* Need full path first (use expand_env() to remove a "~/") */ + hasTilde = (**fnamep == '~'); + if (hasTilde) + pbuf = tfname = expand_env_save(*fnamep); + else + pbuf = tfname = FullName_save(*fnamep, FALSE); + + len = tflen = (int)STRLEN(tfname); + + if (get_short_pathname(&tfname, &pbuf, &len) == FAIL) + return FAIL; + + if (len == 0) + { + /* Don't have a valid filename, so shorten the rest of the + * path if we can. This CAN give us invalid 8.3 filenames, but + * there's not a lot of point in guessing what it might be. + */ + len = tflen; + if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL) + return FAIL; + } + + /* Count the paths backward to find the beginning of the desired string. */ + for (p = tfname + len - 1; p >= tfname; --p) + { + if (has_mbyte) + p -= mb_head_off(tfname, p); + if (vim_ispathsep(*p)) + { + if (sepcount == 0 || (hasTilde && sepcount == 1)) + break; + else + sepcount --; + } + } + if (hasTilde) + { + --p; + if (p >= tfname) + *p = '~'; + else + return FAIL; + } + else + ++p; + + /* Copy in the string - p indexes into tfname - allocated at pbuf */ + vim_free(*bufp); + *fnamelen = (int)STRLEN(p); + *bufp = pbuf; + *fnamep = p; + + return OK; +} +#endif // MSWIN + +/* + * Adjust a filename, according to a string of modifiers. + * *fnamep must be NUL terminated when called. When returning, the length is + * determined by *fnamelen. + * Returns VALID_ flags or -1 for failure. + * When there is an error, *fnamep is set to NULL. + */ + int +modify_fname( + char_u *src, // string with modifiers + int tilde_file, // "~" is a file name, not $HOME + int *usedlen, // characters after src that are used + char_u **fnamep, // file name so far + char_u **bufp, // buffer for allocated file name or NULL + int *fnamelen) // length of fnamep +{ + int valid = 0; + char_u *tail; + char_u *s, *p, *pbuf; + char_u dirname[MAXPATHL]; + int c; + int has_fullname = 0; +#ifdef MSWIN + char_u *fname_start = *fnamep; + int has_shortname = 0; +#endif + +repeat: + /* ":p" - full path/file_name */ + if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p') + { + has_fullname = 1; + + valid |= VALID_PATH; + *usedlen += 2; + + /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */ + if ((*fnamep)[0] == '~' +#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME)) + && ((*fnamep)[1] == '/' +# ifdef BACKSLASH_IN_FILENAME + || (*fnamep)[1] == '\\' +# endif + || (*fnamep)[1] == NUL) +#endif + && !(tilde_file && (*fnamep)[1] == NUL) + ) + { + *fnamep = expand_env_save(*fnamep); + vim_free(*bufp); /* free any allocated file name */ + *bufp = *fnamep; + if (*fnamep == NULL) + return -1; + } + + /* When "/." or "/.." is used: force expansion to get rid of it. */ + for (p = *fnamep; *p != NUL; MB_PTR_ADV(p)) + { + if (vim_ispathsep(*p) + && p[1] == '.' + && (p[2] == NUL + || vim_ispathsep(p[2]) + || (p[2] == '.' + && (p[3] == NUL || vim_ispathsep(p[3]))))) + break; + } + + /* FullName_save() is slow, don't use it when not needed. */ + if (*p != NUL || !vim_isAbsName(*fnamep)) + { + *fnamep = FullName_save(*fnamep, *p != NUL); + vim_free(*bufp); /* free any allocated file name */ + *bufp = *fnamep; + if (*fnamep == NULL) + return -1; + } + +#ifdef MSWIN +# if _WIN32_WINNT >= 0x0500 + if (vim_strchr(*fnamep, '~') != NULL) + { + // Expand 8.3 filename to full path. Needed to make sure the same + // file does not have two different names. + // Note: problem does not occur if _WIN32_WINNT < 0x0500. + WCHAR *wfname = enc_to_utf16(*fnamep, NULL); + WCHAR buf[_MAX_PATH]; + + if (wfname != NULL) + { + if (GetLongPathNameW(wfname, buf, _MAX_PATH)) + { + char_u *p = utf16_to_enc(buf, NULL); + + if (p != NULL) + { + vim_free(*bufp); // free any allocated file name + *bufp = *fnamep = p; + } + } + vim_free(wfname); + } + } +# endif +#endif + /* Append a path separator to a directory. */ + if (mch_isdir(*fnamep)) + { + /* Make room for one or two extra characters. */ + *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2); + vim_free(*bufp); /* free any allocated file name */ + *bufp = *fnamep; + if (*fnamep == NULL) + return -1; + add_pathsep(*fnamep); + } + } + + /* ":." - path relative to the current directory */ + /* ":~" - path relative to the home directory */ + /* ":8" - shortname path - postponed till after */ + while (src[*usedlen] == ':' + && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8')) + { + *usedlen += 2; + if (c == '8') + { +#ifdef MSWIN + has_shortname = 1; /* Postpone this. */ +#endif + continue; + } + pbuf = NULL; + /* Need full path first (use expand_env() to remove a "~/") */ + if (!has_fullname) + { + if (c == '.' && **fnamep == '~') + p = pbuf = expand_env_save(*fnamep); + else + p = pbuf = FullName_save(*fnamep, FALSE); + } + else + p = *fnamep; + + has_fullname = 0; + + if (p != NULL) + { + if (c == '.') + { + mch_dirname(dirname, MAXPATHL); + s = shorten_fname(p, dirname); + if (s != NULL) + { + *fnamep = s; + if (pbuf != NULL) + { + vim_free(*bufp); /* free any allocated file name */ + *bufp = pbuf; + pbuf = NULL; + } + } + } + else + { + home_replace(NULL, p, dirname, MAXPATHL, TRUE); + /* Only replace it when it starts with '~' */ + if (*dirname == '~') + { + s = vim_strsave(dirname); + if (s != NULL) + { + *fnamep = s; + vim_free(*bufp); + *bufp = s; + } + } + } + vim_free(pbuf); + } + } + + tail = gettail(*fnamep); + *fnamelen = (int)STRLEN(*fnamep); + + /* ":h" - head, remove "/file_name", can be repeated */ + /* Don't remove the first "/" or "c:\" */ + while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h') + { + valid |= VALID_HEAD; + *usedlen += 2; + s = get_past_head(*fnamep); + while (tail > s && after_pathsep(s, tail)) + MB_PTR_BACK(*fnamep, tail); + *fnamelen = (int)(tail - *fnamep); +#ifdef VMS + if (*fnamelen > 0) + *fnamelen += 1; /* the path separator is part of the path */ +#endif + if (*fnamelen == 0) + { + /* Result is empty. Turn it into "." to make ":cd %:h" work. */ + p = vim_strsave((char_u *)"."); + if (p == NULL) + return -1; + vim_free(*bufp); + *bufp = *fnamep = tail = p; + *fnamelen = 1; + } + else + { + while (tail > s && !after_pathsep(s, tail)) + MB_PTR_BACK(*fnamep, tail); + } + } + + /* ":8" - shortname */ + if (src[*usedlen] == ':' && src[*usedlen + 1] == '8') + { + *usedlen += 2; +#ifdef MSWIN + has_shortname = 1; +#endif + } + +#ifdef MSWIN + /* + * Handle ":8" after we have done 'heads' and before we do 'tails'. + */ + if (has_shortname) + { + /* Copy the string if it is shortened by :h and when it wasn't copied + * yet, because we are going to change it in place. Avoids changing + * the buffer name for "%:8". */ + if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start) + { + p = vim_strnsave(*fnamep, *fnamelen); + if (p == NULL) + return -1; + vim_free(*bufp); + *bufp = *fnamep = p; + } + + /* Split into two implementations - makes it easier. First is where + * there isn't a full name already, second is where there is. */ + if (!has_fullname && !vim_isAbsName(*fnamep)) + { + if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL) + return -1; + } + else + { + int l = *fnamelen; + + /* Simple case, already have the full-name. + * Nearly always shorter, so try first time. */ + if (get_short_pathname(fnamep, bufp, &l) == FAIL) + return -1; + + if (l == 0) + { + /* Couldn't find the filename, search the paths. */ + l = *fnamelen; + if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL) + return -1; + } + *fnamelen = l; + } + } +#endif // MSWIN + + /* ":t" - tail, just the basename */ + if (src[*usedlen] == ':' && src[*usedlen + 1] == 't') + { + *usedlen += 2; + *fnamelen -= (int)(tail - *fnamep); + *fnamep = tail; + } + + /* ":e" - extension, can be repeated */ + /* ":r" - root, without extension, can be repeated */ + while (src[*usedlen] == ':' + && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r')) + { + /* find a '.' in the tail: + * - for second :e: before the current fname + * - otherwise: The last '.' + */ + if (src[*usedlen + 1] == 'e' && *fnamep > tail) + s = *fnamep - 2; + else + s = *fnamep + *fnamelen - 1; + for ( ; s > tail; --s) + if (s[0] == '.') + break; + if (src[*usedlen + 1] == 'e') /* :e */ + { + if (s > tail) + { + *fnamelen += (int)(*fnamep - (s + 1)); + *fnamep = s + 1; +#ifdef VMS + /* cut version from the extension */ + s = *fnamep + *fnamelen - 1; + for ( ; s > *fnamep; --s) + if (s[0] == ';') + break; + if (s > *fnamep) + *fnamelen = s - *fnamep; +#endif + } + else if (*fnamep <= tail) + *fnamelen = 0; + } + else /* :r */ + { + if (s > tail) /* remove one extension */ + *fnamelen = (int)(s - *fnamep); + } + *usedlen += 2; + } + + /* ":s?pat?foo?" - substitute */ + /* ":gs?pat?foo?" - global substitute */ + if (src[*usedlen] == ':' + && (src[*usedlen + 1] == 's' + || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's'))) + { + char_u *str; + char_u *pat; + char_u *sub; + int sep; + char_u *flags; + int didit = FALSE; + + flags = (char_u *)""; + s = src + *usedlen + 2; + if (src[*usedlen + 1] == 'g') + { + flags = (char_u *)"g"; + ++s; + } + + sep = *s++; + if (sep) + { + /* find end of pattern */ + p = vim_strchr(s, sep); + if (p != NULL) + { + pat = vim_strnsave(s, (int)(p - s)); + if (pat != NULL) + { + s = p + 1; + /* find end of substitution */ + p = vim_strchr(s, sep); + if (p != NULL) + { + sub = vim_strnsave(s, (int)(p - s)); + str = vim_strnsave(*fnamep, *fnamelen); + if (sub != NULL && str != NULL) + { + *usedlen = (int)(p + 1 - src); + s = do_string_sub(str, pat, sub, NULL, flags); + if (s != NULL) + { + *fnamep = s; + *fnamelen = (int)STRLEN(s); + vim_free(*bufp); + *bufp = s; + didit = TRUE; + } + } + vim_free(sub); + vim_free(str); + } + vim_free(pat); + } + } + /* after using ":s", repeat all the modifiers */ + if (didit) + goto repeat; + } + } + + if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S') + { + /* vim_strsave_shellescape() needs a NUL terminated string. */ + c = (*fnamep)[*fnamelen]; + if (c != NUL) + (*fnamep)[*fnamelen] = NUL; + p = vim_strsave_shellescape(*fnamep, FALSE, FALSE); + if (c != NUL) + (*fnamep)[*fnamelen] = c; + if (p == NULL) + return -1; + vim_free(*bufp); + *bufp = *fnamep = p; + *fnamelen = (int)STRLEN(p); + *usedlen += 2; + } + + return valid; +} + +#if defined(FEAT_EVAL) || defined(PROTO) + +/* + * "chdir(dir)" function + */ + void +f_chdir(typval_T *argvars, typval_T *rettv) +{ + char_u *cwd; + cdscope_T scope = CDSCOPE_GLOBAL; + + rettv->v_type = VAR_STRING; + rettv->vval.v_string = NULL; + + if (argvars[0].v_type != VAR_STRING) + return; + + // Return the current directory + cwd = alloc(MAXPATHL); + if (cwd != NULL) + { + if (mch_dirname(cwd, MAXPATHL) != FAIL) + { +#ifdef BACKSLASH_IN_FILENAME + slash_adjust(cwd); +#endif + rettv->vval.v_string = vim_strsave(cwd); + } + vim_free(cwd); + } + + if (curwin->w_localdir != NULL) + scope = CDSCOPE_WINDOW; + else if (curtab->tp_localdir != NULL) + scope = CDSCOPE_TABPAGE; + + if (!changedir_func(argvars[0].vval.v_string, TRUE, scope)) + // Directory change failed + VIM_CLEAR(rettv->vval.v_string); +} + +/* + * "delete()" function + */ + void +f_delete(typval_T *argvars, typval_T *rettv) +{ + char_u nbuf[NUMBUFLEN]; + char_u *name; + char_u *flags; + + rettv->vval.v_number = -1; + if (check_restricted() || check_secure()) + return; + + name = tv_get_string(&argvars[0]); + if (name == NULL || *name == NUL) + { + emsg(_(e_invarg)); + return; + } + + if (argvars[1].v_type != VAR_UNKNOWN) + flags = tv_get_string_buf(&argvars[1], nbuf); + else + flags = (char_u *)""; + + if (*flags == NUL) + /* delete a file */ + rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1; + else if (STRCMP(flags, "d") == 0) + /* delete an empty directory */ + rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1; + else if (STRCMP(flags, "rf") == 0) + /* delete a directory recursively */ + rettv->vval.v_number = delete_recursive(name); + else + semsg(_(e_invexpr2), flags); +} + +/* + * "executable()" function + */ + void +f_executable(typval_T *argvars, typval_T *rettv) +{ + char_u *name = tv_get_string(&argvars[0]); + + /* Check in $PATH and also check directly if there is a directory name. */ + rettv->vval.v_number = mch_can_exe(name, NULL, TRUE); +} + +/* + * "exepath()" function + */ + void +f_exepath(typval_T *argvars, typval_T *rettv) +{ + char_u *p = NULL; + + (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE); + rettv->v_type = VAR_STRING; + rettv->vval.v_string = p; +} + +/* + * "filereadable()" function + */ + void +f_filereadable(typval_T *argvars, typval_T *rettv) +{ + int fd; + char_u *p; + int n; + +#ifndef O_NONBLOCK +# define O_NONBLOCK 0 +#endif + p = tv_get_string(&argvars[0]); + if (*p && !mch_isdir(p) && (fd = mch_open((char *)p, + O_RDONLY | O_NONBLOCK, 0)) >= 0) + { + n = TRUE; + close(fd); + } + else + n = FALSE; + + rettv->vval.v_number = n; +} + +/* + * Return 0 for not writable, 1 for writable file, 2 for a dir which we have + * rights to write into. + */ + void +f_filewritable(typval_T *argvars, typval_T *rettv) +{ + rettv->vval.v_number = filewritable(tv_get_string(&argvars[0])); +} + + void +findfilendir( + typval_T *argvars UNUSED, + typval_T *rettv, + int find_what UNUSED) +{ +#ifdef FEAT_SEARCHPATH + char_u *fname; + char_u *fresult = NULL; + char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path; + char_u *p; + char_u pathbuf[NUMBUFLEN]; + int count = 1; + int first = TRUE; + int error = FALSE; +#endif + + rettv->vval.v_string = NULL; + rettv->v_type = VAR_STRING; + +#ifdef FEAT_SEARCHPATH + fname = tv_get_string(&argvars[0]); + + if (argvars[1].v_type != VAR_UNKNOWN) + { + p = tv_get_string_buf_chk(&argvars[1], pathbuf); + if (p == NULL) + error = TRUE; + else + { + if (*p != NUL) + path = p; + + if (argvars[2].v_type != VAR_UNKNOWN) + count = (int)tv_get_number_chk(&argvars[2], &error); + } + } + + if (count < 0 && rettv_list_alloc(rettv) == FAIL) + error = TRUE; + + if (*fname != NUL && !error) + { + do + { + if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST) + vim_free(fresult); + fresult = find_file_in_path_option(first ? fname : NULL, + first ? (int)STRLEN(fname) : 0, + 0, first, path, + find_what, + curbuf->b_ffname, + find_what == FINDFILE_DIR + ? (char_u *)"" : curbuf->b_p_sua); + first = FALSE; + + if (fresult != NULL && rettv->v_type == VAR_LIST) + list_append_string(rettv->vval.v_list, fresult, -1); + + } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL); + } + + if (rettv->v_type == VAR_STRING) + rettv->vval.v_string = fresult; +#endif +} + +/* + * "finddir({fname}[, {path}[, {count}]])" function + */ + void +f_finddir(typval_T *argvars, typval_T *rettv) +{ + findfilendir(argvars, rettv, FINDFILE_DIR); +} + +/* + * "findfile({fname}[, {path}[, {count}]])" function + */ + void +f_findfile(typval_T *argvars, typval_T *rettv) +{ + findfilendir(argvars, rettv, FINDFILE_FILE); +} + +/* + * "fnamemodify({fname}, {mods})" function + */ + void +f_fnamemodify(typval_T *argvars, typval_T *rettv) +{ + char_u *fname; + char_u *mods; + int usedlen = 0; + int len; + char_u *fbuf = NULL; + char_u buf[NUMBUFLEN]; + + fname = tv_get_string_chk(&argvars[0]); + mods = tv_get_string_buf_chk(&argvars[1], buf); + if (fname == NULL || mods == NULL) + fname = NULL; + else + { + len = (int)STRLEN(fname); + (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len); + } + + rettv->v_type = VAR_STRING; + if (fname == NULL) + rettv->vval.v_string = NULL; + else + rettv->vval.v_string = vim_strnsave(fname, len); + vim_free(fbuf); +} + +/* + * "getcwd()" function + * + * Return the current working directory of a window in a tab page. + * First optional argument 'winnr' is the window number or -1 and the second + * optional argument 'tabnr' is the tab page number. + * + * If no arguments are supplied, then return the directory of the current + * window. + * If only 'winnr' is specified and is not -1 or 0 then return the directory of + * the specified window. + * If 'winnr' is 0 then return the directory of the current window. + * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the + * directory of the specified tab page. Otherwise return the directory of the + * specified window in the specified tab page. + * If the window or the tab page doesn't exist then return NULL. + */ + void +f_getcwd(typval_T *argvars, typval_T *rettv) +{ + win_T *wp = NULL; + tabpage_T *tp = NULL; + char_u *cwd; + int global = FALSE; + + rettv->v_type = VAR_STRING; + rettv->vval.v_string = NULL; + + if (argvars[0].v_type == VAR_NUMBER + && argvars[0].vval.v_number == -1 + && argvars[1].v_type == VAR_UNKNOWN) + global = TRUE; + else + wp = find_tabwin(&argvars[0], &argvars[1], &tp); + + if (wp != NULL && wp->w_localdir != NULL) + rettv->vval.v_string = vim_strsave(wp->w_localdir); + else if (tp != NULL && tp->tp_localdir != NULL) + rettv->vval.v_string = vim_strsave(tp->tp_localdir); + else if (wp != NULL || tp != NULL || global) + { + if (globaldir != NULL) + rettv->vval.v_string = vim_strsave(globaldir); + else + { + cwd = alloc(MAXPATHL); + if (cwd != NULL) + { + if (mch_dirname(cwd, MAXPATHL) != FAIL) + rettv->vval.v_string = vim_strsave(cwd); + vim_free(cwd); + } + } + } +#ifdef BACKSLASH_IN_FILENAME + if (rettv->vval.v_string != NULL) + slash_adjust(rettv->vval.v_string); +#endif +} + +/* + * "getfperm({fname})" function + */ + void +f_getfperm(typval_T *argvars, typval_T *rettv) +{ + char_u *fname; + stat_T st; + char_u *perm = NULL; + char_u flags[] = "rwx"; + int i; + + fname = tv_get_string(&argvars[0]); + + rettv->v_type = VAR_STRING; + if (mch_stat((char *)fname, &st) >= 0) + { + perm = vim_strsave((char_u *)"---------"); + if (perm != NULL) + { + for (i = 0; i < 9; i++) + { + if (st.st_mode & (1 << (8 - i))) + perm[i] = flags[i % 3]; + } + } + } + rettv->vval.v_string = perm; +} + +/* + * "getfsize({fname})" function + */ + void +f_getfsize(typval_T *argvars, typval_T *rettv) +{ + char_u *fname; + stat_T st; + + fname = tv_get_string(&argvars[0]); + + rettv->v_type = VAR_NUMBER; + + if (mch_stat((char *)fname, &st) >= 0) + { + if (mch_isdir(fname)) + rettv->vval.v_number = 0; + else + { + rettv->vval.v_number = (varnumber_T)st.st_size; + + /* non-perfect check for overflow */ + if ((off_T)rettv->vval.v_number != (off_T)st.st_size) + rettv->vval.v_number = -2; + } + } + else + rettv->vval.v_number = -1; +} + +/* + * "getftime({fname})" function + */ + void +f_getftime(typval_T *argvars, typval_T *rettv) +{ + char_u *fname; + stat_T st; + + fname = tv_get_string(&argvars[0]); + + if (mch_stat((char *)fname, &st) >= 0) + rettv->vval.v_number = (varnumber_T)st.st_mtime; + else + rettv->vval.v_number = -1; +} + +/* + * "getftype({fname})" function + */ + void +f_getftype(typval_T *argvars, typval_T *rettv) +{ + char_u *fname; + stat_T st; + char_u *type = NULL; + char *t; + + fname = tv_get_string(&argvars[0]); + + rettv->v_type = VAR_STRING; + if (mch_lstat((char *)fname, &st) >= 0) + { + if (S_ISREG(st.st_mode)) + t = "file"; + else if (S_ISDIR(st.st_mode)) + t = "dir"; + else if (S_ISLNK(st.st_mode)) + t = "link"; + else if (S_ISBLK(st.st_mode)) + t = "bdev"; + else if (S_ISCHR(st.st_mode)) + t = "cdev"; + else if (S_ISFIFO(st.st_mode)) + t = "fifo"; + else if (S_ISSOCK(st.st_mode)) + t = "socket"; + else + t = "other"; + type = vim_strsave((char_u *)t); + } + rettv->vval.v_string = type; +} + +/* + * "glob()" function + */ + void +f_glob(typval_T *argvars, typval_T *rettv) +{ + int options = WILD_SILENT|WILD_USE_NL; + expand_T xpc; + int error = FALSE; + + /* When the optional second argument is non-zero, don't remove matches + * for 'wildignore' and don't put matches for 'suffixes' at the end. */ + rettv->v_type = VAR_STRING; + if (argvars[1].v_type != VAR_UNKNOWN) + { + if (tv_get_number_chk(&argvars[1], &error)) + options |= WILD_KEEP_ALL; + if (argvars[2].v_type != VAR_UNKNOWN) + { + if (tv_get_number_chk(&argvars[2], &error)) + rettv_list_set(rettv, NULL); + if (argvars[3].v_type != VAR_UNKNOWN + && tv_get_number_chk(&argvars[3], &error)) + options |= WILD_ALLLINKS; + } + } + if (!error) + { + ExpandInit(&xpc); + xpc.xp_context = EXPAND_FILES; + if (p_wic) + options += WILD_ICASE; + if (rettv->v_type == VAR_STRING) + rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]), + NULL, options, WILD_ALL); + else if (rettv_list_alloc(rettv) != FAIL) + { + int i; + + ExpandOne(&xpc, tv_get_string(&argvars[0]), + NULL, options, WILD_ALL_KEEP); + for (i = 0; i < xpc.xp_numfiles; i++) + list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1); + + ExpandCleanup(&xpc); + } + } + else + rettv->vval.v_string = NULL; +} + +/* + * "glob2regpat()" function + */ + void +f_glob2regpat(typval_T *argvars, typval_T *rettv) +{ + char_u *pat = tv_get_string_chk(&argvars[0]); + + rettv->v_type = VAR_STRING; + rettv->vval.v_string = (pat == NULL) + ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE); +} + +/* + * "globpath()" function + */ + void +f_globpath(typval_T *argvars, typval_T *rettv) +{ + int flags = WILD_IGNORE_COMPLETESLASH; + char_u buf1[NUMBUFLEN]; + char_u *file = tv_get_string_buf_chk(&argvars[1], buf1); + int error = FALSE; + garray_T ga; + int i; + + // When the optional second argument is non-zero, don't remove matches + // for 'wildignore' and don't put matches for 'suffixes' at the end. + rettv->v_type = VAR_STRING; + if (argvars[2].v_type != VAR_UNKNOWN) + { + if (tv_get_number_chk(&argvars[2], &error)) + flags |= WILD_KEEP_ALL; + if (argvars[3].v_type != VAR_UNKNOWN) + { + if (tv_get_number_chk(&argvars[3], &error)) + rettv_list_set(rettv, NULL); + if (argvars[4].v_type != VAR_UNKNOWN + && tv_get_number_chk(&argvars[4], &error)) + flags |= WILD_ALLLINKS; + } + } + if (file != NULL && !error) + { + ga_init2(&ga, (int)sizeof(char_u *), 10); + globpath(tv_get_string(&argvars[0]), file, &ga, flags); + if (rettv->v_type == VAR_STRING) + rettv->vval.v_string = ga_concat_strings(&ga, "\n"); + else if (rettv_list_alloc(rettv) != FAIL) + for (i = 0; i < ga.ga_len; ++i) + list_append_string(rettv->vval.v_list, + ((char_u **)(ga.ga_data))[i], -1); + ga_clear_strings(&ga); + } + else + rettv->vval.v_string = NULL; +} + +/* + * "isdirectory()" function + */ + void +f_isdirectory(typval_T *argvars, typval_T *rettv) +{ + rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0])); +} + +/* + * Evaluate "expr" (= "context") for readdir(). + */ + static int +readdir_checkitem(void *context, char_u *name) +{ + typval_T *expr = (typval_T *)context; + typval_T save_val; + typval_T rettv; + typval_T argv[2]; + int retval = 0; + int error = FALSE; + + if (expr->v_type == VAR_UNKNOWN) + return 1; + + prepare_vimvar(VV_VAL, &save_val); + set_vim_var_string(VV_VAL, name, -1); + argv[0].v_type = VAR_STRING; + argv[0].vval.v_string = name; + + if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL) + goto theend; + + retval = tv_get_number_chk(&rettv, &error); + if (error) + retval = -1; + clear_tv(&rettv); + +theend: + set_vim_var_string(VV_VAL, NULL, 0); + restore_vimvar(VV_VAL, &save_val); + return retval; +} + +/* + * Create the directory in which "dir" is located, and higher levels when + * needed. + * Return OK or FAIL. + */ + static int +mkdir_recurse(char_u *dir, int prot) +{ + char_u *p; + char_u *updir; + int r = FAIL; + + /* Get end of directory name in "dir". + * We're done when it's "/" or "c:/". */ + p = gettail_sep(dir); + if (p <= get_past_head(dir)) + return OK; + + /* If the directory exists we're done. Otherwise: create it.*/ + updir = vim_strnsave(dir, (int)(p - dir)); + if (updir == NULL) + return FAIL; + if (mch_isdir(updir)) + r = OK; + else if (mkdir_recurse(updir, prot) == OK) + r = vim_mkdir_emsg(updir, prot); + vim_free(updir); + return r; +} + +/* + * "mkdir()" function + */ + void +f_mkdir(typval_T *argvars, typval_T *rettv) +{ + char_u *dir; + char_u buf[NUMBUFLEN]; + int prot = 0755; + + rettv->vval.v_number = FAIL; + if (check_restri |