xref: /onnv-gate/usr/src/cmd/ypcmd/multi.awk.sh (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!/usr/bin/nawk -f
2*0Sstevel@tonic-gate#
3*0Sstevel@tonic-gate# CDDL HEADER START
4*0Sstevel@tonic-gate#
5*0Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6*0Sstevel@tonic-gate# Common Development and Distribution License, Version 1.0 only
7*0Sstevel@tonic-gate# (the "License").  You may not use this file except in compliance
8*0Sstevel@tonic-gate# with the License.
9*0Sstevel@tonic-gate#
10*0Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11*0Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
12*0Sstevel@tonic-gate# See the License for the specific language governing permissions
13*0Sstevel@tonic-gate# and limitations under the License.
14*0Sstevel@tonic-gate#
15*0Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
16*0Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17*0Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
18*0Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
19*0Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
20*0Sstevel@tonic-gate#
21*0Sstevel@tonic-gate# CDDL HEADER END
22*0Sstevel@tonic-gate#
23*0Sstevel@tonic-gate#
24*0Sstevel@tonic-gate#ident	"%Z%%M%	%I%	%E% SMI"
25*0Sstevel@tonic-gate#
26*0Sstevel@tonic-gate# Copyright (c) 1996, by Sun Microsystems, Inc.
27*0Sstevel@tonic-gate# All rights reserved.
28*0Sstevel@tonic-gate#
29*0Sstevel@tonic-gate# Awk code to handle the creation of the YP_MULTI_ entries
30*0Sstevel@tonic-gate# in the hosts.byname map.  Called by multi directly.
31*0Sstevel@tonic-gate#
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate{
34*0Sstevel@tonic-gate    # Here we loop through the list of hostnames
35*0Sstevel@tonic-gate    # doing two separate things...
36*0Sstevel@tonic-gate    # First, we're building a list of hostnames
37*0Sstevel@tonic-gate    # for the current IP address ($1).
38*0Sstevel@tonic-gate    # Second, if we've seen a name before then
39*0Sstevel@tonic-gate    # we add the current address ($1) to a list
40*0Sstevel@tonic-gate    # of address associated with this particular
41*0Sstevel@tonic-gate    # name ($i).
42*0Sstevel@tonic-gate    #
43*0Sstevel@tonic-gate    # Note, that we're pretty careful about keeping
44*0Sstevel@tonic-gate    # out duplicates (and this has a cost).
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate    for (i = 2; i <= NF; i++) {
47*0Sstevel@tonic-gate	# Make the namelist for this address
48*0Sstevel@tonic-gate	if (namelist[$1] == "") {
49*0Sstevel@tonic-gate	    namelist[$1] = $i;
50*0Sstevel@tonic-gate	} else if (namelist[$1] == $i) {
51*0Sstevel@tonic-gate	    ;
52*0Sstevel@tonic-gate	} else if (index(namelist[$1], $i) == 0) {
53*0Sstevel@tonic-gate	    namelist[$1] = namelist[$1] " " $i;
54*0Sstevel@tonic-gate	} else {
55*0Sstevel@tonic-gate	    nf = 1;
56*0Sstevel@tonic-gate	    numnames = split(namelist[$1], n);
57*0Sstevel@tonic-gate	    for (j = 1; j <= numnames; j++) {
58*0Sstevel@tonic-gate		if (n[j] == $i) {
59*0Sstevel@tonic-gate		    nf = 0;
60*0Sstevel@tonic-gate		    break;
61*0Sstevel@tonic-gate		}
62*0Sstevel@tonic-gate	    }
63*0Sstevel@tonic-gate	    if (nf) {
64*0Sstevel@tonic-gate		namelist[$1] = namelist[$1] " " $i;
65*0Sstevel@tonic-gate		nf = 0;
66*0Sstevel@tonic-gate	    }
67*0Sstevel@tonic-gate	}
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate	# Do we have an address for this name?
70*0Sstevel@tonic-gate        # If not, and it's not already there, add it.
71*0Sstevel@tonic-gate	if (addr[$i] == "") {
72*0Sstevel@tonic-gate	    addr[$i] = $1;
73*0Sstevel@tonic-gate	} else if (index(addr[$i], $1) == 0) {
74*0Sstevel@tonic-gate	    addr[$i] = addr[$i] "," $1
75*0Sstevel@tonic-gate	}
76*0Sstevel@tonic-gate    }
77*0Sstevel@tonic-gate}
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gateEND {
80*0Sstevel@tonic-gate    # There are now a bunch o addresses in the addr
81*0Sstevel@tonic-gate    # array that are actually lists.  We go through
82*0Sstevel@tonic-gate    # all of them here and build a list of hostname
83*0Sstevel@tonic-gate    # aliases into the namelist array.
84*0Sstevel@tonic-gate    #
85*0Sstevel@tonic-gate    for (host in addr) {
86*0Sstevel@tonic-gate	if (index(addr[host], ",") == 0)
87*0Sstevel@tonic-gate	    continue;
88*0Sstevel@tonic-gate	numaddr = split(addr[host], tmpaddr, ",");
89*0Sstevel@tonic-gate	for (i = 1; i <= numaddr; i++) {
90*0Sstevel@tonic-gate	    numnames = split(namelist[tmpaddr[i]], tmpname);
91*0Sstevel@tonic-gate	    for (j = 1; j <= numnames; j++) {
92*0Sstevel@tonic-gate		if (namelist[addr[host]] == "") {
93*0Sstevel@tonic-gate		    namelist[addr[host]] = tmpname[j];
94*0Sstevel@tonic-gate		    continue;
95*0Sstevel@tonic-gate		}
96*0Sstevel@tonic-gate		if (namelist[addr[host]] == tmpname[j]) {
97*0Sstevel@tonic-gate		    continue;
98*0Sstevel@tonic-gate		}
99*0Sstevel@tonic-gate		if (index(namelist[addr[host]], tmpname[j]) == 0) {
100*0Sstevel@tonic-gate		    namelist[addr[host]] = namelist[addr[host]] " " tmpname[j];
101*0Sstevel@tonic-gate		    continue;
102*0Sstevel@tonic-gate		} else {
103*0Sstevel@tonic-gate		    nf = 1;
104*0Sstevel@tonic-gate		    for (k = 1; k <= numnames; k++) {
105*0Sstevel@tonic-gate			if (tmpname[j] == tmpname[k]) {
106*0Sstevel@tonic-gate			    nf = 0;
107*0Sstevel@tonic-gate			    break;
108*0Sstevel@tonic-gate			}
109*0Sstevel@tonic-gate		    }
110*0Sstevel@tonic-gate		    if (nf == 1) {
111*0Sstevel@tonic-gate			namelist[addr[host]] = namelist[addr[host]] " " tmpname[j];
112*0Sstevel@tonic-gate			nf = 1;
113*0Sstevel@tonic-gate		    }
114*0Sstevel@tonic-gate		}
115*0Sstevel@tonic-gate	    }
116*0Sstevel@tonic-gate	}
117*0Sstevel@tonic-gate    }
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gate    # Now do that funky output thang...
120*0Sstevel@tonic-gate    for (host in addr) {
121*0Sstevel@tonic-gate	if (index(addr[host], ",")) {
122*0Sstevel@tonic-gate	    printf("YP_MULTI_");
123*0Sstevel@tonic-gate	}
124*0Sstevel@tonic-gate	printf("%s %s\t%s\n",
125*0Sstevel@tonic-gate	       host, addr[host], namelist[addr[host]]);
126*0Sstevel@tonic-gate    }
127*0Sstevel@tonic-gate}
128