|
Line 0
Link Here
|
|
|
1 |
/* |
| 2 |
* Copyright 2001 The Aerospace Corporation. All rights reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions |
| 6 |
* are met: |
| 7 |
* 1. Redistributions of source code must retain the above copyright |
| 8 |
* notice, this list of conditions and the following disclaimer. |
| 9 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer in the |
| 11 |
* documentation and/or other materials provided with the distribution. |
| 12 |
* |
| 13 |
* THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION ``AS IS'' AND |
| 14 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 16 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE |
| 17 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 18 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 19 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 20 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 21 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 22 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 23 |
* SUCH DAMAGE. |
| 24 |
*/ |
| 25 |
|
| 26 |
/*- |
| 27 |
* Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc. |
| 28 |
* All rights reserved. |
| 29 |
* |
| 30 |
* This code is derived from software contributed to The NetBSD Foundation |
| 31 |
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, |
| 32 |
* NASA Ames Research Center. |
| 33 |
* |
| 34 |
* Redistribution and use in source and binary forms, with or without |
| 35 |
* modification, are permitted provided that the following conditions |
| 36 |
* are met: |
| 37 |
* 1. Redistributions of source code must retain the above copyright |
| 38 |
* notice, this list of conditions and the following disclaimer. |
| 39 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 40 |
* notice, this list of conditions and the following disclaimer in the |
| 41 |
* documentation and/or other materials provided with the distribution. |
| 42 |
* 3. All advertising materials mentioning features or use of this software |
| 43 |
* must display the following acknowledgement: |
| 44 |
* This product includes software developed by the NetBSD |
| 45 |
* Foundation, Inc. and its contributors. |
| 46 |
* 4. Neither the name of The NetBSD Foundation nor the names of its |
| 47 |
* contributors may be used to endorse or promote products derived |
| 48 |
* from this software without specific prior written permission. |
| 49 |
* |
| 50 |
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
| 51 |
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 52 |
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 53 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
| 54 |
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 55 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 56 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 57 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 58 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 59 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 60 |
* POSSIBILITY OF SUCH DAMAGE. |
| 61 |
*/ |
| 62 |
|
| 63 |
#include <sys/param.h> |
| 64 |
#include <sys/ioctl.h> |
| 65 |
#include <sys/socket.h> |
| 66 |
#include <sys/sysctl.h> |
| 67 |
#include <sys/time.h> |
| 68 |
|
| 69 |
#include <net/ethernet.h> |
| 70 |
#include <net/if.h> |
| 71 |
#include <net/if_dl.h> |
| 72 |
#include <net/if_types.h> |
| 73 |
#include <net/route.h> |
| 74 |
#include <net/if_ieee80211.h> |
| 75 |
|
| 76 |
#include <ctype.h> |
| 77 |
#include <err.h> |
| 78 |
#include <errno.h> |
| 79 |
#include <fcntl.h> |
| 80 |
#include <stdio.h> |
| 81 |
#include <stdlib.h> |
| 82 |
#include <string.h> |
| 83 |
#include <unistd.h> |
| 84 |
|
| 85 |
#include "ifconfig.h" |
| 86 |
|
| 87 |
static void set80211(int s, int type, int val, int len, u_int8_t *data); |
| 88 |
static const char *get_string(const char *val, const char *sep, |
| 89 |
u_int8_t *buf, int *lenp); |
| 90 |
static void print_string(const u_int8_t *buf, int len); |
| 91 |
|
| 92 |
void |
| 93 |
set80211ssid(const char *val, int d, int s, const struct afswtch *rafp) |
| 94 |
{ |
| 95 |
int ssid; |
| 96 |
int len; |
| 97 |
u_int8_t data[33]; |
| 98 |
|
| 99 |
ssid = 0; |
| 100 |
|
| 101 |
bzero(data, sizeof(data)); |
| 102 |
len = sizeof(data); |
| 103 |
get_string(val, NULL, data, &len); |
| 104 |
|
| 105 |
set80211(s, IEEE80211_IOC_SSID, ssid, len, data); |
| 106 |
} |
| 107 |
|
| 108 |
void |
| 109 |
set80211stationname(const char *val, int d, int s, const struct afswtch *rafp) |
| 110 |
{ |
| 111 |
int len; |
| 112 |
u_int8_t data[33]; |
| 113 |
|
| 114 |
bzero(data, sizeof(data)); |
| 115 |
len = sizeof(data); |
| 116 |
get_string(val, NULL, data, &len); |
| 117 |
|
| 118 |
set80211(s, IEEE80211_IOC_STATIONNAME, 0, len, data); |
| 119 |
} |
| 120 |
|
| 121 |
void |
| 122 |
set80211channel(const char *val, int d, int s, const struct afswtch *rafp) |
| 123 |
{ |
| 124 |
set80211(s, IEEE80211_IOC_CHANNEL, atoi(val), 0, NULL); |
| 125 |
} |
| 126 |
|
| 127 |
void |
| 128 |
set80211authmode(const char *val, int d, int s, const struct afswtch *rafp) |
| 129 |
{ |
| 130 |
int mode; |
| 131 |
|
| 132 |
if(strcasecmp(val, "none") == 0) { |
| 133 |
mode = IEEE80211_AUTH_NONE; |
| 134 |
} else if(strcasecmp(val, "open") == 0) { |
| 135 |
mode = IEEE80211_AUTH_OPEN; |
| 136 |
} else if(strcasecmp(val, "shared") == 0) { |
| 137 |
mode = IEEE80211_AUTH_SHARED; |
| 138 |
} else { |
| 139 |
err(1, "unknown authmode"); |
| 140 |
} |
| 141 |
|
| 142 |
set80211(s, IEEE80211_IOC_AUTHMODE, mode, 0, NULL); |
| 143 |
} |
| 144 |
|
| 145 |
void |
| 146 |
set80211powersavemode(const char *val, int d, int s, const struct afswtch *rafp) |
| 147 |
{ |
| 148 |
int mode; |
| 149 |
|
| 150 |
if(strcasecmp(val, "off") == 0) { |
| 151 |
mode = IEEE80211_POWERSAVE_OFF; |
| 152 |
} else if(strcasecmp(val, "on") == 0) { |
| 153 |
mode = IEEE80211_POWERSAVE_ON; |
| 154 |
} else if(strcasecmp(val, "cam") == 0) { |
| 155 |
mode = IEEE80211_POWERSAVE_CAM; |
| 156 |
} else if(strcasecmp(val, "psp") == 0) { |
| 157 |
mode = IEEE80211_POWERSAVE_PSP; |
| 158 |
} else if(strcasecmp(val, "psp-cam") == 0) { |
| 159 |
mode = IEEE80211_POWERSAVE_PSP_CAM; |
| 160 |
} else { |
| 161 |
err(1, "unknown powersavemode"); |
| 162 |
} |
| 163 |
|
| 164 |
set80211(s, IEEE80211_IOC_POWERSAVE, mode, 0, NULL); |
| 165 |
} |
| 166 |
|
| 167 |
void |
| 168 |
set80211powersave(const char *val, int d, int s, const struct afswtch *rafp) |
| 169 |
{ |
| 170 |
if (d == 0) |
| 171 |
set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_OFF, |
| 172 |
0, NULL); |
| 173 |
else |
| 174 |
set80211(s, IEEE80211_IOC_POWERSAVE, IEEE80211_POWERSAVE_ON, |
| 175 |
0, NULL); |
| 176 |
} |
| 177 |
|
| 178 |
void |
| 179 |
set80211powersavesleep(const char *val, int d, int s, const struct afswtch *rafp) |
| 180 |
{ |
| 181 |
set80211(s, IEEE80211_IOC_POWERSAVESLEEP, atoi(val), 0, NULL); |
| 182 |
} |
| 183 |
|
| 184 |
void |
| 185 |
set80211wepmode(const char *val, int d, int s, const struct afswtch *rafp) |
| 186 |
{ |
| 187 |
int mode; |
| 188 |
|
| 189 |
if(strcasecmp(val, "off") == 0) { |
| 190 |
mode = IEEE80211_WEP_OFF; |
| 191 |
} else if(strcasecmp(val, "on") == 0) { |
| 192 |
mode = IEEE80211_WEP_ON; |
| 193 |
} else if(strcasecmp(val, "mixed") == 0) { |
| 194 |
mode = IEEE80211_WEP_MIXED; |
| 195 |
} else { |
| 196 |
err(1, "unknown wep mode"); |
| 197 |
} |
| 198 |
|
| 199 |
set80211(s, IEEE80211_IOC_WEP, mode, 0, NULL); |
| 200 |
} |
| 201 |
|
| 202 |
void |
| 203 |
set80211wep(const char *val, int d, int s, const struct afswtch *rafp) |
| 204 |
{ |
| 205 |
set80211(s, IEEE80211_IOC_WEP, d, 0, NULL); |
| 206 |
} |
| 207 |
|
| 208 |
void |
| 209 |
set80211weptxkey(const char *val, int d, int s, const struct afswtch *rafp) |
| 210 |
{ |
| 211 |
set80211(s, IEEE80211_IOC_WEPTXKEY, atoi(val)-1, 0, NULL); |
| 212 |
} |
| 213 |
|
| 214 |
void |
| 215 |
set80211wepkey(const char *val, int d, int s, const struct afswtch *rafp) |
| 216 |
{ |
| 217 |
int key = 0; |
| 218 |
int len; |
| 219 |
u_int8_t data[14]; |
| 220 |
|
| 221 |
if(isdigit(val[0]) && val[1] == ':') { |
| 222 |
key = atoi(val)-1; |
| 223 |
val += 2; |
| 224 |
} |
| 225 |
|
| 226 |
bzero(data, sizeof(data)); |
| 227 |
len = sizeof(data); |
| 228 |
get_string(val, NULL, data, &len); |
| 229 |
|
| 230 |
set80211(s, IEEE80211_IOC_WEPKEY, key, len, data); |
| 231 |
} |
| 232 |
|
| 233 |
/* |
| 234 |
* This function is purly a NetBSD compatability interface. The NetBSD |
| 235 |
* iterface is too inflexable, but it's there so we'll support it since |
| 236 |
* it's not all that hard. |
| 237 |
*/ |
| 238 |
void |
| 239 |
set80211nwkey(const char *val, int d, int s, const struct afswtch *rafp) |
| 240 |
{ |
| 241 |
int txkey; |
| 242 |
int i, len; |
| 243 |
u_int8_t data[14]; |
| 244 |
|
| 245 |
set80211(s, IEEE80211_IOC_WEP, IEEE80211_WEP_ON, 0, NULL); |
| 246 |
|
| 247 |
if(isdigit(val[0]) && val[1] == ':') { |
| 248 |
txkey = val[0]-'0'-1; |
| 249 |
val += 2; |
| 250 |
|
| 251 |
for(i = 0; i < 4; i++) { |
| 252 |
bzero(data, sizeof(data)); |
| 253 |
len = sizeof(data); |
| 254 |
val = get_string(val, ",", data, &len); |
| 255 |
|
| 256 |
set80211(s, IEEE80211_IOC_WEPKEY, i, len, data); |
| 257 |
} |
| 258 |
} else { |
| 259 |
bzero(data, sizeof(data)); |
| 260 |
len = sizeof(data); |
| 261 |
get_string(val, NULL, data, &len); |
| 262 |
txkey = 0; |
| 263 |
|
| 264 |
set80211(s, IEEE80211_IOC_WEPKEY, 0, len, data); |
| 265 |
|
| 266 |
bzero(data, sizeof(data)); |
| 267 |
for(i = 1; i < 4; i++) |
| 268 |
set80211(s, IEEE80211_IOC_WEPKEY, i, 0, data); |
| 269 |
} |
| 270 |
|
| 271 |
set80211(s, IEEE80211_IOC_WEPTXKEY, txkey, 0, NULL); |
| 272 |
} |
| 273 |
|
| 274 |
void |
| 275 |
ieee80211_status (s, info) |
| 276 |
int s; |
| 277 |
struct rt_addrinfo *info __unused; |
| 278 |
{ |
| 279 |
int i; |
| 280 |
int num; |
| 281 |
struct ieee80211req ireq; |
| 282 |
u_int8_t data[32]; |
| 283 |
char spacer; |
| 284 |
|
| 285 |
(void) memset(&ireq, 0, sizeof(ireq)); |
| 286 |
(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); |
| 287 |
ireq.i_data = &data; |
| 288 |
|
| 289 |
ireq.i_type = IEEE80211_IOC_SSID; |
| 290 |
ireq.i_val = -1; |
| 291 |
if (ioctl(s, SIOCG80211, &ireq) < 0) { |
| 292 |
/* If we can't get the SSID, the this isn't an 802.11 device. */ |
| 293 |
return; |
| 294 |
} |
| 295 |
printf("\tssid "); |
| 296 |
print_string(data, ireq.i_len); |
| 297 |
printf("\n"); |
| 298 |
|
| 299 |
ireq.i_type = IEEE80211_IOC_STATIONNAME; |
| 300 |
if (ioctl(s, SIOCG80211, &ireq) != -1) { |
| 301 |
printf("\tstationname "); |
| 302 |
print_string(data, ireq.i_len); |
| 303 |
printf("\n"); |
| 304 |
} |
| 305 |
|
| 306 |
ireq.i_type = IEEE80211_IOC_CHANNEL; |
| 307 |
if (ioctl(s, SIOCG80211, &ireq) < 0) { |
| 308 |
goto end; |
| 309 |
} |
| 310 |
printf("\tchannel %d", ireq.i_val); |
| 311 |
|
| 312 |
ireq.i_type = IEEE80211_IOC_AUTHMODE; |
| 313 |
if (ioctl(s, SIOCG80211, &ireq) != -1) { |
| 314 |
printf(" authmode"); |
| 315 |
switch (ireq.i_val) { |
| 316 |
case IEEE80211_AUTH_NONE: |
| 317 |
printf(" NONE"); |
| 318 |
break; |
| 319 |
case IEEE80211_AUTH_OPEN: |
| 320 |
printf(" OPEN"); |
| 321 |
break; |
| 322 |
case IEEE80211_AUTH_SHARED: |
| 323 |
printf(" SHARED"); |
| 324 |
break; |
| 325 |
default: |
| 326 |
printf(" UNKNOWN"); |
| 327 |
break; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
ireq.i_type = IEEE80211_IOC_POWERSAVE; |
| 332 |
if (ioctl(s, SIOCG80211, &ireq) != -1 && |
| 333 |
ireq.i_val != IEEE80211_POWERSAVE_NOSUP ) { |
| 334 |
printf(" powersavemode"); |
| 335 |
switch (ireq.i_val) { |
| 336 |
case IEEE80211_POWERSAVE_OFF: |
| 337 |
printf(" OFF"); |
| 338 |
break; |
| 339 |
case IEEE80211_POWERSAVE_CAM: |
| 340 |
printf(" CAM"); |
| 341 |
break; |
| 342 |
case IEEE80211_POWERSAVE_PSP: |
| 343 |
printf(" PSP"); |
| 344 |
break; |
| 345 |
case IEEE80211_POWERSAVE_PSP_CAM: |
| 346 |
printf(" PSP-CAM"); |
| 347 |
break; |
| 348 |
} |
| 349 |
|
| 350 |
ireq.i_type = IEEE80211_IOC_POWERSAVESLEEP; |
| 351 |
if (ioctl(s, SIOCG80211, &ireq) != -1) { |
| 352 |
if(ireq.i_val) |
| 353 |
printf(" powersavesleep %d", ireq.i_val); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
printf("\n"); |
| 358 |
|
| 359 |
ireq.i_type = IEEE80211_IOC_WEP; |
| 360 |
if (ioctl(s, SIOCG80211, &ireq) < 0 || |
| 361 |
ireq.i_val == IEEE80211_WEP_NOSUP) |
| 362 |
goto nowep; |
| 363 |
else { |
| 364 |
printf("\twepmode"); |
| 365 |
switch (ireq.i_val) { |
| 366 |
case IEEE80211_WEP_OFF: |
| 367 |
printf(" OFF"); |
| 368 |
break; |
| 369 |
case IEEE80211_WEP_ON: |
| 370 |
printf(" ON"); |
| 371 |
break; |
| 372 |
case IEEE80211_WEP_MIXED: |
| 373 |
printf(" MIXED"); |
| 374 |
break; |
| 375 |
default: |
| 376 |
printf(" UNKNOWN"); |
| 377 |
break; |
| 378 |
} |
| 379 |
|
| 380 |
/* |
| 381 |
* If we get here then we've got WEP support so we need |
| 382 |
* to print WEP status. |
| 383 |
*/ |
| 384 |
|
| 385 |
ireq.i_type = IEEE80211_IOC_WEPTXKEY; |
| 386 |
if (ioctl(s, SIOCG80211, &ireq) < 0) { |
| 387 |
warn("WEP support, but no tx key!"); |
| 388 |
goto end; |
| 389 |
} |
| 390 |
printf(" weptxkey %d", ireq.i_val+1); |
| 391 |
|
| 392 |
ireq.i_type = IEEE80211_IOC_NUMWEPKEYS; |
| 393 |
if (ioctl(s, SIOCG80211, &ireq) < 0) { |
| 394 |
warn("WEP support, but no NUMWEPKEYS support!"); |
| 395 |
goto end; |
| 396 |
} |
| 397 |
num = ireq.i_val; |
| 398 |
|
| 399 |
printf("\n"); |
| 400 |
|
| 401 |
ireq.i_type = IEEE80211_IOC_WEPKEY; |
| 402 |
spacer = '\t'; |
| 403 |
for(i = 0; i < num; i++) { |
| 404 |
ireq.i_val = i; |
| 405 |
if (ioctl(s, SIOCG80211, &ireq) < 0) { |
| 406 |
warn("WEP support, but can get keys!"); |
| 407 |
goto end; |
| 408 |
} |
| 409 |
if(ireq.i_len == 0 || ireq.i_len > 13) |
| 410 |
continue; |
| 411 |
printf("%cwepkey %d:%s", spacer, i+1, |
| 412 |
ireq.i_len <= 5 ? "64-bit" : "128-bit"); |
| 413 |
if(spacer == '\t') |
| 414 |
spacer = ' '; |
| 415 |
} |
| 416 |
|
| 417 |
printf("\n"); |
| 418 |
} |
| 419 |
nowep: |
| 420 |
|
| 421 |
|
| 422 |
end: |
| 423 |
return; |
| 424 |
} |
| 425 |
|
| 426 |
static void |
| 427 |
set80211(int s, int type, int val, int len, u_int8_t *data) |
| 428 |
{ |
| 429 |
struct ieee80211req ireq; |
| 430 |
|
| 431 |
(void) memset(&ireq, 0, sizeof(ireq)); |
| 432 |
(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name)); |
| 433 |
ireq.i_type = type; |
| 434 |
ireq.i_val = val; |
| 435 |
ireq.i_len = len; |
| 436 |
ireq.i_data = data; |
| 437 |
if(ioctl(s, SIOCS80211, &ireq) < 0) |
| 438 |
err(1, "SIOCS80211"); |
| 439 |
} |
| 440 |
|
| 441 |
static const char * |
| 442 |
get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp) |
| 443 |
{ |
| 444 |
int len; |
| 445 |
int hexstr; |
| 446 |
u_int8_t *p; |
| 447 |
|
| 448 |
len = *lenp; |
| 449 |
p = buf; |
| 450 |
hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x'); |
| 451 |
if (hexstr) |
| 452 |
val += 2; |
| 453 |
for (;;) { |
| 454 |
if (*val == '\0') |
| 455 |
break; |
| 456 |
if (sep != NULL && strchr(sep, *val) != NULL) { |
| 457 |
val++; |
| 458 |
break; |
| 459 |
} |
| 460 |
if (hexstr) { |
| 461 |
if (!isxdigit((u_char)val[0]) || |
| 462 |
!isxdigit((u_char)val[1])) { |
| 463 |
warnx("bad hexadecimal digits"); |
| 464 |
return NULL; |
| 465 |
} |
| 466 |
} |
| 467 |
if (p > buf + len) { |
| 468 |
if (hexstr) |
| 469 |
warnx("hexadecimal digits too long"); |
| 470 |
else |
| 471 |
warnx("strings too long"); |
| 472 |
return NULL; |
| 473 |
} |
| 474 |
if (hexstr) { |
| 475 |
#define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10) |
| 476 |
*p++ = (tohex((u_char)val[0]) << 4) | |
| 477 |
tohex((u_char)val[1]); |
| 478 |
#undef tohex |
| 479 |
val += 2; |
| 480 |
} else |
| 481 |
*p++ = *val++; |
| 482 |
} |
| 483 |
len = p - buf; |
| 484 |
/* The string "-" is treated as the empty string. */ |
| 485 |
if (!hexstr && len == 1 && buf[0] == '-') |
| 486 |
len = 0; |
| 487 |
if (len < *lenp) |
| 488 |
memset(p, 0, *lenp - len); |
| 489 |
*lenp = len; |
| 490 |
return val; |
| 491 |
} |
| 492 |
|
| 493 |
static void |
| 494 |
print_string(const u_int8_t *buf, int len) |
| 495 |
{ |
| 496 |
int i; |
| 497 |
int hasspc; |
| 498 |
|
| 499 |
i = 0; |
| 500 |
hasspc = 0; |
| 501 |
for(; i < len; i++) { |
| 502 |
if (!isprint(buf[i]) && buf[i] != '\0') |
| 503 |
break; |
| 504 |
if (isspace(buf[i])) |
| 505 |
hasspc++; |
| 506 |
} |
| 507 |
if (i == len) { |
| 508 |
if (hasspc || len == 0 || buf[0] == '\0') |
| 509 |
printf("\"%.*s\"", len, buf); |
| 510 |
else |
| 511 |
printf("%.*s", len, buf); |
| 512 |
} else { |
| 513 |
printf("0x"); |
| 514 |
for (i = 0; i < len; i++) |
| 515 |
printf("%02x", buf[i]); |
| 516 |
} |
| 517 |
} |
| 518 |
|