]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
1 | /* |
2 | * utils.c | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | |
10 | * | |
11 | * | |
12 | * Changes: | |
13 | * | |
14 | * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses | |
15 | */ | |
16 | ||
17 | #include <stdio.h> | |
18 | #include <stdlib.h> | |
19 | #include <unistd.h> | |
20 | #include <syslog.h> | |
21 | #include <fcntl.h> | |
22 | #include <sys/socket.h> | |
23 | #include <netinet/in.h> | |
24 | #include <string.h> | |
25 | #include <netdb.h> | |
26 | #include <arpa/inet.h> | |
27 | #include <resolv.h> | |
5dfe5561 | 28 | #include <asm/types.h> |
aba5acdf | 29 | #include <linux/pkt_sched.h> |
90f93024 SH |
30 | #include <time.h> |
31 | #include <sys/time.h> | |
32 | ||
aba5acdf SH |
33 | |
34 | #include "utils.h" | |
35 | ||
4094db72 | 36 | int get_integer(int *val, const char *arg, int base) |
aba5acdf SH |
37 | { |
38 | long res; | |
39 | char *ptr; | |
40 | ||
41 | if (!arg || !*arg) | |
42 | return -1; | |
43 | res = strtol(arg, &ptr, base); | |
44 | if (!ptr || ptr == arg || *ptr || res > INT_MAX || res < INT_MIN) | |
45 | return -1; | |
46 | *val = res; | |
47 | return 0; | |
48 | } | |
49 | ||
5a0d1cbf AH |
50 | /* a valid netmask must be 2^n - 1 (n = 1..31) */ |
51 | static int is_valid_netmask(const inet_prefix *addr) | |
52 | { | |
53 | uint32_t host; | |
54 | ||
55 | if (addr->family != AF_INET) | |
56 | return 0; | |
57 | ||
58 | host = ~ntohl(addr->data[0]); | |
59 | ||
60 | return (host & (host + 1)) == 0; | |
61 | } | |
62 | ||
63 | static int get_netmask(unsigned *val, const char *arg, int base) | |
64 | { | |
65 | inet_prefix addr; | |
66 | ||
67 | if (!get_unsigned(val, arg, base)) | |
68 | return 0; | |
69 | ||
70 | /* try coverting dotted quad to CIDR */ | |
71 | if (!get_addr_1(&addr, arg, AF_INET)) { | |
72 | u_int32_t mask; | |
73 | ||
74 | *val=0; | |
75 | for (mask = addr.data[0]; mask; mask >>= 1) | |
76 | (*val)++; | |
77 | ||
78 | if (is_valid_netmask(&addr)) | |
79 | return 0; | |
80 | } | |
81 | ||
82 | return -1; | |
83 | } | |
84 | ||
4094db72 | 85 | int get_unsigned(unsigned *val, const char *arg, int base) |
aba5acdf SH |
86 | { |
87 | unsigned long res; | |
88 | char *ptr; | |
89 | ||
90 | if (!arg || !*arg) | |
91 | return -1; | |
92 | res = strtoul(arg, &ptr, base); | |
93 | if (!ptr || ptr == arg || *ptr || res > UINT_MAX) | |
94 | return -1; | |
95 | *val = res; | |
96 | return 0; | |
97 | } | |
98 | ||
54e0b2e7 RJ |
99 | /* |
100 | * get_jiffies is "translated" from a similar routine "get_time" in | |
101 | * tc_util.c. we don't use the exact same routine because tc passes | |
102 | * microseconds to the kernel and the callers of get_jiffies want | |
103 | * to pass jiffies, and have a different assumption for the units of | |
104 | * a "raw" number. | |
105 | */ | |
106 | ||
107 | int get_jiffies(unsigned *jiffies, const char *arg, int base, int *raw) | |
108 | { | |
109 | double t; | |
110 | unsigned long res; | |
111 | char *p; | |
112 | ||
113 | if (strchr(arg,'.') != NULL) { | |
114 | t = strtod(arg,&p); | |
115 | if (t < 0.0) | |
116 | return -1; | |
117 | } | |
118 | else { | |
119 | res = strtoul(arg,&p,base); | |
120 | if (res > UINT_MAX) | |
121 | return -1; | |
122 | t = (double)res; | |
123 | } | |
124 | if (p == arg) | |
125 | return -1; | |
126 | ||
127 | if (__iproute2_hz_internal == 0) | |
128 | __iproute2_hz_internal = __get_hz(); | |
129 | ||
130 | *raw = 1; | |
131 | ||
132 | if (*p) { | |
133 | *raw = 0; | |
134 | if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 || | |
135 | strcasecmp(p, "secs")==0) | |
136 | t *= __iproute2_hz_internal; | |
137 | else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 || | |
138 | strcasecmp(p, "msecs") == 0) | |
139 | t *= __iproute2_hz_internal/1000.0; | |
140 | else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 || | |
141 | strcasecmp(p, "usecs") == 0) | |
142 | t *= __iproute2_hz_internal/1000000.0; | |
143 | else if (strcasecmp(p, "ns") == 0 || strcasecmp(p, "nsec")==0 || | |
144 | strcasecmp(p, "nsecs") == 0) | |
145 | t *= __iproute2_hz_internal/1000000000.0; | |
146 | else if (strcasecmp(p, "j") == 0 || strcasecmp(p, "hz") == 0 || | |
147 | strcasecmp(p,"jiffies") == 0) | |
148 | t *= 1.0; /* allow suffix, do nothing */ | |
149 | else | |
150 | return -1; | |
151 | } | |
152 | ||
153 | /* emulate ceil() without having to bring-in -lm and always be >= 1 */ | |
154 | ||
155 | *jiffies = t; | |
156 | if (*jiffies < t) | |
157 | *jiffies += 1; | |
158 | ||
159 | return 0; | |
160 | ||
161 | } | |
162 | ||
c7699875 | 163 | int get_u64(__u64 *val, const char *arg, int base) |
164 | { | |
165 | unsigned long long res; | |
166 | char *ptr; | |
167 | ||
168 | if (!arg || !*arg) | |
169 | return -1; | |
170 | res = strtoull(arg, &ptr, base); | |
171 | if (!ptr || ptr == arg || *ptr || res == 0xFFFFFFFFULL) | |
172 | return -1; | |
173 | *val = res; | |
174 | return 0; | |
175 | } | |
176 | ||
4094db72 | 177 | int get_u32(__u32 *val, const char *arg, int base) |
aba5acdf SH |
178 | { |
179 | unsigned long res; | |
180 | char *ptr; | |
181 | ||
182 | if (!arg || !*arg) | |
183 | return -1; | |
184 | res = strtoul(arg, &ptr, base); | |
185 | if (!ptr || ptr == arg || *ptr || res > 0xFFFFFFFFUL) | |
186 | return -1; | |
187 | *val = res; | |
188 | return 0; | |
189 | } | |
190 | ||
4094db72 | 191 | int get_u16(__u16 *val, const char *arg, int base) |
aba5acdf SH |
192 | { |
193 | unsigned long res; | |
194 | char *ptr; | |
195 | ||
196 | if (!arg || !*arg) | |
197 | return -1; | |
198 | res = strtoul(arg, &ptr, base); | |
199 | if (!ptr || ptr == arg || *ptr || res > 0xFFFF) | |
200 | return -1; | |
201 | *val = res; | |
202 | return 0; | |
203 | } | |
204 | ||
4094db72 | 205 | int get_u8(__u8 *val, const char *arg, int base) |
aba5acdf SH |
206 | { |
207 | unsigned long res; | |
208 | char *ptr; | |
209 | ||
210 | if (!arg || !*arg) | |
211 | return -1; | |
212 | res = strtoul(arg, &ptr, base); | |
213 | if (!ptr || ptr == arg || *ptr || res > 0xFF) | |
214 | return -1; | |
215 | *val = res; | |
216 | return 0; | |
217 | } | |
218 | ||
4094db72 | 219 | int get_s16(__s16 *val, const char *arg, int base) |
aba5acdf SH |
220 | { |
221 | long res; | |
222 | char *ptr; | |
223 | ||
224 | if (!arg || !*arg) | |
225 | return -1; | |
226 | res = strtol(arg, &ptr, base); | |
227 | if (!ptr || ptr == arg || *ptr || res > 0x7FFF || res < -0x8000) | |
228 | return -1; | |
229 | *val = res; | |
230 | return 0; | |
231 | } | |
232 | ||
4094db72 | 233 | int get_s8(__s8 *val, const char *arg, int base) |
aba5acdf SH |
234 | { |
235 | long res; | |
236 | char *ptr; | |
237 | ||
238 | if (!arg || !*arg) | |
239 | return -1; | |
240 | res = strtol(arg, &ptr, base); | |
241 | if (!ptr || ptr == arg || *ptr || res > 0x7F || res < -0x80) | |
242 | return -1; | |
243 | *val = res; | |
244 | return 0; | |
245 | } | |
246 | ||
4094db72 | 247 | int get_addr_1(inet_prefix *addr, const char *name, int family) |
aba5acdf | 248 | { |
4094db72 | 249 | const char *cp; |
aba5acdf SH |
250 | unsigned char *ap = (unsigned char*)addr->data; |
251 | int i; | |
252 | ||
253 | memset(addr, 0, sizeof(*addr)); | |
254 | ||
255 | if (strcmp(name, "default") == 0 || | |
256 | strcmp(name, "all") == 0 || | |
257 | strcmp(name, "any") == 0) { | |
258 | if (family == AF_DECnet) | |
259 | return -1; | |
260 | addr->family = family; | |
261 | addr->bytelen = (family == AF_INET6 ? 16 : 4); | |
262 | addr->bitlen = -1; | |
263 | return 0; | |
264 | } | |
265 | ||
266 | if (strchr(name, ':')) { | |
267 | addr->family = AF_INET6; | |
268 | if (family != AF_UNSPEC && family != AF_INET6) | |
269 | return -1; | |
270 | if (inet_pton(AF_INET6, name, addr->data) <= 0) | |
271 | return -1; | |
272 | addr->bytelen = 16; | |
273 | addr->bitlen = -1; | |
274 | return 0; | |
275 | } | |
276 | ||
277 | if (family == AF_DECnet) { | |
278 | struct dn_naddr dna; | |
279 | addr->family = AF_DECnet; | |
280 | if (dnet_pton(AF_DECnet, name, &dna) <= 0) | |
281 | return -1; | |
282 | memcpy(addr->data, dna.a_addr, 2); | |
283 | addr->bytelen = 2; | |
284 | addr->bitlen = -1; | |
285 | return 0; | |
286 | } | |
287 | ||
288 | addr->family = AF_INET; | |
289 | if (family != AF_UNSPEC && family != AF_INET) | |
290 | return -1; | |
291 | addr->bytelen = 4; | |
292 | addr->bitlen = -1; | |
293 | for (cp=name, i=0; *cp; cp++) { | |
294 | if (*cp <= '9' && *cp >= '0') { | |
295 | ap[i] = 10*ap[i] + (*cp-'0'); | |
296 | continue; | |
297 | } | |
298 | if (*cp == '.' && ++i <= 3) | |
299 | continue; | |
300 | return -1; | |
301 | } | |
302 | return 0; | |
303 | } | |
304 | ||
fb9b1d0f | 305 | int get_prefix_1(inet_prefix *dst, char *arg, int family) |
aba5acdf SH |
306 | { |
307 | int err; | |
308 | unsigned plen; | |
309 | char *slash; | |
310 | ||
311 | memset(dst, 0, sizeof(*dst)); | |
312 | ||
313 | if (strcmp(arg, "default") == 0 || | |
314 | strcmp(arg, "any") == 0 || | |
315 | strcmp(arg, "all") == 0) { | |
316 | if (family == AF_DECnet) | |
317 | return -1; | |
318 | dst->family = family; | |
319 | dst->bytelen = 0; | |
320 | dst->bitlen = 0; | |
321 | return 0; | |
322 | } | |
323 | ||
324 | slash = strchr(arg, '/'); | |
325 | if (slash) | |
326 | *slash = 0; | |
fb9b1d0f | 327 | |
aba5acdf SH |
328 | err = get_addr_1(dst, arg, family); |
329 | if (err == 0) { | |
330 | switch(dst->family) { | |
331 | case AF_INET6: | |
332 | dst->bitlen = 128; | |
333 | break; | |
334 | case AF_DECnet: | |
335 | dst->bitlen = 16; | |
336 | break; | |
337 | default: | |
338 | case AF_INET: | |
339 | dst->bitlen = 32; | |
340 | } | |
341 | if (slash) { | |
5a0d1cbf AH |
342 | if (get_netmask(&plen, slash+1, 0) |
343 | || plen > dst->bitlen) { | |
aba5acdf SH |
344 | err = -1; |
345 | goto done; | |
346 | } | |
f082b64f | 347 | dst->flags |= PREFIXLEN_SPECIFIED; |
aba5acdf SH |
348 | dst->bitlen = plen; |
349 | } | |
350 | } | |
351 | done: | |
352 | if (slash) | |
353 | *slash = '/'; | |
354 | return err; | |
355 | } | |
356 | ||
c7699875 | 357 | int get_addr(inet_prefix *dst, const char *arg, int family) |
aba5acdf SH |
358 | { |
359 | if (family == AF_PACKET) { | |
360 | fprintf(stderr, "Error: \"%s\" may be inet address, but it is not allowed in this context.\n", arg); | |
361 | exit(1); | |
362 | } | |
363 | if (get_addr_1(dst, arg, family)) { | |
364 | fprintf(stderr, "Error: an inet address is expected rather than \"%s\".\n", arg); | |
365 | exit(1); | |
366 | } | |
367 | return 0; | |
368 | } | |
369 | ||
fb9b1d0f | 370 | int get_prefix(inet_prefix *dst, char *arg, int family) |
aba5acdf SH |
371 | { |
372 | if (family == AF_PACKET) { | |
373 | fprintf(stderr, "Error: \"%s\" may be inet prefix, but it is not allowed in this context.\n", arg); | |
374 | exit(1); | |
375 | } | |
376 | if (get_prefix_1(dst, arg, family)) { | |
377 | fprintf(stderr, "Error: an inet prefix is expected rather than \"%s\".\n", arg); | |
378 | exit(1); | |
379 | } | |
380 | return 0; | |
381 | } | |
382 | ||
c7699875 | 383 | __u32 get_addr32(const char *name) |
aba5acdf SH |
384 | { |
385 | inet_prefix addr; | |
386 | if (get_addr_1(&addr, name, AF_INET)) { | |
387 | fprintf(stderr, "Error: an IP address is expected rather than \"%s\"\n", name); | |
388 | exit(1); | |
389 | } | |
390 | return addr.data[0]; | |
391 | } | |
392 | ||
4094db72 | 393 | void incomplete_command(void) |
aba5acdf SH |
394 | { |
395 | fprintf(stderr, "Command line is not complete. Try option \"help\"\n"); | |
396 | exit(-1); | |
397 | } | |
398 | ||
c7699875 | 399 | void missarg(const char *key) |
400 | { | |
401 | fprintf(stderr, "Error: argument \"%s\" is required\n", key); | |
402 | exit(-1); | |
403 | } | |
404 | ||
4094db72 | 405 | void invarg(const char *msg, const char *arg) |
aba5acdf SH |
406 | { |
407 | fprintf(stderr, "Error: argument \"%s\" is wrong: %s\n", arg, msg); | |
408 | exit(-1); | |
409 | } | |
410 | ||
4094db72 | 411 | void duparg(const char *key, const char *arg) |
aba5acdf SH |
412 | { |
413 | fprintf(stderr, "Error: duplicate \"%s\": \"%s\" is the second value.\n", key, arg); | |
414 | exit(-1); | |
415 | } | |
416 | ||
4094db72 | 417 | void duparg2(const char *key, const char *arg) |
aba5acdf SH |
418 | { |
419 | fprintf(stderr, "Error: either \"%s\" is duplicate, or \"%s\" is a garbage.\n", key, arg); | |
420 | exit(-1); | |
421 | } | |
422 | ||
4094db72 | 423 | int matches(const char *cmd, const char *pattern) |
aba5acdf SH |
424 | { |
425 | int len = strlen(cmd); | |
426 | if (len > strlen(pattern)) | |
427 | return -1; | |
428 | return memcmp(pattern, cmd, len); | |
429 | } | |
430 | ||
4094db72 | 431 | int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits) |
aba5acdf | 432 | { |
9626dfd5 SH |
433 | const __u32 *a1 = a->data; |
434 | const __u32 *a2 = b->data; | |
aba5acdf SH |
435 | int words = bits >> 0x05; |
436 | ||
437 | bits &= 0x1f; | |
438 | ||
439 | if (words) | |
440 | if (memcmp(a1, a2, words << 2)) | |
441 | return -1; | |
442 | ||
443 | if (bits) { | |
444 | __u32 w1, w2; | |
445 | __u32 mask; | |
446 | ||
447 | w1 = a1[words]; | |
448 | w2 = a2[words]; | |
449 | ||
450 | mask = htonl((0xffffffff) << (0x20 - bits)); | |
451 | ||
452 | if ((w1 ^ w2) & mask) | |
453 | return 1; | |
454 | } | |
455 | ||
456 | return 0; | |
457 | } | |
458 | ||
459 | int __iproute2_hz_internal; | |
460 | ||
461 | int __get_hz(void) | |
462 | { | |
463 | char name[1024]; | |
464 | int hz = 0; | |
465 | FILE *fp; | |
466 | ||
467 | if (getenv("HZ")) | |
468 | return atoi(getenv("HZ")) ? : HZ; | |
469 | ||
470 | if (getenv("PROC_NET_PSCHED")) { | |
471 | snprintf(name, sizeof(name)-1, "%s", getenv("PROC_NET_PSCHED")); | |
c7699875 | 472 | } else if (getenv("PROC_ROOT")) { |
aba5acdf SH |
473 | snprintf(name, sizeof(name)-1, "%s/net/psched", getenv("PROC_ROOT")); |
474 | } else { | |
475 | strcpy(name, "/proc/net/psched"); | |
476 | } | |
477 | fp = fopen(name, "r"); | |
478 | ||
479 | if (fp) { | |
480 | unsigned nom, denom; | |
481 | if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2) | |
482 | if (nom == 1000000) | |
483 | hz = denom; | |
484 | fclose(fp); | |
485 | } | |
486 | if (hz) | |
487 | return hz; | |
488 | return HZ; | |
489 | } | |
490 | ||
5e8bc631 | 491 | int __iproute2_user_hz_internal; |
492 | ||
493 | int __get_user_hz(void) | |
494 | { | |
495 | return sysconf(_SC_CLK_TCK); | |
496 | } | |
497 | ||
4094db72 | 498 | const char *rt_addr_n2a(int af, int len, const void *addr, char *buf, int buflen) |
aba5acdf SH |
499 | { |
500 | switch (af) { | |
501 | case AF_INET: | |
502 | case AF_INET6: | |
503 | return inet_ntop(af, addr, buf, buflen); | |
504 | case AF_IPX: | |
505 | return ipx_ntop(af, addr, buf, buflen); | |
506 | case AF_DECnet: | |
507 | { | |
508 | struct dn_naddr dna = { 2, { 0, 0, }}; | |
509 | memcpy(dna.a_addr, addr, 2); | |
510 | return dnet_ntop(af, &dna, buf, buflen); | |
511 | } | |
512 | default: | |
513 | return "???"; | |
514 | } | |
515 | } | |
516 | ||
517 | #ifdef RESOLVE_HOSTNAMES | |
518 | struct namerec | |
519 | { | |
520 | struct namerec *next; | |
521 | inet_prefix addr; | |
522 | char *name; | |
523 | }; | |
524 | ||
525 | static struct namerec *nht[256]; | |
526 | ||
4094db72 | 527 | char *resolve_address(const char *addr, int len, int af) |
aba5acdf SH |
528 | { |
529 | struct namerec *n; | |
530 | struct hostent *h_ent; | |
531 | unsigned hash; | |
532 | static int notfirst; | |
533 | ||
534 | ||
535 | if (af == AF_INET6 && ((__u32*)addr)[0] == 0 && | |
536 | ((__u32*)addr)[1] == 0 && ((__u32*)addr)[2] == htonl(0xffff)) { | |
537 | af = AF_INET; | |
538 | addr += 12; | |
539 | len = 4; | |
540 | } | |
541 | ||
542 | hash = addr[len-1] ^ addr[len-2] ^ addr[len-3] ^ addr[len-4]; | |
543 | ||
544 | for (n = nht[hash]; n; n = n->next) { | |
545 | if (n->addr.family == af && | |
546 | n->addr.bytelen == len && | |
547 | memcmp(n->addr.data, addr, len) == 0) | |
548 | return n->name; | |
549 | } | |
550 | if ((n = malloc(sizeof(*n))) == NULL) | |
551 | return NULL; | |
552 | n->addr.family = af; | |
553 | n->addr.bytelen = len; | |
554 | n->name = NULL; | |
555 | memcpy(n->addr.data, addr, len); | |
556 | n->next = nht[hash]; | |
557 | nht[hash] = n; | |
558 | if (++notfirst == 1) | |
559 | sethostent(1); | |
560 | fflush(stdout); | |
561 | ||
c7699875 | 562 | if ((h_ent = gethostbyaddr(addr, len, af)) != NULL) |
aba5acdf SH |
563 | n->name = strdup(h_ent->h_name); |
564 | ||
565 | /* Even if we fail, "negative" entry is remembered. */ | |
566 | return n->name; | |
567 | } | |
568 | #endif | |
569 | ||
570 | ||
c7699875 | 571 | const char *format_host(int af, int len, const void *addr, |
4094db72 | 572 | char *buf, int buflen) |
aba5acdf SH |
573 | { |
574 | #ifdef RESOLVE_HOSTNAMES | |
575 | if (resolve_hosts) { | |
576 | char *n; | |
577 | if (len <= 0) { | |
578 | switch (af) { | |
579 | case AF_INET: | |
580 | len = 4; | |
581 | break; | |
582 | case AF_INET6: | |
583 | len = 16; | |
584 | break; | |
585 | case AF_IPX: | |
586 | len = 10; | |
587 | break; | |
588 | #ifdef AF_DECnet | |
589 | /* I see no reasons why gethostbyname | |
590 | may not work for DECnet */ | |
591 | case AF_DECnet: | |
592 | len = 2; | |
593 | break; | |
594 | #endif | |
595 | default: ; | |
596 | } | |
597 | } | |
598 | if (len > 0 && | |
599 | (n = resolve_address(addr, len, af)) != NULL) | |
600 | return n; | |
601 | } | |
602 | #endif | |
603 | return rt_addr_n2a(af, len, addr, buf, buflen); | |
604 | } | |
605 | ||
606 | ||
f332d169 | 607 | char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen) |
aba5acdf | 608 | { |
f332d169 | 609 | char *ptr = buf; |
aba5acdf SH |
610 | int i; |
611 | ||
612 | for (i=0; i<len; i++) { | |
613 | if (blen < 3) | |
614 | break; | |
615 | sprintf(ptr, "%02x", str[i]); | |
616 | ptr += 2; | |
617 | blen -= 2; | |
618 | if (i != len-1 && blen > 1) { | |
619 | *ptr++ = ':'; | |
620 | blen--; | |
621 | } | |
622 | } | |
623 | return buf; | |
624 | } | |
625 | ||
f332d169 | 626 | __u8* hexstring_a2n(const char *str, __u8 *buf, int blen) |
aba5acdf SH |
627 | { |
628 | int cnt = 0; | |
629 | ||
630 | for (;;) { | |
631 | unsigned acc; | |
632 | char ch; | |
633 | ||
634 | acc = 0; | |
635 | ||
636 | while ((ch = *str) != ':' && ch != 0) { | |
637 | if (ch >= '0' && ch <= '9') | |
638 | ch -= '0'; | |
639 | else if (ch >= 'a' && ch <= 'f') | |
640 | ch -= 'a'-10; | |
641 | else if (ch >= 'A' && ch <= 'F') | |
642 | ch -= 'A'-10; | |
643 | else | |
644 | return NULL; | |
645 | acc = (acc<<4) + ch; | |
646 | str++; | |
647 | } | |
648 | ||
649 | if (acc > 255) | |
650 | return NULL; | |
651 | if (cnt < blen) { | |
652 | buf[cnt] = acc; | |
653 | cnt++; | |
654 | } | |
655 | if (ch == 0) | |
656 | break; | |
657 | ++str; | |
658 | } | |
659 | if (cnt < blen) | |
660 | memset(buf+cnt, 0, blen-cnt); | |
661 | return buf; | |
662 | } | |
90f93024 SH |
663 | |
664 | int print_timestamp(FILE *fp) | |
665 | { | |
666 | struct timeval tv; | |
667 | char *tstr; | |
668 | ||
669 | memset(&tv, 0, sizeof(tv)); | |
670 | gettimeofday(&tv, NULL); | |
671 | ||
672 | tstr = asctime(localtime(&tv.tv_sec)); | |
673 | tstr[strlen(tstr)-1] = 0; | |
674 | fprintf(fp, "Timestamp: %s %lu usec\n", tstr, tv.tv_usec); | |
675 | return 0; | |
676 | } | |
351efcde SH |
677 | |
678 | int cmdlineno; | |
679 | ||
680 | /* Like glibc getline but handle continuation lines and comments */ | |
d21e8835 | 681 | ssize_t getcmdline(char **linep, size_t *lenp, FILE *in) |
351efcde | 682 | { |
d21e8835 | 683 | ssize_t cc; |
351efcde | 684 | char *cp; |
ae665a52 | 685 | |
351efcde SH |
686 | if ((cc = getline(linep, lenp, in)) < 0) |
687 | return cc; /* eof or error */ | |
688 | ++cmdlineno; | |
689 | ||
690 | cp = strchr(*linep, '#'); | |
ae665a52 | 691 | if (cp) |
351efcde | 692 | *cp = '\0'; |
ae665a52 | 693 | |
351efcde SH |
694 | while ((cp = strstr(*linep, "\\\n")) != NULL) { |
695 | char *line1 = NULL; | |
696 | size_t len1 = 0; | |
697 | size_t cc1; | |
698 | ||
699 | if ((cc1 = getline(&line1, &len1, in)) < 0) { | |
700 | fprintf(stderr, "Missing continuation line\n"); | |
701 | return cc1; | |
702 | } | |
703 | ||
704 | ++cmdlineno; | |
705 | *cp = 0; | |
706 | ||
707 | cp = strchr(line1, '#'); | |
ae665a52 | 708 | if (cp) |
351efcde SH |
709 | *cp = '\0'; |
710 | ||
d21e8835 AH |
711 | *lenp = strlen(*linep) + strlen(line1) + 1; |
712 | *linep = realloc(*linep, *lenp); | |
351efcde SH |
713 | if (!*linep) { |
714 | fprintf(stderr, "Out of memory\n"); | |
d21e8835 | 715 | *lenp = 0; |
351efcde SH |
716 | return -1; |
717 | } | |
718 | cc += cc1 - 2; | |
719 | strcat(*linep, line1); | |
720 | free(line1); | |
721 | } | |
722 | return cc; | |
723 | } | |
724 | ||
725 | /* split command line into argument vector */ | |
726 | int makeargs(char *line, char *argv[], int maxargs) | |
727 | { | |
728 | static const char ws[] = " \t\r\n"; | |
729 | char *cp; | |
730 | int argc = 0; | |
731 | ||
732 | for (cp = strtok(line, ws); cp; cp = strtok(NULL, ws)) { | |
733 | if (argc >= (maxargs - 1)) { | |
734 | fprintf(stderr, "Too many arguments to command\n"); | |
735 | exit(1); | |
736 | } | |
737 | argv[argc++] = cp; | |
738 | } | |
739 | argv[argc] = NULL; | |
740 | ||
741 | return argc; | |
742 | } |