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