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