]> arthur.barton.de Git - netatalk.git/blob - bin/nbp/nbplkup.c
Initial revision
[netatalk.git] / bin / nbp / nbplkup.c
1 /*
2  * Copyright (c) 1990,1991 Regents of The University of Michigan.
3  * All Rights Reserved.
4  *
5  * Permission to use, copy, modify, and distribute this software and
6  * its documentation for any purpose and without fee is hereby granted,
7  * provided that the above copyright notice appears in all copies and
8  * that both that copyright notice and this permission notice appear
9  * in supporting documentation, and that the name of The University
10  * of Michigan not be used in advertising or publicity pertaining to
11  * distribution of the software without specific, written prior
12  * permission. This software is supplied as is without expressed or
13  * implied warranties of any kind.
14  *
15  *      Research Systems Unix Group
16  *      The University of Michigan
17  *      c/o Mike Clark
18  *      535 W. William Street
19  *      Ann Arbor, Michigan
20  *      +1-313-763-0525
21  *      netatalk@itd.umich.edu
22  */
23
24 #include <sys/types.h>
25 #include <netatalk/endian.h>
26 #include <netatalk/at.h>
27 #include <atalk/nbp.h>
28 #include <atalk/util.h>
29 #include <string.h>
30 #include <stdio.h>
31 #if !defined( sun ) || !defined( i386 )
32 #include <stdlib.h>
33 #endif sun i386
34
35 char *Obj = "=";
36 char *Type = "=";
37 char *Zone = "*";
38
39 Usage( av0 )
40     char        *av0;
41 {
42     char        *p;
43
44     if (( p = strrchr( av0, '/' )) == 0 ) {
45         p = av0;
46     } else {
47         p++;
48     }
49
50     printf( "Usage:\t%s [ -A address ] [ -r responses] [ obj:type@zone ]\n", p );
51     exit( 1 );
52 }
53
54 main( ac, av )
55     int         ac;
56     char        **av;
57 {
58     struct nbpnve       *nn;
59     char                *name;
60     int                 i, c, nresp = 1000;
61     struct at_addr      addr;
62
63     extern char         *optarg;
64     extern int          optind;
65
66     memset(&addr, 0, sizeof(addr));
67     while (( c = getopt( ac, av, "r:A:" )) != EOF ) {
68         switch ( c ) {
69         case 'A':
70             if (!atalk_aton(optarg, &addr)) {
71                 fprintf(stderr, "Bad address.\n");
72                 exit(1);
73             }
74             break;
75         case 'r' :
76             nresp = atoi( optarg );
77             break;
78
79         default :
80             Usage( av[ 0 ] );
81             exit( 1 );
82         }
83     }
84
85     if (( nn = (struct nbpnve *)malloc( nresp * sizeof( struct nbpnve )))
86             == NULL ) {
87         perror( "malloc" );
88         exit( 1 );
89     }
90
91     if ( ac - optind > 1 ) {
92         Usage( av[ 0 ] );
93         exit( 1 );
94     }
95
96     /*
97      * Get default values from the environment. We need to copy out
98      * the results, here, since nbp_name returns it's parameters
99      * in static space, and we'll clobber them when we call it again
100      * later.
101      */
102     if (( name = getenv( "NBPLKUP" )) != NULL ) {
103         if ( nbp_name( name, &Obj, &Type, &Zone )) {
104             fprintf( stderr,
105                     "Environment variable syntax error: NBPLKUP = %s\n",
106                     name );
107             exit( 1 );
108         }
109
110         if (( name = (char *)malloc( strlen( Obj ) + 1 )) == NULL ) {
111             perror( "malloc" );
112             exit( 1 );
113         }
114         strcpy( name, Obj );
115         Obj = name;
116
117         if (( name = (char *)malloc( strlen( Type ) + 1 )) == NULL ) {
118             perror( "malloc" );
119             exit( 1 );
120         }
121         strcpy( name, Type );
122         Type = name;
123
124         if (( name = (char *)malloc( strlen( Zone ) + 1 )) == NULL ) {
125             perror( "malloc" );
126             exit( 1 );
127         }
128         strcpy( name, Zone );
129         Zone = name;
130
131     }
132
133     if ( ac - optind == 1 ) {
134         if ( nbp_name( av[ optind ], &Obj, &Type, &Zone )) {
135             Usage( av[ 0 ] );
136             exit( 1 );
137         }
138     }
139
140     if (( c = nbp_lookup( Obj, Type, Zone, nn, nresp, &addr)) < 0 ) {
141         perror( "nbp_lookup" );
142         exit( -1 );
143     }
144     for ( i = 0; i < c; i++ ) {
145         printf( "%31.*s:%-34.*s %u.%u:%u\n",
146                 nn[ i ].nn_objlen, nn[ i ].nn_obj,
147                 nn[ i ].nn_typelen, nn[ i ].nn_type,
148                 ntohs( nn[ i ].nn_sat.sat_addr.s_net ),
149                 nn[ i ].nn_sat.sat_addr.s_node,
150                 nn[ i ].nn_sat.sat_port );
151     }
152
153 }