Tue Mar 17 00:53:42 2009 UTC ()
fix sign-compare issues


(lukem)
diff -r1.38 -r1.39 src/sbin/route/show.c

cvs diff -r1.38 -r1.39 src/sbin/route/show.c (switch to unified diff)

--- src/sbin/route/show.c 2008/09/10 01:06:58 1.38
+++ src/sbin/route/show.c 2009/03/17 00:53:42 1.39
@@ -1,408 +1,408 @@ @@ -1,408 +1,408 @@
1/* $NetBSD: show.c,v 1.38 2008/09/10 01:06:58 dyoung Exp $ */ 1/* $NetBSD: show.c,v 1.39 2009/03/17 00:53:42 lukem Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1983, 1988, 1993 4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors 15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software 16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission. 17 * without specific prior written permission.
18 * 18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE. 29 * SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33#ifndef lint 33#ifndef lint
34#if 0 34#if 0
35static char sccsid[] = "from: @(#)route.c 8.3 (Berkeley) 3/9/94"; 35static char sccsid[] = "from: @(#)route.c 8.3 (Berkeley) 3/9/94";
36#else 36#else
37__RCSID("$NetBSD: show.c,v 1.38 2008/09/10 01:06:58 dyoung Exp $"); 37__RCSID("$NetBSD: show.c,v 1.39 2009/03/17 00:53:42 lukem Exp $");
38#endif 38#endif
39#endif /* not lint */ 39#endif /* not lint */
40 40
41#include <sys/param.h> 41#include <sys/param.h>
42#include <sys/protosw.h> 42#include <sys/protosw.h>
43#include <sys/socket.h> 43#include <sys/socket.h>
44#include <sys/mbuf.h> 44#include <sys/mbuf.h>
45 45
46#include <net/if.h> 46#include <net/if.h>
47#include <net/if_dl.h> 47#include <net/if_dl.h>
48#include <net/if_types.h> 48#include <net/if_types.h>
49#include <net/route.h> 49#include <net/route.h>
50#include <netinet/in.h> 50#include <netinet/in.h>
51 51
52#include <sys/sysctl.h> 52#include <sys/sysctl.h>
53 53
54#include <netdb.h> 54#include <netdb.h>
55#include <stdbool.h> 55#include <stdbool.h>
56#include <stdio.h> 56#include <stdio.h>
57#include <stdlib.h> 57#include <stdlib.h>
58#include <string.h> 58#include <string.h>
59#include <unistd.h> 59#include <unistd.h>
60#include <err.h> 60#include <err.h>
61 61
62#include "keywords.h" 62#include "keywords.h"
63#include "extern.h" 63#include "extern.h"
64 64
65#define ROUNDUP(a) \ 65#define ROUNDUP(a) \
66 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 66 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
67#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 67#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
68 68
69/* 69/*
70 * Definitions for showing gateway flags. 70 * Definitions for showing gateway flags.
71 */ 71 */
72struct bits { 72struct bits {
73 short b_mask; 73 short b_mask;
74 char b_val; 74 char b_val;
75}; 75};
76static const struct bits bits[] = { 76static const struct bits bits[] = {
77 { RTF_UP, 'U' }, 77 { RTF_UP, 'U' },
78 { RTF_GATEWAY, 'G' }, 78 { RTF_GATEWAY, 'G' },
79 { RTF_HOST, 'H' }, 79 { RTF_HOST, 'H' },
80 { RTF_REJECT, 'R' }, 80 { RTF_REJECT, 'R' },
81 { RTF_DYNAMIC, 'D' }, 81 { RTF_DYNAMIC, 'D' },
82 { RTF_MODIFIED, 'M' }, 82 { RTF_MODIFIED, 'M' },
83 { RTF_DONE, 'd' }, /* Completed -- for routing messages only */ 83 { RTF_DONE, 'd' }, /* Completed -- for routing messages only */
84 { RTF_MASK, 'm' }, /* Mask Present -- for routing messages only */ 84 { RTF_MASK, 'm' }, /* Mask Present -- for routing messages only */
85 { RTF_CLONING, 'C' }, 85 { RTF_CLONING, 'C' },
86 { RTF_XRESOLVE, 'X' }, 86 { RTF_XRESOLVE, 'X' },
87 { RTF_LLINFO, 'L' }, 87 { RTF_LLINFO, 'L' },
88 { RTF_STATIC, 'S' }, 88 { RTF_STATIC, 'S' },
89 { RTF_BLACKHOLE, 'B' }, 89 { RTF_BLACKHOLE, 'B' },
90 { RTF_CLONED, 'c' }, 90 { RTF_CLONED, 'c' },
91 { RTF_PROTO1, '1' }, 91 { RTF_PROTO1, '1' },
92 { RTF_PROTO2, '2' }, 92 { RTF_PROTO2, '2' },
93 { 0, '\0' } 93 { 0, '\0' }
94}; 94};
95 95
96static void pr_rthdr(int); 96static void pr_rthdr(int);
97static void p_rtentry(struct rt_msghdr *); 97static void p_rtentry(struct rt_msghdr *);
98static void pr_family(int); 98static void pr_family(int);
99static void p_sockaddr(struct sockaddr *, struct sockaddr *, int, int ); 99static void p_sockaddr(struct sockaddr *, struct sockaddr *, int, int );
100static void p_flags(int); 100static void p_flags(int);
101 101
102void 102void
103parse_show_opts(int argc, char * const *argv, int *afp, int *flagsp, 103parse_show_opts(int argc, char * const *argv, int *afp, int *flagsp,
104 const char **afnamep, bool nolink) 104 const char **afnamep, bool nolink)
105{ 105{
106 const char *afname = "unspec"; 106 const char *afname = "unspec";
107 int af, flags; 107 int af, flags;
108 108
109 flags = 0; 109 flags = 0;
110 af = AF_UNSPEC; 110 af = AF_UNSPEC;
111 for (; argc >= 2; argc--) { 111 for (; argc >= 2; argc--) {
112 if (*argv[argc - 1] != '-') 112 if (*argv[argc - 1] != '-')
113 goto bad; 113 goto bad;
114 switch (keyword(argv[argc - 1] + 1)) { 114 switch (keyword(argv[argc - 1] + 1)) {
115 case K_HOST: 115 case K_HOST:
116 flags |= RTF_HOST; 116 flags |= RTF_HOST;
117 break; 117 break;
118 case K_LLINFO: 118 case K_LLINFO:
119 flags |= RTF_LLINFO; 119 flags |= RTF_LLINFO;
120 break; 120 break;
121 case K_INET: 121 case K_INET:
122 af = AF_INET; 122 af = AF_INET;
123 afname = argv[argc - 1] + 1; 123 afname = argv[argc - 1] + 1;
124 break; 124 break;
125#ifdef INET6 125#ifdef INET6
126 case K_INET6: 126 case K_INET6:
127 af = AF_INET6; 127 af = AF_INET6;
128 afname = argv[argc - 1] + 1; 128 afname = argv[argc - 1] + 1;
129 break; 129 break;
130#endif 130#endif
131#ifndef SMALL 131#ifndef SMALL
132 case K_ATALK: 132 case K_ATALK:
133 af = AF_APPLETALK; 133 af = AF_APPLETALK;
134 afname = argv[argc - 1] + 1; 134 afname = argv[argc - 1] + 1;
135 break; 135 break;
136 case K_ISO: 136 case K_ISO:
137 case K_OSI: 137 case K_OSI:
138 af = AF_ISO; 138 af = AF_ISO;
139 afname = argv[argc - 1] + 1; 139 afname = argv[argc - 1] + 1;
140 break; 140 break;
141#endif /* SMALL */ 141#endif /* SMALL */
142 case K_LINK: 142 case K_LINK:
143 if (nolink) 143 if (nolink)
144 goto bad; 144 goto bad;
145 af = AF_LINK; 145 af = AF_LINK;
146 afname = argv[argc - 1] + 1; 146 afname = argv[argc - 1] + 1;
147 break; 147 break;
148 default: 148 default:
149 goto bad; 149 goto bad;
150 } 150 }
151 } 151 }
152 switch (argc) { 152 switch (argc) {
153 case 1: 153 case 1:
154 case 0: 154 case 0:
155 break; 155 break;
156 default: 156 default:
157 bad: 157 bad:
158 usage(argv[argc - 1]); 158 usage(argv[argc - 1]);
159 } 159 }
160 if (afnamep != NULL) 160 if (afnamep != NULL)
161 *afnamep = afname; 161 *afnamep = afname;
162 *afp = af; 162 *afp = af;
163 *flagsp = flags; 163 *flagsp = flags;
164} 164}
165 165
166/* 166/*
167 * Print routing tables. 167 * Print routing tables.
168 */ 168 */
169void 169void
170show(int argc, char *const *argv) 170show(int argc, char *const *argv)
171{ 171{
172 size_t needed; 172 size_t needed;
173 int af, flags, mib[6]; 173 int af, flags, mib[6];
174 char *buf, *next, *lim; 174 char *buf, *next, *lim;
175 struct rt_msghdr *rtm; 175 struct rt_msghdr *rtm;
176 struct sockaddr *sa; 176 struct sockaddr *sa;
177 177
178 parse_show_opts(argc, argv, &af, &flags, NULL, true); 178 parse_show_opts(argc, argv, &af, &flags, NULL, true);
179 mib[0] = CTL_NET; 179 mib[0] = CTL_NET;
180 mib[1] = PF_ROUTE; 180 mib[1] = PF_ROUTE;
181 mib[2] = 0; 181 mib[2] = 0;
182 mib[3] = 0; 182 mib[3] = 0;
183 mib[4] = NET_RT_DUMP; 183 mib[4] = NET_RT_DUMP;
184 mib[5] = 0; 184 mib[5] = 0;
185 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 185 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
186 err(EXIT_FAILURE, "route-sysctl-estimate"); 186 err(EXIT_FAILURE, "route-sysctl-estimate");
187 buf = lim = NULL; 187 buf = lim = NULL;
188 if (needed) { 188 if (needed) {
189 if ((buf = malloc(needed)) == 0) 189 if ((buf = malloc(needed)) == 0)
190 err(EXIT_FAILURE, "malloc"); 190 err(EXIT_FAILURE, "malloc");
191 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 191 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
192 err(EXIT_FAILURE, "sysctl of routing table"); 192 err(EXIT_FAILURE, "sysctl of routing table");
193 lim = buf + needed; 193 lim = buf + needed;
194 } 194 }
195 195
196 printf("Routing table%s\n", (af == AF_UNSPEC)? "s" : ""); 196 printf("Routing table%s\n", (af == AF_UNSPEC)? "s" : "");
197 197
198 if (needed) { 198 if (needed) {
199 for (next = buf; next < lim; next += rtm->rtm_msglen) { 199 for (next = buf; next < lim; next += rtm->rtm_msglen) {
200 rtm = (struct rt_msghdr *)next; 200 rtm = (struct rt_msghdr *)next;
201 sa = (struct sockaddr *)(rtm + 1); 201 sa = (struct sockaddr *)(rtm + 1);
202 if ((rtm->rtm_flags & flags) != flags) 202 if ((rtm->rtm_flags & flags) != flags)
203 continue; 203 continue;
204 if (af == AF_UNSPEC || af == sa->sa_family) 204 if (af == AF_UNSPEC || af == sa->sa_family)
205 p_rtentry(rtm); 205 p_rtentry(rtm);
206 } 206 }
207 free(buf); 207 free(buf);
208 } 208 }
209} 209}
210 210
211 211
212/* column widths; each followed by one space */ 212/* column widths; each followed by one space */
213#ifndef INET6 213#ifndef INET6
214#define WID_DST(af) 18 /* width of destination column */ 214#define WID_DST(af) 18 /* width of destination column */
215#define WID_GW(af) 18 /* width of gateway column */ 215#define WID_GW(af) 18 /* width of gateway column */
216#else 216#else
217/* width of destination/gateway column */ 217/* width of destination/gateway column */
218#if 1 218#if 1
219/* strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4 */ 219/* strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4 */
220#define WID_DST(af) ((af) == AF_INET6 ? (nflag ? 34 : 18) : 18) 220#define WID_DST(af) ((af) == AF_INET6 ? (nflag ? 34 : 18) : 18)
221#define WID_GW(af) ((af) == AF_INET6 ? (nflag ? 30 : 18) : 18) 221#define WID_GW(af) ((af) == AF_INET6 ? (nflag ? 30 : 18) : 18)
222#else 222#else
223/* strlen("fe80::aaaa:bbbb:cccc:dddd") == 25, strlen("/128") == 4 */ 223/* strlen("fe80::aaaa:bbbb:cccc:dddd") == 25, strlen("/128") == 4 */
224#define WID_DST(af) ((af) == AF_INET6 ? (nflag ? 29 : 18) : 18) 224#define WID_DST(af) ((af) == AF_INET6 ? (nflag ? 29 : 18) : 18)
225#define WID_GW(af) ((af) == AF_INET6 ? (nflag ? 25 : 18) : 18) 225#define WID_GW(af) ((af) == AF_INET6 ? (nflag ? 25 : 18) : 18)
226#endif 226#endif
227#endif /* INET6 */ 227#endif /* INET6 */
228 228
229/* 229/*
230 * Print header for routing table columns. 230 * Print header for routing table columns.
231 */ 231 */
232static void 232static void
233pr_rthdr(int af) 233pr_rthdr(int af)
234{ 234{
235 235
236 printf("%-*.*s %-*.*s %-6.6s\n", 236 printf("%-*.*s %-*.*s %-6.6s\n",
237 WID_DST(af), WID_DST(af), "Destination", 237 WID_DST(af), WID_DST(af), "Destination",
238 WID_GW(af), WID_GW(af), "Gateway", 238 WID_GW(af), WID_GW(af), "Gateway",
239 "Flags"); 239 "Flags");
240} 240}
241 241
242 242
243/* 243/*
244 * Print a routing table entry. 244 * Print a routing table entry.
245 */ 245 */
246static void 246static void
247p_rtentry(struct rt_msghdr *rtm) 247p_rtentry(struct rt_msghdr *rtm)
248{ 248{
249 struct sockaddr *sa = (struct sockaddr *)(rtm + 1); 249 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
250#ifdef notdef 250#ifdef notdef
251 static int masks_done, banner_printed; 251 static int masks_done, banner_printed;
252#endif 252#endif
253 static int old_af; 253 static int old_af;
254 int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST | 254 int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST |
255 RTF_REJECT | RTF_LLINFO; 255 RTF_REJECT | RTF_LLINFO;
256 256
257#ifdef notdef 257#ifdef notdef
258 /* for the moment, netmasks are skipped over */ 258 /* for the moment, netmasks are skipped over */
259 if (!banner_printed) { 259 if (!banner_printed) {
260 printf("Netmasks:\n"); 260 printf("Netmasks:\n");
261 banner_printed = 1; 261 banner_printed = 1;
262 } 262 }
263 if (masks_done == 0) { 263 if (masks_done == 0) {
264 if (rtm->rtm_addrs != RTA_DST ) { 264 if (rtm->rtm_addrs != RTA_DST ) {
265 masks_done = 1; 265 masks_done = 1;
266 af = sa->sa_family; 266 af = sa->sa_family;
267 } 267 }
268 } else 268 } else
269#endif 269#endif
270 af = sa->sa_family; 270 af = sa->sa_family;
271 if (old_af != af) { 271 if (old_af != af) {
272 old_af = af; 272 old_af = af;
273 pr_family(af); 273 pr_family(af);
274 pr_rthdr(af); 274 pr_rthdr(af);
275 } 275 }
276 if (rtm->rtm_addrs == RTA_DST) 276 if (rtm->rtm_addrs == RTA_DST)
277 p_sockaddr(sa, NULL, 0, WID_DST(af) + 1 + WID_GW(af) + 1); 277 p_sockaddr(sa, NULL, 0, WID_DST(af) + 1 + WID_GW(af) + 1);
278 else { 278 else {
279 struct sockaddr *nm; 279 struct sockaddr *nm;
280 280
281 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) 281 if ((rtm->rtm_addrs & RTA_NETMASK) == 0)
282 nm = NULL; 282 nm = NULL;
283 else { 283 else {
284 /* skip to gateway */ 284 /* skip to gateway */
285 nm = (struct sockaddr *) 285 nm = (struct sockaddr *)
286 (ROUNDUP(sa->sa_len) + (char *)sa); 286 (ROUNDUP(sa->sa_len) + (char *)sa);
287 /* skip over gateway to netmask */ 287 /* skip over gateway to netmask */
288 nm = (struct sockaddr *) 288 nm = (struct sockaddr *)
289 (ROUNDUP(nm->sa_len) + (char *)nm); 289 (ROUNDUP(nm->sa_len) + (char *)nm);
290 } 290 }
291 291
292 p_sockaddr(sa, nm, rtm->rtm_flags, WID_DST(af)); 292 p_sockaddr(sa, nm, rtm->rtm_flags, WID_DST(af));
293 sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa); 293 sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
294 p_sockaddr(sa, NULL, 0, WID_GW(af)); 294 p_sockaddr(sa, NULL, 0, WID_GW(af));
295 } 295 }
296 p_flags(rtm->rtm_flags & interesting); 296 p_flags(rtm->rtm_flags & interesting);
297 putchar('\n'); 297 putchar('\n');
298} 298}
299 299
300 300
301/* 301/*
302 * Print address family header before a section of the routing table. 302 * Print address family header before a section of the routing table.
303 */ 303 */
304static void 304static void
305pr_family(int af) 305pr_family(int af)
306{ 306{
307 const char *afname; 307 const char *afname;
308 308
309 switch (af) { 309 switch (af) {
310 case AF_INET: 310 case AF_INET:
311 afname = "Internet"; 311 afname = "Internet";
312 break; 312 break;
313#ifdef INET6 313#ifdef INET6
314 case AF_INET6: 314 case AF_INET6:
315 afname = "Internet6"; 315 afname = "Internet6";
316 break; 316 break;
317#endif /* INET6 */ 317#endif /* INET6 */
318#ifndef SMALL 318#ifndef SMALL
319 case AF_ISO: 319 case AF_ISO:
320 afname = "ISO"; 320 afname = "ISO";
321 break; 321 break;
322#endif /* SMALL */ 322#endif /* SMALL */
323 case AF_APPLETALK: 323 case AF_APPLETALK:
324 afname = "AppleTalk"; 324 afname = "AppleTalk";
325 break; 325 break;
326 default: 326 default:
327 afname = NULL; 327 afname = NULL;
328 break; 328 break;
329 } 329 }
330 if (afname) 330 if (afname)
331 printf("\n%s:\n", afname); 331 printf("\n%s:\n", afname);
332 else 332 else
333 printf("\nProtocol Family %d:\n", af); 333 printf("\nProtocol Family %d:\n", af);
334} 334}
335 335
336 336
337static void 337static void
338p_sockaddr(struct sockaddr *sa, struct sockaddr *nm, int flags, int width) 338p_sockaddr(struct sockaddr *sa, struct sockaddr *nm, int flags, int width)
339{ 339{
340 char workbuf[128]; 340 char workbuf[128];
341 const char *cp; 341 const char *cp;
342 342
343 switch(sa->sa_family) { 343 switch(sa->sa_family) {
344 344
345 case AF_LINK: 345 case AF_LINK:
346 if (getnameinfo(sa, sa->sa_len, workbuf, sizeof(workbuf), 346 if (getnameinfo(sa, sa->sa_len, workbuf, sizeof(workbuf),
347 NULL, 0, NI_NUMERICHOST) != 0) 347 NULL, 0, NI_NUMERICHOST) != 0)
348 strlcpy(workbuf, "invalid", sizeof(workbuf)); 348 strlcpy(workbuf, "invalid", sizeof(workbuf));
349 cp = workbuf; 349 cp = workbuf;
350 break; 350 break;
351 351
352 case AF_INET: 352 case AF_INET:
353 cp = routename(sa, nm, flags); 353 cp = routename(sa, nm, flags);
354 break; 354 break;
355 355
356#ifdef INET6 356#ifdef INET6
357 case AF_INET6: 357 case AF_INET6:
358 cp = routename(sa, nm, flags); 358 cp = routename(sa, nm, flags);
359 /* make sure numeric address is not truncated */ 359 /* make sure numeric address is not truncated */
360 if (strchr(cp, ':') != NULL && strlen(cp) > width) 360 if (strchr(cp, ':') != NULL && (int)strlen(cp) > width)
361 width = strlen(cp); 361 width = strlen(cp);
362 break; 362 break;
363#endif /* INET6 */ 363#endif /* INET6 */
364 364
365#ifndef SMALL 365#ifndef SMALL
366#endif /* SMALL */ 366#endif /* SMALL */
367 367
368 default: 368 default:
369 { 369 {
370 u_char *s = (u_char *)sa->sa_data, *slim; 370 u_char *s = (u_char *)sa->sa_data, *slim;
371 char *wp = workbuf, *wplim; 371 char *wp = workbuf, *wplim;
372 372
373 slim = sa->sa_len + (u_char *)sa; 373 slim = sa->sa_len + (u_char *)sa;
374 wplim = wp + sizeof(workbuf) - 6; 374 wplim = wp + sizeof(workbuf) - 6;
375 wp += snprintf(wp, wplim - wp, "(%d)", sa->sa_family); 375 wp += snprintf(wp, wplim - wp, "(%d)", sa->sa_family);
376 while (s < slim && wp < wplim) { 376 while (s < slim && wp < wplim) {
377 wp += snprintf(wp, wplim - wp, " %02x", *s++); 377 wp += snprintf(wp, wplim - wp, " %02x", *s++);
378 if (s < slim) 378 if (s < slim)
379 wp += snprintf(wp, wplim - wp, "%02x", *s++); 379 wp += snprintf(wp, wplim - wp, "%02x", *s++);
380 } 380 }
381 cp = workbuf; 381 cp = workbuf;
382 } 382 }
383 } 383 }
384 if (width < 0 ) 384 if (width < 0 )
385 printf("%s ", cp); 385 printf("%s ", cp);
386 else { 386 else {
387 if (nflag) 387 if (nflag)
388 printf("%-*s ", width, cp); 388 printf("%-*s ", width, cp);
389 else 389 else
390 printf("%-*.*s ", width, width, cp); 390 printf("%-*.*s ", width, width, cp);
391 } 391 }
392} 392}
393 393
394static void 394static void
395p_flags(int f) 395p_flags(int f)
396{ 396{
397 char name[33], *flags; 397 char name[33], *flags;
398 const struct bits *p = bits; 398 const struct bits *p = bits;
399 399
400 for (flags = name; p->b_mask; p++) 400 for (flags = name; p->b_mask; p++)
401 if (p->b_mask & f) 401 if (p->b_mask & f)
402 *flags++ = p->b_val; 402 *flags++ = p->b_val;
403 else if (Sflag) 403 else if (Sflag)
404 *flags++ = ' '; 404 *flags++ = ' ';
405 *flags = '\0'; 405 *flags = '\0';
406 printf("%-6.6s ", name); 406 printf("%-6.6s ", name);
407} 407}
408 408