mirrored from git://git.code.sf.net/p/zsh/code
-
Notifications
You must be signed in to change notification settings - Fork 519
Expand file tree
/
Copy pathutils.c
More file actions
7633 lines (6941 loc) · 169 KB
/
Copy pathutils.c
File metadata and controls
7633 lines (6941 loc) · 169 KB
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
/*
* utils.c - miscellaneous utilities
*
* This file is part of zsh, the Z shell.
*
* Copyright (c) 1992-1997 Paul Falstad
* All rights reserved.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and to distribute modified versions of this software for any
* purpose, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* In no event shall Paul Falstad or the Zsh Development Group be liable
* to any party for direct, indirect, special, incidental, or consequential
* damages arising out of the use of this software and its documentation,
* even if Paul Falstad and the Zsh Development Group have been advised of
* the possibility of such damage.
*
* Paul Falstad and the Zsh Development Group specifically disclaim any
* warranties, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose. The software
* provided hereunder is on an "as is" basis, and Paul Falstad and the
* Zsh Development Group have no obligation to provide maintenance,
* support, updates, enhancements, or modifications.
*
*/
#include "zsh.mdh"
#include "utils.pro"
/* name of script being sourced */
/**/
mod_export char *scriptname; /* is sometimes a function name */
/* filename of script or other file containing code source e.g. autoload */
/**/
mod_export char *scriptfilename;
/* != 0 if we are in a new style completion function */
/**/
mod_export int incompfunc;
#ifdef MULTIBYTE_SUPPORT
struct widechar_array {
wchar_t *chars;
size_t len;
};
typedef struct widechar_array *Widechar_array;
/*
* The wordchars variable turned into a wide character array.
* This is much more convenient for testing.
*/
static struct widechar_array wordchars_wide;
/*
* The same for the separators (IFS) array.
*/
static struct widechar_array ifs_wide;
/* Function to set one of the above from the multibyte array */
static void
set_widearray(char *mb_array, Widechar_array wca)
{
if (wca->chars) {
free(wca->chars);
wca->chars = NULL;
}
wca->len = 0;
if (!isset(MULTIBYTE))
return;
if (mb_array) {
VARARR(wchar_t, tmpwcs, strlen(mb_array));
wchar_t *wcptr = tmpwcs;
wint_t wci;
mb_charinit();
while (*mb_array) {
int mblen;
if (STOUC(*mb_array) <= 0x7f) {
mb_array++;
*wcptr++ = (wchar_t)*mb_array;
continue;
}
mblen = mb_metacharlenconv(mb_array, &wci);
if (!mblen)
break;
/* No good unless all characters are convertible */
if (wci == WEOF)
return;
*wcptr++ = (wchar_t)wci;
#ifdef DEBUG
/*
* This generates a warning from the compiler (and is
* indeed useless) if chars are unsigned. It's
* extreme paranoia anyway.
*/
if (wcptr[-1] < 0)
fprintf(stderr, "BUG: Bad cast to wchar_t\n");
#endif
mb_array += mblen;
}
wca->len = wcptr - tmpwcs;
wca->chars = (wchar_t *)zalloc(wca->len * sizeof(wchar_t));
wmemcpy(wca->chars, tmpwcs, wca->len);
}
}
#endif
/* Print an error
The following functions use the following printf-like format codes
(implemented by zerrmsg()):
Code Argument types Prints
%s const char * C string (null terminated)
%l const char *, int C string of given length (null not required)
%L long decimal value
%d int decimal value
%z zlong decimal value
%% (none) literal '%'
%c int character at that codepoint
%e int strerror() message (argument is typically 'errno')
*/
static void
zwarning(const char *cmd, const char *fmt, va_list ap)
{
if (isatty(2))
zleentry(ZLE_CMD_TRASH);
char *prefix = scriptname ? scriptname : (argzero ? argzero : "");
if (cmd) {
if (unset(SHINSTDIN) || locallevel) {
nicezputs(prefix, stderr);
fputc((unsigned char)':', stderr);
}
nicezputs(cmd, stderr);
fputc((unsigned char)':', stderr);
} else {
/*
* scriptname is set when sourcing scripts, so that we get the
* correct name instead of the generic name of whatever
* program/script is running. It's also set in shell functions,
* so test locallevel, too.
*/
nicezputs((isset(SHINSTDIN) && !locallevel) ? "zsh" : prefix, stderr);
fputc((unsigned char)':', stderr);
}
zerrmsg(stderr, fmt, ap);
}
/**/
mod_export void
zerr(VA_ALIST1(const char *fmt))
VA_DCL
{
va_list ap;
VA_DEF_ARG(const char *fmt);
if (errflag || noerrs) {
if (noerrs < 2)
errflag |= ERRFLAG_ERROR;
return;
}
errflag |= ERRFLAG_ERROR;
VA_START(ap, fmt);
VA_GET_ARG(ap, fmt, const char *);
zwarning(NULL, fmt, ap);
va_end(ap);
}
/**/
mod_export void
zerrnam(VA_ALIST2(const char *cmd, const char *fmt))
VA_DCL
{
va_list ap;
VA_DEF_ARG(const char *cmd);
VA_DEF_ARG(const char *fmt);
if (errflag || noerrs)
return;
errflag |= ERRFLAG_ERROR;
VA_START(ap, fmt);
VA_GET_ARG(ap, cmd, const char *);
VA_GET_ARG(ap, fmt, const char *);
zwarning(cmd, fmt, ap);
va_end(ap);
}
/**/
mod_export void
zwarn(VA_ALIST1(const char *fmt))
VA_DCL
{
va_list ap;
VA_DEF_ARG(const char *fmt);
if (errflag || noerrs)
return;
VA_START(ap, fmt);
VA_GET_ARG(ap, fmt, const char *);
zwarning(NULL, fmt, ap);
va_end(ap);
}
/**/
mod_export void
zwarnnam(VA_ALIST2(const char *cmd, const char *fmt))
VA_DCL
{
va_list ap;
VA_DEF_ARG(const char *cmd);
VA_DEF_ARG(const char *fmt);
if (errflag || noerrs)
return;
VA_START(ap, fmt);
VA_GET_ARG(ap, cmd, const char *);
VA_GET_ARG(ap, fmt, const char *);
zwarning(cmd, fmt, ap);
va_end(ap);
}
#ifdef DEBUG
/**/
mod_export void
dputs(VA_ALIST1(const char *message))
VA_DCL
{
char *filename;
FILE *file;
va_list ap;
VA_DEF_ARG(const char *message);
VA_START(ap, message);
VA_GET_ARG(ap, message, const char *);
if ((filename = getsparam_u("ZSH_DEBUG_LOG")) != NULL &&
(file = fopen(filename, "a")) != NULL) {
zerrmsg(file, message, ap);
fclose(file);
} else
zerrmsg(stderr, message, ap);
va_end(ap);
}
#endif /* DEBUG */
#ifdef __CYGWIN__
/*
* This works around an occasional problem with dllwrap on Cygwin, seen
* on at least two installations. It fails to find the last symbol
* exported in alphabetical order (in our case zwarnnam). Until this is
* properly categorised and fixed we add a dummy symbol at the end.
*/
mod_export void
zz_plural_z_alpha(void)
{
}
#endif
/**/
void
zerrmsg(FILE *file, const char *fmt, va_list ap)
{
const char *str;
int num;
long lnum;
#ifdef HAVE_STRERROR_R
#define ERRBUFSIZE (80)
int olderrno;
char errbuf[ERRBUFSIZE];
#endif
char *errmsg;
if ((unset(SHINSTDIN) || locallevel) && lineno) {
#if defined(ZLONG_IS_LONG_LONG) && defined(PRINTF_HAS_LLD)
fprintf(file, "%lld: ", lineno);
#else
fprintf(file, "%ld: ", (long)lineno);
#endif
} else
fputc((unsigned char)' ', file);
while (*fmt)
if (*fmt == '%') {
fmt++;
switch (*fmt++) {
case 's':
str = va_arg(ap, const char *);
nicezputs(str, file);
break;
case 'l': {
char *s;
str = va_arg(ap, const char *);
num = va_arg(ap, int);
num = metalen(str, num);
s = zhalloc(num + 1);
memcpy(s, str, num);
s[num] = '\0';
nicezputs(s, file);
break;
}
case 'L':
lnum = va_arg(ap, long);
fprintf(file, "%ld", lnum);
break;
case 'd':
num = va_arg(ap, int);
fprintf(file, "%d", num);
break;
case 'z':
{
zlong znum = va_arg(ap, zlong);
char buf[DIGBUFSIZE];
convbase(buf, znum, 10);
fputs(buf, file);
break;
}
case '%':
putc('%', file);
break;
case 'c':
num = va_arg(ap, int);
#ifdef MULTIBYTE_SUPPORT
mb_charinit();
zputs(wcs_nicechar(num, NULL, NULL), file);
#else
zputs(nicechar(num), file);
#endif
break;
case 'e':
/* print the corresponding message for this errno */
num = va_arg(ap, int);
if (num == EINTR) {
fputs("interrupt\n", file);
errflag |= ERRFLAG_ERROR;
return;
}
errmsg = strerror(num);
/* If the message is not about I/O problems, it looks better *
* if we uncapitalize the first letter of the message */
if (num == EIO)
fputs(errmsg, file);
else {
fputc(tulower(errmsg[0]), file);
fputs(errmsg + 1, file);
}
break;
/* When adding format codes, update the comment above zwarning(). */
}
} else {
putc(*fmt == Meta ? *++fmt ^ 32 : *fmt, file);
fmt++;
}
putc('\n', file);
fflush(file);
}
/*
* Wrapper for setupterm() and del_curterm().
* These are called from terminfo.c and termcap.c.
*/
static int term_count; /* reference count of cur_term */
/**/
mod_export void
zsetupterm(void)
{
#ifdef HAVE_SETUPTERM
int errret;
DPUTS(term_count < 0 || (term_count > 0 && !cur_term),
"inconsistent term_count and/or cur_term");
/*
* Just because we can't set up the terminal doesn't
* mean the modules hasn't booted---TERM may change,
* and it should be handled dynamically---so ignore errors here.
*/
if (term_count++ == 0)
(void)setupterm((char *)0, 1, &errret);
#endif
}
/**/
mod_export void
zdeleteterm(void)
{
#ifdef HAVE_SETUPTERM
DPUTS(term_count < 1 || !cur_term,
"inconsistent term_count and/or cur_term");
if (--term_count == 0)
del_curterm(cur_term);
#endif
}
/* Output a single character, for the termcap routines. *
* This is used instead of putchar since it can be a macro. */
/**/
mod_export int
putraw(int c)
{
putc(c, stdout);
return 0;
}
/* Output a single character, for the termcap routines. */
/**/
mod_export int
putshout(int c)
{
putc(c, shout);
return 0;
}
#ifdef MULTIBYTE_SUPPORT
/*
* Turn a character into a visible representation thereof. The visible
* string is put together in a static buffer, and this function returns
* a pointer to it. Printable characters stand for themselves, DEL is
* represented as "^?", newline and tab are represented as "\n" and
* "\t", and normal control characters are represented in "^C" form.
* Characters with bit 7 set, if unprintable, are represented as "\M-"
* followed by the visible representation of the character with bit 7
* stripped off. Tokens are interpreted, rather than being treated as
* literal characters.
*
* Note that the returned string is metafied, so that it must be
* treated like any other zsh internal string (and not, for example,
* output directly).
*
* This function is used even if MULTIBYTE_SUPPORT is defined: we
* use it as a fallback in case we couldn't identify a wide character
* in a multibyte string.
*/
/**/
mod_export char *
nicechar_sel(int c, int quotable)
{
static char buf[10];
char *s = buf;
c &= 0xff;
if (ZISPRINT(c))
goto done;
if (c & 0x80) {
if (isset(PRINTEIGHTBIT))
goto done;
*s++ = '\\';
*s++ = 'M';
*s++ = '-';
c &= 0x7f;
if(ZISPRINT(c))
goto done;
}
if (c == 0x7f) {
if (quotable) {
*s++ = '\\';
*s++ = 'C';
*s++ = '-';
} else
*s++ = '^';
c = '?';
} else if (c == '\n') {
*s++ = '\\';
c = 'n';
} else if (c == '\t') {
*s++ = '\\';
c = 't';
} else if (c < 0x20) {
if (quotable) {
*s++ = '\\';
*s++ = 'C';
*s++ = '-';
} else
*s++ = '^';
c += 0x40;
}
done:
/*
* The resulting string is still metafied, so check if
* we are returning a character in the range that needs metafication.
* This can't happen if the character is printed "nicely", so
* this results in a maximum of two bytes total (plus the null).
*/
if (imeta(c)) {
*s++ = Meta;
*s++ = c ^ 32;
} else
*s++ = c;
*s = 0;
return buf;
}
/**/
mod_export char *
nicechar(int c)
{
return nicechar_sel(c, 0);
}
#else /* MULTIBYTE_SUPPORT */
/**/
mod_export char *
nicechar(int c)
{
static char buf[10];
char *s = buf;
c &= 0xff;
if (ZISPRINT(c))
goto done;
if (c & 0x80) {
if (isset(PRINTEIGHTBIT))
goto done;
*s++ = '\\';
*s++ = 'M';
*s++ = '-';
c &= 0x7f;
if(ZISPRINT(c))
goto done;
}
if (c == 0x7f) {
*s++ = '\\';
*s++ = 'C';
*s++ = '-';
c = '?';
} else if (c == '\n') {
*s++ = '\\';
c = 'n';
} else if (c == '\t') {
*s++ = '\\';
c = 't';
} else if (c < 0x20) {
*s++ = '\\';
*s++ = 'C';
*s++ = '-';
c += 0x40;
}
done:
/*
* The resulting string is still metafied, so check if
* we are returning a character in the range that needs metafication.
* This can't happen if the character is printed "nicely", so
* this results in a maximum of two bytes total (plus the null).
*/
if (imeta(c)) {
*s++ = Meta;
*s++ = c ^ 32;
} else
*s++ = c;
*s = 0;
return buf;
}
#endif /* MULTIBYTE_SUPPORT */
/*
* Return 1 if nicechar() would reformat this character.
*/
/**/
mod_export int
is_nicechar(int c)
{
c &= 0xff;
if (ZISPRINT(c))
return 0;
if (c & 0x80)
return !isset(PRINTEIGHTBIT);
return (c == 0x7f || c == '\n' || c == '\t' || c < 0x20);
}
/**/
#ifdef MULTIBYTE_SUPPORT
static mbstate_t mb_shiftstate;
/*
* Initialise multibyte state: called before a sequence of
* wcs_nicechar(), mb_metacharlenconv(), or
* mb_charlenconv().
*/
/**/
mod_export void
mb_charinit(void)
{
memset(&mb_shiftstate, 0, sizeof(mb_shiftstate));
}
/*
* The number of bytes we need to allocate for a "nice" representation
* of a multibyte character.
*
* We double MB_CUR_MAX to take account of the fact that
* we may need to metafy. In fact the representation probably
* doesn't allow every character to be in the meta range, but
* we don't need to be too pedantic.
*
* The 12 is for the output of a UCS-4 code; we don't actually
* need this at the same time as MB_CUR_MAX, but again it's
* not worth calculating more exactly.
*/
#define NICECHAR_MAX (12 + 2*MB_CUR_MAX)
/*
* Input a wide character. Output a printable representation,
* which is a metafied multibyte string. With widthp return
* the printing width.
*
* swide, if non-NULL, is used to help the completion code, which needs
* to know the printing width of the each part of the representation.
* *swide is set to the part of the returned string where the wide
* character starts. Any string up to that point is ASCII characters,
* so the width of it is (*swide - <return_value>). Anything left is
* a single wide character corresponding to the remaining width.
* Either the initial ASCII part or the wide character part may be empty
* (but not both). (Note the complication that the wide character
* part may contain metafied characters.)
*
* The caller needs to call mb_charinit() before the first call, to
* set up the multibyte shift state for a range of characters.
*/
/**/
mod_export char *
wcs_nicechar_sel(wchar_t c, size_t *widthp, char **swidep, int quotable)
{
static char *buf;
static int bufalloc = 0, newalloc;
char *s, *mbptr;
int ret = 0;
VARARR(char, mbstr, MB_CUR_MAX);
/*
* We want buf to persist beyond the return. MB_CUR_MAX and hence
* NICECHAR_MAX may not be constant, so we have to allocate this at
* run time. (We could probably get away with just allocating a
* large buffer, in practice.) For efficiency, only reallocate if
* we really need to, since this function will be called frequently.
*/
newalloc = NICECHAR_MAX;
if (bufalloc != newalloc)
{
bufalloc = newalloc;
buf = (char *)zrealloc(buf, bufalloc);
}
s = buf;
if (!WC_ISPRINT(c) && (c < 0x80 || !isset(PRINTEIGHTBIT))) {
if (c == 0x7f) {
if (quotable) {
*s++ = '\\';
*s++ = 'C';
*s++ = '-';
} else
*s++ = '^';
c = '?';
} else if (c == L'\n') {
*s++ = '\\';
c = 'n';
} else if (c == L'\t') {
*s++ = '\\';
c = 't';
} else if (c < 0x20) {
if (quotable) {
*s++ = '\\';
*s++ = 'C';
*s++ = '-';
} else
*s++ = '^';
c += 0x40;
} else if (c >= 0x80) {
ret = -1;
}
}
if (ret != -1)
ret = wcrtomb(mbstr, c, &mb_shiftstate);
if (ret == -1) {
memset(&mb_shiftstate, 0, sizeof(mb_shiftstate));
/*
* Can't or don't want to convert character: use UCS-2 or
* UCS-4 code in print escape format.
*
* This comparison fails and generates a compiler warning
* if wchar_t is 16 bits, but the code is still correct.
*/
if (c >= 0x10000) {
sprintf(buf, "\\U%.8x", (unsigned int)c);
if (widthp)
*widthp = 10;
} else if (c >= 0x100) {
sprintf(buf, "\\u%.4x", (unsigned int)c);
if (widthp)
*widthp = 6;
} else {
strcpy(buf, nicechar_sel((int)c, quotable));
/*
* There may be metafied characters from nicechar(),
* so compute width and end position independently.
*/
if (widthp)
*widthp = ztrlen(buf);
if (swidep)
*swidep = buf + strlen(buf);
return buf;
}
if (swidep)
*swidep = widthp ? buf + *widthp : buf;
return buf;
}
if (widthp) {
int wcw = WCWIDTH(c);
*widthp = (s - buf);
if (wcw >= 0)
*widthp += wcw;
else
(*widthp)++;
}
if (swidep)
*swidep = s;
for (mbptr = mbstr; ret; s++, mbptr++, ret--) {
DPUTS(s >= buf + NICECHAR_MAX,
"BUG: buffer too small in wcs_nicechar");
if (imeta(*mbptr)) {
*s++ = Meta;
DPUTS(s >= buf + NICECHAR_MAX,
"BUG: buffer too small for metafied char in wcs_nicechar");
*s = *mbptr ^ 32;
} else {
*s = *mbptr;
}
}
*s = 0;
return buf;
}
/**/
mod_export char *
wcs_nicechar(wchar_t c, size_t *widthp, char **swidep)
{
return wcs_nicechar_sel(c, widthp, swidep, 0);
}
/*
* Return 1 if wcs_nicechar() would reformat this character for display.
*/
/**/
mod_export int is_wcs_nicechar(wchar_t c)
{
if (!WC_ISPRINT(c) && (c < 0x80 || !isset(PRINTEIGHTBIT))) {
if (c == 0x7f || c == L'\n' || c == L'\t' || c < 0x20)
return 1;
if (c >= 0x80) {
return (c >= 0x100 || is_nicechar((int)c));
}
}
return 0;
}
/**/
mod_export int
zwcwidth(wint_t wc)
{
int wcw;
/* assume a single-byte character if not valid */
if (wc == WEOF || unset(MULTIBYTE))
return 1;
wcw = WCWIDTH(wc);
/* if not printable, assume width 1 */
if (wcw < 0)
return 1;
return wcw;
}
/**/
#endif /* MULTIBYTE_SUPPORT */
/*
* Search the path for prog and return the file name.
* The returned value is unmetafied and in the unmeta storage
* area (N.B. should be duplicated if not used immediately and not
* equal to *namep).
*
* If namep is not NULL, *namep is set to the metafied programme
* name, which is in heap storage.
*/
/**/
char *
pathprog(char *prog, char **namep)
{
char **pp, ppmaxlen = 0, *buf, *funmeta;
struct stat st;
for (pp = path; *pp; pp++)
{
int len = strlen(*pp);
if (len > ppmaxlen)
ppmaxlen = len;
}
buf = zhalloc(ppmaxlen + strlen(prog) + 2);
for (pp = path; *pp; pp++) {
sprintf(buf, "%s/%s", *pp, prog);
funmeta = unmeta(buf);
if (access(funmeta, F_OK) == 0 &&
stat(funmeta, &st) >= 0 &&
!S_ISDIR(st.st_mode)) {
if (namep)
*namep = buf;
return funmeta;
}
}
return NULL;
}
/* get a symlink-free pathname for s relative to PWD */
/**/
char *
findpwd(char *s)
{
char *t;
if (*s == '/')
return xsymlink(s, 0);
s = tricat((pwd[1]) ? pwd : "", "/", s);
t = xsymlink(s, 0);
zsfree(s);
return t;
}
/* Check whether a string contains the *
* name of the present directory. */
/**/
int
ispwd(char *s)
{
struct stat sbuf, tbuf;
/* POSIX: environment PWD must be absolute */
if (*s != '/')
return 0;
if (stat((s = unmeta(s)), &sbuf) == 0 && stat(".", &tbuf) == 0)
if (sbuf.st_dev == tbuf.st_dev && sbuf.st_ino == tbuf.st_ino) {
/* POSIX: No element of $PWD may be "." or ".." */
while (*s) {
if (s[0] == '.' &&
(!s[1] || s[1] == '/' ||
(s[1] == '.' && (!s[2] || s[2] == '/'))))
break;
while (*s++ != '/' && *s)
continue;
}
return !*s;
}
return 0;
}
static char xbuf[PATH_MAX*2+1];
/**/
static char **
slashsplit(char *s)
{
char *t, **r, **q;
int t0;
if (!*s)
return (char **) zshcalloc(sizeof(char *));
for (t = s, t0 = 0; *t; t++)
if (*t == '/')
t0++;
q = r = (char **) zalloc(sizeof(char *) * (t0 + 2));
while ((t = strchr(s, '/'))) {
*q++ = ztrduppfx(s, t - s);
while (*t == '/')
t++;
if (!*t) {
*q = NULL;
return r;
}
s = t;
}
*q++ = ztrdup(s);
*q = NULL;
return r;
}
/* expands .. or . expressions and one level of symlinks
*
* Puts the result in the global "xbuf"
*/
/**/
static int
xsymlinks(char *s)
{
char **pp, **opp;
char xbuf2[PATH_MAX*3+1], xbuf3[PATH_MAX*2+1];
int t0, ret = 0;
zulong xbuflen = strlen(xbuf), pplen;
opp = pp = slashsplit(s);
for (; xbuflen < sizeof(xbuf) && *pp && ret >= 0; pp++) {
if (!strcmp(*pp, "."))
continue;
if (!strcmp(*pp, "..")) {
char *p;
if (!strcmp(xbuf, "/"))
continue;
if (!*xbuf)
continue;
p = xbuf + xbuflen;
while (*--p != '/')
xbuflen--;
*p = '\0';
/* The \0 isn't included in the length */
xbuflen--;
continue;
}
/* Includes null byte. */
pplen = strlen(*pp) + 1;
if (xbuflen + pplen + 1 > sizeof(xbuf2)) {
*xbuf = 0;
ret = -1;
break;
}
memcpy(xbuf2, xbuf, xbuflen);
xbuf2[xbuflen] = '/';
memcpy(xbuf2 + xbuflen + 1, *pp, pplen);
t0 = readlink(unmeta(xbuf2), xbuf3, PATH_MAX);
if (t0 == -1) {
if ((xbuflen += pplen) < sizeof(xbuf)) {
strcat(xbuf, "/");
strcat(xbuf, *pp);
} else {
*xbuf = 0;
ret = -1;
break;
}
} else {
ret = 1;
metafy(xbuf3, t0, META_NOALLOC);
{
/*
* If only one expansion requested, ensure the
* full path is in xbuf.
*/
zulong len = xbuflen;
if (*xbuf3 == '/')
strcpy(xbuf, xbuf3);
else if ((len += strlen(xbuf3) + 1) < sizeof(xbuf)) {
strcpy(xbuf + xbuflen, "/");
strcpy(xbuf + xbuflen + 1, xbuf3);
} else {
*xbuf = 0;
ret = -1;
break;
}
while (*++pp) {
zulong newlen = len + strlen(*pp) + 1;
if (newlen < sizeof(xbuf)) {
strcpy(xbuf + len, "/");
strcpy(xbuf + len + 1, *pp);
len = newlen;