2 * print.c - common print support functions for lsof
7 * Copyright 1994 Purdue Research Foundation, West Lafayette, Indiana
8 * 47907. All rights reserved.
10 * Written by Victor A. Abell
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.
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:
19 * 1. Neither the authors nor Purdue University are responsible for any
20 * consequences of the use of this software.
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.
26 * 3. Altered versions must be plainly marked as such, and must not be
27 * misrepresented as being the original software.
29 * 4. This notice may not be removed or altered.
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 $";
43 * Local definitions, structures and function prototypes
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() */
55 unsigned char a[MAX_AF_ADDR]; /* numeric address */
56 int af; /* address family -- e.g., AF_INET
58 char *name; /* name */
63 MALLOC_S nl; /* name length (excluding '\0') */
64 int ss; /* service name status, 0 = lookup not
71 #if defined(HASNORPC_H)
72 static struct porttab **Pth[2] = { NULL, NULL };
74 * Pth[0] for TCP service names
75 * Pth[1] for UDP service names
77 #else /* !defined(HASNORPC_H) */
78 static struct porttab **Pth[4] = { NULL, NULL, NULL, NULL };
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
85 #endif /* defined(HASNORPC_H) */
87 #define HASHPORT(p) (((((int)(p)) * 31415) >> 3) & (PORTHASHBUCKETS - 1))
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) */
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));
102 * endnm() - locate end of Namech
107 size_t *sz; /* returned remaining size */
112 for (s = Namech, tsz = Namechl; *s; s++, tsz--)
119 #if !defined(HASNORPC_H)
121 * fill_portmap() -- fill the RPC portmap program name table via a conversation
122 * with the portmapper
124 * The following copyright notice acknowledges that this function was adapted
125 * from getrpcportnam() of the source code of the OpenBSD netstat program.
129 * Copyright (c) 1983, 1988, 1993
130 * The Regents of the University of California. All rights reserved.
132 * Redistribution and use in source and binary forms, with or without
133 * modification, are permitted provided that the following conditions
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.
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
164 char buf[128], *cp, *nm;
168 struct pmaplist *p = (struct pmaplist *)NULL;
171 struct TIMEVAL_LSOF tm;
173 #if !defined(CAN_USE_CLNT_CREATE)
175 struct sockaddr_in ia;
177 #endif /* !defined(CAN_USE_CLNT_CREATE) */
180 * Construct structures for communicating with the portmapper.
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) */
194 * Get an RPC client handle. Then ask for a dump of the port map.
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) */
204 if (clnt_call(c, PMAPPROC_DUMP, XDR_VOID, NULL, XDR_PMAPLIST,
211 * Loop through the port map dump, creating portmap table entries from TCP
214 for (; p; p = p->pml_next) {
217 * Determine the port map entry's protocol; ignore all but TCP and UDP.
219 if (p->pml_map.pm_prot == IPPROTO_TCP)
221 else if (p->pml_map.pm_prot == IPPROTO_UDP)
226 * See if there's already a portmap entry for this port. If there is,
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)
237 * Save the registration name or number.
240 if ((r = (struct rpcent *)getrpcbynumber(p->pml_map.pm_prog))) {
241 if (r->r_name && strlen(r->r_name))
245 (void) snpf(buf, sizeof(buf), "%lu",
246 (unsigned long)p->pml_map.pm_prog);
252 * Allocate space for the portmap name entry and copy it there.
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);
261 (void) free((FREE_P *)nm);
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.
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);
277 pt->next = Pth[pr][h];
283 #endif /* !defined(HASNORPC_H) */
287 * fill_porttab() -- fill the TCP and UDP service name port table with a
302 * Scan the services data base for TCP and UDP entries that have a non-null
303 * name associated with them.
305 (void) setservent(1);
306 while ((se = getservent())) {
307 if (!se->s_name || !se->s_proto)
309 if (strcasecmp(se->s_proto, "TCP") == 0)
311 else if (strcasecmp(se->s_proto, "UDP") == 0)
315 if (!se->s_name || !strlen(se->s_name))
317 p = ntohs(se->s_port);
319 * See if a port->service entry is already cached for this port and
320 * prototcol. If it is, leave it alone.
323 for (pt = Pth[pr][h]; pt; pt = pt->next) {
330 * Add a new entry to the cache for this port and protocol.
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);
339 (void) free((FREE_P *)nm);
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",
351 pt->next = Pth[pr][h];
360 * gethostnm() - get host name
365 unsigned char *ia; /* Internet address */
366 int af; /* address family -- e.g., AF_INET
369 int al = MIN_AF_ADDR;
371 static struct hostcache *hc = (struct hostcache *)NULL;
374 struct hostent *he = (struct hostent *)NULL;
385 #endif /* defined(HASIPv6) */
387 for (i = 0; i < hcx; i++) {
390 for (j = 0; j < al; j++) {
391 if (ia[j] != hc[i].a[j])
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.
403 he = gethostbyaddr((char *)ia, al, af);
404 if (!he || !he->h_name) {
407 if (af == AF_INET6) {
410 * Since IPv6 numeric addresses use `:' as a separator, enclose
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]");
419 (void) snpf(&hbuf[len], sizeof(hbuf) - len, "]");
422 #endif /* defined(HASIPv6) */
425 (void) snpf(hbuf, sizeof(hbuf), "%u.%u.%u.%u", ia[0], ia[1],
428 (void) snpf(hbuf, sizeof(hbuf), "(unknown AF value: %d)", af);
431 hn = (char *)he->h_name;
433 * Allocate space for name and copy name to it.
435 if (!(np = mkstrcpy(hn, (MALLOC_S *)NULL))) {
436 (void) fprintf(stderr, "%s: no space for host name: ", Pn);
437 safestrprt(hn, stderr, 1);
441 * Add address/name entry to cache. Allocate cache space in HCINC chunks.
445 len = (MALLOC_S)(nhc * sizeof(struct hostcache));
447 hc = (struct hostcache *)malloc(len);
449 hc = (struct hostcache *)realloc((MALLOC_P *)hc, len);
451 (void) fprintf(stderr, "%s: no space for host cache\n", Pn);
456 for (i = 0; i < al; i++) {
457 hc[hcx].a[i] = ia[i];
465 * lkup_port() - look up port for protocol
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
482 * If the hash buckets haven't been allocated, do so.
486 #if defined(HASNORPC_H)
488 #else /* !defined(HASNORPC_H) */
489 nh = FportMap ? 4 : 2;
490 #endif /* defined(HASNORPC_H) */
492 for (h = 0; h < nh; h++) {
493 if (!(Pth[h] = (struct porttab **)calloc(PORTHASHBUCKETS,
494 sizeof(struct porttab *))))
496 (void) fprintf(stderr,
497 "%s: can't allocate %d bytes for %s %s hash buckets\n",
499 (int)(2 * (PORTHASHBUCKETS * sizeof(struct porttab *))),
500 (h & 1) ? "UDP" : "TCP",
501 (h > 1) ? "portmap" : "port");
507 #if !defined(HASNORPC_H)
509 * If we're looking up program names for portmapped ports, make sure the
510 * portmap table has been loaded.
512 if (FportMap && !pm) {
513 (void) fill_portmap();
516 #endif /* !defined(HASNORPC_H) */
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.
524 #if !defined(HASNORPC_H)
525 if (!src && FportMap) {
526 for (pt = Pth[pr+2][h]; pt; pt = pt->next) {
530 pn = Fport ? lkup_svcnam(h, p, pr, 0) : (char *)NULL;
532 (void) snpf(pb, sizeof(pb), "%d", p);
535 (void) update_portmap(pt, pn);
540 #endif /* !defined(HASNORPC_H) */
542 for (pt = Pth[pr][h]; pt; pt = pt->next) {
547 * Search for a possible service name, unless the -P option has been specified.
549 * If there is no service name, return a %d conversion.
551 * Don't cache %d conversions; a zero port number is a %d conversion that
552 * is represented by "*".
554 pn = Fport ? lkup_svcnam(h, p, pr, 1) : (char *)NULL;
555 if (!pn || !strlen(pn)) {
557 (void) snpf(pb, sizeof(pb), "%d", p);
563 * Allocate a new porttab entry for the TCP or UDP service name.
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);
571 * Allocate space for the name; copy it to the porttab entry; and link the
572 * porttab entry to its hash bucket.
574 * Return a pointer to the name.
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);
585 pt->next = Pth[pr][h];
593 * lkup_svcnam() - look up service name for port
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 */
604 static int fl[PORTTABTHRESH];
612 * Do nothing if -P has been specified.
615 return((char *)NULL);
620 * Search service name cache, if it hasn't already been done.
621 * Return the name of a match.
624 for (pt = Pth[pr][h]; pt; pt = pt->next) {
630 * If fill_porttab() has been called, there is no service name.
632 * Do PORTTABTHRES getservbport() calls, remembering the failures, so they
635 * After PORTABTHRESH getservbyport() calls, call fill_porttab() once,
639 if (gsbp < PORTTABTHRESH) {
640 for (i = 0; i < fln; i++) {
642 return((char *)NULL);
645 if ((se = getservbyport(htons(p), pr ? "udp" : "tcp")))
647 if (fln < PORTTABTHRESH)
649 return((char *)NULL);
651 (void) fill_porttab();
655 return((char *)NULL);
660 * print_file() - print file
667 char *cp = (char *)NULL;
671 if (PrPass && !Hdr) {
674 * Print the header line if this is the second pass and the
675 * header hasn't already been printed.
677 (void) printf("%-*.*s %*s", CmdColW, CmdColW, CMDTTL, PidColW,
680 #if defined(HASTASKS)
682 (void) printf(" %*s", TidColW, TIDTTL);
683 #endif /* defined(HASTASKS) */
685 #if defined(HASZONES)
687 (void) printf(" %-*s", ZoneColW, ZONETTL);
688 #endif /* defined(HASZONES) */
690 #if defined(HASSELINUX)
692 (void) printf(" %-*s", CntxColW, CNTXTTL);
693 #endif /* defined(HASSELINUX) */
697 (void) printf(" %*s", PpidColW, PPIDTTL);
698 #endif /* defined(HASPPID) */
701 (void) printf(" %*s", PgidColW, PGIDTTL);
702 (void) printf(" %*s %*s %*s",
707 #if defined(HASFSTRUCT)
710 # if !defined(HASNOFSADDR)
712 (void) printf(" %*s", FsColW, FSTTL);
713 # endif /* !defined(HASNOFSADDR) */
715 # if !defined(HASNOFSCOUNT)
717 (void) printf(" %*s", FcColW, FCTTL);
718 # endif /* !defined(HASNOFSCOUNT) */
720 # if !defined(HASNOFSFLAGS)
722 (void) printf(" %*s", FgColW, FGTTL);
723 # endif /* !defined(HASNOFSFLAGS) */
725 # if !defined(HASNOFSNADDR)
727 (void) printf(" %*s", NiColW, NiTtl);
728 # endif /* !defined(HASNOFSNADDR) */
731 #endif /* defined(HASFSTRUCT) */
733 (void) printf(" %*s", DevColW, DEVTTL);
735 (void) printf(" %*s", SzOffColW, OFFTTL);
737 (void) printf(" %*s", SzOffColW, SZTTL);
739 (void) printf(" %*s", SzOffColW, SZOFFTTL);
741 (void) printf(" %*s", NlColW, NLTTL);
742 (void) printf(" %*s %s\n", NodeColW, NODETTL, NMTTL);
746 * Size or print the command.
748 cp = (Lp->cmd && *Lp->cmd != '\0') ? Lp->cmd : "(unknown)";
750 len = safestrlen(cp, 2);
751 if (CmdLim && (len > CmdLim))
756 safestrprtn(cp, CmdColW, stdout, 2);
758 * Size or print the process ID.
761 (void) snpf(buf, sizeof(buf), "%d", Lp->pid);
762 if ((len = strlen(buf)) > PidColW)
765 (void) printf(" %*d", PidColW, Lp->pid);
767 #if defined(HASTASKS)
769 * Size or print task ID.
773 (void) snpf(buf, sizeof(buf), "%d", Lp->tid);
774 if ((len = strlen(buf)) > TidColW)
778 } else if (TaskPrtFl) {
780 (void) printf(" %*d", TidColW, Lp->tid);
782 (void) printf(" %*s", TidColW, "");
784 #endif /* defined(HASTASKS) */
786 #if defined(HASZONES)
788 * Size or print the zone.
793 if ((len = strlen(Lp->zn)) > ZoneColW)
797 (void) printf(" %-*s", ZoneColW, Lp->zn ? Lp->zn : "");
799 #endif /* defined(HASZONES) */
801 #if defined(HASSELINUX)
803 * Size or print the context.
808 if ((len = strlen(Lp->cntx)) > CntxColW)
812 (void) printf(" %-*s", CntxColW, Lp->cntx ? Lp->cntx : "");
814 #endif /* defined(HASSELINUX) */
820 * Size or print the parent process ID.
823 (void) snpf(buf, sizeof(buf), "%d", Lp->ppid);
824 if ((len = strlen(buf)) > PpidColW)
827 (void) printf(" %*d", PpidColW, Lp->ppid);
829 #endif /* defined(HASPPID) */
834 * Size or print the process group ID.
837 (void) snpf(buf, sizeof(buf), "%d", Lp->pgid);
838 if ((len = strlen(buf)) > PgidColW)
841 (void) printf(" %*d", PgidColW, Lp->pgid);
844 * Size or print the user ID or login name.
847 if ((len = strlen(printuid((UID_ARG)Lp->uid, NULL))) > UserColW)
850 (void) printf(" %*.*s", UserColW, UserColW,
851 printuid((UID_ARG)Lp->uid, NULL));
853 * Size or print the file descriptor, access mode and lock status.
856 (void) snpf(buf, sizeof(buf), "%s%c%c",
858 (Lf->lock == ' ') ? Lf->access
859 : (Lf->access == ' ') ? '-'
862 if ((len = strlen(buf)) > FdColW)
865 (void) printf(" %*.*s%c%c", FdColW - 2, FdColW - 2, Lf->fd,
866 (Lf->lock == ' ') ? Lf->access
867 : (Lf->access == ' ') ? '-'
871 * Size or print the type.
874 if ((len = strlen(Lf->type)) > TypeColW)
877 (void) printf(" %*.*s", TypeColW, TypeColW, Lf->type);
879 #if defined(HASFSTRUCT)
881 * Size or print the file structure address, file usage count, and node
887 # if !defined(HASNOFSADDR)
889 cp = (Lf->fsv & FSV_FA) ? print_kptr(Lf->fsa, buf, sizeof(buf))
892 if ((len = strlen(cp)) > FsColW)
895 (void) printf(" %*.*s", FsColW, FsColW, cp);
898 # endif /* !defined(HASNOFSADDR) */
900 # if !defined(HASNOFSCOUNT)
902 if (Lf->fsv & FSV_CT) {
903 (void) snpf(buf, sizeof(buf), "%ld", Lf->fct);
908 if ((len = strlen(cp)) > FcColW)
911 (void) printf(" %*.*s", FcColW, FcColW, cp);
913 # endif /* !defined(HASNOFSCOUNT) */
915 # if !defined(HASNOFSFLAGS)
917 if ((Lf->fsv & FSV_FG) && (FsvFlagX || Lf->ffg || Lf->pof))
918 cp = print_fflags(Lf->ffg, Lf->pof);
922 if ((len = strlen(cp)) > FgColW)
925 (void) printf(" %*.*s", FgColW, FgColW, cp);
927 # endif /* !defined(HASNOFSFLAGS) */
929 # if !defined(HASNOFSNADDR)
931 cp = (Lf->fsv & FSV_NI) ? print_kptr(Lf->fna, buf, sizeof(buf))
934 if ((len = strlen(cp)) > NiColW)
937 (void) printf(" %*.*s", NiColW, NiColW, cp);
939 # endif /* !defined(HASNOFSNADDR) */
942 #endif /* defined(HASFSTRUCT) */
945 * Size or print the device information.
951 } else if (Lf->dev_def) {
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),
964 #endif /* defined(HASPRINTDEV) */
972 len = strlen(Lf->dev_ch);
979 (void) printf(" %*.*s", DevColW, DevColW, cp);
982 (void) printf(" %*.*s", DevColW, DevColW, Lf->dev_ch);
984 (void) printf(" %*.*s", DevColW, DevColW, "");
988 * Size or print the size or offset.
993 #if defined(HASPRINTSZ)
995 #else /* !defined(HASPRINTSZ) */
996 (void) snpf(buf, sizeof(buf), SzOffFmt_d, Lf->sz);
998 #endif /* defined(HASPRINTSZ) */
1001 } else if (Lf->off_def) {
1003 #if defined(HASPRINTOFF)
1004 cp = HASPRINTOFF(Lf, 0);
1005 #else /* !defined(HASPRINTOFF) */
1006 (void) snpf(buf, sizeof(buf), SzOffFmt_0t, Lf->off);
1008 #endif /* defined(HASPRINTOFF) */
1011 if (OffDecDig && len > (OffDecDig + 2)) {
1013 #if defined(HASPRINTOFF)
1014 cp = HASPRINTOFF(Lf, 1);
1015 #else /* !defined(HASPRINTOFF) */
1016 (void) snpf(buf, sizeof(buf), SzOffFmt_x, Lf->off);
1018 #endif /* defined(HASPRINTOFF) */
1024 if (len > SzOffColW)
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) */
1036 else if (Lf->off_def) {
1038 #if defined(HASPRINTOFF)
1039 cp = HASPRINTOFF(Lf, 0);
1040 #else /* !defined(HASPRINTOFF) */
1041 (void) snpf(buf, sizeof(buf), SzOffFmt_0t, Lf->off);
1043 #endif /* defined(HASPRINTOFF) */
1045 if (OffDecDig && (int)strlen(cp) > (OffDecDig + 2)) {
1047 #if defined(HASPRINTOFF)
1048 cp = HASPRINTOFF(Lf, 1);
1049 #else /* !defined(HASPRINTOFF) */
1050 (void) snpf(buf, sizeof(buf), SzOffFmt_x, Lf->off);
1052 #endif /* defined(HASPRINTOFF) */
1055 (void) printf("%*.*s", SzOffColW, SzOffColW, cp);
1057 (void) printf("%*.*s", SzOffColW, SzOffColW, "");
1060 * Size or print the link count.
1063 if (Lf->nlink_def) {
1064 (void) snpf(buf, sizeof(buf), " %ld", Lf->nlink);
1069 if ((len = strlen(cp)) > NlColW)
1072 (void) printf(" %*s", NlColW, cp);
1075 * Size or print the inode information.
1077 switch (Lf->inp_ty) {
1080 #if defined(HASPRINTINO)
1081 cp = HASPRINTINO(Lf);
1082 #else /* !defined(HASPRINTINO) */
1083 (void) snpf(buf, sizeof(buf), InodeFmt_d, Lf->inode);
1085 #endif /* defined(HASPRINTINO) */
1095 (void) snpf(buf, sizeof(buf), InodeFmt_x, Lf->inode);
1102 if ((len = strlen(cp)) > NodeColW)
1105 (void) printf(" %*.*s", NodeColW, NodeColW, cp);
1108 * If this is the second pass, print the name column. (It doesn't need
1114 #if defined(HASPRINTNM)
1116 #else /* !defined(HASPRINTNM) */
1118 #endif /* defined(HASPRINTNM) */
1125 * printinaddr() - print Internet addresses
1133 int nl = Namechl - 1;
1137 * Process local network address first. If there's a foreign address,
1138 * separate it from the local address with "->".
1140 for (i = 0, *np = '\0'; i < 2; i++) {
1143 host = port = (char *)NULL;
1147 * If this is the foreign address, insert the separator.
1154 (void) snpf(Namech, Namechl,
1155 "network addresses too long");
1158 (void) snpf(np, nl, "->");
1163 * Convert the address to a host name.
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))
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)
1178 host = gethostnm((unsigned char *)&Lf->li[i].ia, Lf->li[i].af);
1179 #endif /* defined(HASIPv6) */
1182 * Process the port number.
1184 if (Lf->li[i].p > 0) {
1188 #if !defined(HASNORPC_H)
1190 #endif /* defined(HASNORPC_H) */
1195 * If converting port numbers to service names, or looking
1196 * up portmap program names and numbers, do so by protocol.
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.)
1205 #if !defined(HASNORPC_H)
1206 if ((src = i) && FportMap) {
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,
1216 # endif /* defined(HASIPv6) */
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
1225 #endif /* !defined(HASNORPC_H) */
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);
1233 (void) snpf(pbuf, sizeof(pbuf), "%d", Lf->li[i].p);
1236 } else if (Lf->li[i].p == 0)
1239 * Enter the host name.
1242 if ((len = strlen(host)) > nl)
1245 (void) snpf(np, nl, "%s", host);
1251 * Enter the port number, preceded by a colon.
1254 if (((len = strlen(port)) + 1) >= nl)
1256 (void) snpf(np, nl, ":%s", port);
1262 safestrprt(Namech, stdout, 0);
1270 * print_init() - initialize for printing
1278 * Preset standard values.
1280 PrPass = (Ffield || Fterse) ? 1 : 0;
1284 * Size columns by their titles.
1286 CmdColW = strlen(CMDTTL);
1287 DevColW = strlen(DEVTTL);
1288 FdColW = strlen(FDTTL);
1290 NlColW = strlen(NLTTL);
1291 NmColW = strlen(NMTTL);
1292 NodeColW = strlen(NODETTL);
1293 PgidColW = strlen(PGIDTTL);
1294 PidColW = strlen(PIDTTL);
1295 PpidColW = strlen(PPIDTTL);
1297 SzOffColW = strlen(SZTTL);
1299 SzOffColW = strlen(OFFTTL);
1301 SzOffColW = strlen(SZOFFTTL);
1303 #if defined(HASTASKS)
1304 TidColW = strlen(TIDTTL);
1305 #endif /* defined(HASTASKS) */
1307 TypeColW = strlen(TYPETTL);
1308 UserColW = strlen(USERTTL);
1310 #if defined(HASFSTRUCT)
1312 # if !defined(HASNOFSADDR)
1313 FsColW = strlen(FSTTL);
1314 # endif /* !defined(HASNOFSADDR) */
1316 # if !defined(HASNOFSCOUNT)
1317 FcColW = strlen(FCTTL);
1318 # endif /* !defined(HASNOFSCOUNT) */
1320 # if !defined(HASNOFSFLAGS)
1321 FgColW = strlen(FGTTL);
1322 # endif /* !defined(HASNOFSFLAGS) */
1324 # if !defined(HASNOFSNADDR)
1325 NiColW = strlen(NiTtl);
1326 # endif /* !defined(HASNOFSNADDR) */
1327 #endif /* defined(HASFSTRUCT) */
1329 #if defined(HASSELINUX)
1331 CntxColW = strlen(CNTXTTL);
1332 #endif /* defined(HASSELINUX) */
1334 #if defined(HASZONES)
1336 ZoneColW = strlen(ZONETTL);
1337 #endif /* defined(HASZONES) */
1342 #if !defined(HASPRIVPRIPP)
1344 * printiproto() - print Internet protocol name
1349 int p; /* protocol number */
1357 #if defined(IPPROTO_TCP)
1361 #endif /* defined(IPPROTO_TCP) */
1363 #if defined(IPPROTO_UDP)
1367 #endif /* defined(IPPROTO_UDP) */
1369 #if defined(IPPROTO_IP)
1370 # if !defined(IPPROTO_HOPOPTS) || IPPROTO_IP!=IPPROTO_HOPOPTS
1374 # endif /* !defined(IPPROTO_HOPOPTS) || IPPROTO_IP!=IPPROTO_HOPOPTS */
1375 #endif /* defined(IPPROTO_IP) */
1377 #if defined(IPPROTO_ICMP)
1381 #endif /* defined(IPPROTO_ICMP) */
1383 #if defined(IPPROTO_ICMPV6)
1384 case IPPROTO_ICMPV6:
1387 #endif /* defined(IPPROTO_ICMPV6) */
1389 #if defined(IPPROTO_IGMP)
1393 #endif /* defined(IPPROTO_IGMP) */
1395 #if defined(IPPROTO_GGP)
1399 #endif /* defined(IPPROTO_GGP) */
1401 #if defined(IPPROTO_EGP)
1405 #endif /* defined(IPPROTO_EGP) */
1407 #if defined(IPPROTO_PUP)
1411 #endif /* defined(IPPROTO_PUP) */
1413 #if defined(IPPROTO_IDP)
1417 #endif /* defined(IPPROTO_IDP) */
1419 #if defined(IPPROTO_ND)
1423 #endif /* defined(IPPROTO_ND) */
1425 #if defined(IPPROTO_RAW)
1429 #endif /* defined(IPPROTO_RAW) */
1431 #if defined(IPPROTO_HELLO)
1435 #endif /* defined(IPPROTO_HELLO) */
1437 #if defined(IPPROTO_PXP)
1441 #endif /* defined(IPPROTO_PXP) */
1443 #if defined(IPPROTO_RAWIP)
1447 #endif /* defined(IPPROTO_RAWIP) */
1449 #if defined(IPPROTO_RAWIF)
1453 #endif /* defined(IPPROTO_RAWIF) */
1455 #if defined(IPPROTO_HOPOPTS)
1456 case IPPROTO_HOPOPTS:
1459 #endif /* defined(IPPROTO_HOPOPTS) */
1461 #if defined(IPPROTO_IPIP)
1465 #endif /* defined(IPPROTO_IPIP) */
1467 #if defined(IPPROTO_ST)
1471 #endif /* defined(IPPROTO_ST) */
1473 #if defined(IPPROTO_PIGP)
1477 #endif /* defined(IPPROTO_PIGP) */
1479 #if defined(IPPROTO_RCCMON)
1480 case IPPROTO_RCCMON:
1483 #endif /* defined(IPPROTO_RCCMON) */
1485 #if defined(IPPROTO_NVPII)
1489 #endif /* defined(IPPROTO_NVPII) */
1491 #if defined(IPPROTO_ARGUS)
1495 #endif /* defined(IPPROTO_ARGUS) */
1497 #if defined(IPPROTO_EMCON)
1501 #endif /* defined(IPPROTO_EMCON) */
1503 #if defined(IPPROTO_XNET)
1507 #endif /* defined(IPPROTO_XNET) */
1509 #if defined(IPPROTO_CHAOS)
1513 #endif /* defined(IPPROTO_CHAOS) */
1515 #if defined(IPPROTO_MUX)
1519 #endif /* defined(IPPROTO_MUX) */
1521 #if defined(IPPROTO_MEAS)
1525 #endif /* defined(IPPROTO_MEAS) */
1527 #if defined(IPPROTO_HMP)
1531 #endif /* defined(IPPROTO_HMP) */
1533 #if defined(IPPROTO_PRM)
1537 #endif /* defined(IPPROTO_PRM) */
1539 #if defined(IPPROTO_TRUNK1)
1540 case IPPROTO_TRUNK1:
1543 #endif /* defined(IPPROTO_TRUNK1) */
1545 #if defined(IPPROTO_TRUNK2)
1546 case IPPROTO_TRUNK2:
1549 #endif /* defined(IPPROTO_TRUNK2) */
1551 #if defined(IPPROTO_LEAF1)
1555 #endif /* defined(IPPROTO_LEAF1) */
1557 #if defined(IPPROTO_LEAF2)
1561 #endif /* defined(IPPROTO_LEAF2) */
1563 #if defined(IPPROTO_RDP)
1567 #endif /* defined(IPPROTO_RDP) */
1569 #if defined(IPPROTO_IRTP)
1573 #endif /* defined(IPPROTO_IRTP) */
1575 #if defined(IPPROTO_TP)
1579 #endif /* defined(IPPROTO_TP) */
1581 #if defined(IPPROTO_BLT)
1585 #endif /* defined(IPPROTO_BLT) */
1587 #if defined(IPPROTO_NSP)
1591 #endif /* defined(IPPROTO_NSP) */
1593 #if defined(IPPROTO_INP)
1597 #endif /* defined(IPPROTO_INP) */
1599 #if defined(IPPROTO_SEP)
1603 #endif /* defined(IPPROTO_SEP) */
1605 #if defined(IPPROTO_3PC)
1609 #endif /* defined(IPPROTO_3PC) */
1611 #if defined(IPPROTO_IDPR)
1615 #endif /* defined(IPPROTO_IDPR) */
1617 #if defined(IPPROTO_XTP)
1621 #endif /* defined(IPPROTO_XTP) */
1623 #if defined(IPPROTO_DDP)
1627 #endif /* defined(IPPROTO_DDP) */
1629 #if defined(IPPROTO_CMTP)
1633 #endif /* defined(IPPROTO_CMTP) */
1635 #if defined(IPPROTO_TPXX)
1639 #endif /* defined(IPPROTO_TPXX) */
1641 #if defined(IPPROTO_IL)
1645 #endif /* defined(IPPROTO_IL) */
1647 #if defined(IPPROTO_IPV6)
1651 #endif /* defined(IPPROTO_IPV6) */
1653 #if defined(IPPROTO_SDRP)
1657 #endif /* defined(IPPROTO_SDRP) */
1659 #if defined(IPPROTO_ROUTING)
1660 case IPPROTO_ROUTING:
1663 #endif /* defined(IPPROTO_ROUTING) */
1665 #if defined(IPPROTO_FRAGMENT)
1666 case IPPROTO_FRAGMENT:
1669 #endif /* defined(IPPROTO_FRAGMENT) */
1671 #if defined(IPPROTO_IDRP)
1675 #endif /* defined(IPPROTO_IDRP) */
1677 #if defined(IPPROTO_RSVP)
1681 #endif /* defined(IPPROTO_RSVP) */
1683 #if defined(IPPROTO_GRE)
1687 #endif /* defined(IPPROTO_GRE) */
1689 #if defined(IPPROTO_MHRP)
1693 #endif /* defined(IPPROTO_MHRP) */
1695 #if defined(IPPROTO_BHA)
1699 #endif /* defined(IPPROTO_BHA) */
1701 #if defined(IPPROTO_ESP)
1705 #endif /* defined(IPPROTO_ESP) */
1707 #if defined(IPPROTO_AH)
1711 #endif /* defined(IPPROTO_AH) */
1713 #if defined(IPPROTO_INLSP)
1717 #endif /* defined(IPPROTO_INLSP) */
1719 #if defined(IPPROTO_SWIPE)
1723 #endif /* defined(IPPROTO_SWIPE) */
1725 #if defined(IPPROTO_NHRP)
1729 #endif /* defined(IPPROTO_NHRP) */
1731 #if defined(IPPROTO_NONE)
1735 #endif /* defined(IPPROTO_NONE) */
1737 #if defined(IPPROTO_DSTOPTS)
1738 case IPPROTO_DSTOPTS:
1741 #endif /* defined(IPPROTO_DSTOPTS) */
1743 #if defined(IPPROTO_AHIP)
1747 #endif /* defined(IPPROTO_AHIP) */
1749 #if defined(IPPROTO_CFTP)
1753 #endif /* defined(IPPROTO_CFTP) */
1755 #if defined(IPPROTO_SATEXPAK)
1756 case IPPROTO_SATEXPAK:
1759 #endif /* defined(IPPROTO_SATEXPAK) */
1761 #if defined(IPPROTO_KRYPTOLAN)
1762 case IPPROTO_KRYPTOLAN:
1765 #endif /* defined(IPPROTO_KRYPTOLAN) */
1767 #if defined(IPPROTO_RVD)
1771 #endif /* defined(IPPROTO_RVD) */
1773 #if defined(IPPROTO_IPPC)
1777 #endif /* defined(IPPROTO_IPPC) */
1779 #if defined(IPPROTO_ADFS)
1783 #endif /* defined(IPPROTO_ADFS) */
1785 #if defined(IPPROTO_SATMON)
1786 case IPPROTO_SATMON:
1789 #endif /* defined(IPPROTO_SATMON) */
1791 #if defined(IPPROTO_VISA)
1795 #endif /* defined(IPPROTO_VISA) */
1797 #if defined(IPPROTO_IPCV)
1801 #endif /* defined(IPPROTO_IPCV) */
1803 #if defined(IPPROTO_CPNX)
1807 #endif /* defined(IPPROTO_CPNX) */
1809 #if defined(IPPROTO_CPHB)
1813 #endif /* defined(IPPROTO_CPHB) */
1815 #if defined(IPPROTO_WSN)
1819 #endif /* defined(IPPROTO_WSN) */
1821 #if defined(IPPROTO_PVP)
1825 #endif /* defined(IPPROTO_PVP) */
1827 #if defined(IPPROTO_BRSATMON)
1828 case IPPROTO_BRSATMON:
1831 #endif /* defined(IPPROTO_BRSATMON) */
1833 #if defined(IPPROTO_WBMON)
1837 #endif /* defined(IPPROTO_WBMON) */
1839 #if defined(IPPROTO_WBEXPAK)
1840 case IPPROTO_WBEXPAK:
1843 #endif /* defined(IPPROTO_WBEXPAK) */
1845 #if defined(IPPROTO_EON)
1849 #endif /* defined(IPPROTO_EON) */
1851 #if defined(IPPROTO_VMTP)
1855 #endif /* defined(IPPROTO_VMTP) */
1857 #if defined(IPPROTO_SVMTP)
1861 #endif /* defined(IPPROTO_SVMTP) */
1863 #if defined(IPPROTO_VINES)
1867 #endif /* defined(IPPROTO_VINES) */
1869 #if defined(IPPROTO_TTP)
1873 #endif /* defined(IPPROTO_TTP) */
1875 #if defined(IPPROTO_IGP)
1879 #endif /* defined(IPPROTO_IGP) */
1881 #if defined(IPPROTO_DGP)
1885 #endif /* defined(IPPROTO_DGP) */
1887 #if defined(IPPROTO_TCF)
1891 #endif /* defined(IPPROTO_TCF) */
1893 #if defined(IPPROTO_IGRP)
1897 #endif /* defined(IPPROTO_IGRP) */
1899 #if defined(IPPROTO_OSPFIGP)
1900 case IPPROTO_OSPFIGP:
1903 #endif /* defined(IPPROTO_OSPFIGP) */
1905 #if defined(IPPROTO_SRPC)
1909 #endif /* defined(IPPROTO_SRPC) */
1911 #if defined(IPPROTO_LARP)
1915 #endif /* defined(IPPROTO_LARP) */
1917 #if defined(IPPROTO_MTP)
1921 #endif /* defined(IPPROTO_MTP) */
1923 #if defined(IPPROTO_AX25)
1927 #endif /* defined(IPPROTO_AX25) */
1929 #if defined(IPPROTO_IPEIP)
1933 #endif /* defined(IPPROTO_IPEIP) */
1935 #if defined(IPPROTO_MICP)
1939 #endif /* defined(IPPROTO_MICP) */
1941 #if defined(IPPROTO_SCCSP)
1945 #endif /* defined(IPPROTO_SCCSP) */
1947 #if defined(IPPROTO_ETHERIP)
1948 case IPPROTO_ETHERIP:
1951 #endif /* defined(IPPROTO_ETHERIP) */
1953 #if defined(IPPROTO_ENCAP)
1954 # if !defined(IPPROTO_IPIP) || IPPROTO_IPIP!=IPPROTO_ENCAP
1958 # endif /* !defined(IPPROTO_IPIP) || IPPROTO_IPIP!=IPPROTO_ENCAP */
1959 #endif /* defined(IPPROTO_ENCAP) */
1961 #if defined(IPPROTO_APES)
1965 #endif /* defined(IPPROTO_APES) */
1967 #if defined(IPPROTO_GMTP)
1971 #endif /* defined(IPPROTO_GMTP) */
1973 #if defined(IPPROTO_DIVERT)
1974 case IPPROTO_DIVERT:
1977 #endif /* defined(IPPROTO_DIVERT) */
1983 (void) snpf(Lf->iproto, sizeof(Lf->iproto), "%.*s", IPROTOL-1, s);
1986 for (i = 0, m = 1; i < IPROTOL-2; i++)
1990 (void) snpf(Lf->iproto, sizeof(Lf->iproto), "%d?", p);
1992 (void) snpf(Lf->iproto, sizeof(Lf->iproto), "*%d?", p % (m/10));
1995 #endif /* !defined(HASPRIVPRIPP) */
1999 * printname() - print output name field
2004 int nl; /* NL status */
2007 #if defined(HASNCACHE)
2008 char buf[MAXPATHLEN];
2011 #endif /* defined(HASNCACHE) */
2015 if (Lf->nm && Lf->nm[0]) {
2018 * Print the name characters, if there are some.
2020 safestrprt(Lf->nm, stdout, 0);
2022 if (!Lf->li[0].af && !Lf->li[1].af)
2025 if (Lf->li[0].af || Lf->li[1].af) {
2029 * If the file has Internet addresses, print them.
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))
2041 * If this is a block or character device and it has a name, print it.
2049 * If this is a common node, print that fact.
2051 (void) fputs("COMMON: ", stdout);
2056 #if defined(HASPRIVNMCACHE)
2057 if (HASPRIVNMCACHE(Lf)) {
2061 #endif /* defined(HASPRIVNMCACHE) */
2066 * Do a deferred local mount info table search for the file system
2067 * (mounted) directory name and inode number, and mounted device name.
2069 for (mp = readmnt(); mp; mp = mp->next) {
2070 if (Lf->dev == mp->dev) {
2071 Lf->fsdir = mp->dir;
2072 Lf->fsdev = mp->fsname;
2074 #if defined(HASFSINO)
2075 Lf->fs_ino = mp->inode;
2076 #endif /* defined(HASFSINO) */
2083 if (Lf->fsdir || Lf->fsdev) {
2086 * Print the file system directory name, device name, and
2087 * possible path name components.
2090 #if !defined(HASNCACHE) || HASNCACHE<2
2092 safestrprt(Lf->fsdir, stdout, 0);
2095 #endif /* !defined(HASNCACHE) || HASNCACHE<2 */
2097 #if defined(HASNCACHE)
2103 # if defined(NCACHELDPFX)
2105 # endif /* defined(NCACHELDPFX) */
2107 (void) ncache_load();
2109 # if defined(NCACHELDSFX)
2111 # endif /* defined(NCACHELDSFX) */
2115 if ((cp = ncache_lookup(buf, sizeof(buf), &fp))) {
2120 if (fp && Lf->fsdir) {
2122 cp1 = strrchr(Lf->fsdir, '/');
2123 if (cp1 == (char *)NULL || *(cp1 + 1) != '\0')
2127 (void) fputs(" -- ", stdout);
2128 safestrprt(cp, stdout, 0);
2133 # else /* HASNCACHE>1 */
2136 # if defined(NCACHELDPFX)
2138 # endif /* defined(NCACHELDPFX) */
2140 (void) ncache_load();
2142 # if defined(NCACHELDSFX)
2144 # endif /* defined(NCACHELDSFX) */
2148 if ((cp = ncache_lookup(buf, sizeof(buf), &fp))) {
2150 safestrprt(cp, stdout, 0);
2154 safestrprt(Lf->fsdir, stdout, 0);
2158 (void) fputs(" -- ", stdout);
2159 safestrprt(cp, stdout, 0);
2166 safestrprt(Lf->fsdir, stdout, 0);
2169 # endif /* HASNCACHE<2 */
2170 #endif /* defined(HASNCACHE) */
2174 (void) fputs(" (", stdout);
2176 (void) putchar('(');
2177 safestrprt(Lf->fsdev, stdout, 0);
2178 (void) putchar(')');
2183 * Print the NAME column addition, if there is one. If there isn't
2184 * make sure a NL is printed, as requested.
2192 safestrprt(Lf->nma, stdout, 0);
2196 * If this file has TCP/IP state information, print it.
2198 if (!Ffield && Ftcptpi
2199 && (Lf->lts.type >= 0
2201 #if defined(HASTCPTPIQ)
2202 || ((Ftcptpi & TCPTPI_QUEUES) && (Lf->lts.rqs || Lf->lts.sqs))
2203 #endif /* defined(HASTCPTPIQ) */
2205 #if defined(HASTCPTPIW)
2206 || ((Ftcptpi & TCPTPI_WINDOWS) && (Lf->lts.rws || Lf->lts.wws))
2207 #endif /* defined(HASTCPTPIW) */
2212 (void) print_tcptpi(0);
2220 * printrawaddr() - print raw socket address
2225 struct sockaddr *sa; /* socket address */
2231 (void) snpf(ep, sz, "%u/%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
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]);
2251 * printsockty() - print socket type
2256 int ty; /* socket type -- e.g., from so_type */
2258 static char buf[64];
2263 #if defined(SOCK_STREAM)
2267 #endif /* defined(SOCK_STREAM) */
2269 #if defined(SOCK_STREAM)
2273 #endif /* defined(SOCK_DGRAM) */
2275 #if defined(SOCK_RAW)
2279 #endif /* defined(SOCK_RAW) */
2281 #if defined(SOCK_RDM)
2285 #endif /* defined(SOCK_RDM) */
2287 #if defined(SOCK_SEQPACKET)
2288 case SOCK_SEQPACKET:
2291 #endif /* defined(SOCK_SEQPACKET) */
2294 (void) snpf(buf, sizeof(buf), "SOCK_%#x", ty);
2297 (void) snpf(buf, sizeof(buf), "SOCK_%s", cp);
2303 * printuid() - print User ID or login name
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 */
2317 static struct stat sbs;
2318 static struct uidcache {
2321 struct uidcache *next;
2322 } **uc = (struct uidcache **)NULL;
2323 struct uidcache *up, *upn;
2324 static char user[USERPRTL+1];
2330 * Get the mtime and ctime of /etc/passwd, as required.
2332 if (stat("/etc/passwd", &sb) != 0) {
2333 (void) fprintf(stderr, "%s: can't stat(/etc/passwd): %s\n",
2334 Pn, strerror(errno));
2339 * Define the UID cache, if necessary.
2342 if (!(uc = (struct uidcache **)calloc(UIDCACHEL,
2343 sizeof(struct uidcache *))))
2345 (void) fprintf(stderr,
2346 "%s: no space for %d byte UID cache hash buckets\n",
2347 Pn, (int)(UIDCACHEL * (sizeof(struct uidcache *))));
2356 * If it's time to check /etc/passwd and if its the mtime/ctime has
2357 * changed, destroy the existing UID cache.
2360 if (sbs.st_mtime != sb.st_mtime || sbs.st_ctime != sb.st_ctime)
2362 for (i = 0; i < UIDCACHEL; i++) {
2366 (void) free((FREE_P *)up);
2367 } while ((up = upn) != (struct uidcache *)NULL);
2368 uc[i] = (struct uidcache *)NULL;
2376 * Search the UID cache.
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) {
2387 * The UID is not in the cache.
2389 * Look up the login name from the UID for a new cache entry.
2391 if (!(pw = getpwuid((uid_t)uid))) {
2393 (void) fprintf(stderr, "%s: no pwd entry for UID %lu\n",
2394 Pn, (unsigned long)uid);
2399 * Allocate and fill a new cache entry. Link it to its hash bucket.
2401 if (!(upn = (struct uidcache *)malloc(sizeof(struct uidcache))))
2403 (void) fprintf(stderr,
2404 "%s: no space for UID cache entry for: %lu, %s)\n",
2405 Pn, (unsigned long)uid, pw->pw_name);
2408 (void) strncpy(upn->nm, pw->pw_name, LOGINML);
2409 upn->nm[LOGINML] = '\0';
2410 upn->uid = (uid_t)uid;
2419 * Produce a numeric conversion of the UID.
2421 (void) snpf(user, sizeof(user), "%*lu", USERPRTL, (unsigned long)uid);
2429 * printunkaf() - print unknown address family
2434 int fam; /* unknown address family */
2435 int ty; /* output type: 0 = terse; 1 = full */
2442 #if defined(AF_UNSPEC)
2446 #endif /* defined(AF_UNSPEC) */
2448 #if defined(AF_UNIX)
2452 #endif /* defined(AF_UNIX) */
2454 #if defined(AF_INET)
2458 #endif /* defined(AF_INET) */
2460 #if defined(AF_INET6)
2464 #endif /* defined(AF_INET6) */
2466 #if defined(AF_IMPLINK)
2470 #endif /* defined(AF_IMPLINK) */
2476 #endif /* defined(AF_PUP) */
2478 #if defined(AF_CHAOS)
2482 #endif /* defined(AF_CHAOS) */
2488 #endif /* defined(AF_NS) */
2494 #endif /* defined(AF_ISO) */
2497 # if !defined(AF_ISO) || AF_NBS!=AF_ISO
2501 # endif /* !defined(AF_ISO) || AF_NBS!=AF_ISO */
2502 #endif /* defined(AF_NBS) */
2504 #if defined(AF_ECMA)
2508 #endif /* defined(AF_ECMA) */
2510 #if defined(AF_DATAKIT)
2514 #endif /* defined(AF_DATAKIT) */
2516 #if defined(AF_CCITT)
2520 #endif /* defined(AF_CCITT) */
2526 #endif /* defined(AF_SNA) */
2528 #if defined(AF_DECnet)
2532 #endif /* defined(AF_DECnet) */
2538 #endif /* defined(AF_DLI) */
2544 #endif /* defined(AF_LAT) */
2546 #if defined(AF_HYLINK)
2550 #endif /* defined(AF_HYLINK) */
2552 #if defined(AF_APPLETALK)
2556 #endif /* defined(AF_APPLETALK) */
2562 #endif /* defined(AF_BSC) */
2568 #endif /* defined(AF_DSS) */
2570 #if defined(AF_ROUTE)
2574 #endif /* defined(AF_ROUTE) */
2580 #endif /* defined(AF_RAW) */
2582 #if defined(AF_LINK)
2586 #endif /* defined(AF_LINK) */
2588 #if defined(pseudo_AF_XTP)
2593 #endif /* defined(pseudo_AF_XTP) */
2599 #endif /* defined(AF_RMP) */
2601 #if defined(AF_COIP)
2605 #endif /* defined(AF_COIP) */
2611 #endif /* defined(AF_CNT) */
2613 #if defined(pseudo_AF_RTIP)
2614 case pseudo_AF_RTIP:
2618 #endif /* defined(pseudo_AF_RTIP) */
2620 #if defined(AF_NETMAN)
2624 #endif /* defined(AF_NETMAN) */
2626 #if defined(AF_INTF)
2630 #endif /* defined(AF_INTF) */
2632 #if defined(AF_NETWARE)
2636 #endif /* defined(AF_NETWARE) */
2642 #endif /* defined(AF_NDD) */
2645 # if !defined(AF_ROUTE) || AF_ROUTE!=AF_NIT
2649 # endif /* !defined(AF_ROUTE) || AF_ROUTE!=AF_NIT */
2650 #endif /* defined(AF_NIT) */
2653 # if !defined(AF_RAW) || AF_RAW!=AF_802
2657 # endif /* !defined(AF_RAW) || AF_RAW!=AF_802 */
2658 #endif /* defined(AF_802) */
2664 #endif /* defined(AF_X25) */
2670 #endif /* defined(AF_CTF) */
2676 #endif /* defined(AF_WAN) */
2678 #if defined(AF_OSINET)
2679 # if defined(AF_INET) && AF_INET!=AF_OSINET
2683 # endif /* defined(AF_INET) && AF_INET!=AF_OSINET */
2684 #endif /* defined(AF_OSINET) */
2686 #if defined(AF_GOSIP)
2690 #endif /* defined(AF_GOSIP) */
2696 #endif /* defined(AF_SDL) */
2702 #endif /* defined(AF_IPX) */
2708 #endif /* defined(AF_SIP) */
2710 #if defined(psuedo_AF_PIP)
2715 #endif /* defined(psuedo_AF_PIP) */
2721 #endif /* defined(AF_OTS) */
2723 #if defined(pseudo_AF_BLUE)
2724 case pseudo_AF_BLUE: /* packets for Blue box */
2728 #endif /* defined(pseudo_AF_BLUE) */
2730 #if defined(AF_NDRV) /* network driver raw access */
2734 #endif /* defined(AF_NDRV) */
2736 #if defined(AF_SYSTEM) /* kernel event messages */
2740 #endif /* defined(AF_SYSTEM) */
2742 #if defined(AF_USER)
2746 #endif /* defined(AF_USER) */
2748 #if defined(pseudo_AF_KEY)
2753 #endif /* defined(pseudo_AF_KEY) */
2755 #if defined(AF_KEY) /* Security Association DB socket */
2759 #endif /* defined(AF_KEY) */
2761 #if defined(AF_NCA) /* NCA socket */
2765 #endif /* defined(AF_NCA) */
2767 #if defined(AF_POLICY) /* Security Policy DB socket */
2771 #endif /* defined(AF_POLICY) */
2773 #if defined(AF_PPP) /* PPP socket */
2777 #endif /* defined(AF_PPP) */
2781 (void) snpf(Namech, Namechl, "%#x", fam);
2783 (void) snpf(Namech, Namechl,
2784 "no further information on family %#x", fam);
2788 (void) snpf(Namech, Namechl, "%sAF_%s", p, s);
2790 (void) snpf(Namech, Namechl, "no further information on %sAF_%s",
2796 #if !defined(HASNORPC_H)
2798 * update_portmap() - update a portmap entry with its port number or service
2803 update_portmap(pt, pn)
2804 struct porttab *pt; /* porttab entry */
2805 char *pn; /* port name */
2812 if (!(al = strlen(pn))) {
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);
2823 (void) snpf(cp, nl + 1, "%s[%s]", pn, pt->name);
2824 (void) free((FREE_P *)pt->name);
2829 #endif /* !defined(HASNORPC_H) */