/
glob.c
2476 lines (2280 loc) · 57 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
/*
* $Id: glob.c,v 2.35 1996/10/15 20:16:35 hzoli Exp $
*
* glob.c - filename generation
*
* This file is part of zsh, the Z shell.
*
* Copyright (c) 1992-1996 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.h"
static int
exists(char *s)
{
char *us = unmeta(s);
return access(us,0) == 0 || readlink(us,NULL,0) == 0;
}
#define GS_NAME 1
#define GS_DEPTH 2
#define GS_SIZE 4
#define GS_ATIME 8
#define GS_MTIME 16
#define GS_CTIME 32
#define GS__NAME 64
#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_DESC 4096
#define GS_NORMAL (GS_SIZE | GS_ATIME | GS_MTIME | GS_CTIME )
#define GS_LINKED (GS_NORMAL << GS_SHIFT)
static int sorttype=GS_NAME;
static int mode; /* != 0 if we are parsing glob patterns */
static int pathpos; /* position in pathbuf */
static int matchsz; /* size of matchbuf */
static int matchct; /* number of matches found */
static char pathbuf[PATH_MAX]; /* pathname buffer */
static char **matchbuf; /* array of matches */
static char **matchptr; /* &matchbuf[matchct] */
static char *colonmod; /* colon modifiers in qualifier list */
static ino_t old_ino; /* ) remember old file and */
static dev_t old_dev; /* ) position in path in case */
static int old_pos; /* ) matching multiple directories */
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_BYTES 0
#define TT_POSIX_BLOCKS 1
#define TT_KILOBYTES 2
#define TT_MEGABYTES 3
/* max # of qualifiers */
typedef int (*TestMatchFunc) _((struct stat *, long));
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 */
long data; /* 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 */
};
/* Qualifiers pertaining to current pattern */
static struct qual *quals;
/* Other state values for current pattern */
static int qualct, qualorct;
static int range, amc, units;
static int gf_nullglob, gf_markdirs, gf_noglobdots, gf_listtypes, gf_follow;
/* Prefix, suffix for doing zle trickery */
char *glob_pre, *glob_suf;
/* pathname component in filename patterns */
struct complist {
Complist next;
Comp comp;
int closure; /* 1 if this is a (foo/)# */
int follow; /* 1 to go thru symlinks */
};
struct comp {
Comp left, right, next, exclude;
char *str;
int stat;
};
/* Type of Comp: a closure with one or two #'s, the end of a *
* pattern or path component, a piece of path to be added. */
#define C_ONEHASH 1
#define C_TWOHASH 2
#define C_CLOSURE (C_ONEHASH|C_TWOHASH)
#define C_LAST 4
#define C_PATHADD 8
/* Test macros for the above */
#define CLOSUREP(c) (c->stat & C_CLOSURE)
#define ONEHASHP(c) (c->stat & C_ONEHASH)
#define TWOHASHP(c) (c->stat & C_TWOHASH)
#define LASTP(c) (c->stat & C_LAST)
#define PATHADDP(c) (c->stat & C_PATHADD)
/* Main entry point to the globbing code for filename globbing. *
* np points to a node in the list list which will be expanded *
* into a series of nodes. */
/**/
void
glob(LinkList list, LinkNode np)
{
struct qual *qo, *qn, *ql;
LinkNode node = prevnode(np);
char *str; /* the pattern */
int sl; /* length of the pattern */
Complist q; /* pattern after parsing */
char *ostr = (char *)getdata(np); /* the pattern before the parser */
/* chops it up */
sorttype=GS_NAME;
MUSTUSEHEAP("glob");
if (unset(GLOBOPT) || !(str = haswilds(ostr))) {
untokenize(ostr);
return;
}
if (*str == ':' && str[-1] == Inpar) {
str[-1] = '\0';
modify((char **) &getdata(np), &str);
return;
}
str = dupstring(ostr);
sl = strlen(str);
uremnode(list, np);
/* Initialise state variables for current file pattern */
qo = qn = quals = ql = NULL;
qualct = qualorct = 0;
colonmod = NULL;
gf_nullglob = isset(NULLGLOB);
gf_markdirs = isset(MARKDIRS);
gf_listtypes = gf_follow = 0;
gf_noglobdots = unset(GLOBDOTS);
if (str[sl - 1] == Outpar) { /* check for qualifiers */
char *s;
int sense = 0; /* bit 0 for match (0)/don't match (1) */
/* bit 1 for follow links (2), don't (0) */
long data = 0; /* Any numerical argument required */
int (*func) _((Statptr, long));
/* Check these are really qualifiers, not a set of *
* alternatives or exclusions */
for (s = str + sl - 2; s != str; s--)
if (*s == Bar || *s == Outpar || *s == Inpar
|| (isset(EXTENDEDGLOB) && *s == Tilde))
break;
if (*s == Inpar) {
/* Real qualifiers found. */
*s++ = '\0';
while (*s != Outpar && !colonmod) {
func = (int (*) _((Statptr, long)))0;
if (idigit(*s)) {
/* Store numeric argument for qualifier */
func = qualflags;
data = 0;
while (idigit(*s))
data = data * 010 + (*s++ - '0');
} else if (*s == ',') {
/* A comma separates alternative sets of qualifiers */
s++;
sense = 0;
if (qualct) {
qn = (struct qual *)hcalloc(sizeof *qn);
qo->or = qn;
qo = qn;
qualorct++;
qualct = 0;
ql = NULL;
}
} else
switch (*s++) {
case ':':
/* Remaining arguments are history-type *
* colon substitutions, handled separately. */
colonmod = s - 1;
break;
case Hat:
case '^':
/* Toggle sense: go from positive to *
* negative match and vice versa. */
sense ^= 1;
break;
case '-':
/* Toggle matching of symbolic links */
sense ^= 2;
break;
#ifdef S_IFLNK
case '@':
/* Match symbolic links */
func = qualmode;
data = S_IFLNK;
break;
#endif
#ifdef S_IFSOCK
case Equals:
case '=':
/* Match sockets */
func = qualmode;
data = S_IFSOCK;
break;
#endif
#ifdef S_IFIFO
case 'p':
/* Match named pipes */
func = qualmode;
data = S_IFIFO;
break;
#endif
case '/':
/* Match directories */
func = qualmode;
data = S_IFDIR;
break;
case '.':
/* Match regular files */
func = qualmode;
data = S_IFREG;
break;
case '%':
/* Match special files: block, *
* character or any device */
if (*s == 'b')
s++, func = qualisblk;
else if (*s == 'c')
s++, func = qualischar;
else
func = qualisdev;
break;
case Star:
/* Match executable plain files */
func = qualiscom;
break;
case 'R':
/* Match world-readable files */
func = qualflags;
data = 0004;
break;
case 'W':
/* Match world-writeable files */
func = qualflags;
data = 0002;
break;
case 'X':
/* Match world-executable files */
func = qualflags;
data = 0001;
break;
case 'A':
func = qualflags;
data = 0040;
break;
case 'I':
func = qualflags;
data = 0020;
break;
case 'E':
func = qualflags;
data = 0010;
break;
case 'r':
/* Match files readable by current process */
func = qualflags;
data = 0400;
break;
case 'w':
/* Match files writeable by current process */
func = qualflags;
data = 0200;
break;
case 'x':
/* Match files executable by current process */
func = qualflags;
data = 0100;
break;
case 's':
/* Match setuid files */
func = qualflags;
data = 04000;
break;
case 'S':
/* Match setgid files */
func = qualflags;
data = 02000;
break;
case 't':
func = qualflags;
data = 01000;
break;
case 'd':
/* Match device files by device number *
* (as given by stat's st_dev element). */
func = qualdev;
data = qgetnum(&s);
break;
case 'l':
/* Match files with the given no. of hard links */
func = qualnlink;
amc = -1;
goto getrange;
case 'U':
/* Match files owned by effective user ID */
func = qualuid;
data = geteuid();
break;
case 'G':
/* Match files owned by effective group ID */
func = qualgid;
data = getegid();
break;
case 'u':
/* Match files owned by given user id */
func = qualuid;
/* either the actual uid... */
if (idigit(*s))
data = qgetnum(&s);
else {
/* ... or a user name */
struct passwd *pw;
char sav, *tt;
/* Find matching delimiters */
tt = get_strarg(s);
if (!*tt) {
zerr("missing end of name",
NULL, 0);
data = 0;
} else {
sav = *tt;
*tt = '\0';
if ((pw = getpwnam(s + 1)))
data = pw->pw_uid;
else {
zerr("unknown user", NULL, 0);
data = 0;
}
if ((*tt = sav) != Outpar)
s = tt + 1;
else
s = tt;
}
}
break;
case 'g':
/* Given gid or group id... works like `u' */
func = qualgid;
/* either the actual gid... */
if (idigit(*s))
data = qgetnum(&s);
else {
/* ...or a delimited group name. */
struct group *gr;
char sav, *tt;
tt = get_strarg(s);
if (!*tt) {
zerr("missing end of name",
NULL, 0);
data = 0;
} else {
sav = *tt;
*tt = '\0';
if ((gr = getgrnam(s + 1)))
data = gr->gr_gid;
else {
zerr("unknown group", NULL, 0);
data = 0;
}
if ((*tt = sav) != Outpar)
s = tt + 1;
else
s = tt;
}
}
break;
case 'q':
/* Match octal mode of file exactly. *
* Currently undocumented. */
func = qualeqflags;
data = qgetoctnum(&s);
break;
case 'M':
/* Mark directories with a / */
if ((gf_markdirs = !(sense & 1)))
gf_follow = sense & 2;
break;
case 'T':
/* Mark types in a `ls -F' type fashion */
if ((gf_listtypes = !(sense & 1)))
gf_follow = sense & 2;
break;
case 'N':
/* Nullglob: remove unmatched patterns. */
gf_nullglob = !(sense & 1);
break;
case 'D':
/* Glob dots: match leading dots implicitly */
gf_noglobdots = sense & 1;
break;
case 'a':
/* Access time in given range */
amc = 0;
func = qualtime;
goto getrange;
case 'm':
/* Modification time in given range */
amc = 1;
func = qualtime;
goto getrange;
case 'c':
/* Inode creation time in given range */
amc = 2;
func = qualtime;
goto getrange;
case 'L':
/* File size (Length) in given range */
func = qualsize;
amc = -1;
/* Get size multiplier */
units = TT_BYTES;
if (*s == 'p' || *s == 'P')
units = TT_POSIX_BLOCKS, ++s;
else if (*s == 'k' || *s == 'K')
units = TT_KILOBYTES, ++s;
else if (*s == 'm' || *s == 'M')
units = TT_MEGABYTES, ++s;
getrange:
/* Get time multiplier */
if (amc >= 0) {
units = TT_DAYS;
if (*s == 'h')
units = TT_HOURS, ++s;
else if (*s == 'm')
units = TT_MINS, ++s;
else if (*s == 'w')
units = TT_WEEKS, ++s;
else if (*s == 'M')
units = TT_MONTHS, ++s;
}
/* See if it's greater than, equal to, or less than */
if ((range = *s == '+' ? 1 : *s == '-' ? -1 : 0))
++s;
data = qgetnum(&s);
break;
case 'o':
{
switch (*s) {
case 'N': sorttype = GS__NAME; break;
case 'S': sorttype = GS__SIZE; break;
case 'A': sorttype = GS__ATIME; break;
case 'M': sorttype = GS__MTIME; break;
case 'C': sorttype = GS__CTIME; break;
case 'n': sorttype = GS_NAME; break;
case 's': sorttype = GS_SIZE; break;
case 'a': sorttype = GS_ATIME; break;
case 'm': sorttype = GS_MTIME; break;
case 'c': sorttype = GS_CTIME; break;
default:
zerr("unknown sort specifier", NULL, 0);
break;
}
++s;
break;
}
default:
zerr("unknown file attribute", NULL, 0);
return;
}
if (func) {
/* Requested test is performed by function func */
if (!qn)
qn = (struct qual *)hcalloc(sizeof *qn);
if (ql)
ql->next = qn;
ql = qn;
if (!quals)
quals = qo = qn;
qn->func = func;
qn->sense = sense;
qn->data = data;
qn->range = range;
qn->units = units;
qn->amc = amc;
qn = NULL;
qualct++;
}
if (errflag)
return;
}
}
}
if (*str == '/') { /* pattern has absolute path */
str++;
pathbuf[0] = '/';
pathbuf[pathpos = 1] = '\0';
} else /* pattern is relative to pwd */
pathbuf[pathpos = 0] = '\0';
q = parsepat(str);
if (!q || errflag) { /* if parsing failed */
if (unset(BADPATTERN)) {
untokenize(ostr);
insertlinknode(list, node, ostr);
return;
}
errflag = 0;
zerr("bad pattern: %s", ostr, 0);
return;
}
/* Initialise receptacle for matched files, *
* expanded by insert() where necessary. */
matchptr = matchbuf = (char **)zalloc((matchsz = 16) * sizeof(char *));
matchct = 0;
/* Initialise memory of last file matched */
old_ino = (ino_t) 0;
old_dev = (dev_t) 0;
old_pos = -1;
/* The actual processing takes place here: matches go into *
* matchbuf. This is the only top-level call to scanner(). */
scanner(q);
/* Deal with failures to match depending on options */
if (matchct)
badcshglob |= 2; /* at least one cmd. line expansion O.K. */
else if (!gf_nullglob)
if (isset(CSHNULLGLOB)) {
badcshglob |= 1; /* at least one cmd. line expansion failed */
} else if (isset(NOMATCH)) {
zerr("no matches found: %s", ostr, 0);
free(matchbuf);
return;
} else {
/* treat as an ordinary string */
untokenize(*matchptr++ = dupstring(ostr));
matchct = 1;
}
/* Sort arguments in to lexical (and possibly numeric) order. *
* This is reversed to facilitate insertion into the list. */
qsort((void *) & matchbuf[0], matchct, sizeof(char *),
(int (*) _((const void *, const void *)))notstrcmp);
matchptr = matchbuf;
while (matchct--) /* insert matches in the arg list */
insertlinknode(list, node, *matchptr++);
free(matchbuf);
}
/* get number after qualifier */
/**/
long
qgetnum(char **s)
{
long v = 0;
if (!idigit(**s)) {
zerr("number expected", NULL, 0);
return 0;
}
while (idigit(**s))
v = v * 10 + *(*s)++ - '0';
return v;
}
/* get octal number after qualifier */
/**/
long
qgetoctnum(char **s)
{
long v = 0;
if (!idigit(**s)) {
zerr("octal number expected", NULL, 0);
return 0;
}
while (**s >= '0' && **s <= '7')
v = v * 010 + *(*s)++ - '0';
return v;
}
/* Return the order of two strings, taking into account *
* possible numeric order if NUMERICGLOBSORT is set. *
* The comparison here is reversed. */
/**/
int
notstrcmp(char **a, char **b)
{
char *c = *b, *d = *a;
int cmp;
struct stat st, *sta, *stb;
if ((sorttype==GS_NAME) || (sorttype==GS__NAME)) {
if (sorttype==GS__NAME) {c = *a; d = *b;}
#ifdef HAVE_STRCOLL
cmp = strcoll(c, d);
#endif
for (; *c == *d && *c; c++, d++);
#ifndef HAVE_STRCOLL
cmp = (int)STOUC(*c) - (int)STOUC(*d);
#endif
if (isset(NUMERICGLOBSORT) && (idigit(*c) || idigit(*d))) {
for (; c > *b && idigit(c[-1]); c--, d--);
if (idigit(*c) && idigit(*d)) {
while (*c == '0')
c++;
while (*d == '0')
d++;
for (; idigit(*c) && *c == *d; c++, d++);
if (idigit(*c) || idigit(*d)) {
cmp = (int)STOUC(*c) - (int)STOUC(*d);
while (idigit(*c) && idigit(*d))
c++, d++;
if (idigit(*c) && !idigit(*d))
return 1;
if (idigit(*d) && !idigit(*c))
return -1;
}
}
}
return cmp;
}
if (!( sta=getstat(*a) )) {
return 0;
}
st=*sta;
sta=&st;
if (!( stb=getstat(*b) )) {
return 0;
}
switch (sorttype) {
case GS_SIZE:
return(sta->st_size - stb->st_size);
break;
case GS_ATIME:
return(sta->st_atime - stb->st_atime);
break;
case GS_MTIME:
return(sta->st_mtime - stb->st_mtime);
break;
case GS_CTIME:
return(sta->st_ctime - stb->st_ctime);
break;
case GS__SIZE:
return(stb->st_size - sta->st_size);
break;
case GS__ATIME:
return(stb->st_atime - sta->st_atime);
break;
case GS__MTIME:
return(stb->st_mtime - sta->st_mtime);
break;
case GS__CTIME:
return(stb->st_ctime - sta->st_ctime);
break;
}
return 0;
}
/* add a match to the list */
/**/
void
insert(char *s)
{
struct stat buf, buf2, *bp;
char *news = s;
int statted = 0;
if (gf_listtypes || gf_markdirs) {
/* Add the type marker to the end of the filename */
statted = -1;
if (!lstat(unmeta(s), &buf)) {
mode_t mode = buf.st_mode;
statted = 1;
if (gf_follow) {
if (!S_ISLNK(mode) || stat(unmeta(s), &buf2))
memcpy(&buf2, &buf, sizeof(buf));
statted = 2;
mode = buf2.st_mode;
}
if (gf_listtypes || S_ISDIR(mode)) {
int ll = strlen(s);
news = (char *)ncalloc(ll + 2);
strcpy(news, s);
news[ll] = file_type(buf.st_mode);
news[ll + 1] = '\0';
}
}
}
if (qualct || qualorct) {
/* Go through the qualifiers, rejecting the file if appropriate */
struct qual *qo, *qn;
int t = 0; /* reject file unless t is set */
if (statted >= 0 && (statted || !lstat(unmeta(s), &buf))) {
for (qo = quals; qo && !t; qo = qo->or) {
t = 1;
for (qn = qo; t && qn && qn->func; qn = qn->next) {
range = qn->range;
amc = qn->amc;
units = qn->units;
if ((qn->sense & 2) && statted != 2) {
/* If (sense & 2), we're following links */
if (!S_ISLNK(buf.st_mode) || stat(unmeta(s), &buf2))
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) (bp, qn->data)) ^ qn->sense) & 1) {
t = 0;
break;
}
}
}
}
if (!t)
return;
}
if (colonmod) {
/* Handle the remainder of the qualifer: e.g. (:r:s/foo/bar/). */
s = colonmod;
modify(&news, &s);
}
*matchptr++ = news;
if (++matchct == matchsz) {
matchbuf = (char **)realloc((char *)matchbuf,
sizeof(char **) * (matchsz *= 2));
matchptr = matchbuf + matchct;
}
}
/* Return the trailing character for marking file types */
/**/
char
file_type(mode_t filemode)
{
switch (filemode & S_IFMT) {/* screw POSIX */
case S_IFDIR:
return '/';
#ifdef S_IFIFO
case S_IFIFO:
return '|';
#endif
case S_IFCHR:
return '%';
case S_IFBLK:
return '#';
#ifdef S_IFLNK
case S_IFLNK:
return /* (access(pbuf, F_OK) == -1) ? '&' :*/ '@';
#endif
#ifdef S_IFSOCK
case S_IFSOCK:
return '=';
#endif
default:
if (filemode & 0111)
return '*';
else
return ' ';
}
}
/* check to see if str is eligible for filename generation
* It returns NULL if no wilds or modifiers found.
* If a leading % is immediately followed by a ?, that single
* ? is not treated as a wildcard.
* If str has wilds it returns a pointer to the first wildcard.
* If str has no wilds but ends in a (:...) type modifier it returns
* a pointer to the colon.
* If str has no wilds but ends in (...:...) it returns a pointer
* to the terminating null character of str.
*/
/**/
char *
haswilds(char *str)
{
char *mod = NULL;
int parlev = 0;
if ((*str == Inbrack || *str == Outbrack) && !str[1])
return NULL;
/* 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:
parlev++;
break;
case Outpar:
if (! --parlev && str[1])
mod = NULL;
break;
case ':':
if (parlev == 1 && !mod)
mod = str;
break;
case Bar:
if (!parlev) {
*str = '|';
break;
} /* else fall through */
case Star:
case Inbrack:
case Inang:
case Quest:
return str;
case Pound:
case Hat:
if (isset(EXTENDEDGLOB))
return str;
}
if (!mod || parlev)
return NULL;
if (mod[-1] == Inpar)
return mod;
return str;
}
/* check to see if str is eligible for brace expansion */
/**/
int
hasbraces(char *str)
{
char *lbr, *mbr, *comma;
if (isset(BRACECCL)) {
/* In this case, any properly formed brace expression *
* will match and expand to the characters in between. */
int bc;
for (bc = 0; *str; ++str)
if (*str == Inbrace) {
if (!bc && str[1] == Outbrace)
*str++ = '{', *str = '}';
else
bc++;
} else if (*str == Outbrace)
if (!bc)
*str = '}';
else if (!--bc)
return 1;
return 0;
}
/* Otherwise we need to look for... */
lbr = mbr = comma = NULL;
for (;;) {
switch (*str++) {
case Inbrace:
if (!lbr) {
lbr = str - 1;
while (idigit(*str))
str++;
if (*str == '.' && str[1] == '.') {
str++;
while (idigit(*++str));
if (*str == Outbrace &&
(idigit(lbr[1]) || idigit(str[-1])))
return 1;
}
} else {
char *s = --str;
if (skipparens(Inbrace, Outbrace, &str)) {
*lbr = *s = '{';
if (comma)
str = comma;
if (mbr && mbr < str)
str = mbr;
lbr = mbr = comma = NULL;
} else if (!mbr)
mbr = s;
}
break;
case Outbrace:
if (!lbr)
str[-1] = '}';
else if (comma)
return 1;
else {
*lbr = '{';
str[-1] = '}';
if (mbr)
str = mbr;
mbr = lbr = NULL;
}
break;
case Comma:
if (!lbr)
str[-1] = ',';
else if (!comma)
comma = str - 1;
break;
case '\0':
if (lbr)
*lbr = '{';
if (!mbr && !comma)
return 0;
if (comma)
str = comma;
if (mbr && mbr < str)
str = mbr;
lbr = mbr = comma = NULL;
break;
}
}
}
/* expand stuff like >>*.c */
/**/
int
xpandredir(struct redir *fn, LinkList tab)
{
LinkList fake;
char *nam;
struct redir *ff;
int ret = 0;
/* Stick the name in a list... */
fake = newlinklist();
addlinknode(fake, fn->name);
/* ...which undergoes all the usual shell expansions */
prefork(fake, isset(MULTIOS) ? 0 : 4);
/* Globbing is only done for multios. */
if (!errflag && isset(MULTIOS))
globlist(fake);
if (errflag)
return 0;