forked from lsof-org/lsof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.c
2823 lines (2490 loc) · 71.2 KB
/
print.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
/*
* print.c - common print 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.
*/
#include "lsof.h"
/*
* Local definitions, structures and function prototypes
*/
#define HCINC 64 /* host cache size increase chunk */
#define PORTHASHBUCKETS \
128 /* port hash bucket count \
* !!MUST BE A POWER OF 2!! */
#define PORTTABTHRESH \
10 /* threshold at which we will switch \
* from using getservbyport() to \
* getservent() -- see lkup_port() \
* and fill_porttab() */
struct hostcache {
unsigned char a[MAX_AF_ADDR]; /* numeric address */
int af; /* address family -- e.g., AF_INET
* or AF_INET6 */
char *name; /* name */
};
struct porttab {
int port;
MALLOC_S nl; /* name length (excluding '\0') */
int ss; /* service name status, 0 = lookup not
* yet performed */
char *name;
struct porttab *next;
};
#if defined(HASNORPC_H)
static struct porttab **Pth[2] = {NULL, NULL};
/* port hash buckets:
* Pth[0] for TCP service names
* Pth[1] for UDP service names
*/
#else /* !defined(HASNORPC_H) */
static struct porttab **Pth[4] = {NULL, NULL, NULL, NULL};
/* port hash buckets:
* Pth[0] for TCP service names
* Pth[1] for UDP service names
* Pth[2] for TCP portmap info
* Pth[3] for UDP portmap info
*/
#endif /* defined(HASNORPC_H) */
#define HASHPORT(p) (((((int)(p)) * 31415) >> 3) & (PORTHASHBUCKETS - 1))
#if !defined(HASNORPC_H)
_PROTOTYPE(static void fill_portmap, (void));
_PROTOTYPE(static void update_portmap, (struct porttab * pt, char *pn));
#endif /* !defined(HASNORPC_H) */
_PROTOTYPE(static void fill_porttab, (void));
_PROTOTYPE(static char *lkup_port, (int p, int pr, int src));
_PROTOTYPE(static char *lkup_svcnam, (int h, int p, int pr, int ss));
_PROTOTYPE(static int printinaddr, (void));
_PROTOTYPE(static int human_readable_size, (SZOFFTYPE sz, int print, int col));
/*
* endnm() - locate end of Namech
*/
char *endnm(sz)
size_t *sz; /* returned remaining size */
{
register char *s;
register size_t tsz;
for (s = Namech, tsz = Namechl; *s; s++, tsz--)
;
*sz = tsz;
return (s);
}
#if !defined(HASNORPC_H)
/*
* fill_portmap() -- fill the RPC portmap program name table via a conversation
* with the portmapper
*
* The following copyright notice acknowledges that this function was adapted
* from getrpcportnam() of the source code of the OpenBSD netstat program.
*/
/*
* Copyright (c) 1983, 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 THE REGENTS OR CONTRIBUTORS 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.
*/
static void fill_portmap() {
static int already_run = 0;
char buf[128], *cp, *nm;
CLIENT *c;
int h, port, pr;
MALLOC_S nl;
struct pmaplist *p = (struct pmaplist *)NULL;
struct porttab *pt;
struct rpcent *r;
struct TIMEVAL_LSOF tm;
# if !defined(CAN_USE_CLNT_CREATE)
struct hostent *he;
struct sockaddr_in ia;
int s = RPC_ANYSOCK;
# endif /* !defined(CAN_USE_CLNT_CREATE) */
/*
* Make sure this is only run once.
*/
if (already_run)
return;
already_run = 1;
/*
* Construct structures for communicating with the portmapper.
*/
# if !defined(CAN_USE_CLNT_CREATE)
zeromem(&ia, sizeof(ia));
ia.sin_family = AF_INET;
if ((he = gethostbyname("localhost")))
MEMMOVE((caddr_t)&ia.sin_addr, he->h_addr, he->h_length);
ia.sin_port = htons(PMAPPORT);
# endif /* !defined(CAN_USE_CLNT_CREATE) */
tm.tv_sec = 60;
tm.tv_usec = 0;
/*
* Get an RPC client handle. Then ask for a dump of the port map.
*/
# if defined(CAN_USE_CLNT_CREATE)
if (!(c = clnt_create("localhost", PMAPPROG, PMAPVERS, "tcp")))
# else /* !defined(CAN_USE_CLNT_CREATE) */
if (!(c = clnttcp_create(&ia, PMAPPROG, PMAPVERS, &s, 0, 0)))
# endif /* defined(CAN_USE_CLNT_CREATE) */
return;
if (clnt_call(c, PMAPPROC_DUMP, XDR_VOID, NULL, XDR_PMAPLIST, (caddr_t)&p,
tm) != RPC_SUCCESS) {
clnt_destroy(c);
return;
}
/*
* Loop through the port map dump, creating portmap table entries from TCP
* and UDP members.
*/
for (; p; p = p->pml_next) {
/*
* Determine the port map entry's protocol; ignore all but TCP and UDP.
*/
if (p->pml_map.pm_prot == IPPROTO_TCP)
pr = 2;
else if (p->pml_map.pm_prot == IPPROTO_UDP)
pr = 3;
else
continue;
/*
* See if there's already a portmap entry for this port. If there is,
* ignore this entry.
*/
h = HASHPORT((port = (int)p->pml_map.pm_port));
for (pt = Pth[pr][h]; pt; pt = pt->next) {
if (pt->port == port)
break;
}
if (pt)
continue;
/*
* Save the registration name or number.
*/
cp = (char *)NULL;
if ((r = (struct rpcent *)getrpcbynumber(p->pml_map.pm_prog))) {
if (r->r_name && strlen(r->r_name))
cp = r->r_name;
}
if (!cp) {
(void)snpf(buf, sizeof(buf), "%lu",
(unsigned long)p->pml_map.pm_prog);
cp = buf;
}
if (!strlen(cp))
continue;
/*
* Allocate space for the portmap name entry and copy it there.
*/
if (!(nm = mkstrcpy(cp, &nl))) {
(void)fprintf(stderr,
"%s: can't allocate space for portmap entry: ", Pn);
safestrprt(cp, stderr, 1);
Error();
}
if (!nl) {
(void)free((FREE_P *)nm);
continue;
}
/*
* Allocate and fill a porttab struct entry for the portmap table.
* Link it to the head of its hash bucket, and make it the new head.
*/
if (!(pt = (struct porttab *)malloc(sizeof(struct porttab)))) {
(void)fprintf(stderr,
"%s: can't allocate porttab entry for portmap: ", Pn);
safestrprt(nm, stderr, 1);
Error();
}
pt->name = nm;
pt->nl = nl;
pt->port = port;
pt->next = Pth[pr][h];
pt->ss = 0;
Pth[pr][h] = pt;
}
clnt_destroy(c);
}
#endif /* !defined(HASNORPC_H) */
/*
* fill_porttab() -- fill the TCP and UDP service name port table with a
* getservent() scan
*/
static void fill_porttab() {
int h, p, pr;
MALLOC_S nl;
char *nm;
struct porttab *pt;
struct servent *se;
(void)endservent();
/*
* Scan the services data base for TCP and UDP entries that have a non-null
* name associated with them.
*/
(void)setservent(1);
while ((se = getservent())) {
if (!se->s_name || !se->s_proto)
continue;
if (strcasecmp(se->s_proto, "TCP") == 0)
pr = 0;
else if (strcasecmp(se->s_proto, "UDP") == 0)
pr = 1;
else
continue;
if (!se->s_name || !strlen(se->s_name))
continue;
p = ntohs(se->s_port);
/*
* See if a port->service entry is already cached for this port and
* prototcol. If it is, leave it alone.
*/
h = HASHPORT(p);
for (pt = Pth[pr][h]; pt; pt = pt->next) {
if (pt->port == p)
break;
}
if (pt)
continue;
/*
* Add a new entry to the cache for this port and protocol.
*/
if (!(nm = mkstrcpy(se->s_name, &nl))) {
(void)fprintf(stderr,
"%s: can't allocate %d bytes for port %d name: %s\n",
Pn, (int)(nl + 1), p, se->s_name);
Error();
}
if (!nl) {
(void)free((FREE_P *)nm);
continue;
}
if (!(pt = (struct porttab *)malloc(sizeof(struct porttab)))) {
(void)fprintf(stderr,
"%s: can't allocate porttab entry for port %d: %s\n",
Pn, p, se->s_name);
Error();
}
pt->name = nm;
pt->nl = nl - 1;
pt->port = p;
pt->next = Pth[pr][h];
pt->ss = 0;
Pth[pr][h] = pt;
}
(void)endservent();
}
/*
* gethostnm() - get host name
*/
char *gethostnm(ia, af)
unsigned char *ia; /* Internet address */
int af; /* address family -- e.g., AF_INET
* or AF_INET6 */
{
int al = MIN_AF_ADDR;
char hbuf[256];
static struct hostcache *hc = (struct hostcache *)NULL;
static int hcx = 0;
char *hn, *np;
struct hostent *he = (struct hostent *)NULL;
int i, j;
MALLOC_S len;
static int nhc = 0;
/*
* Search cache.
*/
#if defined(HASIPv6)
if (af == AF_INET6)
al = MAX_AF_ADDR;
#endif /* defined(HASIPv6) */
for (i = 0; i < hcx; i++) {
if (af != hc[i].af)
continue;
for (j = 0; j < al; j++) {
if (ia[j] != hc[i].a[j])
break;
}
if (j >= al)
return (hc[i].name);
}
/*
* If -n has been specified, construct a numeric address. Otherwise, look
* up host name by address. If that fails, or if there is no name in the
* returned hostent structure, construct a numeric version of the address.
*/
if (Fhost)
he = gethostbyaddr((char *)ia, al, af);
if (!he || !he->h_name) {
#if defined(HASIPv6)
if (af == AF_INET6) {
/*
* Since IPv6 numeric addresses use `:' as a separator, enclose
* them in brackets.
*/
hbuf[0] = '[';
if (!inet_ntop(af, ia, hbuf + 1, sizeof(hbuf) - 3)) {
(void)snpf(&hbuf[1], (sizeof(hbuf) - 1),
"can't format IPv6 address]");
} else {
len = strlen(hbuf);
(void)snpf(&hbuf[len], sizeof(hbuf) - len, "]");
}
} else
#endif /* defined(HASIPv6) */
if (af == AF_INET)
(void)snpf(hbuf, sizeof(hbuf), "%u.%u.%u.%u", ia[0], ia[1],
ia[2], ia[3]);
else
(void)snpf(hbuf, sizeof(hbuf), "(unknown AF value: %d)", af);
hn = hbuf;
} else
hn = (char *)he->h_name;
/*
* Allocate space for name and copy name to it.
*/
if (!(np = mkstrcpy(hn, (MALLOC_S *)NULL))) {
(void)fprintf(stderr, "%s: no space for host name: ", Pn);
safestrprt(hn, stderr, 1);
Error();
}
/*
* Add address/name entry to cache. Allocate cache space in HCINC chunks.
*/
if (hcx >= nhc) {
nhc += HCINC;
len = (MALLOC_S)(nhc * sizeof(struct hostcache));
if (!hc)
hc = (struct hostcache *)malloc(len);
else
hc = (struct hostcache *)realloc((MALLOC_P *)hc, len);
if (!hc) {
(void)fprintf(stderr, "%s: no space for host cache\n", Pn);
Error();
}
}
hc[hcx].af = af;
for (i = 0; i < al; i++) {
hc[hcx].a[i] = ia[i];
}
hc[hcx++].name = np;
return (np);
}
/*
* lkup_port() - look up port for protocol
*/
static char *lkup_port(p, pr, src)
int p; /* port number */
int pr; /* protocol index: 0 = tcp, 1 = udp */
int src; /* port source: 0 = local
* 1 = foreign */
{
int h, nh;
MALLOC_S nl;
char *nm, *pn;
static char pb[128];
struct porttab *pt;
/*
* If the hash buckets haven't been allocated, do so.
*/
if (!Pth[0]) {
#if defined(HASNORPC_H)
nh = 2;
#else /* !defined(HASNORPC_H) */
nh = FportMap ? 4 : 2;
#endif /* defined(HASNORPC_H) */
for (h = 0; h < nh; h++) {
if (!(Pth[h] = (struct porttab **)calloc(
PORTHASHBUCKETS, sizeof(struct porttab *)))) {
(void)fprintf(
stderr,
"%s: can't allocate %d bytes for %s %s hash buckets\n", Pn,
(int)(2 * (PORTHASHBUCKETS * sizeof(struct porttab *))),
(h & 1) ? "UDP" : "TCP", (h > 1) ? "portmap" : "port");
Error();
}
}
}
#if !defined(HASNORPC_H)
/*
* If we're looking up program names for portmapped ports, make sure the
* portmap table has been loaded.
*/
if (FportMap)
(void)fill_portmap();
#endif /* !defined(HASNORPC_H) */
/*
* Hash the port and see if its name has been cached. Look for a local
* port first in the portmap, if portmap searching is enabled.
*/
h = HASHPORT(p);
#if !defined(HASNORPC_H)
if (!src && FportMap) {
for (pt = Pth[pr + 2][h]; pt; pt = pt->next) {
if (pt->port != p)
continue;
if (!pt->ss) {
pn = Fport ? lkup_svcnam(h, p, pr, 0) : (char *)NULL;
if (!pn) {
(void)snpf(pb, sizeof(pb), "%d", p);
pn = pb;
}
(void)update_portmap(pt, pn);
}
return (pt->name);
}
}
#endif /* !defined(HASNORPC_H) */
for (pt = Pth[pr][h]; pt; pt = pt->next) {
if (pt->port == p)
return (pt->name);
}
/*
* Search for a possible service name, unless the -P option has been
* specified.
*
* If there is no service name, return a %d conversion.
*
* Don't cache %d conversions; a zero port number is a %d conversion that
* is represented by "*".
*/
pn = Fport ? lkup_svcnam(h, p, pr, 1) : (char *)NULL;
if (!pn || !strlen(pn)) {
if (p) {
(void)snpf(pb, sizeof(pb), "%d", p);
return (pb);
} else
return ("*");
}
/*
* Allocate a new porttab entry for the TCP or UDP service name.
*/
if (!(pt = (struct porttab *)malloc(sizeof(struct porttab)))) {
(void)fprintf(stderr, "%s: can't allocate porttab entry for port %d\n",
Pn, p);
Error();
}
/*
* Allocate space for the name; copy it to the porttab entry; and link the
* porttab entry to its hash bucket.
*
* Return a pointer to the name.
*/
if (!(nm = mkstrcpy(pn, &nl))) {
(void)fprintf(stderr, "%s: can't allocate space for port name: ", Pn);
safestrprt(pn, stderr, 1);
Error();
}
pt->name = nm;
pt->nl = nl;
pt->port = p;
pt->next = Pth[pr][h];
pt->ss = 0;
Pth[pr][h] = pt;
return (nm);
}
/*
* lkup_svcnam() - look up service name for port
*/
static char *lkup_svcnam(h, p, pr, ss)
int h; /* porttab hash index */
int p; /* port number */
int pr; /* protocol: 0 = TCP, 1 = UDP */
int ss; /* search status: 1 = Pth[pr][h]
* already searched */
{
static int fl[PORTTABTHRESH];
static int fln = 0;
static int gsbp = 0;
int i;
struct porttab *pt;
static int ptf = 0;
struct servent *se;
/*
* Do nothing if -P has been specified.
*/
if (!Fport)
return ((char *)NULL);
for (;;) {
/*
* Search service name cache, if it hasn't already been done.
* Return the name of a match.
*/
if (!ss) {
for (pt = Pth[pr][h]; pt; pt = pt->next) {
if (pt->port == p)
return (pt->name);
}
}
/*
* If fill_porttab() has been called, there is no service name.
*
* Do PORTTABTHRES getservbport() calls, remembering the failures, so
* they won't be repeated.
*
* After PORTABTHRESH getservbyport() calls, call fill_porttab() once,
*/
if (ptf)
break;
if (gsbp < PORTTABTHRESH) {
for (i = 0; i < fln; i++) {
if (fl[i] == p)
return ((char *)NULL);
}
gsbp++;
if ((se = getservbyport(htons(p), pr ? "udp" : "tcp")))
return (se->s_name);
if (fln < PORTTABTHRESH)
fl[fln++] = p;
return ((char *)NULL);
}
(void)fill_porttab();
ptf++;
ss = 0;
}
return ((char *)NULL);
}
/*
* print_file() - print file
*/
void print_file() {
char buf[128];
char *cp = (char *)NULL;
dev_t dev;
int devs, len;
if (PrPass && !Hdr) {
/*
* Print the header line if this is the second pass and the
* header hasn't already been printed.
*/
(void)printf("%-*.*s %*s", CmdColW, CmdColW, CMDTTL, PidColW, PIDTTL);
#if defined(HASTASKS)
if (TaskPrtTid)
(void)printf(" %*s", TaskTidColW, TASKTIDTTL);
if (TaskPrtCmd)
(void)printf(" %-*.*s", TaskCmdColW, TaskCmdColW, TASKCMDTTL);
#endif /* defined(HASTASKS) */
#if defined(HASZONES)
if (Fzone)
(void)printf(" %-*s", ZoneColW, ZONETTL);
#endif /* defined(HASZONES) */
#if defined(HASSELINUX)
if (Fcntx)
(void)printf(" %-*s", CntxColW, CNTXTTL);
#endif /* defined(HASSELINUX) */
#if defined(HASPPID)
if (Fppid)
(void)printf(" %*s", PpidColW, PPIDTTL);
#endif /* defined(HASPPID) */
if (Fpgid)
(void)printf(" %*s", PgidColW, PGIDTTL);
(void)printf(" %*s %*s %*s", UserColW, USERTTL, FdColW - 2, FDTTL,
TypeColW, TYPETTL);
#if defined(HASFSTRUCT)
if (Fsv) {
# if !defined(HASNOFSADDR)
if (Fsv & FSV_FA)
(void)printf(" %*s", FsColW, FSTTL);
# endif /* !defined(HASNOFSADDR) */
# if !defined(HASNOFSCOUNT)
if (Fsv & FSV_CT)
(void)printf(" %*s", FcColW, FCTTL);
# endif /* !defined(HASNOFSCOUNT) */
# if !defined(HASNOFSFLAGS)
if (Fsv & FSV_FG)
(void)printf(" %*s", FgColW, FGTTL);
# endif /* !defined(HASNOFSFLAGS) */
# if !defined(HASNOFSNADDR)
if (Fsv & FSV_NI)
(void)printf(" %*s", NiColW, NiTtl);
# endif /* !defined(HASNOFSNADDR) */
}
#endif /* defined(HASFSTRUCT) */
(void)printf(" %*s", DevColW, DEVTTL);
if (Foffset)
(void)printf(" %*s", SzOffColW, OFFTTL);
else if (Fsize)
(void)printf(" %*s", SzOffColW, SZTTL);
else
(void)printf(" %*s", SzOffColW, SZOFFTTL);
if (Fnlink)
(void)printf(" %*s", NlColW, NLTTL);
(void)printf(" %*s %s\n", NodeColW, NODETTL, NMTTL);
Hdr++;
}
/*
* Size or print the command.
*
* CAUTION: command can be empty, see issue #246,
* use NULL to represent failure instead of empty string
*/
cp = Lp->cmd ? Lp->cmd : "(unknown)";
if (!PrPass) {
len = safestrlen(cp, 2);
if (CmdLim && (len > CmdLim))
len = CmdLim;
if (len > CmdColW)
CmdColW = len;
} else
safestrprtn(cp, CmdColW, stdout, 2);
/*
* Size or print the process ID.
*/
if (!PrPass) {
(void)snpf(buf, sizeof(buf), "%d", Lp->pid);
if ((len = strlen(buf)) > PidColW)
PidColW = len;
} else
(void)printf(" %*d", PidColW, Lp->pid);
#if defined(HASTASKS)
/*
* Size or print task ID and command name.
*/
if (!PrPass) {
if ((cp = Lp->tcmd)) {
len = safestrlen(cp, 2);
if (TaskCmdLim && (len > TaskCmdLim))
len = TaskCmdLim;
if (len > TaskCmdColW)
TaskCmdColW = len;
TaskPrtCmd = 1;
}
if (Lp->tid) {
(void)snpf(buf, sizeof(buf), "%d", Lp->tid);
if ((len = strlen(buf)) > TaskTidColW)
TaskTidColW = len;
TaskPrtTid = 1;
}
} else {
if (TaskPrtTid) {
if (Lp->tid)
(void)printf(" %*d", TaskTidColW, Lp->tid);
else
(void)printf(" %*s", TaskTidColW, "");
}
if (TaskPrtCmd) {
cp = Lp->tcmd ? Lp->tcmd : "";
printf(" ");
safestrprtn(cp, TaskCmdColW, stdout, 2);
}
}
#endif /* defined(HASTASKS) */
#if defined(HASZONES)
/*
* Size or print the zone.
*/
if (Fzone) {
if (!PrPass) {
if (Lp->zn) {
if ((len = strlen(Lp->zn)) > ZoneColW)
ZoneColW = len;
}
} else
(void)printf(" %-*s", ZoneColW, Lp->zn ? Lp->zn : "");
}
#endif /* defined(HASZONES) */
#if defined(HASSELINUX)
/*
* Size or print the context.
*/
if (Fcntx) {
if (!PrPass) {
if (Lp->cntx) {
if ((len = strlen(Lp->cntx)) > CntxColW)
CntxColW = len;
}
} else
(void)printf(" %-*s", CntxColW, Lp->cntx ? Lp->cntx : "");
}
#endif /* defined(HASSELINUX) */
#if defined(HASPPID)
if (Fppid) {
/*
* Size or print the parent process ID.
*/
if (!PrPass) {
(void)snpf(buf, sizeof(buf), "%d", Lp->ppid);
if ((len = strlen(buf)) > PpidColW)
PpidColW = len;
} else
(void)printf(" %*d", PpidColW, Lp->ppid);
}
#endif /* defined(HASPPID) */
if (Fpgid) {
/*
* Size or print the process group ID.
*/
if (!PrPass) {
(void)snpf(buf, sizeof(buf), "%d", Lp->pgid);
if ((len = strlen(buf)) > PgidColW)
PgidColW = len;
} else
(void)printf(" %*d", PgidColW, Lp->pgid);
}
/*
* Size or print the user ID or login name.
*/
if (!PrPass) {
if ((len = strlen(printuid((UID_ARG)Lp->uid, NULL))) > UserColW)
UserColW = len;
} else
(void)printf(" %*.*s", UserColW, UserColW,
printuid((UID_ARG)Lp->uid, NULL));
/*
* Size or print the file descriptor, access mode and lock status.
*/
if (!PrPass) {
(void)snpf(buf, sizeof(buf), "%s%c%c", Lf->fd,
(Lf->lock == ' ') ? Lf->access
: (Lf->access == ' ') ? '-'
: Lf->access,
Lf->lock);
if ((len = strlen(buf)) > FdColW)
FdColW = len;
} else
(void)printf(" %*.*s%c%c", FdColW - 2, FdColW - 2, Lf->fd,
(Lf->lock == ' ') ? Lf->access
: (Lf->access == ' ') ? '-'
: Lf->access,
Lf->lock);
/*
* Size or print the type.
*/
if (!PrPass) {
if ((len = strlen(Lf->type)) > TypeColW)
TypeColW = len;
} else
(void)printf(" %*.*s", TypeColW, TypeColW, Lf->type);
#if defined(HASFSTRUCT)
/*
* Size or print the file structure address, file usage count, and node
* ID (address).
*/
if (Fsv) {
# if !defined(HASNOFSADDR)
if (Fsv & FSV_FA) {
cp =
(Lf->fsv & FSV_FA) ? print_kptr(Lf->fsa, buf, sizeof(buf)) : "";
if (!PrPass) {
if ((len = strlen(cp)) > FsColW)
FsColW = len;
} else
(void)printf(" %*.*s", FsColW, FsColW, cp);
}
# endif /* !defined(HASNOFSADDR) */
# if !defined(HASNOFSCOUNT)
if (Fsv & FSV_CT) {
if (Lf->fsv & FSV_CT) {
(void)snpf(buf, sizeof(buf), "%ld", Lf->fct);
cp = buf;
} else
cp = "";
if (!PrPass) {
if ((len = strlen(cp)) > FcColW)
FcColW = len;
} else
(void)printf(" %*.*s", FcColW, FcColW, cp);
}
# endif /* !defined(HASNOFSCOUNT) */
# if !defined(HASNOFSFLAGS)
if (Fsv & FSV_FG) {
if ((Lf->fsv & FSV_FG) && (FsvFlagX || Lf->ffg || Lf->pof))
cp = print_fflags(Lf->ffg, Lf->pof);
else
cp = "";
if (!PrPass) {
if ((len = strlen(cp)) > FgColW)
FgColW = len;
} else
(void)printf(" %*.*s", FgColW, FgColW, cp);
}
# endif /* !defined(HASNOFSFLAGS) */
# if !defined(HASNOFSNADDR)
if (Fsv & FSV_NI) {
cp =
(Lf->fsv & FSV_NI) ? print_kptr(Lf->fna, buf, sizeof(buf)) : "";
if (!PrPass) {
if ((len = strlen(cp)) > NiColW)
NiColW = len;
} else
(void)printf(" %*.*s", NiColW, NiColW, cp);
}
# endif /* !defined(HASNOFSNADDR) */
}
#endif /* defined(HASFSTRUCT) */
/*
* Size or print the device information.
*/
if (Lf->rdev_def) {
dev = Lf->rdev;
devs = 1;
} else if (Lf->dev_def) {
dev = Lf->dev;
devs = 1;
} else
devs = 0;
if (devs) {
#if defined(HASPRINTDEV)
cp = HASPRINTDEV(Lf, &dev);
#else /* !defined(HASPRINTDEV) */
(void)snpf(buf, sizeof(buf), "%u,%u", GET_MAJ_DEV(dev),
GET_MIN_DEV(dev));
cp = buf;
#endif /* defined(HASPRINTDEV) */
}
if (!PrPass) {
if (devs)
len = strlen(cp);
else if (Lf->dev_ch)
len = strlen(Lf->dev_ch);
else
len = 0;
if (len > DevColW)
DevColW = len;
} else {
if (devs)
(void)printf(" %*.*s", DevColW, DevColW, cp);
else {
if (Lf->dev_ch)
(void)printf(" %*.*s", DevColW, DevColW, Lf->dev_ch);
else
(void)printf(" %*.*s", DevColW, DevColW, "");
}
}
/*
* Size or print the size or offset.
*/
if (!PrPass) {
if (Lf->sz_def) {
if (Fhuman) {
len = human_readable_size(Lf->sz, 0, 0);
} else {
(void)snpf(buf, sizeof(buf), SzOffFmt_d, Lf->sz);
len = strlen(buf);
}
} else if (Lf->off_def) {
#if defined(HASPRINTOFF)
cp = HASPRINTOFF(Lf, 0);
#else /* !defined(HASPRINTOFF) */
(void)snpf(buf, sizeof(buf), SzOffFmt_0t, Lf->off);
cp = buf;
#endif /* defined(HASPRINTOFF) */
len = strlen(cp);
if (OffDecDig && len > (OffDecDig + 2)) {
#if defined(HASPRINTOFF)
cp = HASPRINTOFF(Lf, 1);