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