Imported Upstream version 4.89
[platform/upstream/lsof.git] / print.c
1 /*
2  * print.c - common print support functions for lsof
3  */
4
5
6 /*
7  * Copyright 1994 Purdue Research Foundation, West Lafayette, Indiana
8  * 47907.  All rights reserved.
9  *
10  * Written by Victor A. Abell
11  *
12  * This software is not subject to any license of the American Telephone
13  * and Telegraph Company or the Regents of the University of California.
14  *
15  * Permission is granted to anyone to use this software for any purpose on
16  * any computer system, and to alter it and redistribute it freely, subject
17  * to the following restrictions:
18  *
19  * 1. Neither the authors nor Purdue University are responsible for any
20  *    consequences of the use of this software.
21  *
22  * 2. The origin of this software must not be misrepresented, either by
23  *    explicit claim or by omission.  Credit to the authors and Purdue
24  *    University must appear in documentation and sources.
25  *
26  * 3. Altered versions must be plainly marked as such, and must not be
27  *    misrepresented as being the original software.
28  *
29  * 4. This notice may not be removed or altered.
30  */
31
32 #ifndef lint
33 static char copyright[] =
34 "@(#) Copyright 1994 Purdue Research Foundation.\nAll rights reserved.\n";
35 static char *rcsid = "$Id: print.c,v 1.55 2013/01/02 17:14:59 abe Exp $";
36 #endif
37
38
39 #include "lsof.h"
40
41
42 /*
43  * Local definitions, structures and function prototypes
44  */
45
46 #define HCINC           64              /* host cache size increase chunk */
47 #define PORTHASHBUCKETS 128             /* port hash bucket count
48                                          * !!MUST BE A POWER OF 2!! */
49 #define PORTTABTHRESH   10              /* threshold at which we will switch
50                                          * from using getservbyport() to
51                                          * getservent() -- see lkup_port()
52                                          * and fill_porttab() */
53
54 struct hostcache {
55         unsigned char a[MAX_AF_ADDR];   /* numeric address */
56         int af;                         /* address family -- e.g., AF_INET
57                                          * or AF_INET6 */
58         char *name;                     /* name */
59 };
60
61 struct porttab {
62         int port;
63         MALLOC_S nl;                    /* name length (excluding '\0') */
64         int ss;                         /* service name status, 0 = lookup not
65                                          * yet performed */
66         char *name;
67         struct porttab *next;
68 };
69
70
71 #if     defined(HASNORPC_H)
72 static struct porttab **Pth[2] = { NULL, NULL };
73                                                 /* port hash buckets:
74                                                  * Pth[0] for TCP service names
75                                                  * Pth[1] for UDP service names
76                                                  */
77 #else   /* !defined(HASNORPC_H) */
78 static struct porttab **Pth[4] = { NULL, NULL, NULL, NULL };
79                                                 /* port hash buckets:
80                                                  * Pth[0] for TCP service names
81                                                  * Pth[1] for UDP service names
82                                                  * Pth[2] for TCP portmap info
83                                                  * Pth[3] for UDP portmap info
84                                                  */
85 #endif  /* defined(HASNORPC_H) */
86
87 #define HASHPORT(p)     (((((int)(p)) * 31415) >> 3) & (PORTHASHBUCKETS - 1))
88
89
90 #if     !defined(HASNORPC_H)
91 _PROTOTYPE(static void fill_portmap,(void));
92 _PROTOTYPE(static void update_portmap,(struct porttab *pt, char *pn));
93 #endif  /* !defined(HASNORPC_H) */
94
95 _PROTOTYPE(static void fill_porttab,(void));
96 _PROTOTYPE(static char *lkup_port,(int p, int pr, int src));
97 _PROTOTYPE(static char *lkup_svcnam,(int h, int p, int pr, int ss));
98 _PROTOTYPE(static int printinaddr,(void));
99
100
101 /*
102  * endnm() - locate end of Namech
103  */
104
105 char *
106 endnm(sz)
107         size_t *sz;                     /* returned remaining size */
108 {
109         register char *s;
110         register size_t tsz;
111
112         for (s = Namech, tsz = Namechl; *s; s++, tsz--)
113                 ;
114         *sz = tsz;
115         return(s);
116 }
117
118
119 #if !defined(HASNORPC_H)
120 /*
121  * fill_portmap() -- fill the RPC portmap program name table via a conversation
122  *                   with the portmapper
123  *
124  * The following copyright notice acknowledges that this function was adapted
125  * from getrpcportnam() of the source code of the OpenBSD netstat program.
126  */
127
128 /*
129 * Copyright (c) 1983, 1988, 1993
130 *      The Regents of the University of California.  All rights reserved.
131 *
132 * Redistribution and use in source and binary forms, with or without
133 * modification, are permitted provided that the following conditions
134 * are met:
135 * 1. Redistributions of source code must retain the above copyright
136 *    notice, this list of conditions and the following disclaimer.
137 * 2. Redistributions in binary form must reproduce the above copyright
138 *    notice, this list of conditions and the following disclaimer in the
139 *    documentation and/or other materials provided with the distribution.
140 * 3. All advertising materials mentioning features or use of this software
141 *    must display the following acknowledgement:
142 *      This product includes software developed by the University of
143 *      California, Berkeley and its contributors.
144 * 4. Neither the name of the University nor the names of its contributors
145 *    may be used to endorse or promote products derived from this software
146 *    without specific prior written permission.
147 *
148 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
149 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
150 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
151 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
152 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
153 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
154 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
155 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
156 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
157 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
158 * SUCH DAMAGE.
159 */
160
161 static void
162 fill_portmap()
163 {
164         char buf[128], *cp, *nm;
165         CLIENT *c;
166         int h, port, pr;
167         MALLOC_S nl;
168         struct pmaplist *p = (struct pmaplist *)NULL;
169         struct porttab *pt;
170         struct rpcent *r;
171         struct TIMEVAL_LSOF tm;
172
173 #if     !defined(CAN_USE_CLNT_CREATE)
174         struct hostent *he;
175         struct sockaddr_in ia;
176         int s = RPC_ANYSOCK;
177 #endif  /* !defined(CAN_USE_CLNT_CREATE) */
178
179 /*
180  * Construct structures for communicating with the portmapper.
181  */
182
183 #if     !defined(CAN_USE_CLNT_CREATE)
184         zeromem(&ia, sizeof(ia));
185         ia.sin_family = AF_INET;
186         if ((he = gethostbyname("localhost")))
187             MEMMOVE((caddr_t)&ia.sin_addr, he->h_addr, he->h_length);
188         ia.sin_port = htons(PMAPPORT);
189 #endif  /* !defined(CAN_USE_CLNT_CREATE) */
190
191         tm.tv_sec = 60;
192         tm.tv_usec = 0;
193 /*
194  * Get an RPC client handle.  Then ask for a dump of the port map.
195  */
196
197 #if     defined(CAN_USE_CLNT_CREATE)
198         if (!(c = clnt_create("localhost", PMAPPROG, PMAPVERS, "tcp")))
199 #else   /* !defined(CAN_USE_CLNT_CREATE) */
200         if (!(c = clnttcp_create(&ia, PMAPPROG, PMAPVERS, &s, 0, 0)))
201 #endif  /* defined(CAN_USE_CLNT_CREATE) */
202
203             return;
204         if (clnt_call(c, PMAPPROC_DUMP, XDR_VOID, NULL, XDR_PMAPLIST,
205                       (caddr_t)&p, tm)
206         != RPC_SUCCESS) {
207             clnt_destroy(c);
208             return;
209         }
210 /*
211  * Loop through the port map dump, creating portmap table entries from TCP
212  * and UDP members.
213  */
214         for (; p; p = p->pml_next) {
215         
216         /*
217          * Determine the port map entry's protocol; ignore all but TCP and UDP.
218          */
219             if (p->pml_map.pm_prot == IPPROTO_TCP)
220                 pr = 2;
221             else if (p->pml_map.pm_prot == IPPROTO_UDP)
222                 pr = 3;
223             else
224                 continue;
225         /*
226          * See if there's already a portmap entry for this port.  If there is,
227          * ignore this entry.
228          */
229             h = HASHPORT((port = (int)p->pml_map.pm_port));
230             for (pt = Pth[pr][h]; pt; pt = pt->next) {
231                 if (pt->port == port)
232                     break;
233             }
234             if (pt)
235                 continue;
236         /*
237          * Save the registration name or number.
238          */
239             cp = (char *)NULL;
240             if ((r = (struct rpcent *)getrpcbynumber(p->pml_map.pm_prog))) {
241                 if (r->r_name && strlen(r->r_name))
242                     cp = r->r_name;
243             }
244             if (!cp) {
245                 (void) snpf(buf, sizeof(buf), "%lu",
246                             (unsigned long)p->pml_map.pm_prog);
247                 cp = buf;
248             }
249             if (!strlen(cp))
250                 continue;
251         /*
252          * Allocate space for the portmap name entry and copy it there.
253          */
254             if (!(nm = mkstrcpy(cp, &nl))) {
255                 (void) fprintf(stderr,
256                     "%s: can't allocate space for portmap entry: ", Pn);
257                 safestrprt(cp, stderr, 1);
258                 Exit(1);
259             }
260             if (!nl) {
261                 (void) free((FREE_P *)nm);
262                 continue;
263             }
264         /*
265          * Allocate and fill a porttab struct entry for the portmap table.
266          * Link it to the head of its hash bucket, and make it the new head.
267          */
268             if (!(pt = (struct porttab *)malloc(sizeof(struct porttab)))) {
269                 (void) fprintf(stderr,
270                     "%s: can't allocate porttab entry for portmap: ", Pn);
271                 safestrprt(nm, stderr, 1);
272                 Exit(1);
273             }
274             pt->name = nm;
275             pt->nl = nl;
276             pt->port = port;
277             pt->next = Pth[pr][h];
278             pt->ss = 0;
279             Pth[pr][h] = pt;
280         }
281         clnt_destroy(c);
282 }
283 #endif  /* !defined(HASNORPC_H) */
284
285
286 /*
287  * fill_porttab() -- fill the TCP and UDP service name port table with a
288  *                   getservent() scan
289  */
290
291 static void
292 fill_porttab()
293 {
294         int h, p, pr;
295         MALLOC_S nl;
296         char *nm;
297         struct porttab *pt;
298         struct servent *se;
299
300         (void) endservent();
301 /*
302  * Scan the services data base for TCP and UDP entries that have a non-null
303  * name associated with them.
304  */
305         (void) setservent(1);
306         while ((se = getservent())) {
307             if (!se->s_name || !se->s_proto)
308                 continue;
309             if (strcasecmp(se->s_proto, "TCP") == 0)
310                 pr = 0;
311             else if (strcasecmp(se->s_proto, "UDP") == 0)
312                 pr = 1;
313             else
314                 continue;
315             if (!se->s_name || !strlen(se->s_name))
316                 continue;
317             p = ntohs(se->s_port);
318         /*
319          * See if a port->service entry is already cached for this port and
320          * prototcol.  If it is, leave it alone.
321          */
322             h = HASHPORT(p);
323             for (pt = Pth[pr][h]; pt; pt = pt->next) {
324                 if (pt->port == p)
325                     break;
326             }
327             if (pt)
328                 continue;
329         /*
330          * Add a new entry to the cache for this port and protocol.
331          */
332             if (!(nm = mkstrcpy(se->s_name, &nl))) {
333                 (void) fprintf(stderr,
334                     "%s: can't allocate %d bytes for port %d name: %s\n",
335                     Pn, (int)(nl + 1), p, se->s_name);
336                 Exit(1);
337             }
338             if (!nl) {
339                 (void) free((FREE_P *)nm);
340                 continue;
341             }
342             if (!(pt = (struct porttab *)malloc(sizeof(struct porttab)))) {
343                 (void) fprintf(stderr,
344                     "%s: can't allocate porttab entry for port %d: %s\n",
345                     Pn, p, se->s_name);
346                 Exit(1);
347             }
348             pt->name = nm;
349             pt->nl = nl - 1;
350             pt->port = p;
351             pt->next = Pth[pr][h];
352             pt->ss = 0;
353             Pth[pr][h] = pt;
354         }
355         (void) endservent();
356 }
357
358
359 /*
360  * gethostnm() - get host name
361  */
362
363 char *
364 gethostnm(ia, af)
365         unsigned char *ia;              /* Internet address */
366         int af;                         /* address family -- e.g., AF_INET
367                                          * or AF_INET6 */
368 {
369         int al = MIN_AF_ADDR;
370         char hbuf[256];
371         static struct hostcache *hc = (struct hostcache *)NULL;
372         static int hcx = 0;
373         char *hn, *np;
374         struct hostent *he = (struct hostent *)NULL;
375         int i, j;
376         MALLOC_S len;
377         static int nhc = 0;
378 /*
379  * Search cache.
380  */
381
382 #if     defined(HASIPv6)
383         if (af == AF_INET6)
384             al = MAX_AF_ADDR;
385 #endif  /* defined(HASIPv6) */
386
387         for (i = 0; i < hcx; i++) {
388             if (af != hc[i].af)
389                 continue;
390             for (j = 0; j < al; j++) {
391                 if (ia[j] != hc[i].a[j])
392                     break;
393             }
394             if (j >= al)
395                 return(hc[i].name);
396         }
397 /*
398  * If -n has been specified, construct a numeric address.  Otherwise, look up
399  * host name by address.  If that fails, or if there is no name in the returned
400  * hostent structure, construct a numeric version of the address.
401  */
402         if (Fhost)
403             he = gethostbyaddr((char *)ia, al, af);
404         if (!he || !he->h_name) {
405
406 #if     defined(HASIPv6)
407             if (af == AF_INET6) {
408
409             /*
410              * Since IPv6 numeric addresses use `:' as a separator, enclose
411              * them in brackets.
412              */
413                 hbuf[0] = '[';
414                 if (!inet_ntop(af, ia, hbuf + 1, sizeof(hbuf) - 3)) {
415                     (void) snpf(&hbuf[1], (sizeof(hbuf) - 1),
416                         "can't format IPv6 address]");
417                 } else {
418                     len = strlen(hbuf);
419                     (void) snpf(&hbuf[len], sizeof(hbuf) - len, "]");
420                 }
421             } else
422 #endif  /* defined(HASIPv6) */
423
424             if (af == AF_INET)
425                 (void) snpf(hbuf, sizeof(hbuf), "%u.%u.%u.%u", ia[0], ia[1],
426                             ia[2], ia[3]);
427             else
428                 (void) snpf(hbuf, sizeof(hbuf), "(unknown AF value: %d)", af);
429             hn = hbuf;
430         } else
431             hn = (char *)he->h_name;
432 /*
433  * Allocate space for name and copy name to it.
434  */
435         if (!(np = mkstrcpy(hn, (MALLOC_S *)NULL))) {
436             (void) fprintf(stderr, "%s: no space for host name: ", Pn);
437             safestrprt(hn, stderr, 1);
438             Exit(1);
439         }
440 /*
441  * Add address/name entry to cache.  Allocate cache space in HCINC chunks.
442  */
443         if (hcx >= nhc) {
444             nhc += HCINC;
445             len = (MALLOC_S)(nhc * sizeof(struct hostcache));
446             if (!hc)
447                 hc = (struct hostcache *)malloc(len);
448             else
449                 hc = (struct hostcache *)realloc((MALLOC_P *)hc, len);
450             if (!hc) {
451                 (void) fprintf(stderr, "%s: no space for host cache\n", Pn);
452                 Exit(1);
453             }
454         }
455         hc[hcx].af = af;
456         for (i = 0; i < al; i++) {
457             hc[hcx].a[i] = ia[i];
458         }
459         hc[hcx++].name = np;
460         return(np);
461 }
462
463
464 /*
465  * lkup_port() - look up port for protocol
466  */
467
468 static char *
469 lkup_port(p, pr, src)
470         int p;                          /* port number */
471         int pr;                         /* protocol index: 0 = tcp, 1 = udp */
472         int src;                        /* port source: 0 = local
473                                          *              1 = foreign */
474 {
475         int h, nh;
476         MALLOC_S nl;
477         char *nm, *pn;
478         static char pb[128];
479         static int pm = 0;
480         struct porttab *pt;
481 /*
482  * If the hash buckets haven't been allocated, do so.
483  */
484         if (!Pth[0]) {
485
486 #if     defined(HASNORPC_H)
487             nh = 2;
488 #else   /* !defined(HASNORPC_H) */
489             nh = FportMap ? 4 : 2;
490 #endif  /* defined(HASNORPC_H) */
491
492             for (h = 0; h < nh; h++) {
493                 if (!(Pth[h] = (struct porttab **)calloc(PORTHASHBUCKETS,
494                                 sizeof(struct porttab *))))
495                 {
496                     (void) fprintf(stderr,
497                       "%s: can't allocate %d bytes for %s %s hash buckets\n",
498                       Pn,
499                       (int)(2 * (PORTHASHBUCKETS * sizeof(struct porttab *))),
500                       (h & 1) ? "UDP" : "TCP",
501                       (h > 1) ? "portmap" : "port");
502                     Exit(1);
503                 }
504             }
505         }
506
507 #if     !defined(HASNORPC_H)
508 /*
509  * If we're looking up program names for portmapped ports, make sure the
510  * portmap table has been loaded.
511  */
512         if (FportMap && !pm) {
513             (void) fill_portmap();
514             pm++;
515         }
516 #endif  /* !defined(HASNORPC_H) */
517
518 /*
519  * Hash the port and see if its name has been cached.  Look for a local
520  * port first in the portmap, if portmap searching is enabled.
521  */
522         h = HASHPORT(p);
523
524 #if     !defined(HASNORPC_H)
525         if (!src && FportMap) {
526             for (pt = Pth[pr+2][h]; pt; pt = pt->next) {
527                 if (pt->port != p)
528                     continue;
529                 if (!pt->ss) {
530                     pn = Fport ? lkup_svcnam(h, p, pr, 0) : (char *)NULL;
531                     if (!pn) {
532                         (void) snpf(pb, sizeof(pb), "%d", p);
533                         pn = pb;
534                     }
535                     (void) update_portmap(pt, pn);
536                 }
537                 return(pt->name);
538             }
539         }
540 #endif  /* !defined(HASNORPC_H) */
541
542         for (pt = Pth[pr][h]; pt; pt = pt->next) {
543             if (pt->port == p)
544                 return(pt->name);
545         }
546 /*
547  * Search for a possible service name, unless the -P option has been specified.
548  *
549  * If there is no service name, return a %d conversion.
550  *
551  * Don't cache %d conversions; a zero port number is a %d conversion that
552  * is represented by "*".
553  */
554         pn = Fport ? lkup_svcnam(h, p, pr, 1) : (char *)NULL;
555         if (!pn || !strlen(pn)) {
556             if (p) {
557                 (void) snpf(pb, sizeof(pb), "%d", p);
558                 return(pb);
559             } else
560                 return("*");
561         }
562 /*
563  * Allocate a new porttab entry for the TCP or UDP service name.
564  */
565         if (!(pt = (struct porttab *)malloc(sizeof(struct porttab)))) {
566             (void) fprintf(stderr,
567                 "%s: can't allocate porttab entry for port %d\n", Pn, p);
568             Exit(1);
569         }
570 /*
571  * Allocate space for the name; copy it to the porttab entry; and link the
572  * porttab entry to its hash bucket.
573  *
574  * Return a pointer to the name.
575  */
576         if (!(nm = mkstrcpy(pn, &nl))) {
577             (void) fprintf(stderr,
578                 "%s: can't allocate space for port name: ", Pn);
579             safestrprt(pn, stderr, 1);
580             Exit(1);
581         }
582         pt->name = nm;
583         pt->nl = nl;
584         pt->port = p;
585         pt->next = Pth[pr][h];
586         pt->ss = 0;
587         Pth[pr][h] = pt;
588         return(nm);
589 }
590
591
592 /*
593  * lkup_svcnam() - look up service name for port
594  */
595
596 static char *
597 lkup_svcnam(h, p, pr, ss)
598         int h;                          /* porttab hash index */
599         int p;                          /* port number */
600         int pr;                         /* protocol: 0 = TCP, 1 = UDP */
601         int ss;                         /* search status: 1 = Pth[pr][h]
602                                          *                already searched */
603 {
604         static int fl[PORTTABTHRESH];
605         static int fln = 0;
606         static int gsbp = 0;
607         int i;
608         struct porttab *pt;
609         static int ptf = 0;
610         struct servent *se;
611 /*
612  * Do nothing if -P has been specified.
613  */
614         if (!Fport)
615             return((char *)NULL);
616
617         for (;;) {
618
619         /*
620          * Search service name cache, if it hasn't already been done.
621          * Return the name of a match.
622          */
623             if (!ss) {
624                 for (pt = Pth[pr][h]; pt; pt = pt->next) {
625                     if (pt->port == p)
626                         return(pt->name);
627                 }
628             }
629 /*
630  * If fill_porttab() has been called, there is no service name.
631  *
632  * Do PORTTABTHRES getservbport() calls, remembering the failures, so they
633  * won't be repeated.
634  *
635  * After PORTABTHRESH getservbyport() calls, call fill_porttab() once,
636  */
637             if (ptf)
638                 break;
639             if (gsbp < PORTTABTHRESH) {
640                 for (i = 0; i < fln; i++) {
641                     if (fl[i] == p)
642                         return((char *)NULL);
643                 }
644                 gsbp++;
645                 if ((se = getservbyport(htons(p), pr ? "udp" : "tcp")))
646                     return(se->s_name);
647                 if (fln < PORTTABTHRESH)
648                     fl[fln++] = p;
649                 return((char *)NULL);
650             }
651             (void) fill_porttab();
652             ptf++;
653             ss = 0;
654         }
655         return((char *)NULL);
656 }
657
658
659 /*
660  * print_file() - print file
661  */
662
663 void
664 print_file()
665 {
666         char buf[128];
667         char *cp = (char *)NULL;
668         dev_t dev;
669         int devs, len;
670
671         if (PrPass && !Hdr) {
672
673         /*
674          * Print the header line if this is the second pass and the
675          * header hasn't already been printed.
676          */
677             (void) printf("%-*.*s %*s", CmdColW, CmdColW, CMDTTL, PidColW,
678                 PIDTTL);
679
680 #if     defined(HASTASKS)
681             if (TaskPrtFl)
682                 (void) printf(" %*s", TidColW, TIDTTL);
683 #endif  /* defined(HASTASKS) */
684
685 #if     defined(HASZONES)
686             if (Fzone)
687                 (void) printf(" %-*s", ZoneColW, ZONETTL);
688 #endif  /* defined(HASZONES) */
689
690 #if     defined(HASSELINUX)
691             if (Fcntx)
692                 (void) printf(" %-*s", CntxColW, CNTXTTL);
693 #endif /* defined(HASSELINUX) */
694
695 #if     defined(HASPPID)
696             if (Fppid)
697                 (void) printf(" %*s", PpidColW, PPIDTTL);
698 #endif  /* defined(HASPPID) */
699
700             if (Fpgid)
701                 (void) printf(" %*s", PgidColW, PGIDTTL);
702             (void) printf(" %*s %*s   %*s",
703                 UserColW, USERTTL,
704                 FdColW - 2, FDTTL,
705                 TypeColW, TYPETTL);
706
707 #if     defined(HASFSTRUCT)
708             if (Fsv) {
709
710 # if    !defined(HASNOFSADDR)
711                 if (Fsv & FSV_FA)
712                     (void) printf(" %*s", FsColW, FSTTL);
713 # endif /* !defined(HASNOFSADDR) */
714
715 # if    !defined(HASNOFSCOUNT)
716                 if (Fsv & FSV_CT)
717                     (void) printf(" %*s", FcColW, FCTTL);
718 # endif /* !defined(HASNOFSCOUNT) */
719
720 # if    !defined(HASNOFSFLAGS)
721                 if (Fsv & FSV_FG)
722                     (void) printf(" %*s", FgColW, FGTTL);
723 # endif /* !defined(HASNOFSFLAGS) */
724
725 # if    !defined(HASNOFSNADDR)
726                 if (Fsv & FSV_NI)
727                     (void) printf(" %*s", NiColW, NiTtl);
728 # endif /* !defined(HASNOFSNADDR) */
729
730             }
731 #endif  /* defined(HASFSTRUCT) */
732
733             (void) printf(" %*s", DevColW, DEVTTL);
734             if (Foffset)
735                 (void) printf(" %*s", SzOffColW, OFFTTL);
736             else if (Fsize)
737                 (void) printf(" %*s", SzOffColW, SZTTL);
738             else
739                 (void) printf(" %*s", SzOffColW, SZOFFTTL);
740             if (Fnlink)
741                 (void) printf(" %*s", NlColW, NLTTL);
742             (void) printf(" %*s %s\n", NodeColW, NODETTL, NMTTL);
743             Hdr++;
744         }
745 /*
746  * Size or print the command.
747  */
748         cp = (Lp->cmd && *Lp->cmd != '\0') ? Lp->cmd : "(unknown)";
749         if (!PrPass) {
750             len = safestrlen(cp, 2);
751             if (CmdLim && (len > CmdLim))
752                 len = CmdLim;
753             if (len > CmdColW)
754                 CmdColW = len;
755         } else
756             safestrprtn(cp, CmdColW, stdout, 2);
757 /*
758  * Size or print the process ID.
759  */
760         if (!PrPass) {
761             (void) snpf(buf, sizeof(buf), "%d", Lp->pid);
762             if ((len = strlen(buf)) > PidColW)
763                 PidColW = len;
764         } else
765             (void) printf(" %*d", PidColW, Lp->pid);
766
767 #if     defined(HASTASKS)
768 /*
769  * Size or print task ID.
770  */
771         if (!PrPass) {
772             if (Lp->tid) {
773                 (void) snpf(buf, sizeof(buf), "%d", Lp->tid);
774                 if ((len = strlen(buf)) > TidColW)
775                     TidColW = len;
776                 TaskPrtFl = 1;
777             }
778         } else if (TaskPrtFl) {
779             if (Lp->tid)
780                 (void) printf(" %*d", TidColW, Lp->tid);
781             else
782                 (void) printf(" %*s", TidColW, "");
783         }
784 #endif  /* defined(HASTASKS) */
785
786 #if     defined(HASZONES)
787 /*
788  * Size or print the zone.
789  */
790         if (Fzone) {
791             if (!PrPass) {
792                 if (Lp->zn) {
793                     if ((len = strlen(Lp->zn)) > ZoneColW)
794                         ZoneColW = len;
795                 }
796             } else
797                 (void) printf(" %-*s", ZoneColW, Lp->zn ? Lp->zn : "");
798         }
799 #endif  /* defined(HASZONES) */
800
801 #if     defined(HASSELINUX)
802 /*
803  * Size or print the context.
804  */
805         if (Fcntx) {
806             if (!PrPass) {
807                 if (Lp->cntx) {
808                     if ((len = strlen(Lp->cntx)) > CntxColW)
809                         CntxColW = len;
810                 }
811             } else
812                 (void) printf(" %-*s", CntxColW, Lp->cntx ? Lp->cntx : "");
813         }
814 #endif  /* defined(HASSELINUX) */
815
816 #if     defined(HASPPID)
817         if (Fppid) {
818
819         /*
820          * Size or print the parent process ID.
821          */
822             if (!PrPass) {
823                 (void) snpf(buf, sizeof(buf), "%d", Lp->ppid);
824                 if ((len = strlen(buf)) > PpidColW)
825                     PpidColW = len;
826             } else
827                 (void) printf(" %*d", PpidColW, Lp->ppid);
828         }
829 #endif  /* defined(HASPPID) */
830
831         if (Fpgid) {
832
833         /*
834          * Size or print the process group ID.
835          */
836             if (!PrPass) {
837                 (void) snpf(buf, sizeof(buf), "%d", Lp->pgid);
838                 if ((len = strlen(buf)) > PgidColW)
839                     PgidColW = len;
840             } else
841                 (void) printf(" %*d", PgidColW, Lp->pgid);
842         }
843 /*
844  * Size or print the user ID or login name.
845  */
846         if (!PrPass) {
847             if ((len = strlen(printuid((UID_ARG)Lp->uid, NULL))) > UserColW)
848                 UserColW = len;
849         } else
850             (void) printf(" %*.*s", UserColW, UserColW,
851                 printuid((UID_ARG)Lp->uid, NULL));
852 /*
853  * Size or print the file descriptor, access mode and lock status.
854  */
855         if (!PrPass) {
856             (void) snpf(buf, sizeof(buf), "%s%c%c",
857                 Lf->fd,
858                 (Lf->lock == ' ') ? Lf->access
859                                   : (Lf->access == ' ') ? '-'
860                                                         : Lf->access,
861                 Lf->lock);
862             if ((len = strlen(buf)) > FdColW)
863                 FdColW = len;
864         } else
865             (void) printf(" %*.*s%c%c", FdColW - 2, FdColW - 2, Lf->fd,
866                 (Lf->lock == ' ') ? Lf->access
867                                   : (Lf->access == ' ') ? '-'
868                                                         : Lf->access,
869                 Lf->lock);
870 /*
871  * Size or print the type.
872  */
873         if (!PrPass) {
874             if ((len = strlen(Lf->type)) > TypeColW)
875                 TypeColW = len;
876         } else
877             (void) printf(" %*.*s", TypeColW, TypeColW, Lf->type);
878
879 #if     defined(HASFSTRUCT)
880 /*
881  * Size or print the file structure address, file usage count, and node
882  * ID (address).
883  */
884
885         if (Fsv) {
886
887 # if    !defined(HASNOFSADDR)
888             if (Fsv & FSV_FA) {
889                 cp =  (Lf->fsv & FSV_FA) ? print_kptr(Lf->fsa, buf, sizeof(buf))
890                                          : "";
891                 if (!PrPass) {
892                     if ((len = strlen(cp)) > FsColW)
893                         FsColW = len;
894                 } else
895                     (void) printf(" %*.*s", FsColW, FsColW, cp);
896                     
897             }
898 # endif /* !defined(HASNOFSADDR) */
899
900 # if    !defined(HASNOFSCOUNT)
901             if (Fsv & FSV_CT) {
902                 if (Lf->fsv & FSV_CT) {
903                     (void) snpf(buf, sizeof(buf), "%ld", Lf->fct);
904                     cp = buf;
905                 } else
906                     cp = "";
907                 if (!PrPass) {
908                     if ((len = strlen(cp)) > FcColW)
909                         FcColW = len;
910                 } else
911                     (void) printf(" %*.*s", FcColW, FcColW, cp);
912             }
913 # endif /* !defined(HASNOFSCOUNT) */
914
915 # if    !defined(HASNOFSFLAGS)
916             if (Fsv & FSV_FG) {
917                 if ((Lf->fsv & FSV_FG) && (FsvFlagX || Lf->ffg || Lf->pof))
918                     cp = print_fflags(Lf->ffg, Lf->pof);
919                 else
920                     cp = "";
921                 if (!PrPass) {
922                     if ((len = strlen(cp)) > FgColW)
923                         FgColW = len;
924                 } else
925                     (void) printf(" %*.*s", FgColW, FgColW, cp);
926             }
927 # endif /* !defined(HASNOFSFLAGS) */
928
929 # if    !defined(HASNOFSNADDR)
930             if (Fsv & FSV_NI) {
931                 cp = (Lf->fsv & FSV_NI) ? print_kptr(Lf->fna, buf, sizeof(buf))
932                                         : "";
933                 if (!PrPass) {
934                     if ((len = strlen(cp)) > NiColW)
935                         NiColW = len;
936                 } else
937                     (void) printf(" %*.*s", NiColW, NiColW, cp);
938             }
939 # endif /* !defined(HASNOFSNADDR) */
940
941         }
942 #endif  /* defined(HASFSTRUCT) */
943
944 /*
945  * Size or print the device information.
946  */
947
948         if (Lf->rdev_def) {
949             dev = Lf->rdev;
950             devs = 1;
951         } else if (Lf->dev_def) {
952             dev = Lf->dev;
953             devs = 1;
954         } else
955             devs = 0;
956         if (devs) {
957
958 #if     defined(HASPRINTDEV)
959             cp = HASPRINTDEV(Lf, &dev);
960 #else   /* !defined(HASPRINTDEV) */
961             (void) snpf(buf, sizeof(buf), "%u,%u", GET_MAJ_DEV(dev),
962                 GET_MIN_DEV(dev));
963             cp = buf;
964 #endif  /* defined(HASPRINTDEV) */
965
966         }
967
968         if (!PrPass) {
969             if (devs)
970                 len = strlen(cp);
971             else if (Lf->dev_ch)
972                 len = strlen(Lf->dev_ch);
973             else
974                 len = 0;
975             if (len > DevColW)
976                 DevColW = len;
977         } else {
978             if (devs)
979                 (void) printf(" %*.*s", DevColW, DevColW, cp);
980             else {
981                 if (Lf->dev_ch)
982                     (void) printf(" %*.*s", DevColW, DevColW, Lf->dev_ch);
983                 else
984                     (void) printf(" %*.*s", DevColW, DevColW, "");
985             }
986         }
987 /*
988  * Size or print the size or offset.
989  */
990         if (!PrPass) {
991             if (Lf->sz_def) {
992
993 #if     defined(HASPRINTSZ)
994                 cp = HASPRINTSZ(Lf);
995 #else   /* !defined(HASPRINTSZ) */
996                 (void) snpf(buf, sizeof(buf), SzOffFmt_d, Lf->sz);
997                 cp = buf;
998 #endif  /* defined(HASPRINTSZ) */
999
1000                 len = strlen(cp);
1001             } else if (Lf->off_def) {
1002
1003 #if     defined(HASPRINTOFF)
1004                 cp = HASPRINTOFF(Lf, 0);
1005 #else   /* !defined(HASPRINTOFF) */
1006                 (void) snpf(buf, sizeof(buf), SzOffFmt_0t, Lf->off);
1007                 cp = buf;
1008 #endif  /* defined(HASPRINTOFF) */
1009
1010                 len = strlen(cp);
1011                 if (OffDecDig && len > (OffDecDig + 2)) {
1012
1013 #if     defined(HASPRINTOFF)
1014                     cp = HASPRINTOFF(Lf, 1);
1015 #else   /* !defined(HASPRINTOFF) */
1016                     (void) snpf(buf, sizeof(buf), SzOffFmt_x, Lf->off);
1017                     cp = buf;
1018 #endif  /* defined(HASPRINTOFF) */
1019
1020                     len = strlen(cp);
1021                 }
1022             } else
1023                 len = 0;
1024             if (len > SzOffColW)
1025                 SzOffColW = len;
1026         } else {
1027             putchar(' ');
1028             if (Lf->sz_def)
1029
1030 #if     defined(HASPRINTSZ)
1031                 (void) printf("%*.*s", SzOffColW, SzOffColW, HASPRINTSZ(Lf));
1032 #else   /* !defined(HASPRINTSZ) */
1033                 (void) printf(SzOffFmt_dv, SzOffColW, Lf->sz);
1034 #endif  /* defined(HASPRINTSZ) */
1035
1036             else if (Lf->off_def) {
1037
1038 #if     defined(HASPRINTOFF)
1039                 cp = HASPRINTOFF(Lf, 0);
1040 #else   /* !defined(HASPRINTOFF) */
1041                 (void) snpf(buf, sizeof(buf), SzOffFmt_0t, Lf->off);
1042                 cp = buf;
1043 #endif  /* defined(HASPRINTOFF) */
1044
1045                 if (OffDecDig && (int)strlen(cp) > (OffDecDig + 2)) {
1046
1047 #if     defined(HASPRINTOFF)
1048                     cp = HASPRINTOFF(Lf, 1);
1049 #else   /* !defined(HASPRINTOFF) */
1050                     (void) snpf(buf, sizeof(buf), SzOffFmt_x, Lf->off);
1051                     cp = buf;
1052 #endif  /* defined(HASPRINTOFF) */
1053
1054                 }
1055                 (void) printf("%*.*s", SzOffColW, SzOffColW, cp);
1056             } else
1057                 (void) printf("%*.*s", SzOffColW, SzOffColW, "");
1058         }
1059 /*
1060  * Size or print the link count.
1061  */
1062         if (Fnlink) {
1063             if (Lf->nlink_def) {
1064                 (void) snpf(buf, sizeof(buf), " %ld", Lf->nlink);
1065                 cp = buf;
1066            } else
1067                 cp = "";
1068             if (!PrPass) {
1069                 if ((len = strlen(cp)) > NlColW)
1070                     NlColW = len;
1071             } else
1072                 (void) printf(" %*s", NlColW, cp);
1073         }
1074 /*
1075  * Size or print the inode information.
1076  */
1077         switch (Lf->inp_ty) {
1078         case 1:
1079
1080 #if     defined(HASPRINTINO)
1081             cp = HASPRINTINO(Lf);
1082 #else   /* !defined(HASPRINTINO) */
1083             (void) snpf(buf, sizeof(buf), InodeFmt_d, Lf->inode);
1084             cp = buf;
1085 #endif  /* defined(HASPRINTINO) */
1086
1087             break;
1088         case 2:
1089             if (Lf->iproto[0])
1090                 cp = Lf->iproto;
1091             else
1092                 cp = "";
1093             break;
1094         case 3:
1095             (void) snpf(buf, sizeof(buf), InodeFmt_x, Lf->inode);
1096             cp = buf;
1097             break;
1098         default:
1099             cp = "";
1100         }
1101         if (!PrPass) {
1102             if ((len = strlen(cp)) > NodeColW)
1103                 NodeColW = len;
1104         } else {
1105             (void) printf(" %*.*s", NodeColW, NodeColW, cp);
1106         }
1107 /*
1108  * If this is the second pass, print the name column.  (It doesn't need
1109  * to be sized.)
1110  */
1111         if (PrPass) {
1112             putchar(' ');
1113
1114 #if     defined(HASPRINTNM)
1115             HASPRINTNM(Lf);
1116 #else   /* !defined(HASPRINTNM) */
1117             printname(1);
1118 #endif  /* defined(HASPRINTNM) */
1119
1120         }
1121 }
1122
1123
1124 /*
1125  * printinaddr() - print Internet addresses
1126  */
1127
1128 static int
1129 printinaddr()
1130 {
1131         int i, len, src;
1132         char *host, *port;
1133         int nl = Namechl - 1;
1134         char *np = Namech;
1135         char pbuf[32];
1136 /*
1137  * Process local network address first.  If there's a foreign address,
1138  * separate it from the local address with "->".
1139  */
1140         for (i = 0, *np = '\0'; i < 2; i++) {
1141             if (!Lf->li[i].af)
1142                 continue;
1143             host = port = (char *)NULL;
1144             if (i) {
1145
1146             /*
1147              * If this is the foreign address, insert the separator.
1148              */
1149                 if (nl < 2)
1150
1151 addr_too_long:
1152
1153                     {
1154                         (void) snpf(Namech, Namechl,
1155                             "network addresses too long");
1156                         return(1);
1157                     }
1158                 (void) snpf(np, nl, "->");
1159                 np += 2;
1160                 nl -= 2;
1161             }
1162         /*
1163          * Convert the address to a host name.
1164          */
1165
1166 #if     defined(HASIPv6)
1167             if ((Lf->li[i].af == AF_INET6
1168             &&   IN6_IS_ADDR_UNSPECIFIED(&Lf->li[i].ia.a6))
1169             ||  (Lf->li[i].af == AF_INET
1170             &&    Lf->li[i].ia.a4.s_addr == INADDR_ANY))
1171                 host ="*";
1172             else
1173                 host = gethostnm((unsigned char *)&Lf->li[i].ia, Lf->li[i].af);
1174 #else /* !defined(HASIPv6) */
1175             if (Lf->li[i].ia.a4.s_addr == INADDR_ANY)
1176                 host ="*";
1177             else
1178                 host = gethostnm((unsigned char *)&Lf->li[i].ia, Lf->li[i].af);
1179 #endif  /* defined(HASIPv6) */
1180
1181         /*
1182          * Process the port number.
1183          */
1184             if (Lf->li[i].p > 0) {
1185
1186                 if (Fport
1187
1188 #if     !defined(HASNORPC_H)
1189                 ||  FportMap
1190 #endif  /* defined(HASNORPC_H) */
1191
1192                 ) {
1193
1194                 /*
1195                  * If converting port numbers to service names, or looking
1196                  * up portmap program names and numbers, do so by protocol.
1197                  *
1198                  * Identify the port source as local if: 1) it comes from the
1199                  * local entry (0) of the file's Internet address array; or
1200                  * 2) it comes from  the foreign entry (1), and the foreign
1201                  * Internet address matches the local one; or 3) it is the
1202                  * loopback address 127.0.0.1.  (Test 2 may not always work
1203                  * -- e.g., on hosts with multiple interfaces.)
1204                  */
1205 #if     !defined(HASNORPC_H)
1206                     if ((src = i) && FportMap) {
1207
1208 # if    defined(HASIPv6)
1209                         if (Lf->li[0].af == AF_INET6) {
1210                             if (IN6_IS_ADDR_LOOPBACK(&Lf->li[i].ia.a6)
1211                             ||  IN6_ARE_ADDR_EQUAL(&Lf->li[0].ia.a6,
1212                                                    &Lf->li[1].ia.a6)
1213                             )
1214                                 src = 0;
1215                         } else
1216 # endif /* defined(HASIPv6) */
1217
1218                         if (Lf->li[0].af == AF_INET) {
1219                             if (Lf->li[i].ia.a4.s_addr == htonl(INADDR_LOOPBACK)
1220                             ||  Lf->li[0].ia.a4.s_addr == Lf->li[1].ia.a4.s_addr
1221                             )
1222                                 src = 0;
1223                         }
1224                     }
1225 #endif  /* !defined(HASNORPC_H) */
1226
1227                     if (strcasecmp(Lf->iproto, "TCP") == 0)
1228                         port = lkup_port(Lf->li[i].p, 0, src);
1229                     else if (strcasecmp(Lf->iproto, "UDP") == 0)
1230                         port = lkup_port(Lf->li[i].p, 1, src);
1231                 }
1232                 if (!port) {
1233                     (void) snpf(pbuf, sizeof(pbuf), "%d", Lf->li[i].p);
1234                     port = pbuf;
1235                 }
1236             } else if (Lf->li[i].p == 0)
1237                 port = "*";
1238         /*
1239          * Enter the host name.
1240          */
1241             if (host) {
1242                 if ((len = strlen(host)) > nl)
1243                     goto addr_too_long;
1244                 if (len) {
1245                     (void) snpf(np, nl, "%s", host);
1246                     np += len;
1247                     nl -= len;
1248                 }
1249             }
1250         /*
1251          * Enter the port number, preceded by a colon.
1252          */
1253             if (port) {
1254                 if (((len = strlen(port)) + 1) >= nl)
1255                     goto addr_too_long;
1256                 (void) snpf(np, nl, ":%s", port);
1257                 np += len + 1;
1258                 nl -= len - 1;
1259             }
1260         }
1261         if (Namech[0]) {
1262             safestrprt(Namech, stdout, 0);
1263             return(1);
1264         }
1265         return(0);
1266 }
1267
1268
1269 /*
1270  * print_init() - initialize for printing
1271  */
1272
1273 void
1274 print_init()
1275 {
1276
1277 /*
1278  * Preset standard values.
1279  */
1280         PrPass = (Ffield || Fterse) ? 1 : 0;
1281         LastPid = -1;
1282         TaskPrtFl = 0;
1283 /*
1284  * Size columns by their titles.
1285  */
1286         CmdColW = strlen(CMDTTL);
1287         DevColW = strlen(DEVTTL);
1288         FdColW = strlen(FDTTL);
1289         if (Fnlink)
1290             NlColW = strlen(NLTTL);
1291         NmColW = strlen(NMTTL);
1292         NodeColW = strlen(NODETTL);
1293         PgidColW = strlen(PGIDTTL);
1294         PidColW = strlen(PIDTTL);
1295         PpidColW = strlen(PPIDTTL);
1296         if (Fsize)
1297             SzOffColW = strlen(SZTTL);
1298         else if (Foffset)
1299             SzOffColW = strlen(OFFTTL);
1300         else
1301             SzOffColW = strlen(SZOFFTTL);
1302
1303 #if     defined(HASTASKS)
1304         TidColW = strlen(TIDTTL);
1305 #endif  /* defined(HASTASKS) */
1306
1307         TypeColW = strlen(TYPETTL);
1308         UserColW = strlen(USERTTL);
1309
1310 #if     defined(HASFSTRUCT)
1311
1312 # if    !defined(HASNOFSADDR)
1313         FsColW = strlen(FSTTL);
1314 # endif /* !defined(HASNOFSADDR) */
1315
1316 # if    !defined(HASNOFSCOUNT)
1317         FcColW = strlen(FCTTL);
1318 # endif /* !defined(HASNOFSCOUNT) */
1319
1320 # if    !defined(HASNOFSFLAGS)
1321         FgColW = strlen(FGTTL);
1322 # endif /* !defined(HASNOFSFLAGS) */
1323
1324 # if    !defined(HASNOFSNADDR)
1325         NiColW = strlen(NiTtl);
1326 # endif /* !defined(HASNOFSNADDR) */
1327 #endif  /* defined(HASFSTRUCT) */
1328
1329 #if     defined(HASSELINUX)
1330         if (Fcntx)
1331             CntxColW = strlen(CNTXTTL);
1332 #endif  /* defined(HASSELINUX) */
1333
1334 #if     defined(HASZONES)
1335         if (Fzone)
1336             ZoneColW = strlen(ZONETTL);
1337 #endif  /* defined(HASZONES) */
1338
1339 }
1340
1341
1342 #if     !defined(HASPRIVPRIPP)
1343 /*
1344  * printiproto() - print Internet protocol name
1345  */
1346
1347 void
1348 printiproto(p)
1349         int p;                          /* protocol number */
1350 {
1351         int i;
1352         static int m = -1;
1353         char *s;
1354
1355         switch (p) {
1356
1357 #if     defined(IPPROTO_TCP)
1358         case IPPROTO_TCP:
1359             s = "TCP";
1360             break;
1361 #endif  /* defined(IPPROTO_TCP) */
1362
1363 #if     defined(IPPROTO_UDP)
1364         case IPPROTO_UDP:
1365             s = "UDP";
1366             break;
1367 #endif  /* defined(IPPROTO_UDP) */
1368
1369 #if     defined(IPPROTO_IP)
1370 # if    !defined(IPPROTO_HOPOPTS) || IPPROTO_IP!=IPPROTO_HOPOPTS
1371         case IPPROTO_IP:
1372             s = "IP";
1373             break;
1374 # endif /* !defined(IPPROTO_HOPOPTS) || IPPROTO_IP!=IPPROTO_HOPOPTS */
1375 #endif  /* defined(IPPROTO_IP) */
1376
1377 #if     defined(IPPROTO_ICMP)
1378         case IPPROTO_ICMP:
1379             s = "ICMP";
1380             break;
1381 #endif  /* defined(IPPROTO_ICMP) */
1382
1383 #if     defined(IPPROTO_ICMPV6)
1384         case IPPROTO_ICMPV6:
1385             s = "ICMPV6";
1386             break;
1387 #endif  /* defined(IPPROTO_ICMPV6) */
1388
1389 #if     defined(IPPROTO_IGMP)
1390         case IPPROTO_IGMP:
1391             s = "IGMP";
1392             break;
1393 #endif  /* defined(IPPROTO_IGMP) */
1394
1395 #if     defined(IPPROTO_GGP)
1396         case IPPROTO_GGP:
1397             s = "GGP";
1398             break;
1399 #endif  /* defined(IPPROTO_GGP) */
1400
1401 #if     defined(IPPROTO_EGP)
1402         case IPPROTO_EGP:
1403             s = "EGP";
1404             break;
1405 #endif  /* defined(IPPROTO_EGP) */
1406
1407 #if     defined(IPPROTO_PUP)
1408         case IPPROTO_PUP:
1409             s = "PUP";
1410             break;
1411 #endif  /* defined(IPPROTO_PUP) */
1412
1413 #if     defined(IPPROTO_IDP)
1414         case IPPROTO_IDP:
1415             s = "IDP";
1416             break;
1417 #endif  /* defined(IPPROTO_IDP) */
1418
1419 #if     defined(IPPROTO_ND)
1420         case IPPROTO_ND:
1421             s = "ND";
1422             break;
1423 #endif  /* defined(IPPROTO_ND) */
1424
1425 #if     defined(IPPROTO_RAW)
1426         case IPPROTO_RAW:
1427             s = "RAW";
1428             break;
1429 #endif  /* defined(IPPROTO_RAW) */
1430
1431 #if     defined(IPPROTO_HELLO)
1432         case IPPROTO_HELLO:
1433             s = "HELLO";
1434             break;
1435 #endif  /* defined(IPPROTO_HELLO) */
1436
1437 #if     defined(IPPROTO_PXP)
1438         case IPPROTO_PXP:
1439             s = "PXP";
1440             break;
1441 #endif  /* defined(IPPROTO_PXP) */
1442
1443 #if     defined(IPPROTO_RAWIP)
1444         case IPPROTO_RAWIP:
1445             s = "RAWIP";
1446             break;
1447 #endif  /* defined(IPPROTO_RAWIP) */
1448
1449 #if     defined(IPPROTO_RAWIF)
1450         case IPPROTO_RAWIF:
1451             s = "RAWIF";
1452             break;
1453 #endif  /* defined(IPPROTO_RAWIF) */
1454
1455 #if     defined(IPPROTO_HOPOPTS)
1456         case IPPROTO_HOPOPTS:
1457             s = "HOPOPTS";
1458             break;
1459 #endif  /* defined(IPPROTO_HOPOPTS) */
1460
1461 #if     defined(IPPROTO_IPIP)
1462         case IPPROTO_IPIP:
1463             s = "IPIP";
1464             break;
1465 #endif  /* defined(IPPROTO_IPIP) */
1466
1467 #if     defined(IPPROTO_ST)
1468         case IPPROTO_ST:
1469             s = "ST";
1470             break;
1471 #endif  /* defined(IPPROTO_ST) */
1472
1473 #if     defined(IPPROTO_PIGP)
1474         case IPPROTO_PIGP:
1475             s = "PIGP";
1476             break;
1477 #endif  /* defined(IPPROTO_PIGP) */
1478
1479 #if     defined(IPPROTO_RCCMON)
1480         case IPPROTO_RCCMON:
1481             s = "RCCMON";
1482             break;
1483 #endif  /* defined(IPPROTO_RCCMON) */
1484
1485 #if     defined(IPPROTO_NVPII)
1486         case IPPROTO_NVPII:
1487             s = "NVPII";
1488             break;
1489 #endif  /* defined(IPPROTO_NVPII) */
1490
1491 #if     defined(IPPROTO_ARGUS)
1492         case IPPROTO_ARGUS:
1493             s = "ARGUS";
1494             break;
1495 #endif  /* defined(IPPROTO_ARGUS) */
1496
1497 #if     defined(IPPROTO_EMCON)
1498         case IPPROTO_EMCON:
1499             s = "EMCON";
1500             break;
1501 #endif  /* defined(IPPROTO_EMCON) */
1502
1503 #if     defined(IPPROTO_XNET)
1504         case IPPROTO_XNET:
1505             s = "XNET";
1506             break;
1507 #endif  /* defined(IPPROTO_XNET) */
1508
1509 #if     defined(IPPROTO_CHAOS)
1510         case IPPROTO_CHAOS:
1511             s = "CHAOS";
1512             break;
1513 #endif  /* defined(IPPROTO_CHAOS) */
1514
1515 #if     defined(IPPROTO_MUX)
1516         case IPPROTO_MUX:
1517             s = "MUX";
1518             break;
1519 #endif  /* defined(IPPROTO_MUX) */
1520
1521 #if     defined(IPPROTO_MEAS)
1522         case IPPROTO_MEAS:
1523             s = "MEAS";
1524             break;
1525 #endif  /* defined(IPPROTO_MEAS) */
1526
1527 #if     defined(IPPROTO_HMP)
1528         case IPPROTO_HMP:
1529             s = "HMP";
1530             break;
1531 #endif  /* defined(IPPROTO_HMP) */
1532
1533 #if     defined(IPPROTO_PRM)
1534         case IPPROTO_PRM:
1535             s = "PRM";
1536             break;
1537 #endif  /* defined(IPPROTO_PRM) */
1538
1539 #if     defined(IPPROTO_TRUNK1)
1540         case IPPROTO_TRUNK1:
1541             s = "TRUNK1";
1542             break;
1543 #endif  /* defined(IPPROTO_TRUNK1) */
1544
1545 #if     defined(IPPROTO_TRUNK2)
1546         case IPPROTO_TRUNK2:
1547             s = "TRUNK2";
1548             break;
1549 #endif  /* defined(IPPROTO_TRUNK2) */
1550
1551 #if     defined(IPPROTO_LEAF1)
1552         case IPPROTO_LEAF1:
1553             s = "LEAF1";
1554             break;
1555 #endif  /* defined(IPPROTO_LEAF1) */
1556
1557 #if     defined(IPPROTO_LEAF2)
1558         case IPPROTO_LEAF2:
1559             s = "LEAF2";
1560             break;
1561 #endif  /* defined(IPPROTO_LEAF2) */
1562
1563 #if     defined(IPPROTO_RDP)
1564         case IPPROTO_RDP:
1565             s = "RDP";
1566             break;
1567 #endif  /* defined(IPPROTO_RDP) */
1568
1569 #if     defined(IPPROTO_IRTP)
1570         case IPPROTO_IRTP:
1571             s = "IRTP";
1572             break;
1573 #endif  /* defined(IPPROTO_IRTP) */
1574
1575 #if     defined(IPPROTO_TP)
1576         case IPPROTO_TP:
1577             s = "TP";
1578             break;
1579 #endif  /* defined(IPPROTO_TP) */
1580
1581 #if     defined(IPPROTO_BLT)
1582         case IPPROTO_BLT:
1583             s = "BLT";
1584             break;
1585 #endif  /* defined(IPPROTO_BLT) */
1586
1587 #if     defined(IPPROTO_NSP)
1588         case IPPROTO_NSP:
1589             s = "NSP";
1590             break;
1591 #endif  /* defined(IPPROTO_NSP) */
1592
1593 #if     defined(IPPROTO_INP)
1594         case IPPROTO_INP:
1595             s = "INP";
1596             break;
1597 #endif  /* defined(IPPROTO_INP) */
1598
1599 #if     defined(IPPROTO_SEP)
1600         case IPPROTO_SEP:
1601             s = "SEP";
1602             break;
1603 #endif  /* defined(IPPROTO_SEP) */
1604
1605 #if     defined(IPPROTO_3PC)
1606         case IPPROTO_3PC:
1607             s = "3PC";
1608             break;
1609 #endif  /* defined(IPPROTO_3PC) */
1610
1611 #if     defined(IPPROTO_IDPR)
1612         case IPPROTO_IDPR:
1613             s = "IDPR";
1614             break;
1615 #endif  /* defined(IPPROTO_IDPR) */
1616
1617 #if     defined(IPPROTO_XTP)
1618         case IPPROTO_XTP:
1619             s = "XTP";
1620             break;
1621 #endif  /* defined(IPPROTO_XTP) */
1622
1623 #if     defined(IPPROTO_DDP)
1624         case IPPROTO_DDP:
1625             s = "DDP";
1626             break;
1627 #endif  /* defined(IPPROTO_DDP) */
1628
1629 #if     defined(IPPROTO_CMTP)
1630         case IPPROTO_CMTP:
1631             s = "CMTP";
1632             break;
1633 #endif  /* defined(IPPROTO_CMTP) */
1634
1635 #if     defined(IPPROTO_TPXX)
1636         case IPPROTO_TPXX:
1637             s = "TPXX";
1638             break;
1639 #endif  /* defined(IPPROTO_TPXX) */
1640
1641 #if     defined(IPPROTO_IL)
1642         case IPPROTO_IL:
1643             s = "IL";
1644             break;
1645 #endif  /* defined(IPPROTO_IL) */
1646
1647 #if     defined(IPPROTO_IPV6)
1648         case IPPROTO_IPV6:
1649             s = "IPV6";
1650             break;
1651 #endif  /* defined(IPPROTO_IPV6) */
1652
1653 #if     defined(IPPROTO_SDRP)
1654         case IPPROTO_SDRP:
1655             s = "SDRP";
1656             break;
1657 #endif  /* defined(IPPROTO_SDRP) */
1658
1659 #if     defined(IPPROTO_ROUTING)
1660         case IPPROTO_ROUTING:
1661             s = "ROUTING";
1662             break;
1663 #endif  /* defined(IPPROTO_ROUTING) */
1664
1665 #if     defined(IPPROTO_FRAGMENT)
1666         case IPPROTO_FRAGMENT:
1667             s = "FRAGMNT";
1668             break;
1669 #endif  /* defined(IPPROTO_FRAGMENT) */
1670
1671 #if     defined(IPPROTO_IDRP)
1672         case IPPROTO_IDRP:
1673             s = "IDRP";
1674             break;
1675 #endif  /* defined(IPPROTO_IDRP) */
1676
1677 #if     defined(IPPROTO_RSVP)
1678         case IPPROTO_RSVP:
1679             s = "RSVP";
1680             break;
1681 #endif  /* defined(IPPROTO_RSVP) */
1682
1683 #if     defined(IPPROTO_GRE)
1684         case IPPROTO_GRE:
1685             s = "GRE";
1686             break;
1687 #endif  /* defined(IPPROTO_GRE) */
1688
1689 #if     defined(IPPROTO_MHRP)
1690         case IPPROTO_MHRP:
1691             s = "MHRP";
1692             break;
1693 #endif  /* defined(IPPROTO_MHRP) */
1694
1695 #if     defined(IPPROTO_BHA)
1696         case IPPROTO_BHA:
1697             s = "BHA";
1698             break;
1699 #endif  /* defined(IPPROTO_BHA) */
1700
1701 #if     defined(IPPROTO_ESP)
1702         case IPPROTO_ESP:
1703             s = "ESP";
1704             break;
1705 #endif  /* defined(IPPROTO_ESP) */
1706
1707 #if     defined(IPPROTO_AH)
1708         case IPPROTO_AH:
1709             s = "AH";
1710             break;
1711 #endif  /* defined(IPPROTO_AH) */
1712
1713 #if     defined(IPPROTO_INLSP)
1714         case IPPROTO_INLSP:
1715             s = "INLSP";
1716             break;
1717 #endif  /* defined(IPPROTO_INLSP) */
1718
1719 #if     defined(IPPROTO_SWIPE)
1720         case IPPROTO_SWIPE:
1721             s = "SWIPE";
1722             break;
1723 #endif  /* defined(IPPROTO_SWIPE) */
1724
1725 #if     defined(IPPROTO_NHRP)
1726         case IPPROTO_NHRP:
1727             s = "NHRP";
1728             break;
1729 #endif  /* defined(IPPROTO_NHRP) */
1730
1731 #if     defined(IPPROTO_NONE)
1732         case IPPROTO_NONE:
1733             s = "NONE";
1734             break;
1735 #endif  /* defined(IPPROTO_NONE) */
1736
1737 #if     defined(IPPROTO_DSTOPTS)
1738         case IPPROTO_DSTOPTS:
1739             s = "DSTOPTS";
1740             break;
1741 #endif  /* defined(IPPROTO_DSTOPTS) */
1742
1743 #if     defined(IPPROTO_AHIP)
1744         case IPPROTO_AHIP:
1745             s = "AHIP";
1746             break;
1747 #endif  /* defined(IPPROTO_AHIP) */
1748
1749 #if     defined(IPPROTO_CFTP)
1750         case IPPROTO_CFTP:
1751             s = "CFTP";
1752             break;
1753 #endif  /* defined(IPPROTO_CFTP) */
1754
1755 #if     defined(IPPROTO_SATEXPAK)
1756         case IPPROTO_SATEXPAK:
1757             s = "SATEXPK";
1758             break;
1759 #endif  /* defined(IPPROTO_SATEXPAK) */
1760
1761 #if     defined(IPPROTO_KRYPTOLAN)
1762         case IPPROTO_KRYPTOLAN:
1763             s = "KRYPTOL";
1764             break;
1765 #endif  /* defined(IPPROTO_KRYPTOLAN) */
1766
1767 #if     defined(IPPROTO_RVD)
1768         case IPPROTO_RVD:
1769             s = "RVD";
1770             break;
1771 #endif  /* defined(IPPROTO_RVD) */
1772
1773 #if     defined(IPPROTO_IPPC)
1774         case IPPROTO_IPPC:
1775             s = "IPPC";
1776             break;
1777 #endif  /* defined(IPPROTO_IPPC) */
1778
1779 #if     defined(IPPROTO_ADFS)
1780         case IPPROTO_ADFS:
1781             s = "ADFS";
1782             break;
1783 #endif  /* defined(IPPROTO_ADFS) */
1784
1785 #if     defined(IPPROTO_SATMON)
1786         case IPPROTO_SATMON:
1787             s = "SATMON";
1788             break;
1789 #endif  /* defined(IPPROTO_SATMON) */
1790
1791 #if     defined(IPPROTO_VISA)
1792         case IPPROTO_VISA:
1793             s = "VISA";
1794             break;
1795 #endif  /* defined(IPPROTO_VISA) */
1796
1797 #if     defined(IPPROTO_IPCV)
1798         case IPPROTO_IPCV:
1799             s = "IPCV";
1800             break;
1801 #endif  /* defined(IPPROTO_IPCV) */
1802
1803 #if     defined(IPPROTO_CPNX)
1804         case IPPROTO_CPNX:
1805             s = "CPNX";
1806             break;
1807 #endif  /* defined(IPPROTO_CPNX) */
1808
1809 #if     defined(IPPROTO_CPHB)
1810         case IPPROTO_CPHB:
1811             s = "CPHB";
1812             break;
1813 #endif  /* defined(IPPROTO_CPHB) */
1814
1815 #if     defined(IPPROTO_WSN)
1816         case IPPROTO_WSN:
1817             s = "WSN";
1818             break;
1819 #endif  /* defined(IPPROTO_WSN) */
1820
1821 #if     defined(IPPROTO_PVP)
1822         case IPPROTO_PVP:
1823             s = "PVP";
1824             break;
1825 #endif  /* defined(IPPROTO_PVP) */
1826
1827 #if     defined(IPPROTO_BRSATMON)
1828         case IPPROTO_BRSATMON:
1829             s = "BRSATMN";
1830             break;
1831 #endif  /* defined(IPPROTO_BRSATMON) */
1832
1833 #if     defined(IPPROTO_WBMON)
1834         case IPPROTO_WBMON:
1835             s = "WBMON";
1836             break;
1837 #endif  /* defined(IPPROTO_WBMON) */
1838
1839 #if     defined(IPPROTO_WBEXPAK)
1840         case IPPROTO_WBEXPAK:
1841             s = "WBEXPAK";
1842             break;
1843 #endif  /* defined(IPPROTO_WBEXPAK) */
1844
1845 #if     defined(IPPROTO_EON)
1846         case IPPROTO_EON:
1847             s = "EON";
1848             break;
1849 #endif  /* defined(IPPROTO_EON) */
1850
1851 #if     defined(IPPROTO_VMTP)
1852         case IPPROTO_VMTP:
1853             s = "VMTP";
1854             break;
1855 #endif  /* defined(IPPROTO_VMTP) */
1856
1857 #if     defined(IPPROTO_SVMTP)
1858         case IPPROTO_SVMTP:
1859             s = "SVMTP";
1860             break;
1861 #endif  /* defined(IPPROTO_SVMTP) */
1862
1863 #if     defined(IPPROTO_VINES)
1864         case IPPROTO_VINES:
1865             s = "VINES";
1866             break;
1867 #endif  /* defined(IPPROTO_VINES) */
1868
1869 #if     defined(IPPROTO_TTP)
1870         case IPPROTO_TTP:
1871             s = "TTP";
1872             break;
1873 #endif  /* defined(IPPROTO_TTP) */
1874
1875 #if     defined(IPPROTO_IGP)
1876         case IPPROTO_IGP:
1877             s = "IGP";
1878             break;
1879 #endif  /* defined(IPPROTO_IGP) */
1880
1881 #if     defined(IPPROTO_DGP)
1882         case IPPROTO_DGP:
1883             s = "DGP";
1884             break;
1885 #endif  /* defined(IPPROTO_DGP) */
1886
1887 #if     defined(IPPROTO_TCF)
1888         case IPPROTO_TCF:
1889             s = "TCF";
1890             break;
1891 #endif  /* defined(IPPROTO_TCF) */
1892
1893 #if     defined(IPPROTO_IGRP)
1894         case IPPROTO_IGRP:
1895             s = "IGRP";
1896             break;
1897 #endif  /* defined(IPPROTO_IGRP) */
1898
1899 #if     defined(IPPROTO_OSPFIGP)
1900         case IPPROTO_OSPFIGP:
1901             s = "OSPFIGP";
1902             break;
1903 #endif  /* defined(IPPROTO_OSPFIGP) */
1904
1905 #if     defined(IPPROTO_SRPC)
1906         case IPPROTO_SRPC:
1907             s = "SRPC";
1908             break;
1909 #endif  /* defined(IPPROTO_SRPC) */
1910
1911 #if     defined(IPPROTO_LARP)
1912         case IPPROTO_LARP:
1913             s = "LARP";
1914             break;
1915 #endif  /* defined(IPPROTO_LARP) */
1916
1917 #if     defined(IPPROTO_MTP)
1918         case IPPROTO_MTP:
1919             s = "MTP";
1920             break;
1921 #endif  /* defined(IPPROTO_MTP) */
1922
1923 #if     defined(IPPROTO_AX25)
1924         case IPPROTO_AX25:
1925             s = "AX25";
1926             break;
1927 #endif  /* defined(IPPROTO_AX25) */
1928
1929 #if     defined(IPPROTO_IPEIP)
1930         case IPPROTO_IPEIP:
1931             s = "IPEIP";
1932             break;
1933 #endif  /* defined(IPPROTO_IPEIP) */
1934
1935 #if     defined(IPPROTO_MICP)
1936         case IPPROTO_MICP:
1937             s = "MICP";
1938             break;
1939 #endif  /* defined(IPPROTO_MICP) */
1940
1941 #if     defined(IPPROTO_SCCSP)
1942         case IPPROTO_SCCSP:
1943             s = "SCCSP";
1944             break;
1945 #endif  /* defined(IPPROTO_SCCSP) */
1946
1947 #if     defined(IPPROTO_ETHERIP)
1948         case IPPROTO_ETHERIP:
1949             s = "ETHERIP";
1950             break;
1951 #endif  /* defined(IPPROTO_ETHERIP) */
1952
1953 #if     defined(IPPROTO_ENCAP)
1954 # if    !defined(IPPROTO_IPIP) || IPPROTO_IPIP!=IPPROTO_ENCAP
1955         case IPPROTO_ENCAP:
1956             s = "ENCAP";
1957             break;
1958 # endif /* !defined(IPPROTO_IPIP) || IPPROTO_IPIP!=IPPROTO_ENCAP */
1959 #endif  /* defined(IPPROTO_ENCAP) */
1960
1961 #if     defined(IPPROTO_APES)
1962         case IPPROTO_APES:
1963             s = "APES";
1964             break;
1965 #endif  /* defined(IPPROTO_APES) */
1966
1967 #if     defined(IPPROTO_GMTP)
1968         case IPPROTO_GMTP:
1969             s = "GMTP";
1970             break;
1971 #endif  /* defined(IPPROTO_GMTP) */
1972
1973 #if     defined(IPPROTO_DIVERT)
1974         case IPPROTO_DIVERT:
1975             s = "DIVERT";
1976             break;
1977 #endif  /* defined(IPPROTO_DIVERT) */
1978
1979         default:
1980             s = (char *)NULL;
1981         }
1982         if (s)
1983             (void) snpf(Lf->iproto, sizeof(Lf->iproto), "%.*s", IPROTOL-1, s);
1984         else {  
1985             if (m < 0) {
1986                 for (i = 0, m = 1; i < IPROTOL-2; i++)
1987                     m *= 10;
1988             }
1989             if (m > p)
1990                 (void) snpf(Lf->iproto, sizeof(Lf->iproto), "%d?", p);
1991             else
1992                 (void) snpf(Lf->iproto, sizeof(Lf->iproto), "*%d?", p % (m/10));
1993         }
1994 }
1995 #endif  /* !defined(HASPRIVPRIPP) */
1996
1997
1998 /*
1999  * printname() - print output name field
2000  */
2001
2002 void
2003 printname(nl)
2004         int nl;                         /* NL status */
2005 {
2006
2007 #if     defined(HASNCACHE)
2008         char buf[MAXPATHLEN];
2009         char *cp;
2010         int fp;
2011 #endif  /* defined(HASNCACHE) */
2012
2013         int ps = 0;
2014
2015         if (Lf->nm && Lf->nm[0]) {
2016
2017         /*
2018          * Print the name characters, if there are some.
2019          */
2020             safestrprt(Lf->nm, stdout, 0);
2021             ps++;
2022             if (!Lf->li[0].af && !Lf->li[1].af)
2023                 goto print_nma;
2024         }
2025         if (Lf->li[0].af || Lf->li[1].af) {
2026             if (ps)
2027                 putchar(' ');
2028         /*
2029          * If the file has Internet addresses, print them.
2030          */
2031             if (printinaddr())
2032                 ps++;
2033             goto print_nma;
2034         }
2035         if (((Lf->ntype == N_BLK) || (Lf->ntype == N_CHR))
2036         &&  Lf->dev_def && Lf->rdev_def
2037         &&  printdevname(&Lf->dev, &Lf->rdev, 0, Lf->ntype))
2038         {
2039
2040         /*
2041          * If this is a block or character device and it has a name, print it.
2042          */
2043             ps++;
2044             goto print_nma;
2045         }
2046         if (Lf->is_com) {
2047
2048         /*
2049          * If this is a common node, print that fact.
2050          */
2051             (void) fputs("COMMON: ", stdout);
2052             ps++;
2053             goto print_nma;
2054         }
2055
2056 #if     defined(HASPRIVNMCACHE)
2057         if (HASPRIVNMCACHE(Lf)) {
2058             ps++;
2059             goto print_nma;
2060         }
2061 #endif  /* defined(HASPRIVNMCACHE) */
2062
2063         if (Lf->lmi_srch) {
2064             struct mounts *mp;
2065         /*
2066          * Do a deferred local mount info table search for the file system
2067          * (mounted) directory name and inode number, and mounted device name.
2068          */
2069             for (mp = readmnt(); mp; mp = mp->next) {
2070                 if (Lf->dev == mp->dev) {
2071                     Lf->fsdir = mp->dir;
2072                     Lf->fsdev = mp->fsname;
2073
2074 #if     defined(HASFSINO)
2075                     Lf->fs_ino = mp->inode;
2076 #endif  /* defined(HASFSINO) */
2077
2078                     break;
2079                 }
2080             }
2081             Lf->lmi_srch = 0;
2082         }
2083         if (Lf->fsdir || Lf->fsdev) {
2084
2085         /*
2086          * Print the file system directory name, device name, and
2087          * possible path name components.
2088          */
2089
2090 #if     !defined(HASNCACHE) || HASNCACHE<2
2091             if (Lf->fsdir) {
2092                 safestrprt(Lf->fsdir, stdout, 0);
2093                 ps++;
2094             }
2095 #endif  /* !defined(HASNCACHE) || HASNCACHE<2 */
2096
2097 #if     defined(HASNCACHE)
2098
2099 # if    HASNCACHE<2
2100             if (Lf->na) {
2101                 if (NcacheReload) {
2102
2103 #  if   defined(NCACHELDPFX)
2104                     NCACHELDPFX
2105 #  endif        /* defined(NCACHELDPFX) */
2106
2107                     (void) ncache_load();
2108
2109 #  if   defined(NCACHELDSFX)
2110                     NCACHELDSFX
2111 #  endif        /* defined(NCACHELDSFX) */
2112
2113                     NcacheReload = 0;
2114                 }
2115                 if ((cp = ncache_lookup(buf, sizeof(buf), &fp))) {
2116                     char *cp1; 
2117
2118                     if (*cp == '\0')
2119                         goto print_nma;
2120                     if (fp && Lf->fsdir) {
2121                         if (*cp != '/') {
2122                             cp1 = strrchr(Lf->fsdir, '/');
2123                             if (cp1 == (char *)NULL ||  *(cp1 + 1) != '\0')
2124                                 putchar('/');
2125                             }
2126                     } else
2127                         (void) fputs(" -- ", stdout);
2128                     safestrprt(cp, stdout, 0);
2129                     ps++;
2130                     goto print_nma;
2131                 }
2132             }
2133 # else  /* HASNCACHE>1 */
2134             if (NcacheReload) {
2135
2136 #  if   defined(NCACHELDPFX)
2137                     NCACHELDPFX
2138 #  endif        /* defined(NCACHELDPFX) */
2139
2140                 (void) ncache_load();
2141
2142 #  if   defined(NCACHELDSFX)
2143                     NCACHELDSFX
2144 #  endif        /* defined(NCACHELDSFX) */
2145
2146                 NcacheReload = 0;
2147             }
2148             if ((cp = ncache_lookup(buf, sizeof(buf), &fp))) {
2149                 if (fp) {
2150                     safestrprt(cp, stdout, 0);
2151                     ps++;
2152                 } else {
2153                     if (Lf->fsdir) {
2154                         safestrprt(Lf->fsdir, stdout, 0);
2155                         ps++;
2156                     }
2157                     if (*cp) {
2158                         (void) fputs(" -- ", stdout);
2159                         safestrprt(cp, stdout, 0);
2160                         ps++;
2161                     }
2162                 }
2163                 goto print_nma;
2164             }
2165             if (Lf->fsdir) {
2166                 safestrprt(Lf->fsdir, stdout, 0);
2167                 ps++;
2168             }
2169 # endif /* HASNCACHE<2 */
2170 #endif  /* defined(HASNCACHE) */
2171
2172             if (Lf->fsdev) {
2173                 if (Lf->fsdir)
2174                     (void) fputs(" (", stdout);
2175                 else
2176                     (void) putchar('(');
2177                 safestrprt(Lf->fsdev, stdout, 0);
2178                 (void) putchar(')');
2179                 ps++;
2180             }
2181         }
2182 /*
2183  * Print the NAME column addition, if there is one.  If there isn't
2184  * make sure a NL is printed, as requested.
2185  */
2186
2187 print_nma:
2188
2189         if (Lf->nma) {
2190             if (ps)
2191                 putchar(' ');
2192             safestrprt(Lf->nma, stdout, 0);
2193             ps++;
2194         }
2195 /*
2196  * If this file has TCP/IP state information, print it.
2197  */
2198         if (!Ffield && Ftcptpi
2199         &&  (Lf->lts.type >= 0
2200
2201 #if     defined(HASTCPTPIQ)
2202         ||   ((Ftcptpi & TCPTPI_QUEUES) && (Lf->lts.rqs || Lf->lts.sqs))
2203 #endif  /* defined(HASTCPTPIQ) */
2204
2205 #if     defined(HASTCPTPIW)
2206         ||   ((Ftcptpi & TCPTPI_WINDOWS) && (Lf->lts.rws || Lf->lts.wws))
2207 #endif  /* defined(HASTCPTPIW) */
2208
2209         )) {
2210             if (ps)
2211                 putchar(' ');
2212             (void) print_tcptpi(0);
2213         }
2214         if (nl)
2215             putchar('\n');
2216 }
2217
2218
2219 /*
2220  * printrawaddr() - print raw socket address
2221  */
2222
2223 void
2224 printrawaddr(sa)
2225         struct sockaddr *sa;            /* socket address */
2226 {
2227         char *ep;
2228         size_t sz;
2229
2230         ep = endnm(&sz);
2231         (void) snpf(ep, sz, "%u/%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
2232             sa->sa_family,
2233             (unsigned char)sa->sa_data[0],
2234             (unsigned char)sa->sa_data[1],
2235             (unsigned char)sa->sa_data[2],
2236             (unsigned char)sa->sa_data[3],
2237             (unsigned char)sa->sa_data[4],
2238             (unsigned char)sa->sa_data[5],
2239             (unsigned char)sa->sa_data[6],
2240             (unsigned char)sa->sa_data[7],
2241             (unsigned char)sa->sa_data[8],
2242             (unsigned char)sa->sa_data[9],
2243             (unsigned char)sa->sa_data[10],
2244             (unsigned char)sa->sa_data[11],
2245             (unsigned char)sa->sa_data[12],
2246             (unsigned char)sa->sa_data[13]);
2247 }
2248
2249
2250 /*
2251  * printsockty() - print socket type
2252  */
2253
2254 char *
2255 printsockty(ty)
2256         int ty;                         /* socket type -- e.g., from so_type */
2257 {
2258         static char buf[64];
2259         char *cp;
2260
2261         switch (ty) {
2262
2263 #if     defined(SOCK_STREAM)
2264         case SOCK_STREAM:
2265             cp = "STREAM";
2266             break;
2267 #endif  /* defined(SOCK_STREAM) */
2268
2269 #if     defined(SOCK_STREAM)
2270         case SOCK_DGRAM:
2271             cp = "DGRAM";
2272             break;
2273 #endif  /* defined(SOCK_DGRAM) */
2274
2275 #if     defined(SOCK_RAW)
2276         case SOCK_RAW:
2277             cp = "RAW";
2278             break;
2279 #endif  /* defined(SOCK_RAW) */
2280
2281 #if     defined(SOCK_RDM)
2282         case SOCK_RDM:
2283             cp = "RDM";
2284             break;
2285 #endif  /* defined(SOCK_RDM) */
2286
2287 #if     defined(SOCK_SEQPACKET)
2288         case SOCK_SEQPACKET:
2289             cp = "SEQPACKET";
2290             break;
2291 #endif  /* defined(SOCK_SEQPACKET) */
2292
2293         default:
2294             (void) snpf(buf, sizeof(buf), "SOCK_%#x", ty);
2295             return(buf);
2296         }
2297         (void) snpf(buf, sizeof(buf), "SOCK_%s", cp);
2298         return(buf);
2299 }
2300
2301
2302 /*
2303  * printuid() - print User ID or login name
2304  */
2305
2306 char *
2307 printuid(uid, ty)
2308         UID_ARG uid;                    /* User IDentification number */
2309         int *ty;                        /* returned UID type pointer (NULL
2310                                          * (if none wanted).  If non-NULL
2311                                          * then: *ty = 0 = login name
2312                                          *           = 1 = UID number */
2313 {
2314         int i;
2315         struct passwd *pw;
2316         struct stat sb;
2317         static struct stat sbs;
2318         static struct uidcache {
2319             uid_t uid;
2320             char nm[LOGINML+1];
2321             struct uidcache *next;
2322         } **uc = (struct uidcache **)NULL;
2323         struct uidcache *up, *upn;
2324         static char user[USERPRTL+1];
2325
2326         if (Futol) {
2327             if (CkPasswd) {
2328
2329             /*
2330              * Get the mtime and ctime of /etc/passwd, as required.
2331              */
2332                 if (stat("/etc/passwd", &sb) != 0) {
2333                     (void) fprintf(stderr, "%s: can't stat(/etc/passwd): %s\n",
2334                         Pn, strerror(errno));
2335                     Exit(1);
2336                 }
2337             }
2338         /*
2339          * Define the UID cache, if necessary.
2340          */
2341             if (!uc) {
2342                 if (!(uc = (struct uidcache **)calloc(UIDCACHEL,
2343                                                 sizeof(struct uidcache *))))
2344                 {
2345                     (void) fprintf(stderr,
2346                         "%s: no space for %d byte UID cache hash buckets\n",
2347                         Pn, (int)(UIDCACHEL * (sizeof(struct uidcache *))));
2348                     Exit(1);
2349                 }
2350                 if (CkPasswd) {
2351                     sbs = sb;
2352                     CkPasswd = 0;
2353                 }
2354             }
2355         /*
2356          * If it's time to check /etc/passwd and if its the mtime/ctime has
2357          * changed, destroy the existing UID cache.
2358          */
2359             if (CkPasswd) {
2360                 if (sbs.st_mtime != sb.st_mtime || sbs.st_ctime != sb.st_ctime)
2361                 {
2362                     for (i = 0; i < UIDCACHEL; i++) {
2363                         if ((up = uc[i])) {
2364                             do {
2365                                 upn = up->next;
2366                                 (void) free((FREE_P *)up);
2367                             } while ((up = upn) != (struct uidcache *)NULL);
2368                             uc[i] = (struct uidcache *)NULL;
2369                         }
2370                     }
2371                     sbs = sb;
2372                 }
2373                 CkPasswd = 0;
2374             }
2375         /*
2376          * Search the UID cache.
2377          */
2378             i = (int)((((unsigned long)uid * 31415L) >> 7) & (UIDCACHEL - 1));
2379             for (up = uc[i]; up; up = up->next) {
2380                 if (up->uid == (uid_t)uid) {
2381                     if (ty)
2382                         *ty = 0;
2383                     return(up->nm);
2384                 }
2385             }
2386         /*
2387          * The UID is not in the cache.
2388          *
2389          * Look up the login name from the UID for a new cache entry.
2390          */
2391             if (!(pw = getpwuid((uid_t)uid))) {
2392                 if (!Fwarn) {
2393                     (void) fprintf(stderr, "%s: no pwd entry for UID %lu\n",
2394                         Pn, (unsigned long)uid);
2395                 }
2396             } else {
2397
2398             /*
2399              * Allocate and fill a new cache entry.  Link it to its hash bucket.
2400              */
2401                 if (!(upn = (struct uidcache *)malloc(sizeof(struct uidcache))))
2402                 {
2403                     (void) fprintf(stderr,
2404                         "%s: no space for UID cache entry for: %lu, %s)\n",
2405                         Pn, (unsigned long)uid, pw->pw_name);
2406                     Exit(1);
2407                 }
2408                 (void) strncpy(upn->nm, pw->pw_name, LOGINML);
2409                 upn->nm[LOGINML] = '\0';
2410                 upn->uid = (uid_t)uid;
2411                 upn->next = uc[i];
2412                 uc[i] = upn;
2413                 if (ty)
2414                     *ty = 0;
2415                 return(upn->nm);
2416             }
2417         }
2418 /*
2419  * Produce a numeric conversion of the UID.
2420  */
2421         (void) snpf(user, sizeof(user), "%*lu", USERPRTL, (unsigned long)uid);
2422         if (ty)
2423             *ty = 1;
2424         return(user);
2425 }
2426
2427
2428 /*
2429  * printunkaf() - print unknown address family
2430  */
2431
2432 void
2433 printunkaf(fam, ty)
2434         int fam;                        /* unknown address family */
2435         int ty;                         /* output type: 0 = terse; 1 = full */
2436 {
2437         char *p, *s;
2438
2439         p = "";
2440         switch (fam) {
2441
2442 #if     defined(AF_UNSPEC)
2443         case AF_UNSPEC:
2444             s = "UNSPEC";
2445             break;
2446 #endif  /* defined(AF_UNSPEC) */
2447
2448 #if     defined(AF_UNIX)
2449         case AF_UNIX:
2450             s = "UNIX";
2451             break;
2452 #endif  /* defined(AF_UNIX) */
2453
2454 #if     defined(AF_INET)
2455         case AF_INET:
2456             s = "INET";
2457             break;
2458 #endif  /* defined(AF_INET) */
2459
2460 #if     defined(AF_INET6)
2461         case AF_INET6:
2462             s = "INET6";
2463             break;
2464 #endif  /* defined(AF_INET6) */
2465
2466 #if     defined(AF_IMPLINK)
2467         case AF_IMPLINK:
2468             s = "IMPLINK";
2469             break;
2470 #endif  /* defined(AF_IMPLINK) */
2471
2472 #if     defined(AF_PUP)
2473         case AF_PUP:
2474             s = "PUP";
2475             break;
2476 #endif  /* defined(AF_PUP) */
2477
2478 #if     defined(AF_CHAOS)
2479         case AF_CHAOS:
2480             s = "CHAOS";
2481             break;
2482 #endif  /* defined(AF_CHAOS) */
2483
2484 #if     defined(AF_NS)
2485         case AF_NS:
2486             s = "NS";
2487             break;
2488 #endif  /* defined(AF_NS) */
2489
2490 #if     defined(AF_ISO)
2491         case AF_ISO:
2492             s = "ISO";
2493             break;
2494 #endif  /* defined(AF_ISO) */
2495
2496 #if     defined(AF_NBS)
2497 # if    !defined(AF_ISO) || AF_NBS!=AF_ISO
2498         case AF_NBS:
2499             s = "NBS";
2500             break;
2501 # endif /* !defined(AF_ISO) || AF_NBS!=AF_ISO */
2502 #endif  /* defined(AF_NBS) */
2503
2504 #if     defined(AF_ECMA)
2505         case AF_ECMA:
2506             s = "ECMA";
2507             break;
2508 #endif  /* defined(AF_ECMA) */
2509
2510 #if     defined(AF_DATAKIT)
2511         case AF_DATAKIT:
2512             s = "DATAKIT";
2513             break;
2514 #endif  /* defined(AF_DATAKIT) */
2515
2516 #if     defined(AF_CCITT)
2517         case AF_CCITT:
2518             s = "CCITT";
2519             break;
2520 #endif  /* defined(AF_CCITT) */
2521
2522 #if     defined(AF_SNA)
2523         case AF_SNA:
2524             s = "SNA";
2525             break;
2526 #endif  /* defined(AF_SNA) */
2527
2528 #if     defined(AF_DECnet)
2529         case AF_DECnet:
2530             s = "DECnet";
2531             break;
2532 #endif  /* defined(AF_DECnet) */
2533
2534 #if     defined(AF_DLI)
2535         case AF_DLI:
2536             s = "DLI";
2537             break;
2538 #endif  /* defined(AF_DLI) */
2539
2540 #if     defined(AF_LAT)
2541         case AF_LAT:
2542             s = "LAT";
2543             break;
2544 #endif  /* defined(AF_LAT) */
2545
2546 #if     defined(AF_HYLINK)
2547         case AF_HYLINK:
2548             s = "HYLINK";
2549             break;
2550 #endif  /* defined(AF_HYLINK) */
2551
2552 #if     defined(AF_APPLETALK)
2553         case AF_APPLETALK:
2554             s = "APPLETALK";
2555             break;
2556 #endif  /* defined(AF_APPLETALK) */
2557
2558 #if     defined(AF_BSC)
2559         case AF_BSC:
2560             s = "BSC";
2561             break;
2562 #endif  /* defined(AF_BSC) */
2563
2564 #if     defined(AF_DSS)
2565         case AF_DSS:
2566             s = "DSS";
2567             break;
2568 #endif  /* defined(AF_DSS) */
2569
2570 #if     defined(AF_ROUTE)
2571         case AF_ROUTE:
2572             s = "ROUTE";
2573             break;
2574 #endif  /* defined(AF_ROUTE) */
2575
2576 #if     defined(AF_RAW)
2577         case AF_RAW:
2578             s = "RAW";
2579             break;
2580 #endif  /* defined(AF_RAW) */
2581
2582 #if     defined(AF_LINK)
2583         case AF_LINK:
2584             s = "LINK";
2585             break;
2586 #endif  /* defined(AF_LINK) */
2587
2588 #if     defined(pseudo_AF_XTP)
2589         case pseudo_AF_XTP:
2590             p = "pseudo_";
2591             s = "XTP";
2592             break;
2593 #endif  /* defined(pseudo_AF_XTP) */
2594
2595 #if     defined(AF_RMP)
2596         case AF_RMP:
2597             s = "RMP";
2598             break;
2599 #endif  /* defined(AF_RMP) */
2600
2601 #if     defined(AF_COIP)
2602         case AF_COIP:
2603             s = "COIP";
2604             break;
2605 #endif  /* defined(AF_COIP) */
2606
2607 #if     defined(AF_CNT)
2608         case AF_CNT:
2609             s = "CNT";
2610             break;
2611 #endif  /* defined(AF_CNT) */
2612
2613 #if     defined(pseudo_AF_RTIP)
2614         case pseudo_AF_RTIP:
2615             p = "pseudo_";
2616             s = "RTIP";
2617             break;
2618 #endif  /* defined(pseudo_AF_RTIP) */
2619
2620 #if     defined(AF_NETMAN)
2621         case AF_NETMAN:
2622             s = "NETMAN";
2623             break;
2624 #endif  /* defined(AF_NETMAN) */
2625
2626 #if     defined(AF_INTF)
2627         case AF_INTF:
2628             s = "INTF";
2629             break;
2630 #endif  /* defined(AF_INTF) */
2631
2632 #if     defined(AF_NETWARE)
2633         case AF_NETWARE:
2634             s = "NETWARE";
2635             break;
2636 #endif  /* defined(AF_NETWARE) */
2637
2638 #if     defined(AF_NDD)
2639         case AF_NDD:
2640             s = "NDD";
2641             break;
2642 #endif  /* defined(AF_NDD) */
2643
2644 #if     defined(AF_NIT)
2645 # if    !defined(AF_ROUTE) || AF_ROUTE!=AF_NIT
2646         case AF_NIT:
2647             s = "NIT";
2648             break;
2649 # endif /* !defined(AF_ROUTE) || AF_ROUTE!=AF_NIT */
2650 #endif  /* defined(AF_NIT) */
2651
2652 #if     defined(AF_802)
2653 # if    !defined(AF_RAW) || AF_RAW!=AF_802
2654         case AF_802:
2655             s = "802";
2656             break;
2657 # endif /* !defined(AF_RAW) || AF_RAW!=AF_802 */
2658 #endif  /* defined(AF_802) */
2659
2660 #if     defined(AF_X25)
2661         case AF_X25:
2662             s = "X25";
2663             break;
2664 #endif  /* defined(AF_X25) */
2665
2666 #if     defined(AF_CTF)
2667         case AF_CTF:
2668             s = "CTF";
2669             break;
2670 #endif  /* defined(AF_CTF) */
2671
2672 #if     defined(AF_WAN)
2673         case AF_WAN:
2674             s = "WAN";
2675             break;
2676 #endif  /* defined(AF_WAN) */
2677
2678 #if     defined(AF_OSINET)
2679 # if    defined(AF_INET) && AF_INET!=AF_OSINET
2680         case AF_OSINET:
2681             s = "OSINET";
2682             break;
2683 # endif /* defined(AF_INET) && AF_INET!=AF_OSINET */
2684 #endif  /* defined(AF_OSINET) */
2685
2686 #if     defined(AF_GOSIP)
2687         case AF_GOSIP:
2688             s = "GOSIP";
2689             break;
2690 #endif  /* defined(AF_GOSIP) */
2691
2692 #if     defined(AF_SDL)
2693         case AF_SDL:
2694             s = "SDL";
2695             break;
2696 #endif  /* defined(AF_SDL) */
2697
2698 #if     defined(AF_IPX)
2699         case AF_IPX:
2700             s = "IPX";
2701             break;
2702 #endif  /* defined(AF_IPX) */
2703
2704 #if     defined(AF_SIP)
2705         case AF_SIP:
2706             s = "SIP";
2707             break;
2708 #endif  /* defined(AF_SIP) */
2709
2710 #if     defined(psuedo_AF_PIP)
2711         case psuedo_AF_PIP:
2712             p = "pseudo_";
2713             s = "PIP";
2714             break;
2715 #endif  /* defined(psuedo_AF_PIP) */
2716
2717 #if     defined(AF_OTS)
2718         case AF_OTS:
2719             s = "OTS";
2720             break;
2721 #endif  /* defined(AF_OTS) */
2722
2723 #if     defined(pseudo_AF_BLUE)
2724         case pseudo_AF_BLUE:    /* packets for Blue box */
2725             p = "pseudo_";
2726             s = "BLUE";
2727             break;
2728 #endif  /* defined(pseudo_AF_BLUE) */
2729
2730 #if     defined(AF_NDRV)        /* network driver raw access */
2731         case AF_NDRV:
2732             s = "NDRV";
2733             break;
2734 #endif  /* defined(AF_NDRV) */
2735
2736 #if     defined(AF_SYSTEM)      /* kernel event messages */
2737         case AF_SYSTEM:
2738             s = "SYSTEM";
2739             break;
2740 #endif  /* defined(AF_SYSTEM) */
2741
2742 #if     defined(AF_USER)
2743         case AF_USER:
2744             s = "USER";
2745             break;
2746 #endif  /* defined(AF_USER) */
2747
2748 #if     defined(pseudo_AF_KEY)
2749         case pseudo_AF_KEY:
2750             p = "pseudo_";
2751             s = "KEY";
2752             break;
2753 #endif  /* defined(pseudo_AF_KEY) */
2754
2755 #if     defined(AF_KEY)         /* Security Association DB socket */
2756         case AF_KEY:                    
2757             s = "KEY";
2758             break;
2759 #endif  /* defined(AF_KEY) */
2760
2761 #if     defined(AF_NCA)         /* NCA socket */
2762         case AF_NCA:                    
2763             s = "NCA";
2764             break;
2765 #endif  /* defined(AF_NCA) */
2766
2767 #if     defined(AF_POLICY)              /* Security Policy DB socket */
2768         case AF_POLICY:
2769             s = "POLICY";
2770             break;
2771 #endif  /* defined(AF_POLICY) */
2772
2773 #if     defined(AF_PPP)         /* PPP socket */
2774         case AF_PPP:                    
2775             s = "PPP";
2776             break;
2777 #endif  /* defined(AF_PPP) */
2778
2779         default:
2780             if (!ty)
2781                 (void) snpf(Namech, Namechl, "%#x", fam);
2782             else
2783                 (void) snpf(Namech, Namechl,
2784                     "no further information on family %#x", fam);
2785             return;
2786         }
2787         if (!ty)
2788             (void) snpf(Namech, Namechl, "%sAF_%s", p, s);
2789         else
2790             (void) snpf(Namech, Namechl, "no further information on %sAF_%s",
2791                 p, s);
2792         return;
2793 }
2794
2795
2796 #if     !defined(HASNORPC_H)
2797 /*
2798  * update_portmap() - update a portmap entry with its port number or service
2799  *                    name
2800  */
2801
2802 static void
2803 update_portmap(pt, pn)
2804         struct porttab *pt;             /* porttab entry */
2805         char *pn;                       /* port name */
2806 {
2807         MALLOC_S al, nl;
2808         char *cp;
2809
2810         if (pt->ss)
2811             return;
2812         if (!(al = strlen(pn))) {
2813             pt->ss = 1;
2814             return;
2815         }
2816         nl = al + pt->nl + 2;
2817         if (!(cp = (char *)malloc(nl + 1))) {
2818             (void) fprintf(stderr,
2819                 "%s: can't allocate %d bytes for portmap name: %s[%s]\n",
2820                 Pn, (int)(nl + 1), pn, pt->name);
2821             Exit(1);
2822         }
2823         (void) snpf(cp, nl + 1, "%s[%s]", pn, pt->name);
2824         (void) free((FREE_P *)pt->name);
2825         pt->name = cp;
2826         pt->nl = nl;
2827         pt->ss = 1;
2828 }
2829 #endif  /* !defined(HASNORPC_H) */