summaryrefslogtreecommitdiff
path: root/usr.bin/find/find.c
blob: c56fedc46ac8710b61da7bfe468b28c6c8c611b3 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
/*	find	COMPILE:	cc -o find -s -O -i find.c -lS	*/
/*
 * Changes by Gunnar Ritter, Freiburg i. Br., Germany, September 2003.
 */
/*	from Unix 7th Edition /usr/src/cmd/find.c	*/
/*
 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *   Redistributions of source code and documentation must retain the
 *    above copyright notice, this list of conditions and the following
 *    disclaimer.
 *   Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *   All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed or owned by Caldera
 *      International, Inc.
 *   Neither the name of Caldera International, Inc. nor the names of
 *    other contributors may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
 * LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#if __GNUC__ >= 3 && __GNUC_MINOR__ >= 4 || __GNUC__ >= 4
#define	USED	__attribute__ ((used))
#elif defined __GNUC__
#define	USED	__attribute__ ((unused))
#else
#define	USED
#endif
#if defined (SU3)
static const char sccsid[] USED = "@(#)find_su3.sl	1.45 (gritter) 5/8/06";
#elif defined (SUS)
static const char sccsid[] USED = "@(#)find_sus.sl	1.45 (gritter) 5/8/06";
#else
static const char sccsid[] USED = "@(#)find.sl	1.45 (gritter) 5/8/06";
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <fcntl.h>
#include <unistd.h>
#include <pwd.h>
#include <time.h>
#include <grp.h>
#include <stdarg.h>
#include <libgen.h>
#include <errno.h>
#include <locale.h>
#include <signal.h>
#include <fnmatch.h>
#include <mntent.h>
#ifndef	major
#include <sys/mkdev.h>
#endif
#if __NetBSD_Version__>= 300000000
#include <sys/statvfs.h>
#define statfs statvfs
#endif
#include "getdir.h"
#define A_DAY	86400L /* a day full of seconds */
#define EQ(x, y)	(strcmp(x, y)==0)

#ifndef	MNTTYPE_IGNORE
#define	MNTTYPE_IGNORE	""
#endif

#ifndef	S_IFDOOR
#define	S_IFDOOR	0xD000
#endif

#ifndef	S_IFNWK
#define	S_IFNWK		0x9000
#endif

#undef	ctime
#define	ctime	find_ctime

static char	*Pathname;

struct aggregate {		/* for exec ... {} + */
	long	a_cnt;		/* count of arguments */
	long	a_cur;		/* current position in aggregate */
	long	a_csz;		/* aggregate current length */
	long	a_msz;		/* aggregate maximum length */
	char	**a_vec;	/* arguments */
	char	*a_spc;		/* aggregate space */
	long	a_maxarg;	/* maximum arguments in e_vec */
};

struct anode {
	int	(*F)(struct anode *);
	union anode_l {
		struct anode	*L;
		char *pat;
		time_t t;
		uid_t u;
		gid_t g;
		ino_t i;
		nlink_t link;
		off_t sz;
		mode_t per;
		int com;
		FILE *fp;
		char *fstype;
	} l;
	union anode_r {
		struct anode	*R;
		int	s;
		pid_t pid;
		struct aggregate *a;
	} r;
};
static char	*Fname;
static time_t	Now;
static int	Argc,
	Ai,
	Pi;
static char	**Argv;
/* cpio stuff */
static int	Cpio;

static struct stat Statb;

/*
 * Keep track of all visited directories, to avoid loops caused by
 * symbolic links and to free storage and close files after fork().
 */
static struct visit {
	struct getdb	*v_db;	/* getdb struct for this level */
	ino_t	v_ino;		/* inode number */
	int	v_fd;		/* file descriptor */
	dev_t	v_dev;		/* device id */
} *visited;
static int	vismax;		/* number of members in visited */

/*
 * For -fstype, keep track of all filesystem types known to the system. If
 * we had st_fstype in struct stat as SVR4 does, this would be far more
 * reliable.
 */
#if defined (__linux__) || defined (_AIX) || defined (__hpux)
static struct fstype {
	dev_t	fsdev;		/* device id of filesystem */
	char	*fstype;	/* filesystem type */
} *fstypes, *fscur;
#endif	/* __linux__ || _AIX || __hpux */

static int	Home = -1;
static int	wanthome;
static mode_t	um;		/* user's umask */
static const char	*progname;
static int	status;		/* exit status */
static int	depth;		/* -depth flag */
static int	Print = 1;	/* implicit -print */
static int	Prune;		/* -prune at this point */
static int	Mount;		/* -mount, -xdev */
static int	Execplus;	/* have a -exec command {} + node */
static int	HLflag;		/* -H or -L option given */
static char	*Statfs;	/* result of statfs() on FreeBSD */
static int	incomplete;	/* encountered an incomplete statement */
static int	sysv3;

static int	(*statfn)(const char *, struct stat *) = lstat;

static struct anode *expr(void);
static struct anode *e1(void);
static struct anode *e2(void);
static struct anode *e3(void);
static struct anode *mk(struct anode *);
static void	oper(const char **);
static char	*nxtarg(int);
static int	and(struct anode *);
static int	or(struct anode *);
static int	not(struct anode *);
static int	glob(struct anode *);
static int	print(struct anode *);
static int	prune(struct anode *);
static int	null(struct anode *);
static int	mtime(struct anode *);
static int	atime(struct anode *);
static int	ctime(struct anode *);
static int	user(struct anode *);
static int	ino(struct anode *);
static int	group(struct anode *);
static int	nogroup(struct anode *);
static int	nouser(struct anode *);
static int	links(struct anode *);
static int	size(struct anode *);
static int	sizec(struct anode *);
static int	perm(struct anode *);
static int	type(struct anode *);
static int	exeq(struct anode *);
static int	ok(struct anode *);
static int	cpio(struct anode *);
static int	newer(struct anode *);
static int	cnewer(struct anode *);
static int	anewer(struct anode *);
static int	fstype(struct anode *);
static int	local(struct anode *);
static int	scomp(long long, long long, char);
static int	doex(int, struct aggregate *);
static struct aggregate *mkagg(long);
static uid_t	getunum(const char *);
static gid_t	getgnum(const char *);
static const char	*getuser(uid_t);
static const char	*getgroup(gid_t);
#if defined (__linux__) || defined (_AIX) || defined (__hpux)
static void	getfscur(dev_t);
static void	getfstypes(void);
#endif	/* __linux__ || _AIX || __hpux */
static int	descend(char *, struct anode *, int);
static int	descend1(char *, struct anode *, int);
static int	descend2(char *, struct anode *, int);
static void	setpath(char *, const char *, int);
static void	pr(const char *, ...);
static void	er(const char *, ...);
static void	usage(void);
static void	*srealloc(void *, size_t);
static void	mkcpio(struct anode *, const char *, int);
static void	trailer(struct anode *, int);
static void	mknewer(struct anode *, const char *, int (*)(struct anode *));
static mode_t	newmode(const char *ms, const mode_t pm);

int
main(int argc, char **argv)
{
	struct anode *exlist;
	struct anode nlist = { null, { 0 }, { 0 } };
	int paths;
	register char *sp = 0;
	int	i, j;

	time(&Now);
	umask(um = umask(0));
	progname = basename(argv[0]);
	setlocale(LC_COLLATE, "");
	setlocale(LC_CTYPE, "");
	if (getenv("SYSV3") != NULL)
		sysv3 = 1;
	for (i = 1; i < argc; i++) {
		if (argv[i][0] != '-' || argv[i][1] == '\0')
			break;
		if (argv[i][1] == '-') {
			i++;
			break;
		}
		for (j = 1; argv[i][j]; j++)
			if (argv[i][j] != 'H' && argv[i][j] != 'L')
				goto brk;
		for (j = 1; argv[i][j]; j++)
			HLflag = argv[i][j];
	}
brk:	if (HLflag == 'L')
		statfn = stat;
	argc -= i - 1;
	argv += i - 1;
	Argc = argc; Argv = argv;
	if(argc<2) {
		pr("insufficient number of arguments");
		usage();
	}
	for(Ai = paths = 1; Ai < argc; ++Ai, ++paths)
		if(*Argv[Ai] == '-' || EQ(Argv[Ai], "(") || EQ(Argv[Ai], "!"))
			break;
	if(paths == 1) /* no path-list */
		usage();
	if(Ai<argc) {
		if(!(exlist = expr())) /* parse and compile the arguments */
			er("find: parsing error");
		if(Ai<argc) {
			pr("bad option %s", argv[Ai]);
			usage();
		}
	} else
		exlist = &nlist;
	if (paths > 2)
		wanthome = 1;
	if (wanthome && (Home = open(".", O_RDONLY)) < 0)
		er("bad starting directory");
	for(Pi = 1; Pi < paths; ++Pi) {
		if (Pi > 1 && Home >= 0 && fchdir(Home) < 0)
			er("bad starting directory");
		setpath(Pathname, Argv[Pi], 0);
		Fname = sp = Pathname;
		do
			if (sp[0] == '/')
				Fname = &sp[1];
		while (*sp++);
		descend(Pathname, exlist, 0); /* to find files that match  */
	}
	if(Cpio || Execplus)
		trailer(exlist, 1);
	exit(status);
}

/* compile time functions:  priority is  expr()<e1()<e2()<e3()  */

/*ARGSUSED*/
static struct anode *expr(void) { /* parse ALTERNATION (-o)  */
	register struct anode * p1;
	struct anode n = { 0, { 0 }, { 0 } };

	p1 = e1() /* get left operand */ ;
	if(EQ(nxtarg(0), "-o")) {
		const char	*ops[] = { "-o", "-a", 0 };
		oper(ops);
		n.F = or, n.l.L = p1, n.r.R = expr();
		return(mk(&n));
	}
	else if(Ai <= Argc) --Ai;
	return(p1);
}
static struct anode *e1(void) { /* parse CONCATENATION (formerly -a) */
	register struct anode * p1;
	register char *a;
	struct anode n = { 0, { 0 }, { 0 } };

	p1 = e2();
	a = nxtarg(0);
	if(EQ(a, "-a")) {
		const char	*ops[] = { "-o", "-a", 0 };
		oper(ops);
And:
		n.F = and, n.l.L = p1, n.r.R = e1();
		return(mk(&n));
	} else if(EQ(a, "(") || EQ(a, "!") || (*a=='-' && !EQ(a, "-o"))) {
		--Ai;
		goto And;
	} else if(Ai <= Argc) --Ai;
	return(p1);
}
static struct anode *e2(void) { /* parse NOT (!) */
	struct anode n = { 0, { 0 }, { 0 } };
	if(EQ(nxtarg(0), "!")) {
		const char	*ops[] = { "-o", "-a", "!", 0 };
		oper(ops);
		n.F = not, n.l.L = e3();
		return(mk(&n));
	}
	else if(Ai <= Argc) --Ai;
	return(e3());
}
static struct anode *e3(void) { /* parse parens and predicates */
	struct anode *p1;
	struct anode n = { 0, { 0 }, { 0 } };
	long i, k;
	register char *a, *b, s, *p, *q;

	a = nxtarg(0);
	if(EQ(a, "(")) {
		const char	*ops[] = { "-o", "-a", 0 };
		oper(ops);
		p1 = expr();
		a = nxtarg(1);
		if(!EQ(a, ")")) goto err;
		return(p1);
	}
	else if(EQ(a, "-depth")) {
		depth = 1;
		n.F = null;
	} else if(EQ(a, "-follow")) {
		statfn = stat;
		n.F = null;
	} else if(EQ(a, "-mount") || EQ(a, "-xdev")) {
		Mount = 1;
		n.F = null;
	} else if(EQ(a, "-print")) {
		Print = 0;
		n.F = print;
	} else if(EQ(a, "-prune"))
		n.F = prune;
	else if(EQ(a, "-nogroup"))
		n.F = nogroup;
	else if(EQ(a, "-nouser"))
		n.F = nouser;
	else if(EQ(a, "-local")) {
#if defined (__linux__) || defined (_AIX) || defined (__hpux)
		getfstypes();
#endif	/* __linux__ || _AIX || __hpux */
		n.F = local;
		Statfs = a;
	}
	if (n.F)
		return mk(&n);
	b = nxtarg(2);
	s = *b;
	/*if(s=='+') b++;*/
	if(EQ(a, "-name"))
		n.F = glob, n.l.pat = b;
	else if(EQ(a, "-mtime"))
		n.F = mtime, n.l.t = atol(b), n.r.s = s;
	else if(EQ(a, "-atime"))
		n.F = atime, n.l.t = atol(b), n.r.s = s;
	else if(EQ(a, "-ctime"))
		n.F = ctime, n.l.t = atol(b), n.r.s = s;
	else if(EQ(a, "-user"))
		n.F = user, n.l.u = getunum(b), n.r.s = s;
	else if(EQ(a, "-inum"))
		n.F = ino, n.l.i = atoll(b), n.r.s = s;
	else if(EQ(a, "-group"))
		n.F = group, n.l.g = getgnum(b), n.r.s = s;
	else if(EQ(a, "-size")) {
		n.l.sz = atoll(b), n.r.s = s;
		while (b[0] && b[1])
			b++;
		if (b[0] == 'c')
			n.F = sizec;
		else
			n.F = size;
	}
	else if(EQ(a, "-links"))
		n.F = links, n.l.link = atol(b), n.r.s = s;
	else if(EQ(a, "-perm")) {
		while (*b == '-')
			b++;
		n.F = perm, n.l.per = newmode(b, 0), n.r.s = s;
#if defined (SUS) || defined (SU3)
		if (s == '-')
			n.l.per &= 07777;
#endif
	}
	else if(EQ(a, "-type")) {
		i = b[0] == '-' || b[0] == '+' ? b[1] : b[0];
		i = i=='d' ? S_IFDIR :
		    i=='b' ? S_IFBLK :
		    i=='c' ? S_IFCHR :
		    i=='D' ? S_IFDOOR :
		    i=='f' ? S_IFREG :
		    i=='l' ? S_IFLNK :
		    i=='n' ? S_IFNWK :
		    i=='p' ? S_IFIFO :
		    i=='s' ? S_IFSOCK :
		    0;
		n.F = type, n.l.per = i;
	}
	else if (EQ(a, "-exec")) {
		Print = 0;
		wanthome = 1;
		i = Ai - 1;
		q = "";
		k = 0;
		while(!EQ(p = nxtarg(1), ";")) {
			if (EQ(p, "+") && EQ(q, "{}")) {
				n.r.a = mkagg(k);
				break;
			}
			q = p;
			k += strlen(p) + 1;
		}
		n.F = exeq, n.l.com = i;
	}
	else if (EQ(a, "-ok")) {
		Print = 0;
		wanthome = 1;
		i = Ai - 1;
		while(!EQ(p = nxtarg(1), ";"));
		n.F = ok, n.l.com = i;
	}
	else if(EQ(a, "-cpio"))
		mkcpio(&n, b, 0);
	else if(EQ(a, "-ncpio"))
		mkcpio(&n, b, 1);
	else if(EQ(a, "-newer"))
		mknewer(&n, b, newer);
	else if(EQ(a, "-anewer"))
		mknewer(&n, b, anewer);
	else if(EQ(a, "-cnewer"))
		mknewer(&n, b, cnewer);
	else if(EQ(a, "-fstype")) {
#if defined (__linux__) || defined (_AIX) || defined (__hpux)
		getfstypes();
#endif	/* __linux__ || _AIX || __hpux */
		n.F = fstype, n.l.fstype = b;
		Statfs = a;
	}
	if (n.F) {
		if (incomplete)
			nxtarg(1);
		return mk(&n);
	}
err:	pr("bad option %s", a);
	usage();
	/*NOTREACHED*/
	return 0;
}
static struct anode *mk(struct anode *p)
{
	struct anode	*n;

	n = srealloc(NULL, sizeof *n);
	*n = *p;
	return(n);
}
static void oper(const char **ops)
{
	char	*a;

	a = nxtarg(-1);
	while (*ops)
		if (EQ(a, *ops++))
			er("operand follows operand");
	Ai--;
}

static char *nxtarg(int must) { /* get next arg from command line */
	static int strikes = 0;

	if(must==1 && Ai>=Argc || strikes==3)
		er("incomplete statement");
	if(Ai>=Argc) {
		if (must >= 0)
			strikes++;
		incomplete = 1;
		Ai = Argc + 1;
		return("");
	}
	return(Argv[Ai++]);
}

/* execution time functions */
static int and(register struct anode *p)
{
	return(((*p->l.L->F)(p->l.L)) && ((*p->r.R->F)(p->r.R))?1:0);
}
static int or(register struct anode *p)
{
	 return(((*p->l.L->F)(p->l.L)) || ((*p->r.R->F)(p->r.R))?1:0);
}
static int not(register struct anode *p)
{
	return( !((*p->l.L->F)(p->l.L)));
}
static int glob(register struct anode *p)
{
	int	val;
#ifdef	__GLIBC__
	/* avoid glibc's broken [^...] */
	extern char	**environ;
	char	**savenv = environ;
	char	*newenv[] = { "POSIXLY_CORRECT=", NULL };
	environ = newenv;
#endif	/* __GLIBC__ */
	val = fnmatch(p->l.pat, Fname, FNM_PATHNAME) == 0;
#ifdef	__GLIBC__
	environ = savenv;
#endif	/* __GLIBC__ */
	return val;
}
/*ARGSUSED*/
static int print(register struct anode *p)
{
	puts(Pathname);
	return(1);
}
/*ARGSUSED*/
static int prune(register struct anode *p)
{
	if (!depth)
		Prune = 1;
	return(1);
}
/*ARGSUSED*/
static int null(register struct anode *p)
{
	return(1);
}
static int mtime(register struct anode *p)
{
	return(scomp((Now - Statb.st_mtime) / A_DAY, p->l.t, p->r.s));
}
static int atime(register struct anode *p)
{
	return(scomp((Now - Statb.st_atime) / A_DAY, p->l.t, p->r.s));
}
static int ctime(register struct anode *p)
{
	return(scomp((Now - Statb.st_ctime) / A_DAY, p->l.t, p->r.s));
}
static int user(register struct anode *p)
{
	return(scomp(Statb.st_uid, p->l.u, p->r.s));
}
static int ino(register struct anode *p)
{
	return(scomp(Statb.st_ino, p->l.u, p->r.s));
}
static int group(register struct anode *p)
{
	return(p->l.u == Statb.st_gid);
}
static int nogroup(register struct anode *p)
{
	return(getgroup(Statb.st_gid) == NULL);
}
static int nouser(register struct anode *p)
{
	return(getuser(Statb.st_uid) == NULL);
}
static int links(register struct anode *p)
{
	return(scomp(Statb.st_nlink, p->l.link, p->r.s));
}
static int size(register struct anode *p)
{
	return(scomp(Statb.st_size?(Statb.st_size+511)>>9:0, p->l.sz, p->r.s));
}
static int sizec(register struct anode *p)
{
	return(scomp(Statb.st_size, p->l.sz, p->r.s));
}
static int perm(register struct anode *p)
{
	register int i;
	i = (p->r.s=='-') ? p->l.per : 07777; /* '-' means only arg bits */
	return((Statb.st_mode & i & 07777) == p->l.per);
}
static int type(register struct anode *p)
{
	return((Statb.st_mode&S_IFMT)==p->l.per);
}
static int exeq(register struct anode *p)
{
	if (p->r.a) {
		if (Pathname) {
			size_t	sz = strlen(Pathname) + 1;
			if (p->r.a->a_csz + sz <= p->r.a->a_msz &&
					p->r.a->a_cur < p->r.a->a_maxarg-1) {
				strcpy(p->r.a->a_vec[p->r.a->a_cur++] =
						&p->r.a->a_spc[p->r.a->a_csz],
						Pathname);
				p->r.a->a_csz += sz;
				return 1;
			} else {
				if (p->r.a->a_cur == 0) {
					p->r.a->a_vec[p->r.a->a_cur++] =
						Pathname;
					p->r.a->a_vec[p->r.a->a_cur] = NULL;
				}
				else {
					p->r.a->a_vec[p->r.a->a_cur] = NULL;
					fflush(stdout);
					doex(p->l.com, p->r.a);
					return exeq(p);
				}
			}
		} else {
			if (p->r.a->a_cur == 0)
				return 1;
			p->r.a->a_vec[p->r.a->a_cur] = NULL;
		}
	}
	fflush(stdout); /* to flush possible `-print' */
	return(doex(p->l.com, p->r.a));
}
static int ok(struct anode *p)
{
	char c;  int yes;
	yes = 0;
	fflush(stdout); /* to flush possible `-print' */
	fprintf(stderr, "< %s ... %s >?   ", Argv[p->l.com], Pathname);
	if (read(0, &c, 1) != 1)
		exit(2);
	yes = c == 'y';
	if (c != '\n')
		while (read(0, &c, 1) == 1 && c != '\n');
	if(yes) return(doex(p->l.com, 0));
	return(0);
}

static int cpio(struct anode *p)
{
	if (strchr(Pathname, '\n')) {
		pr("file name \"%s\" contains a newline character; "
			"file not archived", Pathname);
		status |= 1;
	} else
		fprintf(p->l.fp, "%s\n", Pathname);
	return(1);
}
static int newer(register struct anode *p)
{
	return Statb.st_mtime > p->l.t;
}
static int anewer(register struct anode *p)
{
	return Statb.st_atime > p->l.t;
}
static int cnewer(register struct anode *p)
{
	return Statb.st_ctime > p->l.t;
}
static int fstype(register struct anode *p)
{
#if defined (__linux__) || defined (_AIX) || defined (__hpux)
	return(EQ(fscur->fstype, p->l.fstype));
#elif defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) \
		|| defined (__DragonFly__) || defined (__APPLE__)
	return(EQ(Statfs, p->l.fstype));
#else
	return(EQ(Statb.st_fstype, p->l.fstype));
#endif
}
static int local(register struct anode *p)
{
#if defined (__linux__) || defined (_AIX) || defined (__hpux)
	return(strcmp(fscur->fstype, "nfs") && strcmp(fscur->fstype, "smbfs"));
#elif defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) \
		|| defined (__DragonFly__) || defined (__APPLE__)
	return(strcmp(Statfs, "nfs") != 0);
#else
	return(strcmp(Statb.st_fstype, "nfs") != 0);
#endif
}

/* support functions */
/* funny signed compare */
static int scomp(register long long a, register long long b, register char s)
{
	if(s == '+')
		return(a > b);
	if(s == '-')
		return(a < (b * -1));
	return(a == b);
}

static int
doex(int com, struct aggregate *a)
{
	register int np;
	register char *na;
	char **oargv;
	int oargc;
	static char **nargv;
	static int narga;
	static int ccode;
	pid_t	pid;

	ccode = np = 0;
	oargv = Argv;
	oargc = com;
	while (na=oargv[oargc++]) {
		if (np >= narga-1)
			nargv = srealloc(nargv, (narga+=20) * sizeof *nargv);
		if(strcmp(na, ";")==0 && oargv == Argv) break;
		if(strcmp(na, "{}")==0 && oargv == Argv) {
			if (a) {
				oargv = a->a_vec;
				oargc = 0;
			} else
				nargv[np++] = Pathname;
		}
		else nargv[np++] = na;
	}
	if (a) {
		a->a_cur = 0;
		a->a_csz = 0;
	}
	if (np==0) return(9);
	nargv[np] = 0;
	if(pid = fork()) /*parent*/ while (wait(&ccode) != pid);
	else { /*child*/
		if (fchdir(Home) < 0) {
			pr("bad starting directory");
			_exit(1);
		}
		execvp(nargv[0], nargv);
		_exit(1);
	}
	if (a && ccode) {
		if (WIFSIGNALED(ccode))
			status |= WTERMSIG(ccode) | 0200;
		else if (WIFEXITED(ccode))
			status |= WEXITSTATUS(ccode);
	}
	return(ccode && a==NULL ? 0:1);
}

static struct aggregate *mkagg(long baselen)
{
	static size_t	envsz;
	extern char	**environ;
	register int	i;
	struct aggregate	*a;

	a = srealloc(NULL, sizeof *a);
	if (envsz == 0)
		for (i = 0; environ[i]; i++)
			envsz += strlen(environ[i]) + 1;
	a->a_msz = sysconf(_SC_ARG_MAX) - baselen - envsz - 2048;
	a->a_spc = srealloc(NULL, a->a_msz);
	a->a_maxarg = 8192;
	a->a_vec = srealloc(NULL, a->a_maxarg * sizeof *a->a_vec);
	a->a_csz = 0;
	a->a_cur = 0;
	Execplus = 1;
	return a;
}

static uid_t getunum(const char *s) { /* find user name and return number */
	struct passwd *pwd;
	char *x;
	uid_t u;

	if ((pwd = getpwnam(s)) != NULL)
		return pwd->pw_uid;
	u = strtol(s, &x, 10);
	if (*x == '\0')
		return u;
	er("cannot find %s name", s);
	/*NOTREACHED*/
	return 0;
}

static gid_t getgnum(const char *s) { /* find group name and return number */
	struct group *grp;
	char *x;
	gid_t g;

	if ((grp = getgrnam(s)) != NULL)
		return grp->gr_gid;
	g = strtol(s, &x, 10);
	if (*x == '\0')
		return g;
	er("cannot find %s name", s);
	/*NOTREACHED*/
	return 0;
}

#define	CACHESIZE	16

static const char *getuser(uid_t uid)
{
	static struct {
		char	*name;
		uid_t	uid;
	} cache[CACHESIZE];
	static int	last;
	int	i;
	struct passwd	*pwd;
	const char	*name;

	for (i = 0; i < CACHESIZE && cache[i].name; i++)
		if (cache[i].uid == uid)
			goto found;
	if ((pwd = getpwuid(uid)) != NULL)
		name = pwd->pw_name;
	else
		name = "";
	if (i >= CACHESIZE) {
		if (last >= CACHESIZE)
			last = 0;
		i = last++;
	}
	if (cache[i].name)
		free(cache[i].name);
	cache[i].name = strdup(name);
	cache[i].uid = uid;
found:	return cache[i].name[0] ? cache[i].name : NULL;
}

static const char *getgroup(gid_t gid)
{
	static struct {
		char	*name;
		gid_t	gid;
	} cache[CACHESIZE];
	static int	last;
	int	i;
	struct group	*grp;
	const char	*name;

	for (i = 0; i < CACHESIZE && cache[i].name; i++)
		if (cache[i].gid == gid)
			goto found;
	if ((grp = getgrgid(gid)) != NULL)
		name = grp->gr_name;
	else
		name = "";
	if (i >= CACHESIZE) {
		if (last >= CACHESIZE)
			last = 0;
		i = last++;
	}
	if (cache[i].name)
		free(cache[i].name);
	cache[i].name = strdup(name);
	cache[i].gid = gid;
found:	return cache[i].name[0] ? cache[i].name : NULL;
}

#if defined (__linux__) || defined (_AIX) || defined (__hpux)
static void getfscur(dev_t dev)
{
	int	i;

	for (i = 0; fstypes[i].fstype; i++)
		if (fstypes[i].fsdev == dev) {
			fscur = &fstypes[i];
			return;
		}
	er("filesystem type for %s unknown", Pathname);
}

static void getfstypes(void)
{
	struct stat	st;
	FILE	*fp;
	struct mntent	*mp;
#ifdef	__hpux
	const char mtab[] = "/etc/mnttab";
#else	/* __linux__, _AIX */
	const char mtab[] = "/etc/mtab";
#endif	/* __linux__, _AIX */
	int	i = 0;

	if (fstypes)
		return;
	if ((fp = setmntent(mtab, "r")) == NULL)
		er("cannot open %s: %s", mtab, strerror(errno));
	while ((mp = getmntent(fp)) != NULL) {
		if (EQ(mp->mnt_type, MNTTYPE_IGNORE))
			continue;
		if (stat(mp->mnt_dir, &st) < 0)
			continue;
		fstypes = srealloc(fstypes, (i+1) * sizeof *fstypes);
		fstypes[i].fsdev = st.st_dev;
		fstypes[i].fstype = strdup(mp->mnt_type);
		i++;
	}
	endmntent(fp);
}
#endif	/* __linux__ || _AIX || __hpux */

/*
 * First part of descend, called for any file found.
 */
static int descend(char *fname, struct anode *exlist, int level)
{
	struct stat	ost;
	register char *c1;
	int i;
	int rv = 0;

	if(statfn(fname, &Statb)<0) {
		if (statfn != lstat && lstat(fname, &Statb) == 0)
		nof:	c1 = "cannot follow symbolic link %s: %s";
		else if (sysv3)
			c1 = "stat() failed: %s: %s";
		else if (errno == ENOENT || errno == ENOTDIR)
			c1 = "cannot open %s: %s";
		else
			c1 = "stat() error %s: %s";
		pr(c1, Pathname, strerror(errno));
		status = 18;
		return(0);
	}
	if (level == 0 && HLflag == 'H' && (Statb.st_mode&S_IFMT) == S_IFLNK) {
		struct stat	nst;
		if (stat(fname, &nst) == 0)
			Statb = nst;
		else if (errno == ELOOP)
			goto nof;
	}
#if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || \
		defined (__DragonFly__) || defined (__APPLE__)
	if (Statfs != NULL) {
		static struct statfs	sf;
		if (statfs(fname, &sf) < 0) {
			pr("statfs() error %s: %s", Pathname, strerror(errno));
			status = 18;
			return(0);
		}
		Statfs = sf.f_fstypename;
	}
#endif	/* __FreeBSD__, __NetBSD__, __OpenBSD__, __DragonFly__, __APPLE__ */
	if (Mount) {
		static dev_t	curdev;
		if (level == 0)
			curdev = Statb.st_dev;
		else if (curdev != Statb.st_dev)
			return(0);
	}
	Prune = 0;
	if (!depth) {
#if defined (__linux__) || defined (_AIX) || defined (__hpux)
		if (fstypes)
			getfscur(Statb.st_dev);
#endif	/* __linux__ || _AIX || __hpux */
		if((*exlist->F)(exlist) && Print)
		puts(Pathname);
	} else
		ost = Statb;
	if(Prune || (Statb.st_mode&S_IFMT)!=S_IFDIR)
		goto reg;
	if (statfn != lstat) {
		for (i = 0; i < level; i++)
			if (Statb.st_dev == visited[i].v_dev &&
					Statb.st_ino == visited[i].v_ino) {
#ifdef	SU3
				pr("Symbolic link loop at %s", Pathname);
				status = 18;
#endif	/* SU3 */
				goto reg;
			}
	}
	if (level >= vismax) {
		vismax += 20;
		visited = srealloc(visited, sizeof *visited * vismax);
	}
	visited[level].v_dev = Statb.st_dev;
	visited[level].v_ino = Statb.st_ino;

	rv = descend1(fname, exlist, level);

reg:
	if (depth) {
		Statb = ost;
#if defined (__linux__) || defined (_AIX) || defined (__hpux)
		if (fstypes)
			getfscur(Statb.st_dev);
#endif	/* __linux__ || _AIX || __hpux */
		if ((*exlist->F)(exlist) && Print)
			puts(Pathname);
	}
	return(rv);
}

/*
 * Second part of descend, called for any directory found.
 */
static int descend1(char *fname, struct anode *exlist, int level)
{
	int	dir = 0; /* open directory */
	register char *c1;
	struct getdb *db;
	register struct direc *dp;
	int endofname;
	int err;
	int oflags = O_RDONLY;

#ifdef	O_DIRECTORY
	oflags |= O_DIRECTORY;
#endif
#ifdef	O_NOFOLLOW
	if (statfn == lstat && (HLflag != 'H' || level > 0))
		oflags |= O_NOFOLLOW;
#endif
	if ((dir = open(fname, oflags)) < 0 ||
			fcntl(dir, F_SETFD, FD_CLOEXEC) < 0 ||
			fchdir(dir) < 0) {
		if (dir >= 0)
			close(dir);
		else if (errno == EMFILE && descend2(fname, exlist, level))
			/*
			 * A possible performance improvement would be to
			 * call descend2() in the directory above, since
			 * the current method involves one fork() call per
			 * subdirectory at this level. The condition occurs
			 * so rarely that it seems hardly worth optimization
			 * though.
			 */
			return 0;
		pr("cannot open %s: %s", Pathname, strerror(errno));
		status = 18;
		return 0;
	}
	if ((db = getdb_alloc(Pathname, dir)) == NULL) {
		write(2, "no memory\n", 10);
		_exit(077);
	}
	visited[level].v_db = db;
	visited[level].v_fd = dir;
	for(c1 = Pathname; *c1; ++c1);
	if(*(c1-1) == '/')
		--c1;
	endofname = c1 - Pathname;

	while ((dp = getdir(db, &err)) != NULL) {
		if((dp->d_name[0]=='.' && dp->d_name[1]=='\0') ||
				(dp->d_name[0]=='.' &&
				 dp->d_name[1]=='.' && dp->d_name[2]=='\0'))
			continue;
		setpath(&Pathname[endofname], dp->d_name, 1);
		Fname = &Pathname[endofname+1];
		if(descend(Fname, exlist, level+1)) {
			if (fchdir(dir) < 0)
				er("bad directory tree");
		}
	}
	Pathname[endofname] = '\0';
	getdb_free(db);
	if (err) {
		pr("cannot read dir %s: %s", Pathname, strerror(errno));
		status = 18;
	}
	close(dir);
	visited[level].v_fd = -1;
	return 1;
}

/*
 * Third part of descend, called if the limit of open file descriptors
 * is exceeded (EMFILE).
 */
static int descend2(char *fname, struct anode *exlist, int level)
{
	pid_t	pid;
	int	i;

	if (Cpio || Execplus)
		trailer(exlist, 0);
	fflush(stdout);
	switch (pid = fork()) {
	case 0:
		for (i = 0; i < level-1; i++) {
			if (visited[i].v_fd >= 0) {
				getdb_free(visited[i].v_db);
				close(visited[i].v_fd);
				visited[i].v_fd = -1;
			}
		}
		status |= 0;
		descend1(fname, exlist, level);
		if (Cpio || Execplus)
			trailer(exlist, 0);
		exit(status);
		/*NOTREACHED*/
	default:
		while (waitpid(pid, &i, 0) != pid);
		if (i && WIFSIGNALED(i)) {
			struct rlimit	rl;

			rl.rlim_cur = rl.rlim_max = 0;
			setrlimit(RLIMIT_CORE, &rl);
			raise(WTERMSIG(i));
			pause();
		}
		if (i)
			status |= WEXITSTATUS(i);
		return 1;
	case -1:
		return 0;
	}
}
static void setpath(char *eos, const char *fn, int slash)
{
	static char	*pathend;
	char	*opath;

	for (;;) {
		if (eos >= pathend) {
			pathend += 14;
			opath = Pathname;
			Pathname = srealloc(Pathname, pathend - Pathname);
			eos += Pathname - opath;
			pathend += Pathname - opath;
		}
		if (slash) {
			*eos++ = '/';
			slash = 0;
		} else
			if ((*eos++ = *fn++) == '\0')
				break;
	}
}

static void pr(const char *s, ...)
{
	va_list	ap;

	fprintf(stderr, "%s: ", progname);
	va_start(ap, s);
	vfprintf(stderr, s, ap);
	va_end(ap);
	fprintf(stderr, "\n");
}

static void er(const char *s, ...)
{
	va_list	ap;

	fprintf(stderr, "%s: ", progname);
	va_start(ap, s);
	vfprintf(stderr, s, ap);
	va_end(ap);
	fprintf(stderr, "\n");
	exit(1);
}

static void usage(void)
{
	er("path-list predicate-list");
}

static void *srealloc(void *op, size_t n)
{
	void	*np;

	if ((np = realloc(op, n)) == NULL) {
		write(2, "no memory\n", 10);
		_exit(077);
	}
	return np;
}

static void mkcpio(struct anode *p, const char *b, int ascii)
{
	int	fd, pd[2];
	char	flags[20], *cp;

	p->F = cpio;
	if (*b == '\0')
		return;
	depth = 1;
	Print = 0;
	Cpio = 1;
	if (pipe(pd) < 0 || (p->l.fp = fdopen(pd[1], "w")) == NULL)
		er("pipe() %s", strerror(errno));
	if ((fd = creat(b, 0666)) < 0)
		er("cannot create %s", b);
	switch (p->r.pid = fork()) {
	case -1:
		er("can't fork");
		/*NOTREACHED*/
	case 0:
		dup2(pd[0], 0);
		close(pd[0]);
		close(pd[1]);
		dup2(fd, 1);
		close(fd);
		cp = flags;
		*cp++ = '-';
		*cp++ = 'o';
		*cp++ = 'B';
		if (ascii)
			*cp++ = 'c';
		if (statfn == stat)
			*cp++ = 'L';
		*cp = '\0';
		execlp("cpio", "cpio", flags, NULL);
		pr("cannot exec cpio: %s", strerror(errno));
		_exit(0177);
		/*NOTREACHED*/
	}
	close(pd[0]);
	close(fd);
}

static void
trailer(register struct anode *p, int termcpio)
{
	char	*Opath = Pathname;
	Pathname = 0;
	if (p->F == or || p->F == and) {
		trailer(p->l.L, termcpio);
		trailer(p->r.R, termcpio);
	} else if (p->F == not)
		trailer(p->l.L, termcpio);
	else if (p->F == cpio) {
		if (termcpio) {
			int	s;

			fclose(p->l.fp);
			while (waitpid(p->r.pid, &s, 0) != p->r.pid);
			if (s) {
				if (WIFEXITED(s))
					status |= WEXITSTATUS(s);
				else if (WIFSIGNALED(s))
					status |= WTERMSIG(s) | 0200;
			}
		} else
			fflush(p->l.fp);
	} else if (p->F == exeq && p->r.a)
		exeq(p);
	Pathname = Opath;
}

static void
mknewer(struct anode *p, const char *b, int (*f)(struct anode *))
{
	if (*b && stat(b, &Statb) < 0)
		er("cannot access %s", b);
	p->l.t = Statb.st_mtime;
	p->F = f;
}

/*
 * Changes by Gunnar Ritter, Freiburg i. Br., Germany, September 2003.
 */
/*	from Unix 7th Edition /usr/src/cmd/chmod.c	*/
/*
 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *   Redistributions of source code and documentation must retain the
 *    above copyright notice, this list of conditions and the following
 *    disclaimer.
 *   Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *   All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed or owned by Caldera
 *      International, Inc.
 *   Neither the name of Caldera International, Inc. nor the names of
 *    other contributors may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
 * LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#define	USER	05700	/* user's bits */
#define	GROUP	02070	/* group's bits */
#define	OTHER	00007	/* other's bits */
#define	ALL	07777	/* all */

#define	READ	00444	/* read permit */
#define	WRITE	00222	/* write permit */
#define	EXEC	00111	/* exec permit */
#define	SETID	06000	/* set[ug]id */
#define	STICKY	01000	/* sticky bit */

#ifndef	S_ENFMT
#define	S_ENFMT	02000	/* mandatory locking bit */
#endif

static mode_t	absol(const char **);
static mode_t	who(const char **, mode_t *);
static int	what(const char **);
static mode_t	where(const char **, mode_t, int *, int *, const mode_t);

static mode_t
newmode(const char *ms, const mode_t pm)
{
	register mode_t	o, m, b;
	int	lock, setsgid = 0, cleared = 0, copy = 0;
	mode_t	nm, om, mm;
	const char *mo = ms;

	nm = om = pm;
	m = absol(&ms);
	if (!*ms) {
		nm = m;
		goto out;
	}
	if ((lock = (nm&S_IFMT) != S_IFDIR && (nm&(S_ENFMT|S_IXGRP)) == S_ENFMT)
			== 01)
		nm &= ~(mode_t)S_ENFMT;
	do {
		m = who(&ms, &mm);
		while (o = what(&ms)) {
			b = where(&ms, nm, &lock, &copy, pm);
			switch (o) {
			case '+':
				nm |= b & m & ~mm;
				if (b & S_ISGID)
					setsgid = 1;
				if (lock & 04)
					lock |= 02;
				break;
			case '-':
				nm &= ~(b & m & ~mm);
				if (b & S_ISGID)
					setsgid = 1;
				if (lock & 04)
					lock = 0;
				break;
			case '=':
				nm &= ~m;
				nm |= b & m & ~mm;
				lock &= ~01;
				if (lock & 04)
					lock |= 02;
				om = 0;
				if (copy == 0)
					cleared = 1;
				break;
			}
			lock &= ~04;
		}
	} while (*ms++ == ',');
	if (*--ms)
		er("bad permissions: %s", mo);
out:	if (pm & S_IFDIR) {
		if ((pm & S_ISGID) && setsgid == 0)
			nm |= S_ISGID;
		else if ((nm & S_ISGID) && setsgid == 0)
			nm &= ~(mode_t)S_ISGID;
	}
	return(nm);
}

static mode_t
absol(const char **ms)
{
	register int c, i;

	i = 0;
	while ((c = *(*ms)++) >= '0' && c <= '7')
		i = (i << 3) + (c - '0');
	(*ms)--;
	return(i);
}

static mode_t
who(const char **ms, mode_t *mp)
{
	register int m;

	m = 0;
	*mp = 0;
	for (;;) switch (*(*ms)++) {
	case 'u':
		m |= USER;
		continue;
	case 'g':
		m |= GROUP;
		continue;
	case 'o':
		m |= OTHER;
		continue;
	case 'a':
		m |= ALL;
		continue;
	default:
		(*ms)--;
		if (m == 0) {
			m = ALL;
			*mp = um;
		}
		return m;
	}
}

static int
what(const char **ms)
{
	switch (**ms) {
	case '+':
	case '-':
	case '=':
		return *(*ms)++;
	}
	return(0);
}

static mode_t
where(const char **ms, mode_t om, int *lock, int *copy, const mode_t pm)
{
	register mode_t m;

	m = 0;
	*copy = 0;
	switch (**ms) {
	case 'u':
		m = (om & USER) >> 6;
		goto dup;
	case 'g':
		m = (om & GROUP) >> 3;
		goto dup;
	case 'o':
		m = (om & OTHER);
	dup:
		*copy = 1;
		m &= (READ|WRITE|EXEC);
		m |= (m << 3) | (m << 6);
		++(*ms);
		return m;
	}
	for (;;) switch (*(*ms)++) {
	case 'r':
		m |= READ;
		continue;
	case 'w':
		m |= WRITE;
		continue;
	case 'x':
		m |= EXEC;
		continue;
	case 'X':
		if ((pm&S_IFMT) == S_IFDIR || (pm & EXEC))
			m |= EXEC;
		continue;
	case 'l':
		if ((pm&S_IFMT) != S_IFDIR)
			*lock |= 04;
		continue;
	case 's':
		m |= SETID;
		continue;
	case 't':
		m |= STICKY;
		continue;
	default:
		(*ms)--;
		return m;
	}
}