]> arthur.barton.de Git - netatalk.git/blob - bin/nbp/nbplkup.c
second patch from Olaf Hering; gcc 64 warnings.
[netatalk.git] / bin / nbp / nbplkup.c
1 /*
2  * $Id: nbplkup.c,v 1.4.14.2.2.1 2005-01-31 19:50:35 didg 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 #ifdef HAVE_STDLIB_H
38 #include <stdlib.h>
39 #endif
40
41
42 #include <atalk/unicode.h>
43
44 char *Obj = "=";
45 char *Type = "=";
46 char *Zone = "*";
47
48 void Usage( av0 )
49     char        *av0;
50 {
51     char        *p;
52
53     if (( p = strrchr( av0, '/' )) == 0 ) {
54         p = av0;
55     } else {
56         p++;
57     }
58
59     printf( "Usage:\t%s [ -A address ] [ -r responses] [-m Mac charset] [ obj:type@zone ]\n", p );
60     exit( 1 );
61 }
62
63 int main( ac, av )
64     int         ac;
65     char        **av;
66 {
67     struct nbpnve       *nn;
68     char                *name;
69     int                 i, c, nresp = 1000;
70     struct at_addr      addr;
71     char                *obj = NULL;
72     size_t              obj_len;
73     charset_t           chMac = CH_MAC;
74     char *              convname;
75
76     extern char         *optarg;
77     extern int          optind;
78
79     memset(&addr, 0, sizeof(addr));
80     while (( c = getopt( ac, av, "r:A:m:" )) != EOF ) {
81         switch ( c ) {
82         case 'A':
83             if (!atalk_aton(optarg, &addr)) {
84                 fprintf(stderr, "Bad address.\n");
85                 exit(1);
86             }
87             break;
88         case 'r' :
89             nresp = atoi( optarg );
90             break;
91         case 'm':
92             if ((charset_t)-1 == (chMac = add_charset(optarg)) ) {
93                 fprintf(stderr, "Invalid Mac charset.\n");
94                 exit(1);
95             }
96             break;
97
98         default :
99             Usage( av[ 0 ] );
100             exit( 1 );
101         }
102     }
103
104     if (( nn = (struct nbpnve *)malloc( nresp * sizeof( struct nbpnve )))
105             == NULL ) {
106         perror( "malloc" );
107         exit( 1 );
108     }
109
110     if ( ac - optind > 1 ) {
111         Usage( av[ 0 ] );
112         exit( 1 );
113     }
114
115     /*
116      * Get default values from the environment. We need to copy out
117      * the results, here, since nbp_name returns it's parameters
118      * in static space, and we'll clobber them when we call it again
119      * later.
120      */
121     if (( name = getenv( "NBPLKUP" )) != NULL ) {
122         if ( nbp_name( name, &Obj, &Type, &Zone )) {
123             fprintf( stderr,
124                     "Environment variable syntax error: NBPLKUP = %s\n",
125                     name );
126             exit( 1 );
127         }
128
129         if (( name = (char *)malloc( strlen( Obj ) + 1 )) == NULL ) {
130             perror( "malloc" );
131             exit( 1 );
132         }
133         strcpy( name, Obj );
134         Obj = name;
135
136         if (( name = (char *)malloc( strlen( Type ) + 1 )) == NULL ) {
137             perror( "malloc" );
138             exit( 1 );
139         }
140         strcpy( name, Type );
141         Type = name;
142
143         if (( name = (char *)malloc( strlen( Zone ) + 1 )) == NULL ) {
144             perror( "malloc" );
145             exit( 1 );
146         }
147         strcpy( name, Zone );
148         Zone = name;
149
150     }
151
152     if ( ac - optind == 1 ) {
153         if ((size_t)(-1) == convert_string_allocate( CH_UNIX, chMac,
154                            av[ optind ], strlen(av[optind]), &convname))
155             convname = av[ optind ];
156
157         if ( nbp_name( convname, &Obj, &Type, &Zone )) {
158             Usage( av[ 0 ] );
159             exit( 1 );
160         }
161     }
162
163     if (( c = nbp_lookup( Obj, Type, Zone, nn, nresp, &addr)) < 0 ) {
164         perror( "nbp_lookup" );
165         exit( -1 );
166     }
167     for ( i = 0; i < c; i++ ) {
168         
169         if ((size_t)(-1) == (obj_len = convert_string_allocate( chMac, 
170                        CH_UNIX, nn[ i ].nn_obj, nn[ i ].nn_objlen, &obj)) ) {
171             obj_len = nn[ i ].nn_objlen;
172             if (( obj = strdup(nn[ i ].nn_obj)) == NULL ) {
173                 perror( "strdup" );
174                 exit( 1 );
175             }
176         }
177
178         printf( "%31.*s:%-34.*s %u.%u:%u\n",
179                 (int)obj_len, obj,
180                 nn[ i ].nn_typelen, nn[ i ].nn_type,
181                 ntohs( nn[ i ].nn_sat.sat_addr.s_net ),
182                 nn[ i ].nn_sat.sat_addr.s_node,
183                 nn[ i ].nn_sat.sat_port );
184
185         free(obj);
186     }
187
188     free(nn);
189     return 0;
190 }