This repository has been archived by the owner on May 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
arg.c
2499 lines (2324 loc) · 53.8 KB
/
arg.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
/*
* arg.c - common argument processing support functions for lsof
*/
/*
* Copyright 1994 Purdue Research Foundation, West Lafayette, Indiana
* 47907. All rights reserved.
*
* Written by Victor A. Abell
*
* This software is not subject to any license of the American Telephone
* and Telegraph Company or the Regents of the University of California.
*
* Permission is granted to anyone to use this software for any purpose on
* any computer system, and to alter it and redistribute it freely, subject
* to the following restrictions:
*
* 1. Neither the authors nor Purdue University are responsible for any
* consequences of the use of this software.
*
* 2. The origin of this software must not be misrepresented, either by
* explicit claim or by omission. Credit to the authors and Purdue
* University must appear in documentation and sources.
*
* 3. Altered versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 4. This notice may not be removed or altered.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright 1994 Purdue Research Foundation.\nAll rights reserved.\n";
static char *rcsid = "$Id: arg.c,v 1.51 2012/04/10 16:30:06 abe Exp abe $";
#endif
#include "lsof.h"
/*
* Local definitions
*/
#define CMDRXINCR 32 /* CmdRx[] allocation increment */
/*
* Local static variables
*/
static int NCmdRxA = 0; /* space allocated to CmdRx[] */
/*
* Local function prototypes
*/
_PROTOTYPE(static int ckfd_range,(char *first, char *dash, char *last, int *lo, int *hi));
_PROTOTYPE(static int enter_fd_lst,(char *nm, int lo, int hi, int excl));
_PROTOTYPE(static int enter_nwad,(struct nwad *n, int sp, int ep, char *s, struct hostent *he));
_PROTOTYPE(static struct hostent *lkup_hostnm,(char *hn, struct nwad *n));
_PROTOTYPE(static char *isIPv4addr,(char *hn, unsigned char *a, int al));
/*
* ckfd_range() - check fd range
*/
static int
ckfd_range(first, dash, last, lo, hi)
char *first; /* starting character */
char *dash; /* '-' location */
char *last; /* '\0' location */
int *lo; /* returned low value */
int *hi; /* returned high value */
{
char *cp;
/*
* See if the range character pointers make sense.
*/
if (first >= dash || dash >= last) {
(void) fprintf(stderr, "%s: illegal FD range for -d: ", Pn);
safestrprt(first, stderr, 1);
return(1);
}
/*
* Assemble and check the high and low values.
*/
for (cp = first, *lo = 0; *cp && cp < dash; cp++) {
if (!isdigit((unsigned char)*cp)) {
FD_range_nondigit:
(void) fprintf(stderr, "%s: non-digit in -d FD range: ", Pn);
safestrprt(first, stderr, 1);
return(1);
}
*lo = (*lo * 10) + (int)(*cp - '0');
}
for (cp = dash+1, *hi = 0; *cp && cp < last; cp++) {
if (!isdigit((unsigned char)*cp))
goto FD_range_nondigit;
*hi = (*hi * 10) + (int)(*cp - '0');
}
if (*lo >= *hi) {
(void) fprintf(stderr, "%s: -d FD range's low >= its high: ", Pn);
safestrprt(first, stderr, 1);
return(1);
}
return(0);
}
/*
* ck_file_arg() - check file arguments
*/
int
ck_file_arg(i, ac, av, fv, rs, sbp)
int i; /* first file argument index */
int ac; /* argument count */
char *av[]; /* argument vector */
int fv; /* Ffilesys value (real or temporary) */
int rs; /* Readlink() status if argument count == 1:
* 0 = undone; 1 = done */
struct stat *sbp; /* if non-NULL, pointer to stat(2) buffer
* when argument count == 1 */
{
char *ap, *fnm, *fsnm, *path;
short err = 0;
int fsm, ftype, j, k;
MALLOC_S l;
struct mounts *mp;
static struct mounts **mmp = (struct mounts **)NULL;
int mx, nm;
static int nma = 0;
struct stat sb;
struct sfile *sfp;
short ss = 0;
#if defined(CKFA_EXPDEV)
dev_t dev, rdev;
#endif /* defined(CKFA_EXPDEV) */
#if defined(HASPROCFS)
unsigned char ad, an;
int pfsnl = -1;
pid_t pid;
struct procfsid *pfi;
#endif /* defined(HASPROCFS) */
/*
* Loop through arguments.
*/
for (; i < ac; i++) {
if (rs && (ac == 1) && (i == 0))
path = av[i];
else {
if (!(path = Readlink(av[i]))) {
ErrStat = 1;
continue;
}
}
/*
* Remove terminating `/' characters from paths longer than one.
*/
j = k = strlen(path);
while ((k > 1) && (path[k-1] == '/')) {
k--;
}
if (k < j) {
if (path != av[i])
path[k] = '\0';
else {
if (!(ap = (char *)malloc((MALLOC_S)(k + 1)))) {
(void) fprintf(stderr, "%s: no space for copy of %s\n",
Pn, path);
Exit(1);
}
(void) strncpy(ap, path, k);
ap[k] = '\0';
path = ap;
}
}
/*
* Check for file system argument.
*/
for (ftype = 1, mp = readmnt(), nm = 0;
(fv != 1) && mp;
mp = mp->next)
{
fsm = 0;
if (strcmp(mp->dir, path) == 0)
fsm++;
else if (fv == 2 || (mp->fs_mode & S_IFMT) == S_IFBLK) {
if (mp->fsnmres && strcmp(mp->fsnmres, path) == 0)
fsm++;
}
if (!fsm)
continue;
ftype = 0;
/*
* Skip duplicates.
*/
for (mx = 0; mx < nm; mx++) {
if (strcmp(mp->dir, mmp[mx]->dir) == 0
&& mp->dev == mmp[mx]->dev
&& mp->rdev == mmp[mx]->rdev
&& mp->inode == mmp[mx]->inode)
break;
}
if (mx < nm)
continue;
/*
* Allocate space for and save another mount point match and
* the type of match -- directory name (mounted) or file system
* name (mounted-on).
*/
if (nm >= nma) {
nma += 5;
l = (MALLOC_S)(nma * sizeof(struct mounts *));
if (mmp)
mmp = (struct mounts **)realloc((MALLOC_P *)mmp, l);
else
mmp = (struct mounts **)malloc(l);
if (!mmp) {
(void) fprintf(stderr,
"%s: no space for mount pointers\n", Pn);
Exit(1);
}
}
mmp[nm++] = mp;
}
if (fv == 2 && nm == 0) {
(void) fprintf(stderr, "%s: not a file system: ", Pn);
safestrprt(av[i], stderr, 1);
ErrStat = 1;
continue;
}
/*
* Loop through the file system matches. If there were none, make one
* pass through the loop, using simply the path name.
*/
mx = 0;
do {
/*
* Allocate an sfile structure and fill in the type and link.
*/
if (!(sfp = (struct sfile *)malloc(sizeof(struct sfile)))) {
(void) fprintf(stderr, "%s: no space for files\n", Pn);
Exit(1);
}
sfp->next = Sfile;
Sfile = sfp;
sfp->f = 0;
if ((sfp->type = ftype)) {
/*
* For a non-file system path, use the path as the file name
* and set a NULL file system name.
*/
fnm = path;
fsnm = (char *)NULL;
/*
* Stat the path to obtain its characteristics.
*/
if (sbp && (ac == 1))
sb = *sbp;
else {
if (statsafely(fnm, &sb) != 0) {
int en = errno;
(void) fprintf(stderr, "%s: status error on ", Pn);
safestrprt(fnm, stderr, 0);
(void) fprintf(stderr, ": %s\n", strerror(en));
Sfile = sfp->next;
(void) free((FREE_P *)sfp);
ErrStat = 1;
continue;
}
#if defined(HASSPECDEVD)
(void) HASSPECDEVD(fnm, &sb);
#endif /* defined(HASSPECDEVD) */
}
sfp->i = (INODETYPE)sb.st_ino;
sfp->mode = sb.st_mode & S_IFMT;
#if defined(CKFA_EXPDEV)
/*
* Expand device numbers before saving, so that they match the
* already-expanded local mount info table device numbers.
* (This is an EP/IX 2.1.1 and above artifact.)
*/
sfp->dev = expdev(sb.st_dev);
sfp->rdev = expdev(sb.st_rdev);
#else /* !defined(CKFA_EXPDEV) */
sfp->dev = sb.st_dev;
sfp->rdev = sb.st_rdev;
#endif /* defined(CKFA_EXPDEV) */
#if defined(CKFA_MPXCHAN)
/*
* Save a (possible) multiplexed channel number. (This is an
* AIX artifact.)
*/
sfp->ch = getchan(path);
#endif /* defined(CKFA_MPXCHAN) */
} else {
#if defined(SAVE_MP_IN_SFILE)
sfp->mp = mp = mmp[mx++];
#else /* !defined(SAVE_MP_IN_SFILE) */
mp = mmp[mx++];
#endif /* defined(SAVE_MP_IN_SFILE) */
ss++;
#if defined(HASPROCFS)
/*
* If this is a /proc file system, set the search flag and
* abandon the sfile entry.
*/
if (mp == Mtprocfs) {
Sfile = sfp->next;
(void) free((FREE_P *)sfp);
Procsrch = 1;
continue;
}
#endif /* defined(HASPROCFS) */
/*
* Derive file name and file system name for a mount point.
*
* Save the device numbers, inode number, and modes.
*/
fnm = mp->dir;
fsnm = mp->fsname;
sfp->dev = mp->dev;
sfp->rdev = mp->rdev;
sfp->i = mp->inode;
sfp->mode = mp->mode & S_IFMT;
}
ss = 1; /* indicate a "safe" stat() */
/*
* Store the file name and file system name pointers in the sfile
* structure, allocating space as necessary.
*/
if (!fnm || fnm == path) {
sfp->name = fnm;
#if defined(HASPROCFS)
an = 0;
#endif /* defined(HASPROCFS) */
} else {
if (!(sfp->name = mkstrcpy(fnm, (MALLOC_S *)NULL))) {
(void) fprintf(stderr,
"%s: no space for file name: ", Pn);
safestrprt(fnm, stderr, 1);
Exit(1);
}
#if defined(HASPROCFS)
an = 1;
#endif /* defined(HASPROCFS) */
}
if (!fsnm || fsnm == path) {
sfp->devnm = fsnm;
#if defined(HASPROCFS)
ad = 0;
#endif /* defined(HASPROCFS) */
} else {
if (!(sfp->devnm = mkstrcpy(fsnm, (MALLOC_S *)NULL))) {
(void) fprintf(stderr,
"%s: no space for file system name: ", Pn);
safestrprt(fsnm, stderr, 1);
Exit(1);
}
#if defined(HASPROCFS)
ad = 1;
#endif /* defined(HASPROCFS) */
}
if (!(sfp->aname = mkstrcpy(av[i], (MALLOC_S *)NULL))) {
(void) fprintf(stderr,
"%s: no space for argument file name: ", Pn);
safestrprt(av[i], stderr, 1);
Exit(1);
}
#if defined(HASPROCFS)
/*
* See if this is an individual member of a proc file system.
*/
if (!Mtprocfs || Procsrch)
continue;
# if defined(HASFSTYPE) && HASFSTYPE==1
if (strcmp(sb.st_fstype, HASPROCFS) != 0)
continue;
# endif /* defined(HASFSTYPE) && HASFSTYPE==1 */
if (pfsnl == -1)
pfsnl = strlen(Mtprocfs->dir);
if (!pfsnl)
continue;
if (strncmp(Mtprocfs->dir, path, pfsnl) != 0)
continue;
if (path[pfsnl] != '/')
# if defined(HASPINODEN)
pid = 0;
# else /* !defined(HASPINODEN) */
continue;
# endif /* defined(HASPINODEN) */
else {
for (j = pfsnl+1; path[j]; j++) {
if (!isdigit((unsigned char)path[j]))
break;
}
if (path[j] || (j - pfsnl - 1) < 1
|| (sfp->mode & S_IFMT) != S_IFREG)
# if defined(HASPINODEN)
pid = 0;
# else /* !defined(HASPINODEN) */
continue;
# endif /* defined(HASPINODEN) */
else
pid = atoi(&path[pfsnl+1]);
}
if (!(pfi = (struct procfsid *)malloc((MALLOC_S)
sizeof(struct procfsid))))
{
(void) fprintf(stderr, "%s: no space for %s ID: ",
Pn, Mtprocfs->dir);
safestrprt(path, stderr, 1);
Exit(1);
}
pfi->pid = pid;
pfi->f = 0;
pfi->nm = sfp->aname;
pfi->next = Procfsid;
Procfsid = pfi;
# if defined(HASPINODEN)
pfi->inode = (INODETYPE)sfp->i;
# endif /* defined(HASPINODEN) */
/*
* Abandon the Sfile entry, lest it be used in is_file_named().
*/
Sfile = sfp->next;
if (ad)
(void) free((FREE_P *)sfp->devnm);
if (an)
(void) free((FREE_P *)sfp->name);
(void) free((FREE_P *)sfp);
#endif /* defined(HASPROCFS) */
} while (mx < nm);
}
if (!ss)
err = 1;
return((int)err);
}
#if defined(HASDCACHE)
/*
* ctrl_dcache() - enter device cache control
*/
int
ctrl_dcache(c)
char *c; /* control string */
{
int rc = 0;
if (!c) {
(void) fprintf(stderr,
"%s: no device cache option control string\n", Pn);
return(1);
}
/*
* Decode argument function character.
*/
switch (*c) {
case '?':
if (*(c+1) != '\0') {
(void) fprintf(stderr, "%s: nothing should follow -D?\n", Pn);
return(1);
}
DChelp = 1;
return(0);
case 'b':
case 'B':
if (Setuidroot
#if !defined(WILLDROPGID)
|| Myuid
#endif /* !defined(WILLDROPGID) */
)
rc = 1;
else
DCstate = 1;
break;
case 'r':
case 'R':
if (Setuidroot && *(c+1))
rc = 1;
else
DCstate = 2;
break;
case 'u':
case 'U':
if (Setuidroot
#if !defined(WILLDROPGID)
|| Myuid
#endif /* !defined(WILLDROPGID) */
)
rc = 1;
else
DCstate = 3;
break;
case 'i':
case 'I':
if (*(c+1) == '\0') {
DCstate = 0;
return(0);
}
/* fall through */
default:
(void) fprintf(stderr, "%s: unknown -D option: ", Pn);
safestrprt(c, stderr, 1);
return(1);
}
if (rc) {
(void) fprintf(stderr, "%s: -D option restricted to root: ", Pn);
safestrprt(c, stderr, 1);
return(1);
}
/*
* Skip to optional path name and save it.
*/
for (c++; *c && (*c == ' ' || *c == '\t'); c++)
;
if (strlen(c)) {
if (!(DCpathArg = mkstrcpy(c, (MALLOC_S *)NULL))) {
(void) fprintf(stderr, "%s: no space for -D path: ", Pn);
safestrprt(c, stderr, 1);
Exit(1);
}
}
return(0);
}
#endif /* defined(HASDCACHE) */
/*
* enter_cmd_rx() - enter command regular expression
*/
int
enter_cmd_rx(x)
char *x; /* regular expression */
{
int bmod = 0;
int bxmod = 0;
int i, re;
int imod = 0;
int xmod = 0;
int co = REG_NOSUB|REG_EXTENDED;
char reb[256], *xb, *xe, *xm;
MALLOC_S xl;
char *xp = (char *)NULL;
/*
* Make sure the supplied string starts a regular expression.
*/
if (!*x || (*x != '/')) {
(void) fprintf(stderr, "%s: regexp doesn't begin with '/': ", Pn);
if (x)
safestrprt(x, stderr, 1);
return(1);
}
/*
* Skip to the end ('/') of the regular expression.
*/
xb = x + 1;
for (xe = xb; *xe; xe++) {
if (*xe == '/')
break;
}
if (*xe != '/') {
(void) fprintf(stderr, "%s: regexp doesn't end with '/': ", Pn);
safestrprt(x, stderr, 1);
return(1);
}
/*
* Decode any regular expression modifiers.
*/
for (i = 0, xm = xe + 1; *xm; xm++) {
switch(*xm) {
case 'b': /* This is a basic expression. */
if (++bmod > 1) {
if (bmod == 2) {
(void) fprintf(stderr,
"%s: b regexp modifier already used: ", Pn);
safestrprt(x, stderr, 1);
}
i = 1;
} else if (xmod) {
if (++bxmod == 1) {
(void) fprintf(stderr,
"%s: b and x regexp modifiers conflict: ", Pn);
safestrprt(x, stderr, 1);
}
i = 1;
} else
co &= ~REG_EXTENDED;
break;
case 'i': /* Ignore case. */
if (++imod > 1) {
if (imod == 2) {
(void) fprintf(stderr,
"%s: i regexp modifier already used: ", Pn);
safestrprt(x, stderr, 1);
}
i = 1;
} else
co |= REG_ICASE;
break;
case 'x': /* This is an extended expression. */
if (++xmod > 1) {
if (xmod == 2) {
(void) fprintf(stderr,
"%s: x regexp modifier already used: ", Pn);
safestrprt(x, stderr, 1);
}
i = 1;
} else if (bmod) {
if (++bxmod == 1) {
(void) fprintf(stderr,
"%s: b and x regexp modifiers conflict: ", Pn);
safestrprt(x, stderr, 1);
}
i = 1;
} else
co |= REG_EXTENDED;
break;
default:
(void) fprintf(stderr, "%s: invalid regexp modifier: %c\n",
Pn, (int)*xm);
i = 1;
}
}
if (i)
return(1);
/*
* Allocate space to hold expression and copy it there.
*/
xl = (MALLOC_S)(xe - xb);
if (!(xp = (char *)malloc(xl + 1))) {
(void) fprintf(stderr, "%s: no regexp space for: ", Pn);
safestrprt(x, stderr, 1);
Exit(1);
}
(void) strncpy(xp, xb, xl);
xp[(int)xl] = '\0';
/*
* Assign a new CmdRx[] slot for this expression.
*/
if (NCmdRxA >= NCmdRxU) {
/*
* More CmdRx[] space must be assigned.
*/
NCmdRxA += CMDRXINCR;
xl = (MALLOC_S)(NCmdRxA * sizeof(lsof_rx_t));
if (CmdRx)
CmdRx = (lsof_rx_t *)realloc((MALLOC_P *)CmdRx, xl);
else
CmdRx = (lsof_rx_t *)malloc(xl);
if (!CmdRx) {
(void) fprintf(stderr, "%s: no space for regexp: ", Pn);
safestrprt(x, stderr, 1);
Exit(1);
}
}
i = NCmdRxU;
CmdRx[i].exp = xp;
/*
* Compile the expression.
*/
if ((re = regcomp(&CmdRx[i].cx, xp, co))) {
(void) fprintf(stderr, "%s: regexp error: ", Pn);
safestrprt(x, stderr, 0);
(void) regerror(re, &CmdRx[i].cx, &reb[0], sizeof(reb));
(void) fprintf(stderr, ": %s\n", reb);
if (xp) {
(void) free((FREE_P *)xp);
xp = (char *)NULL;
}
return(1);
}
/*
* Complete the CmdRx[] table entry.
*/
CmdRx[i].mc = 0;
CmdRx[i].exp = xp;
NCmdRxU++;
return(0);
}
#if defined(HASEOPT)
/*
* enter_efsys() -- enter path of file system whose kernel blocks are to be
* eliminated
*/
int
enter_efsys(e, rdlnk)
char *e; /* file system path */
int rdlnk; /* avoid readlink(2) if non-zero */
{
char *ec; /* pointer to copy of path */
efsys_list_t *ep; /* file system path list pointer */
int i; /* temporary index */
char *path; /* Readlink() of file system path */
if (!e || (*e != '/')) {
if (!Fwarn)
(void) fprintf(stderr,
"%s: -e not followed by a file system path: \"%s\"\n",
Pn, e);
return(1);
}
if (!(ec = mkstrcpy(e, (MALLOC_S *)NULL))) {
(void) fprintf(stderr, "%s: no space for -e string: ", Pn);
safestrprt(e, stderr, 1);
Exit(1);
}
if (rdlnk)
path = ec;
else {
if (!(path = Readlink(ec)))
return(1);
}
/*
* Remove terminating `/' characters from paths longer than one.
*/
for (i = (int)strlen(path); (i > 1) && (path[i - 1] == '/'); i--) {
path[i - 1] = '\0';
}
/*
* Enter file system path on list, avoiding duplicates.
*/
for (ep = Efsysl; ep; ep = ep->next) {
if (!strcmp(ep->path, path)) {
(void)free((FREE_P *)path);
return (0);
}
}
if (!(ep = (efsys_list_t *)malloc((MALLOC_S)(sizeof(efsys_list_t))))) {
(void) fprintf(stderr, "%s: no space for \"-e %s\" entry\n",
Pn, e);
Exit(1);
}
ep->path = path;
ep->pathl = i;
ep->rdlnk = rdlnk;
ep->mp = (struct mounts *)NULL;
ep->next = Efsysl;
Efsysl = ep;
return(0);
}
#endif /* defined(HASEOPT) */
/*
* enter_fd() - enter file descriptor list for searching
*/
int
enter_fd(f)
char *f; /* file descriptor list pointer */
{
char c, *cp1, *cp2, *dash;
int err, excl, hi, lo;
char *fc;
/*
* Check for non-empty list and make a copy.
*/
if (!f || (strlen(f) + 1) < 2) {
(void) fprintf(stderr, "%s: no file descriptor specified\n", Pn);
return(1);
}
if (!(fc = mkstrcpy(f, (MALLOC_S *)NULL))) {
(void) fprintf(stderr, "%s: no space for fd string: ", Pn);
safestrprt(f, stderr, 1);
Exit(1);
}
/*
* Isolate each file descriptor in the comma-separated list, then enter it
* in the file descriptor string list. If a descriptor has the form:
*
* [0-9]+-[0-9]+
*
* treat it as an ascending range of file descriptor numbers.
*
* Accept a leading '^' as an excusion on match.
*/
for (cp1 = fc, err = 0; *cp1;) {
if (*cp1 == '^') {
excl = 1;
cp1++;
} else
excl = 0;
for (cp2 = cp1, dash = (char *)NULL; *cp2 && *cp2 != ','; cp2++) {
if (*cp2 == '-')
dash = cp2;
}
if ((c = *cp2) != '\0')
*cp2 = '\0';
if (cp2 > cp1) {
if (dash) {
if (ckfd_range(cp1, dash, cp2, &lo, &hi))
err = 1;
else {
if (enter_fd_lst((char *)NULL, lo, hi, excl))
err = 1;
}
} else {
if (enter_fd_lst(cp1, 0, 0, excl))
err = 1;
}
}
if (c == '\0')
break;
cp1 = cp2 + 1;
}
(void) free((FREE_P *)fc);
return(err);
}
/*
* enter_fd_lst() - make an entry in the FD list, Fdl
*/
static int
enter_fd_lst(nm, lo, hi, excl)
char *nm; /* FD name (none if NULL) */
int lo; /* FD low boundary (if nm NULL) */
int hi; /* FD high boundary (if nm NULL) */
int excl; /* exclusion on match */
{
char buf[256], *cp;
int n;
struct fd_lst *f, *ft;
/*
* Don't allow a mixture of exclusions and inclusions.
*/
if (FdlTy >= 0) {
if (FdlTy != excl) {
if (!Fwarn) {
/*
* If warnings are enabled, report a mixture.
*/
if (nm) {
(void) snpf(buf, sizeof(buf) - 1, "%s%s",
excl ? "^" : "", nm);
} else {
if (lo != hi) {
(void) snpf(buf, sizeof(buf) - 1, "%s%d-%d",
excl ? "^" : "", lo, hi);
} else {
(void) snpf(buf, sizeof(buf) - 1, "%s%d",
excl ? "^" : "", lo);
}
}
buf[sizeof(buf) - 1] = '\0';
(void) fprintf(stderr,
"%s: %s in an %s -d list: %s\n", Pn,
excl ? "exclude" : "include",
FdlTy ? "exclude" : "include",
buf);
}
return(1);
}
}
/*
* Allocate an fd_lst entry.
*/
if (!(f = (struct fd_lst *)malloc((MALLOC_S)sizeof(struct fd_lst)))) {
(void) fprintf(stderr, "%s: no space for FD list entry\n", Pn);
Exit(1);
}
if (nm) {
/*
* Process an FD name. First see if it contains only digits; if it
* does, convert them to an integer and set the low and high
* boundaries to the result.
*
* If the name has a non-digit, store it as a string, and set the
* boundaries to impossible values (i.e., low > high).
*/
for (cp = nm, n = 0; *cp; cp++) {
if (!isdigit((unsigned char)*cp))
break;
n = (n * 10) + (int)(*cp - '0');
}
if (*cp) {
if (!(f->nm = mkstrcpy(nm, (MALLOC_S *)NULL))) {
(void) fprintf(stderr,
"%s: no space for copy of: %s\n", Pn, nm);
Exit(1);
}
lo = 1;
hi = 0;
} else {
f->nm = (char *)NULL;
lo = hi = n;
}
} else
f->nm = (char *)NULL;
/*
* Skip duplicates.
*/
for (ft = Fdl; ft; ft = ft->next) {
if (f->nm) {
if (!ft->nm || strcmp(f->nm, ft->nm))
continue;
} else if ((lo != ft->lo) || (hi != ft->hi))
continue;
(void) free((FREE_P *)f);
return(0);
}
/*
* Complete the fd_lst entry and link it to the head of the chain.
*/
f->hi = hi;
f->lo = lo;
f->next = Fdl;
Fdl = f;
FdlTy = excl;
return(0);
}
/*
* enter_dir() - enter the files of a directory for searching
*/
#define EDDEFFNL 128 /* default file name length */
int
enter_dir(d, descend)
char *d; /* directory path name pointer */
int descend; /* subdirectory descend flag:
* 0 = don't descend
* 1 = descend */
{
char *av[2];
dev_t ddev;
DIR *dfp;
char *dn = (char *)NULL;
MALLOC_S dnl, dnamlen;
struct DIRTYPE *dp;
int en, sl;
int fct = 0;
char *fp = (char *)NULL;
MALLOC_S fpl = (MALLOC_S)0;
MALLOC_S fpli = (MALLOC_S)0;
struct stat sb;
/*
* Check the directory path; reduce symbolic links; stat(2) it; make sure it's
* really a directory.
*/
if (!d || !*d || *d == '+' || *d == '-') {
if (!Fwarn)
(void) fprintf(stderr,