/
glob.c
3598 lines (3306 loc) · 87.9 KB
/
glob.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
/*
* glob.c - filename generation
*
* 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 "glob.pro"
#if defined(OFF_T_IS_64_BIT) && defined(__GNUC__)
# define ALIGN64 __attribute__((aligned(8)))
#else
# define ALIGN64
#endif
/* flag for CSHNULLGLOB */
typedef struct gmatch *Gmatch;
struct gmatch {
char *name;
/*
* Array of sort strings: one for each GS_EXEC sort type in
* the glob qualifiers.
*/
char **sortstrs;
off_t size ALIGN64;
long atime;
long mtime;
long ctime;
long links;
off_t _size ALIGN64;
long _atime;
long _mtime;
long _ctime;
long _links;
#ifdef GET_ST_ATIME_NSEC
long ansec;
long _ansec;
#endif
#ifdef GET_ST_MTIME_NSEC
long mnsec;
long _mnsec;
#endif
#ifdef GET_ST_CTIME_NSEC
long cnsec;
long _cnsec;
#endif
};
#define GS_NAME 1
#define GS_DEPTH 2
#define GS_EXEC 4
#define GS_SHIFT_BASE 8
#define GS_SIZE (GS_SHIFT_BASE)
#define GS_ATIME (GS_SHIFT_BASE << 1)
#define GS_MTIME (GS_SHIFT_BASE << 2)
#define GS_CTIME (GS_SHIFT_BASE << 3)
#define GS_LINKS (GS_SHIFT_BASE << 4)
#define GS_SHIFT 5
#define GS__SIZE (GS_SIZE << GS_SHIFT)
#define GS__ATIME (GS_ATIME << GS_SHIFT)
#define GS__MTIME (GS_MTIME << GS_SHIFT)
#define GS__CTIME (GS_CTIME << GS_SHIFT)
#define GS__LINKS (GS_LINKS << GS_SHIFT)
#define GS_DESC (GS_SHIFT_BASE << (2*GS_SHIFT))
#define GS_NONE (GS_SHIFT_BASE << (2*GS_SHIFT+1))
#define GS_NORMAL (GS_SIZE | GS_ATIME | GS_MTIME | GS_CTIME | GS_LINKS)
#define GS_LINKED (GS_NORMAL << GS_SHIFT)
/**/
int badcshglob;
/**/
int pathpos; /* position in pathbuf (needed by pattern code) */
/**/
char *pathbuf; /* pathname buffer (needed by pattern code) */
typedef struct stat *Statptr; /* This makes the Ultrix compiler happy. Go figure. */
/* modifier for unit conversions */
#define TT_DAYS 0
#define TT_HOURS 1
#define TT_MINS 2
#define TT_WEEKS 3
#define TT_MONTHS 4
#define TT_SECONDS 5
#define TT_BYTES 0
#define TT_POSIX_BLOCKS 1
#define TT_KILOBYTES 2
#define TT_MEGABYTES 3
typedef int (*TestMatchFunc) _((char *, struct stat *, off_t, char *));
struct qual {
struct qual *next; /* Next qualifier, must match */
struct qual *or; /* Alternative set of qualifiers to match */
TestMatchFunc func; /* Function to call to test match */
off_t data ALIGN64; /* Argument passed to function */
int sense; /* Whether asserting or negating */
int amc; /* Flag for which time to test (a, m, c) */
int range; /* Whether to test <, > or = (as per signum) */
int units; /* Multiplier for time or size, respectively */
char *sdata; /* currently only: expression to eval */
};
/* Prefix, suffix for doing zle trickery */
/**/
mod_export char *glob_pre, *glob_suf;
/* Element of a glob sort */
struct globsort {
/* Sort type */
int tp;
/* Sort code to eval, if type is GS_EXEC */
char *exec;
};
/* Maximum entries in sort array */
#define MAX_SORTS (12)
/* struct to easily save/restore current state */
struct globdata {
int gd_pathpos;
char *gd_pathbuf;
int gd_matchsz; /* size of matchbuf */
int gd_matchct; /* number of matches found */
int gd_pathbufsz; /* size of pathbuf */
int gd_pathbufcwd; /* where did we chdir()'ed */
Gmatch gd_matchbuf; /* array of matches */
Gmatch gd_matchptr; /* &matchbuf[matchct] */
char *gd_colonmod; /* colon modifiers in qualifier list */
/* Qualifiers pertaining to current pattern */
struct qual *gd_quals;
/* Other state values for current pattern */
int gd_qualct, gd_qualorct;
int gd_range, gd_amc, gd_units;
int gd_gf_nullglob, gd_gf_markdirs, gd_gf_noglobdots, gd_gf_listtypes;
int gd_gf_numsort;
int gd_gf_follow, gd_gf_sorts, gd_gf_nsorts;
struct globsort gd_gf_sortlist[MAX_SORTS];
LinkList gd_gf_pre_words;
char *gd_glob_pre, *gd_glob_suf;
};
/* The variable with the current globbing state and convenience macros */
static struct globdata curglobdata;
#define matchsz (curglobdata.gd_matchsz)
#define matchct (curglobdata.gd_matchct)
#define pathbufsz (curglobdata.gd_pathbufsz)
#define pathbufcwd (curglobdata.gd_pathbufcwd)
#define matchbuf (curglobdata.gd_matchbuf)
#define matchptr (curglobdata.gd_matchptr)
#define colonmod (curglobdata.gd_colonmod)
#define quals (curglobdata.gd_quals)
#define qualct (curglobdata.gd_qualct)
#define qualorct (curglobdata.gd_qualorct)
#define g_range (curglobdata.gd_range)
#define g_amc (curglobdata.gd_amc)
#define g_units (curglobdata.gd_units)
#define gf_nullglob (curglobdata.gd_gf_nullglob)
#define gf_markdirs (curglobdata.gd_gf_markdirs)
#define gf_noglobdots (curglobdata.gd_gf_noglobdots)
#define gf_listtypes (curglobdata.gd_gf_listtypes)
#define gf_numsort (curglobdata.gd_gf_numsort)
#define gf_follow (curglobdata.gd_gf_follow)
#define gf_sorts (curglobdata.gd_gf_sorts)
#define gf_nsorts (curglobdata.gd_gf_nsorts)
#define gf_sortlist (curglobdata.gd_gf_sortlist)
#define gf_pre_words (curglobdata.gd_gf_pre_words)
/* and macros for save/restore */
#define save_globstate(N) \
do { \
memcpy(&(N), &curglobdata, sizeof(struct globdata)); \
(N).gd_pathpos = pathpos; \
(N).gd_pathbuf = pathbuf; \
(N).gd_glob_pre = glob_pre; \
(N).gd_glob_suf = glob_suf; \
pathbuf = NULL; \
} while (0)
#define restore_globstate(N) \
do { \
zfree(pathbuf, pathbufsz); \
memcpy(&curglobdata, &(N), sizeof(struct globdata)); \
pathpos = (N).gd_pathpos; \
pathbuf = (N).gd_pathbuf; \
glob_pre = (N).gd_glob_pre; \
glob_suf = (N).gd_glob_suf; \
} while (0)
/* pathname component in filename patterns */
struct complist {
Complist next;
Patprog pat;
int closure; /* 1 if this is a (foo/)# */
int follow; /* 1 to go thru symlinks */
};
/* Add a component to pathbuf: This keeps track of how *
* far we are into a file name, since each path component *
* must be matched separately. */
/**/
static void
addpath(char *s, int l)
{
DPUTS(!pathbuf, "BUG: pathbuf not initialised");
while (pathpos + l + 1 >= pathbufsz)
pathbuf = realloc(pathbuf, pathbufsz *= 2);
while (l--)
pathbuf[pathpos++] = *s++;
pathbuf[pathpos++] = '/';
pathbuf[pathpos] = '\0';
}
/* stat the filename s appended to pathbuf. l should be true for lstat, *
* false for stat. If st is NULL, the file is only checked for existance. *
* s == "" is treated as s == ".". This is necessary since on most systems *
* foo/ can be used to reference a non-directory foo. Returns nonzero if *
* the file does not exists. */
/**/
static int
statfullpath(const char *s, struct stat *st, int l)
{
char buf[PATH_MAX];
DPUTS(strlen(s) + !*s + pathpos - pathbufcwd >= PATH_MAX,
"BUG: statfullpath(): pathname too long");
strcpy(buf, pathbuf + pathbufcwd);
strcpy(buf + pathpos - pathbufcwd, s);
if (!*s && *buf) {
/*
* Don't add the '.' if the path so far is empty, since
* then we get bogus empty strings inserted as files.
*/
buf[pathpos - pathbufcwd] = '.';
buf[pathpos - pathbufcwd + 1] = '\0';
l = 0;
}
unmetafy(buf, NULL);
if (!st) {
char lbuf[1];
return access(buf, F_OK) && (!l || readlink(buf, lbuf, 1) < 0);
}
return l ? lstat(buf, st) : stat(buf, st);
}
/* This may be set by qualifier functions to an array of strings to insert
* into the list instead of the original string. */
char **inserts;
/* add a match to the list */
/**/
static void
insert(char *s, int checked)
{
struct stat buf, buf2, *bp;
char *news = s;
int statted = 0;
queue_signals();
inserts = NULL;
if (gf_listtypes || gf_markdirs) {
/* Add the type marker to the end of the filename */
mode_t mode;
checked = statted = 1;
if (statfullpath(s, &buf, 1)) {
unqueue_signals();
return;
}
mode = buf.st_mode;
if (gf_follow) {
if (!S_ISLNK(mode) || statfullpath(s, &buf2, 0))
memcpy(&buf2, &buf, sizeof(buf));
statted |= 2;
mode = buf2.st_mode;
}
if (gf_listtypes || S_ISDIR(mode)) {
int ll = strlen(s);
news = (char *) hcalloc(ll + 2);
strcpy(news, s);
news[ll] = file_type(mode);
news[ll + 1] = '\0';
}
}
if (qualct || qualorct) {
/* Go through the qualifiers, rejecting the file if appropriate */
struct qual *qo, *qn;
if (!statted && statfullpath(s, &buf, 1)) {
unqueue_signals();
return;
}
news = dyncat(pathbuf, news);
statted = 1;
qo = quals;
for (qn = qo; qn && qn->func;) {
g_range = qn->range;
g_amc = qn->amc;
g_units = qn->units;
if ((qn->sense & 2) && !(statted & 2)) {
/* If (sense & 2), we're following links */
if (!S_ISLNK(buf.st_mode) || statfullpath(s, &buf2, 0))
memcpy(&buf2, &buf, sizeof(buf));
statted |= 2;
}
bp = (qn->sense & 2) ? &buf2 : &buf;
/* Reject the file if the function returned zero *
* and the sense was positive (sense&1 == 0), or *
* vice versa. */
if ((!((qn->func) (news, bp, qn->data, qn->sdata))
^ qn->sense) & 1) {
/* Try next alternative, or return if there are no more */
if (!(qo = qo->or)) {
unqueue_signals();
return;
}
qn = qo;
continue;
}
qn = qn->next;
}
} else if (!checked) {
if (statfullpath(s, NULL, 1)) {
unqueue_signals();
return;
}
statted = 1;
news = dyncat(pathbuf, news);
} else
news = dyncat(pathbuf, news);
while (!inserts || (news = dupstring(*inserts++))) {
if (colonmod) {
/* Handle the remainder of the qualifier: e.g. (:r:s/foo/bar/). */
s = colonmod;
modify(&news, &s);
}
if (!statted && (gf_sorts & GS_NORMAL)) {
statfullpath(s, &buf, 1);
statted = 1;
}
if (!(statted & 2) && (gf_sorts & GS_LINKED)) {
if (statted) {
if (!S_ISLNK(buf.st_mode) || statfullpath(s, &buf2, 0))
memcpy(&buf2, &buf, sizeof(buf));
} else if (statfullpath(s, &buf2, 0))
statfullpath(s, &buf2, 1);
statted |= 2;
}
matchptr->name = news;
if (statted & 1) {
matchptr->size = buf.st_size;
matchptr->atime = buf.st_atime;
matchptr->mtime = buf.st_mtime;
matchptr->ctime = buf.st_ctime;
matchptr->links = buf.st_nlink;
#ifdef GET_ST_ATIME_NSEC
matchptr->ansec = GET_ST_ATIME_NSEC(buf);
#endif
#ifdef GET_ST_MTIME_NSEC
matchptr->mnsec = GET_ST_MTIME_NSEC(buf);
#endif
#ifdef GET_ST_CTIME_NSEC
matchptr->cnsec = GET_ST_CTIME_NSEC(buf);
#endif
}
if (statted & 2) {
matchptr->_size = buf2.st_size;
matchptr->_atime = buf2.st_atime;
matchptr->_mtime = buf2.st_mtime;
matchptr->_ctime = buf2.st_ctime;
matchptr->_links = buf2.st_nlink;
#ifdef GET_ST_ATIME_NSEC
matchptr->_ansec = GET_ST_ATIME_NSEC(buf2);
#endif
#ifdef GET_ST_MTIME_NSEC
matchptr->_mnsec = GET_ST_MTIME_NSEC(buf2);
#endif
#ifdef GET_ST_CTIME_NSEC
matchptr->_cnsec = GET_ST_CTIME_NSEC(buf2);
#endif
}
matchptr++;
if (++matchct == matchsz) {
matchbuf = (Gmatch )realloc((char *)matchbuf,
sizeof(struct gmatch) * (matchsz *= 2));
matchptr = matchbuf + matchct;
}
if (!inserts)
break;
}
unqueue_signals();
}
/* Check to see if str is eligible for filename generation. */
/**/
mod_export int
haswilds(char *str)
{
/* `[' and `]' are legal even if bad patterns are usually not. */
if ((*str == Inbrack || *str == Outbrack) && !str[1])
return 0;
/* If % is immediately followed by ?, then that ? is *
* not treated as a wildcard. This is so you don't have *
* to escape job references such as %?foo. */
if (str[0] == '%' && str[1] == Quest)
str[1] = '?';
for (; *str; str++) {
switch (*str) {
case Inpar:
case Bar:
case Star:
case Inbrack:
case Inang:
case Quest:
return 1;
case Pound:
case Hat:
if (isset(EXTENDEDGLOB))
return 1;
break;
}
}
return 0;
}
/* Do the globbing: scanner is called recursively *
* with successive bits of the path until we've *
* tried all of it. */
/**/
static void
scanner(Complist q)
{
Patprog p;
int closure;
int pbcwdsav = pathbufcwd;
int errssofar = errsfound;
struct dirsav ds;
init_dirsav(&ds);
if (!q)
return;
if ((closure = q->closure)) {
/* (foo/)# - match zero or more dirs */
if (q->closure == 2) /* (foo/)## - match one or more dirs */
q->closure = 1;
else
scanner(q->next);
}
p = q->pat;
/* Now the actual matching for the current path section. */
if (p->flags & PAT_PURES) {
/*
* It's a straight string to the end of the path section.
*/
char *str = (char *)p + p->startoff;
int l = p->patmlen;
if (l + !l + pathpos - pathbufcwd >= PATH_MAX) {
int err;
if (l >= PATH_MAX)
return;
err = lchdir(pathbuf + pathbufcwd, &ds, 0);
if (err == -1)
return;
if (err) {
zerr("current directory lost during glob");
return;
}
pathbufcwd = pathpos;
}
if (q->next) {
/* Not the last path section. Just add it to the path. */
int oppos = pathpos;
if (!errflag) {
int add = 1;
if (q->closure && *pathbuf) {
if (!strcmp(str, "."))
add = 0;
else if (!strcmp(str, "..")) {
struct stat sc, sr;
add = (stat("/", &sr) || stat(pathbuf, &sc) ||
sr.st_ino != sc.st_ino ||
sr.st_dev != sc.st_dev);
}
}
if (add) {
addpath(str, l);
if (!closure || !statfullpath("", NULL, 1))
scanner((q->closure) ? q : q->next);
pathbuf[pathpos = oppos] = '\0';
}
}
} else {
if (str[l])
str = dupstrpfx(str, l);
insert(str, 0);
}
} else {
/* Do pattern matching on current path section. */
char *fn = pathbuf[pathbufcwd] ? unmeta(pathbuf + pathbufcwd) : ".";
int dirs = !!q->next;
DIR *lock = opendir(fn);
char *subdirs = NULL;
int subdirlen = 0;
if (lock == NULL)
return;
while ((fn = zreaddir(lock, 1)) && !errflag) {
/* prefix and suffix are zle trickery */
if (!dirs && !colonmod &&
((glob_pre && !strpfx(glob_pre, fn))
|| (glob_suf && !strsfx(glob_suf, fn))))
continue;
errsfound = errssofar;
if (pattry(p, fn)) {
/* if this name matchs the pattern... */
if (pbcwdsav == pathbufcwd &&
strlen(fn) + pathpos - pathbufcwd >= PATH_MAX) {
int err;
DPUTS(pathpos == pathbufcwd,
"BUG: filename longer than PATH_MAX");
err = lchdir(pathbuf + pathbufcwd, &ds, 0);
if (err == -1)
break;
if (err) {
zerr("current directory lost during glob");
break;
}
pathbufcwd = pathpos;
}
if (dirs) {
int l;
/*
* If not the last component in the path:
*
* If we made an approximation in the new path segment,
* then it is possible we made too many errors. For
* example, (ab)#(cb)# will match the directory abcb
* with one error if allowed to, even though it can
* match with none. This will stop later parts of the
* path matching, so we need to check by reducing the
* maximum number of errors and seeing if the directory
* still matches. Luckily, this is not a terribly
* common case, since complex patterns typically occur
* in the last part of the path which is not affected
* by this problem.
*/
if (errsfound > errssofar) {
forceerrs = errsfound - 1;
while (forceerrs >= errssofar) {
errsfound = errssofar;
if (!pattry(p, fn))
break;
forceerrs = errsfound - 1;
}
errsfound = forceerrs + 1;
forceerrs = -1;
}
if (closure) {
/* if matching multiple directories */
struct stat buf;
if (statfullpath(fn, &buf, !q->follow)) {
if (errno != ENOENT && errno != EINTR &&
errno != ENOTDIR && !errflag) {
zwarn("%e: %s", errno, fn);
}
continue;
}
if (!S_ISDIR(buf.st_mode))
continue;
}
l = strlen(fn) + 1;
subdirs = hrealloc(subdirs, subdirlen, subdirlen + l
+ sizeof(int));
strcpy(subdirs + subdirlen, fn);
subdirlen += l;
/* store the count of errors made so far, too */
memcpy(subdirs + subdirlen, (char *)&errsfound,
sizeof(int));
subdirlen += sizeof(int);
} else
/* if the last filename component, just add it */
insert(fn, 1);
}
}
closedir(lock);
if (subdirs) {
int oppos = pathpos;
for (fn = subdirs; fn < subdirs+subdirlen; ) {
int l = strlen(fn);
addpath(fn, l);
fn += l + 1;
memcpy((char *)&errsfound, fn, sizeof(int));
fn += sizeof(int);
scanner((q->closure) ? q : q->next); /* scan next level */
pathbuf[pathpos = oppos] = '\0';
}
hrealloc(subdirs, subdirlen, 0);
}
}
if (pbcwdsav < pathbufcwd) {
if (restoredir(&ds))
zerr("current directory lost during glob");
zsfree(ds.dirname);
if (ds.dirfd >= 0)
close(ds.dirfd);
pathbufcwd = pbcwdsav;
}
}
/* This function tokenizes a zsh glob pattern */
/**/
static Complist
parsecomplist(char *instr)
{
Patprog p1;
Complist l1;
char *str;
int compflags = gf_noglobdots ? (PAT_FILE|PAT_NOGLD) : PAT_FILE;
if (instr[0] == Star && instr[1] == Star &&
(instr[2] == '/' || (instr[2] == Star && instr[3] == '/'))) {
/* Match any number of directories. */
int follow;
/* with three stars, follow symbolic links */
follow = (instr[2] == Star);
instr += (3 + follow);
/* Now get the next path component if there is one. */
l1 = (Complist) zhalloc(sizeof *l1);
if ((l1->next = parsecomplist(instr)) == NULL) {
errflag = 1;
return NULL;
}
l1->pat = patcompile(NULL, compflags | PAT_ANY, NULL);
l1->closure = 1; /* ...zero or more times. */
l1->follow = follow;
return l1;
}
/* Parse repeated directories such as (dir/)# and (dir/)## */
if (*(str = instr) == Inpar && !skipparens(Inpar, Outpar, (char **)&str) &&
*str == Pound && isset(EXTENDEDGLOB) && str[-2] == '/') {
instr++;
if (!(p1 = patcompile(instr, compflags, &instr)))
return NULL;
if (instr[0] == '/' && instr[1] == Outpar && instr[2] == Pound) {
int pdflag = 0;
instr += 3;
if (*instr == Pound) {
pdflag = 1;
instr++;
}
l1 = (Complist) zhalloc(sizeof *l1);
l1->pat = p1;
l1->closure = 1 + pdflag;
l1->follow = 0;
l1->next = parsecomplist(instr);
return (l1->pat) ? l1 : NULL;
}
} else {
/* parse single path component */
if (!(p1 = patcompile(instr, compflags|PAT_FILET, &instr)))
return NULL;
/* then do the remaining path components */
if (*instr == '/' || !*instr) {
int ef = *instr == '/';
l1 = (Complist) zhalloc(sizeof *l1);
l1->pat = p1;
l1->closure = 0;
l1->next = ef ? parsecomplist(instr+1) : NULL;
return (ef && !l1->next) ? NULL : l1;
}
}
errflag = 1;
return NULL;
}
/* turn a string into a Complist struct: this has path components */
/**/
static Complist
parsepat(char *str)
{
long assert;
int ignore;
patcompstart();
/*
* Check for initial globbing flags, so that they don't form
* a bogus path component.
*/
if ((*str == Inpar && str[1] == Pound && isset(EXTENDEDGLOB)) ||
(isset(KSHGLOB) && *str == '@' && str[1] == Inpar &&
str[2] == Pound)) {
str += (*str == Inpar) ? 2 : 3;
if (!patgetglobflags(&str, &assert, &ignore))
return NULL;
}
/* Now there is no (#X) in front, we can check the path. */
if (!pathbuf)
pathbuf = zalloc(pathbufsz = PATH_MAX);
DPUTS(pathbufcwd, "BUG: glob changed directory");
if (*str == '/') { /* pattern has absolute path */
str++;
pathbuf[0] = '/';
pathbuf[pathpos = 1] = '\0';
} else /* pattern is relative to pwd */
pathbuf[pathpos = 0] = '\0';
return parsecomplist(str);
}
/* get number after qualifier */
/**/
static off_t
qgetnum(char **s)
{
off_t v = 0;
if (!idigit(**s)) {
zerr("number expected");
return 0;
}
while (idigit(**s))
v = v * 10 + *(*s)++ - '0';
return v;
}
/* get mode spec after qualifier */
/**/
static zlong
qgetmodespec(char **s)
{
zlong yes = 0, no = 0, val, mask, t;
char *p = *s, c, how, end;
if ((c = *p) == '=' || c == Equals || c == '+' || c == '-' ||
c == '?' || c == Quest || (c >= '0' && c <= '7')) {
end = 0;
c = 0;
} else {
end = (c == '<' ? '>' :
(c == '[' ? ']' :
(c == '{' ? '}' :
(c == Inang ? Outang :
(c == Inbrack ? Outbrack :
(c == Inbrace ? Outbrace : c))))));
p++;
}
do {
mask = 0;
while (((c = *p) == 'u' || c == 'g' || c == 'o' || c == 'a') && end) {
switch (c) {
case 'o': mask |= 01007; break;
case 'g': mask |= 02070; break;
case 'u': mask |= 04700; break;
case 'a': mask |= 07777; break;
}
p++;
}
how = ((c == '+' || c == '-') ? c : '=');
if (c == '+' || c == '-' || c == '=' || c == Equals)
p++;
val = 0;
if (mask) {
while ((c = *p++) != ',' && c != end) {
switch (c) {
case 'x': val |= 00111; break;
case 'w': val |= 00222; break;
case 'r': val |= 00444; break;
case 's': val |= 06000; break;
case 't': val |= 01000; break;
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
t = ((zlong) c - '0');
val |= t | (t << 3) | (t << 6);
break;
default:
zerr("invalid mode specification");
return 0;
}
}
if (how == '=' || how == '+') {
yes |= val & mask;
val = ~val;
}
if (how == '=' || how == '-')
no |= val & mask;
} else if (!(end && c == end) && c != ',' && c) {
t = 07777;
while ((c = *p) == '?' || c == Quest ||
(c >= '0' && c <= '7')) {
if (c == '?' || c == Quest) {
t = (t << 3) | 7;
val <<= 3;
} else {
t <<= 3;
val = (val << 3) | ((zlong) c - '0');
}
p++;
}
if (end && c != end && c != ',') {
zerr("invalid mode specification");
return 0;
}
if (how == '=') {
yes = (yes & ~t) | val;
no = (no & ~t) | (~val & ~t);
} else if (how == '+')
yes |= val;
else
no |= val;
} else {
zerr("invalid mode specification");
return 0;
}
} while (end && c != end);
*s = p;
return ((yes & 07777) | ((no & 07777) << 12));
}
static int
gmatchcmp(Gmatch a, Gmatch b)
{
int i;
off_t r = 0L;
struct globsort *s;
char **asortstrp = NULL, **bsortstrp = NULL;
for (i = gf_nsorts, s = gf_sortlist; i; i--, s++) {
switch (s->tp & ~GS_DESC) {
case GS_NAME:
r = zstrcmp(b->name, a->name, gf_numsort ? SORTIT_NUMERICALLY : 0);
break;
case GS_DEPTH:
{
char *aptr = a->name, *bptr = b->name;
int slasha = 0, slashb = 0;
/* Count slashes. Trailing slashes don't count. */
while (*aptr && *aptr == *bptr)
aptr++, bptr++;
if (*aptr)
for (; aptr[1]; aptr++)
if (*aptr == '/') {
slasha = 1;
break;
}
if (*bptr)
for (; bptr[1]; bptr++)
if (*bptr == '/') {
slashb = 1;
break;
}
r = slasha - slashb;
}
break;
case GS_EXEC:
if (!asortstrp) {
asortstrp = a->sortstrs;
bsortstrp = b->sortstrs;
} else {
asortstrp++;
bsortstrp++;
}
r = zstrcmp(*bsortstrp, *asortstrp,
gf_numsort ? SORTIT_NUMERICALLY : 0);
break;
case GS_SIZE:
r = b->size - a->size;
break;
case GS_ATIME:
r = a->atime - b->atime;
#ifdef GET_ST_ATIME_NSEC
if (!r)
r = a->ansec - b->ansec;
#endif
break;
case GS_MTIME:
r = a->mtime - b->mtime;
#ifdef GET_ST_MTIME_NSEC
if (!r)
r = a->mnsec - b->mnsec;
#endif
break;
case GS_CTIME:
r = a->ctime - b->ctime;
#ifdef GET_ST_CTIME_NSEC
if (!r)
r = a->cnsec - b->cnsec;
#endif
break;
case GS_LINKS:
r = b->links - a->links;
break;
case GS__SIZE:
r = b->_size - a->_size;
break;
case GS__ATIME:
r = a->_atime - b->_atime;
#ifdef GET_ST_ATIME_NSEC
if (!r)
r = a->_ansec - b->_ansec;
#endif
break;
case GS__MTIME:
r = a->_mtime - b->_mtime;
#ifdef GET_ST_MTIME_NSEC
if (!r)
r = a->_mnsec - b->_mnsec;
#endif
break;
case GS__CTIME:
r = a->_ctime - b->_ctime;
#ifdef GET_ST_CTIME_NSEC
if (!r)
r = a->_cnsec - b->_cnsec;
#endif
break;
case GS__LINKS:
r = b->_links - a->_links;
break;
}
if (r)
return (int) ((s->tp & GS_DESC) ? -r : r);