forked from vim/vim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filepath.c
4300 lines (3881 loc) · 99.4 KB
/
filepath.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* filepath.c: dealing with file names and paths.
*/
#include "vim.h"
#ifdef MSWIN
/*
* Functions for ":8" filename modifier: get 8.3 version of a filename.
*/
/*
* Get the short path (8.3) for the filename in "fnamep".
* Only works for a valid file name.
* When the path gets longer "fnamep" is changed and the allocated buffer
* is put in "bufp".
* *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
* Returns OK on success, FAIL on failure.
*/
static int
get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
{
int l, len;
WCHAR *newbuf;
WCHAR *wfname;
len = MAXPATHL;
newbuf = malloc(len * sizeof(*newbuf));
if (newbuf == NULL)
return FAIL;
wfname = enc_to_utf16(*fnamep, NULL);
if (wfname == NULL)
{
vim_free(newbuf);
return FAIL;
}
l = GetShortPathNameW(wfname, newbuf, len);
if (l > len - 1)
{
// If that doesn't work (not enough space), then save the string
// and try again with a new buffer big enough.
WCHAR *newbuf_t = newbuf;
newbuf = vim_realloc(newbuf, (l + 1) * sizeof(*newbuf));
if (newbuf == NULL)
{
vim_free(wfname);
vim_free(newbuf_t);
return FAIL;
}
// Really should always succeed, as the buffer is big enough.
l = GetShortPathNameW(wfname, newbuf, l+1);
}
if (l != 0)
{
char_u *p = utf16_to_enc(newbuf, NULL);
if (p != NULL)
{
vim_free(*bufp);
*fnamep = *bufp = p;
}
else
{
vim_free(wfname);
vim_free(newbuf);
return FAIL;
}
}
vim_free(wfname);
vim_free(newbuf);
*fnamelen = l == 0 ? l : (int)STRLEN(*bufp);
return OK;
}
/*
* Get the short path (8.3) for the filename in "fname". The converted
* path is returned in "bufp".
*
* Some of the directories specified in "fname" may not exist. This function
* will shorten the existing directories at the beginning of the path and then
* append the remaining non-existing path.
*
* fname - Pointer to the filename to shorten. On return, contains the
* pointer to the shortened pathname
* bufp - Pointer to an allocated buffer for the filename.
* fnamelen - Length of the filename pointed to by fname
*
* Returns OK on success (or nothing done) and FAIL on failure (out of memory).
*/
static int
shortpath_for_invalid_fname(
char_u **fname,
char_u **bufp,
int *fnamelen)
{
char_u *short_fname, *save_fname, *pbuf_unused;
char_u *endp, *save_endp;
char_u ch;
int old_len, len;
int new_len, sfx_len;
int retval = OK;
// Make a copy
old_len = *fnamelen;
save_fname = vim_strnsave(*fname, old_len);
pbuf_unused = NULL;
short_fname = NULL;
endp = save_fname + old_len - 1; // Find the end of the copy
save_endp = endp;
/*
* Try shortening the supplied path till it succeeds by removing one
* directory at a time from the tail of the path.
*/
len = 0;
for (;;)
{
// go back one path-separator
while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
--endp;
if (endp <= save_fname)
break; // processed the complete path
/*
* Replace the path separator with a NUL and try to shorten the
* resulting path.
*/
ch = *endp;
*endp = 0;
short_fname = save_fname;
len = (int)STRLEN(short_fname) + 1;
if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
{
retval = FAIL;
goto theend;
}
*endp = ch; // preserve the string
if (len > 0)
break; // successfully shortened the path
// failed to shorten the path. Skip the path separator
--endp;
}
if (len > 0)
{
/*
* Succeeded in shortening the path. Now concatenate the shortened
* path with the remaining path at the tail.
*/
// Compute the length of the new path.
sfx_len = (int)(save_endp - endp) + 1;
new_len = len + sfx_len;
*fnamelen = new_len;
vim_free(*bufp);
if (new_len > old_len)
{
// There is not enough space in the currently allocated string,
// copy it to a buffer big enough.
*fname = *bufp = vim_strnsave(short_fname, new_len);
if (*fname == NULL)
{
retval = FAIL;
goto theend;
}
}
else
{
// Transfer short_fname to the main buffer (it's big enough),
// unless get_short_pathname() did its work in-place.
*fname = *bufp = save_fname;
if (short_fname != save_fname)
STRNCPY(save_fname, short_fname, len);
save_fname = NULL;
}
// concat the not-shortened part of the path
if ((*fname + len) != endp)
vim_strncpy(*fname + len, endp, sfx_len);
(*fname)[new_len] = NUL;
}
theend:
vim_free(pbuf_unused);
vim_free(save_fname);
return retval;
}
/*
* Get a pathname for a partial path.
* Returns OK for success, FAIL for failure.
*/
static int
shortpath_for_partial(
char_u **fnamep,
char_u **bufp,
int *fnamelen)
{
int sepcount, len, tflen;
char_u *p;
char_u *pbuf, *tfname;
int hasTilde;
// Count up the path separators from the RHS.. so we know which part
// of the path to return.
sepcount = 0;
for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
if (vim_ispathsep(*p))
++sepcount;
// Need full path first (use expand_env() to remove a "~/")
hasTilde = (**fnamep == '~');
if (hasTilde)
pbuf = tfname = expand_env_save(*fnamep);
else
pbuf = tfname = FullName_save(*fnamep, FALSE);
len = tflen = (int)STRLEN(tfname);
if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
return FAIL;
if (len == 0)
{
// Don't have a valid filename, so shorten the rest of the
// path if we can. This CAN give us invalid 8.3 filenames, but
// there's not a lot of point in guessing what it might be.
len = tflen;
if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
return FAIL;
}
// Count the paths backward to find the beginning of the desired string.
for (p = tfname + len - 1; p >= tfname; --p)
{
if (has_mbyte)
p -= mb_head_off(tfname, p);
if (vim_ispathsep(*p))
{
if (sepcount == 0 || (hasTilde && sepcount == 1))
break;
else
sepcount --;
}
}
if (hasTilde)
{
--p;
if (p >= tfname)
*p = '~';
else
return FAIL;
}
else
++p;
// Copy in the string - p indexes into tfname - allocated at pbuf
vim_free(*bufp);
*fnamelen = (int)STRLEN(p);
*bufp = pbuf;
*fnamep = p;
return OK;
}
#endif // MSWIN
/*
* Adjust a filename, according to a string of modifiers.
* *fnamep must be NUL terminated when called. When returning, the length is
* determined by *fnamelen.
* Returns VALID_ flags or -1 for failure.
* When there is an error, *fnamep is set to NULL.
*/
int
modify_fname(
char_u *src, // string with modifiers
int tilde_file, // "~" is a file name, not $HOME
int *usedlen, // characters after src that are used
char_u **fnamep, // file name so far
char_u **bufp, // buffer for allocated file name or NULL
int *fnamelen) // length of fnamep
{
int valid = 0;
char_u *tail;
char_u *s, *p, *pbuf;
char_u dirname[MAXPATHL];
int c;
int has_fullname = 0;
int has_homerelative = 0;
#ifdef MSWIN
char_u *fname_start = *fnamep;
int has_shortname = 0;
#endif
repeat:
// ":p" - full path/file_name
if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
{
has_fullname = 1;
valid |= VALID_PATH;
*usedlen += 2;
// Expand "~/path" for all systems and "~user/path" for Unix and VMS
if ((*fnamep)[0] == '~'
#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
&& ((*fnamep)[1] == '/'
# ifdef BACKSLASH_IN_FILENAME
|| (*fnamep)[1] == '\\'
# endif
|| (*fnamep)[1] == NUL)
#endif
&& !(tilde_file && (*fnamep)[1] == NUL)
)
{
*fnamep = expand_env_save(*fnamep);
vim_free(*bufp); // free any allocated file name
*bufp = *fnamep;
if (*fnamep == NULL)
return -1;
}
// When "/." or "/.." is used: force expansion to get rid of it.
for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
{
if (vim_ispathsep(*p)
&& p[1] == '.'
&& (p[2] == NUL
|| vim_ispathsep(p[2])
|| (p[2] == '.'
&& (p[3] == NUL || vim_ispathsep(p[3])))))
break;
}
// FullName_save() is slow, don't use it when not needed.
if (*p != NUL || !vim_isAbsName(*fnamep))
{
*fnamep = FullName_save(*fnamep, *p != NUL);
vim_free(*bufp); // free any allocated file name
*bufp = *fnamep;
if (*fnamep == NULL)
return -1;
}
#ifdef MSWIN
# if _WIN32_WINNT >= 0x0500
if (vim_strchr(*fnamep, '~') != NULL)
{
// Expand 8.3 filename to full path. Needed to make sure the same
// file does not have two different names.
// Note: problem does not occur if _WIN32_WINNT < 0x0500.
WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
WCHAR buf[_MAX_PATH];
if (wfname != NULL)
{
if (GetLongPathNameW(wfname, buf, _MAX_PATH))
{
char_u *q = utf16_to_enc(buf, NULL);
if (q != NULL)
{
vim_free(*bufp); // free any allocated file name
*bufp = *fnamep = q;
}
}
vim_free(wfname);
}
}
# endif
#endif
// Append a path separator to a directory.
if (mch_isdir(*fnamep))
{
// Make room for one or two extra characters.
*fnamep = vim_strnsave(*fnamep, STRLEN(*fnamep) + 2);
vim_free(*bufp); // free any allocated file name
*bufp = *fnamep;
if (*fnamep == NULL)
return -1;
add_pathsep(*fnamep);
}
}
// ":." - path relative to the current directory
// ":~" - path relative to the home directory
// ":8" - shortname path - postponed till after
while (src[*usedlen] == ':'
&& ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
{
*usedlen += 2;
if (c == '8')
{
#ifdef MSWIN
has_shortname = 1; // Postpone this.
#endif
continue;
}
pbuf = NULL;
// Need full path first (use expand_env() to remove a "~/")
if (!has_fullname && !has_homerelative)
{
if (**fnamep == '~')
p = pbuf = expand_env_save(*fnamep);
else
p = pbuf = FullName_save(*fnamep, FALSE);
}
else
p = *fnamep;
has_fullname = 0;
if (p != NULL)
{
if (c == '.')
{
size_t namelen;
mch_dirname(dirname, MAXPATHL);
if (has_homerelative)
{
s = vim_strsave(dirname);
if (s != NULL)
{
home_replace(NULL, s, dirname, MAXPATHL, TRUE);
vim_free(s);
}
}
namelen = STRLEN(dirname);
// Do not call shorten_fname() here since it removes the prefix
// even though the path does not have a prefix.
if (fnamencmp(p, dirname, namelen) == 0)
{
p += namelen;
if (vim_ispathsep(*p))
{
while (*p && vim_ispathsep(*p))
++p;
*fnamep = p;
if (pbuf != NULL)
{
// free any allocated file name
vim_free(*bufp);
*bufp = pbuf;
pbuf = NULL;
}
}
}
}
else
{
home_replace(NULL, p, dirname, MAXPATHL, TRUE);
// Only replace it when it starts with '~'
if (*dirname == '~')
{
s = vim_strsave(dirname);
if (s != NULL)
{
*fnamep = s;
vim_free(*bufp);
*bufp = s;
has_homerelative = TRUE;
}
}
}
vim_free(pbuf);
}
}
tail = gettail(*fnamep);
*fnamelen = (int)STRLEN(*fnamep);
// ":h" - head, remove "/file_name", can be repeated
// Don't remove the first "/" or "c:\"
while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
{
valid |= VALID_HEAD;
*usedlen += 2;
s = get_past_head(*fnamep);
while (tail > s && after_pathsep(s, tail))
MB_PTR_BACK(*fnamep, tail);
*fnamelen = (int)(tail - *fnamep);
#ifdef VMS
if (*fnamelen > 0)
*fnamelen += 1; // the path separator is part of the path
#endif
if (*fnamelen == 0)
{
// Result is empty. Turn it into "." to make ":cd %:h" work.
p = vim_strsave((char_u *)".");
if (p == NULL)
return -1;
vim_free(*bufp);
*bufp = *fnamep = tail = p;
*fnamelen = 1;
}
else
{
while (tail > s && !after_pathsep(s, tail))
MB_PTR_BACK(*fnamep, tail);
}
}
// ":8" - shortname
if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
{
*usedlen += 2;
#ifdef MSWIN
has_shortname = 1;
#endif
}
#ifdef MSWIN
/*
* Handle ":8" after we have done 'heads' and before we do 'tails'.
*/
if (has_shortname)
{
// Copy the string if it is shortened by :h and when it wasn't copied
// yet, because we are going to change it in place. Avoids changing
// the buffer name for "%:8".
if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
{
p = vim_strnsave(*fnamep, *fnamelen);
if (p == NULL)
return -1;
vim_free(*bufp);
*bufp = *fnamep = p;
}
// Split into two implementations - makes it easier. First is where
// there isn't a full name already, second is where there is.
if (!has_fullname && !vim_isAbsName(*fnamep))
{
if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
return -1;
}
else
{
int l = *fnamelen;
// Simple case, already have the full-name.
// Nearly always shorter, so try first time.
if (get_short_pathname(fnamep, bufp, &l) == FAIL)
return -1;
if (l == 0)
{
// Couldn't find the filename, search the paths.
l = *fnamelen;
if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
return -1;
}
*fnamelen = l;
}
}
#endif // MSWIN
// ":t" - tail, just the basename
if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
{
*usedlen += 2;
*fnamelen -= (int)(tail - *fnamep);
*fnamep = tail;
}
// ":e" - extension, can be repeated
// ":r" - root, without extension, can be repeated
while (src[*usedlen] == ':'
&& (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
{
// find a '.' in the tail:
// - for second :e: before the current fname
// - otherwise: The last '.'
if (src[*usedlen + 1] == 'e' && *fnamep > tail)
s = *fnamep - 2;
else
s = *fnamep + *fnamelen - 1;
for ( ; s > tail; --s)
if (s[0] == '.')
break;
if (src[*usedlen + 1] == 'e') // :e
{
if (s > tail)
{
*fnamelen += (int)(*fnamep - (s + 1));
*fnamep = s + 1;
#ifdef VMS
// cut version from the extension
s = *fnamep + *fnamelen - 1;
for ( ; s > *fnamep; --s)
if (s[0] == ';')
break;
if (s > *fnamep)
*fnamelen = s - *fnamep;
#endif
}
else if (*fnamep <= tail)
*fnamelen = 0;
}
else // :r
{
char_u *limit = *fnamep;
if (limit < tail)
limit = tail;
if (s > limit) // remove one extension
*fnamelen = (int)(s - *fnamep);
}
*usedlen += 2;
}
// ":s?pat?foo?" - substitute
// ":gs?pat?foo?" - global substitute
if (src[*usedlen] == ':'
&& (src[*usedlen + 1] == 's'
|| (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
{
char_u *str;
char_u *pat;
char_u *sub;
int sep;
char_u *flags;
int didit = FALSE;
flags = (char_u *)"";
s = src + *usedlen + 2;
if (src[*usedlen + 1] == 'g')
{
flags = (char_u *)"g";
++s;
}
sep = *s++;
if (sep)
{
// find end of pattern
p = vim_strchr(s, sep);
if (p != NULL)
{
pat = vim_strnsave(s, p - s);
if (pat != NULL)
{
s = p + 1;
// find end of substitution
p = vim_strchr(s, sep);
if (p != NULL)
{
sub = vim_strnsave(s, p - s);
str = vim_strnsave(*fnamep, *fnamelen);
if (sub != NULL && str != NULL)
{
*usedlen = (int)(p + 1 - src);
s = do_string_sub(str, pat, sub, NULL, flags);
if (s != NULL)
{
*fnamep = s;
*fnamelen = (int)STRLEN(s);
vim_free(*bufp);
*bufp = s;
didit = TRUE;
}
}
vim_free(sub);
vim_free(str);
}
vim_free(pat);
}
}
// after using ":s", repeat all the modifiers
if (didit)
goto repeat;
}
}
if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
{
// vim_strsave_shellescape() needs a NUL terminated string.
c = (*fnamep)[*fnamelen];
if (c != NUL)
(*fnamep)[*fnamelen] = NUL;
p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
if (c != NUL)
(*fnamep)[*fnamelen] = c;
if (p == NULL)
return -1;
vim_free(*bufp);
*bufp = *fnamep = p;
*fnamelen = (int)STRLEN(p);
*usedlen += 2;
}
return valid;
}
/*
* Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
* "trim_len" specifies how many characters to keep for each directory.
* Must be 1 or more.
* It's done in-place.
*/
static void
shorten_dir_len(char_u *str, int trim_len)
{
char_u *tail, *s, *d;
int skip = FALSE;
int dirchunk_len = 0;
tail = gettail(str);
d = str;
for (s = str; ; ++s)
{
if (s >= tail) // copy the whole tail
{
*d++ = *s;
if (*s == NUL)
break;
}
else if (vim_ispathsep(*s)) // copy '/' and next char
{
*d++ = *s;
skip = FALSE;
dirchunk_len = 0;
}
else if (!skip)
{
*d++ = *s; // copy next char
if (*s != '~' && *s != '.') // and leading "~" and "."
{
++dirchunk_len; // only count word chars for the size
// keep copying chars until we have our preferred length (or
// until the above if/else branches move us along)
if (dirchunk_len >= trim_len)
skip = TRUE;
}
if (has_mbyte)
{
int l = mb_ptr2len(s);
while (--l > 0)
*d++ = *++s;
}
}
}
}
/*
* Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
* It's done in-place.
*/
void
shorten_dir(char_u *str)
{
shorten_dir_len(str, 1);
}
/*
* Return TRUE if "fname" is a readable file.
*/
int
file_is_readable(char_u *fname)
{
int fd;
#ifndef O_NONBLOCK
# define O_NONBLOCK 0
#endif
if (*fname && !mch_isdir(fname)
&& (fd = mch_open((char *)fname, O_RDONLY | O_NONBLOCK, 0)) >= 0)
{
close(fd);
return TRUE;
}
return FALSE;
}
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* "chdir(dir)" function
*/
void
f_chdir(typval_T *argvars, typval_T *rettv)
{
char_u *cwd;
cdscope_T scope = CDSCOPE_GLOBAL;
rettv->v_type = VAR_STRING;
rettv->vval.v_string = NULL;
if (argvars[0].v_type != VAR_STRING)
{
// Returning an empty string means it failed.
// No error message, for historic reasons.
if (in_vim9script())
(void) check_for_string_arg(argvars, 0);
return;
}
// Return the current directory
cwd = alloc(MAXPATHL);
if (cwd != NULL)
{
if (mch_dirname(cwd, MAXPATHL) != FAIL)
{
#ifdef BACKSLASH_IN_FILENAME
slash_adjust(cwd);
#endif
rettv->vval.v_string = vim_strsave(cwd);
}
vim_free(cwd);
}
if (curwin->w_localdir != NULL)
scope = CDSCOPE_WINDOW;
else if (curtab->tp_localdir != NULL)
scope = CDSCOPE_TABPAGE;
if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
// Directory change failed
VIM_CLEAR(rettv->vval.v_string);
}
/*
* "delete()" function
*/
void
f_delete(typval_T *argvars, typval_T *rettv)
{
char_u nbuf[NUMBUFLEN];
char_u *name;
char_u *flags;
rettv->vval.v_number = -1;
if (check_restricted() || check_secure())
return;
if (in_vim9script()
&& (check_for_string_arg(argvars, 0) == FAIL
|| check_for_opt_string_arg(argvars, 1) == FAIL))
return;
name = tv_get_string(&argvars[0]);
if (name == NULL || *name == NUL)
{
emsg(_(e_invalid_argument));
return;
}
if (argvars[1].v_type != VAR_UNKNOWN)
flags = tv_get_string_buf(&argvars[1], nbuf);
else
flags = (char_u *)"";
if (*flags == NUL)
// delete a file
rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
else if (STRCMP(flags, "d") == 0)
// delete an empty directory
rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
else if (STRCMP(flags, "rf") == 0)
// delete a directory recursively
rettv->vval.v_number = delete_recursive(name);
else
semsg(_(e_invalid_expression_str), flags);
}
/*
* "executable()" function
*/
void
f_executable(typval_T *argvars, typval_T *rettv)
{
if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
return;
// Check in $PATH and also check directly if there is a directory name.
rettv->vval.v_number = mch_can_exe(tv_get_string(&argvars[0]), NULL, TRUE);
}
/*
* "exepath()" function
*/
void
f_exepath(typval_T *argvars, typval_T *rettv)
{
char_u *p = NULL;
if (in_vim9script() && check_for_nonempty_string_arg(argvars, 0) == FAIL)
return;
(void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
rettv->v_type = VAR_STRING;
rettv->vval.v_string = p;
}
/*
* "filereadable()" function
*/
void
f_filereadable(typval_T *argvars, typval_T *rettv)
{
if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
return;
rettv->vval.v_number = file_is_readable(tv_get_string(&argvars[0]));
}
/*
* Return 0 for not writable, 1 for writable file, 2 for a dir which we have
* rights to write into.
*/
void
f_filewritable(typval_T *argvars, typval_T *rettv)
{
if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
return;
rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
}
static void
findfilendir(
typval_T *argvars UNUSED,
typval_T *rettv,
int find_what UNUSED)
{
char_u *fname;
char_u *fresult = NULL;
char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
char_u *p;
char_u pathbuf[NUMBUFLEN];
int count = 1;
int first = TRUE;
int error = FALSE;
rettv->vval.v_string = NULL;
rettv->v_type = VAR_STRING;
if (in_vim9script()
&& (check_for_nonempty_string_arg(argvars, 0) == FAIL
|| check_for_opt_string_arg(argvars, 1) == FAIL
|| (argvars[1].v_type != VAR_UNKNOWN
&& check_for_opt_number_arg(argvars, 2) == FAIL)))
return;
fname = tv_get_string(&argvars[0]);
if (argvars[1].v_type != VAR_UNKNOWN)
{
p = tv_get_string_buf_chk(&argvars[1], pathbuf);
if (p == NULL)
error = TRUE;
else
{
if (*p != NUL)
path = p;
if (argvars[2].v_type != VAR_UNKNOWN)
count = (int)tv_get_number_chk(&argvars[2], &error);
}
}
if (count < 0 && rettv_list_alloc(rettv) == FAIL)
error = TRUE;
if (*fname != NUL && !error)
{
do
{
if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
vim_free(fresult);
fresult = find_file_in_path_option(first ? fname : NULL,
first ? (int)STRLEN(fname) : 0,
0, first, path,
find_what,
curbuf->b_ffname,
find_what == FINDFILE_DIR
? (char_u *)"" : curbuf->b_p_sua);
first = FALSE;
if (fresult != NULL && rettv->v_type == VAR_LIST)
list_append_string(rettv->vval.v_list, fresult, -1);