utils.c 14.7 KB
Newer Older
osdl.org!shemminger's avatar
osdl.org!shemminger committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * utils.c
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
 *
 *
 * Changes:
 *
 * Rani Assaf <rani@magic.metawire.com> 980929:	resolve addresses
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <resolv.h>
28
#include <asm/types.h>
osdl.org!shemminger's avatar
osdl.org!shemminger committed
29
#include <linux/pkt_sched.h>
30 31 32
#include <time.h>
#include <sys/time.h>

osdl.org!shemminger's avatar
osdl.org!shemminger committed
33 34 35

#include "utils.h"

osdl.org!shemminger's avatar
osdl.org!shemminger committed
36
int get_integer(int *val, const char *arg, int base)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
37 38 39 40 41 42 43 44 45 46 47 48 49
{
	long res;
	char *ptr;

	if (!arg || !*arg)
		return -1;
	res = strtol(arg, &ptr, base);
	if (!ptr || ptr == arg || *ptr || res > INT_MAX || res < INT_MIN)
		return -1;
	*val = res;
	return 0;
}

Stephen Hemminger's avatar
Stephen Hemminger committed
50
int mask2bits(__u32 netmask)
51 52
{
	unsigned bits = 0;
Stephen Hemminger's avatar
Stephen Hemminger committed
53 54
	__u32 mask = ntohl(netmask);
	__u32 host = ~mask;
55

Stephen Hemminger's avatar
Stephen Hemminger committed
56 57 58
	/* a valid netmask must be 2^n - 1 */
	if ((host & (host + 1)) != 0)
		return -1;
59

Stephen Hemminger's avatar
Stephen Hemminger committed
60 61
	for (; mask; mask <<= 1)
		++bits;
62 63 64
	return bits;
}

65 66 67 68 69 70 71 72
static int get_netmask(unsigned *val, const char *arg, int base)
{
	inet_prefix addr;

	if (!get_unsigned(val, arg, base))
		return 0;

	/* try coverting dotted quad to CIDR */
Stephen Hemminger's avatar
Stephen Hemminger committed
73 74 75 76 77
	if (!get_addr_1(&addr, arg, AF_INET) && addr.family == AF_INET) {
		int b = mask2bits(addr.data[0]);
		
		if (b >= 0) {
			*val = b;
78
			return 0;
Stephen Hemminger's avatar
Stephen Hemminger committed
79
		}
80 81 82 83 84
	}

	return -1;
}

osdl.org!shemminger's avatar
osdl.org!shemminger committed
85
int get_unsigned(unsigned *val, const char *arg, int base)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
86 87 88 89 90 91 92 93 94 95 96 97 98
{
	unsigned long res;
	char *ptr;

	if (!arg || !*arg)
		return -1;
	res = strtoul(arg, &ptr, base);
	if (!ptr || ptr == arg || *ptr || res > UINT_MAX)
		return -1;
	*val = res;
	return 0;
}

Rick Jones's avatar
Rick Jones committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
/*
 * get_jiffies is "translated" from a similar routine "get_time" in
 * tc_util.c.  we don't use the exact same routine because tc passes
 * microseconds to the kernel and the callers of get_jiffies want 
 * to pass jiffies, and have a different assumption for the units of
 * a "raw" number.
 */

int get_jiffies(unsigned *jiffies, const char *arg, int base, int *raw)
{
	double t;
	unsigned long res;
	char *p;

	if (strchr(arg,'.') != NULL) {
		t = strtod(arg,&p);
		if (t < 0.0)
			return -1;
	}
	else {
		res = strtoul(arg,&p,base);
		if (res > UINT_MAX)
			return -1;
		t = (double)res;
	}
	if (p == arg)
		return -1;

	if (__iproute2_hz_internal == 0)
                __iproute2_hz_internal = __get_hz();
	
	*raw = 1;

	if (*p) {
		*raw = 0;
                if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 ||
                    strcasecmp(p, "secs")==0)
                        t *= __iproute2_hz_internal;
                else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 ||
                         strcasecmp(p, "msecs") == 0)
                        t *= __iproute2_hz_internal/1000.0;
                else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 ||
                         strcasecmp(p, "usecs") == 0)
                        t *= __iproute2_hz_internal/1000000.0;
                else if (strcasecmp(p, "ns") == 0 || strcasecmp(p, "nsec")==0 ||
                         strcasecmp(p, "nsecs") == 0)
                        t *= __iproute2_hz_internal/1000000000.0;
		else if (strcasecmp(p, "j") == 0 || strcasecmp(p, "hz") == 0 ||
			 strcasecmp(p,"jiffies") == 0)
			t *= 1.0; /* allow suffix, do nothing */
                else
                        return -1;
        }

	/* emulate ceil() without having to bring-in -lm and always be >= 1 */

	*jiffies = t;
	if (*jiffies < t)
		*jiffies += 1;
	
        return 0;

}

163 164 165 166 167 168 169 170 171 172 173 174 175 176
int get_u64(__u64 *val, const char *arg, int base)
{
	unsigned long long res;
	char *ptr;

	if (!arg || !*arg)
		return -1;
	res = strtoull(arg, &ptr, base);
	if (!ptr || ptr == arg || *ptr || res == 0xFFFFFFFFULL)
 		return -1;
 	*val = res;
 	return 0;
}

osdl.org!shemminger's avatar
osdl.org!shemminger committed
177
int get_u32(__u32 *val, const char *arg, int base)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
178 179 180 181 182 183 184 185 186 187 188 189 190
{
	unsigned long res;
	char *ptr;

	if (!arg || !*arg)
		return -1;
	res = strtoul(arg, &ptr, base);
	if (!ptr || ptr == arg || *ptr || res > 0xFFFFFFFFUL)
		return -1;
	*val = res;
	return 0;
}

osdl.org!shemminger's avatar
osdl.org!shemminger committed
191
int get_u16(__u16 *val, const char *arg, int base)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
192 193 194 195 196 197 198 199 200 201 202 203 204
{
	unsigned long res;
	char *ptr;

	if (!arg || !*arg)
		return -1;
	res = strtoul(arg, &ptr, base);
	if (!ptr || ptr == arg || *ptr || res > 0xFFFF)
		return -1;
	*val = res;
	return 0;
}

osdl.org!shemminger's avatar
osdl.org!shemminger committed
205
int get_u8(__u8 *val, const char *arg, int base)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
206 207 208 209 210 211 212 213 214 215 216 217 218
{
	unsigned long res;
	char *ptr;

	if (!arg || !*arg)
		return -1;
	res = strtoul(arg, &ptr, base);
	if (!ptr || ptr == arg || *ptr || res > 0xFF)
		return -1;
	*val = res;
	return 0;
}

osdl.org!shemminger's avatar
osdl.org!shemminger committed
219
int get_s16(__s16 *val, const char *arg, int base)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
220 221 222 223 224 225 226 227 228 229 230 231 232
{
	long res;
	char *ptr;

	if (!arg || !*arg)
		return -1;
	res = strtol(arg, &ptr, base);
	if (!ptr || ptr == arg || *ptr || res > 0x7FFF || res < -0x8000)
		return -1;
	*val = res;
	return 0;
}

osdl.org!shemminger's avatar
osdl.org!shemminger committed
233
int get_s8(__s8 *val, const char *arg, int base)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
234 235 236 237 238 239 240 241 242 243 244 245 246
{
	long res;
	char *ptr;

	if (!arg || !*arg)
		return -1;
	res = strtol(arg, &ptr, base);
	if (!ptr || ptr == arg || *ptr || res > 0x7F || res < -0x80)
		return -1;
	*val = res;
	return 0;
}

247 248 249 250
/* This uses a non-standard parsing (ie not inet_aton, or inet_pton)
 * because of legacy choice to parse 10.8 as 10.8.0.0 not 10.0.0.8
 */
static int get_addr_ipv4(__u8 *ap, const char *cp)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
251
{
252 253 254 255 256 257 258 259 260 261 262 263
	int i;

	for (i = 0; i < 4; i++) {
		unsigned long n;
		char *endp;
		
		n = strtoul(cp, &endp, 0);
		if (n > 255)
			return -1;	/* bogus network value */

		if (endp == cp) /* no digits */
			return -1;
264

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
		ap[i] = n;

		if (*endp == '\0')
			break;

		if (i == 3 || *endp != '.')
			return -1; 	/* extra characters */
		cp = endp + 1;
	}

	return 1;
}

int get_addr_1(inet_prefix *addr, const char *name, int family)
{
osdl.org!shemminger's avatar
osdl.org!shemminger committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	memset(addr, 0, sizeof(*addr));

	if (strcmp(name, "default") == 0 ||
	    strcmp(name, "all") == 0 ||
	    strcmp(name, "any") == 0) {
		if (family == AF_DECnet)
			return -1;
		addr->family = family;
		addr->bytelen = (family == AF_INET6 ? 16 : 4);
		addr->bitlen = -1;
		return 0;
	}

	if (strchr(name, ':')) {
		addr->family = AF_INET6;
		if (family != AF_UNSPEC && family != AF_INET6)
			return -1;
		if (inet_pton(AF_INET6, name, addr->data) <= 0)
			return -1;
		addr->bytelen = 16;
		addr->bitlen = -1;
		return 0;
	}

	if (family == AF_DECnet) {
		struct dn_naddr dna;
		addr->family = AF_DECnet;
		if (dnet_pton(AF_DECnet, name, &dna) <= 0)
			return -1;
		memcpy(addr->data, dna.a_addr, 2);
		addr->bytelen = 2;
		addr->bitlen = -1;
		return 0;
	}

	addr->family = AF_INET;
	if (family != AF_UNSPEC && family != AF_INET)
		return -1;
318

319
	if (get_addr_ipv4((__u8 *)addr->data, name) <= 0)
320
		return -1;
321

osdl.org!shemminger's avatar
osdl.org!shemminger committed
322 323 324 325 326
	addr->bytelen = 4;
	addr->bitlen = -1;
	return 0;
}

327
int get_prefix_1(inet_prefix *dst, char *arg, int family)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
{
	int err;
	unsigned plen;
	char *slash;

	memset(dst, 0, sizeof(*dst));

	if (strcmp(arg, "default") == 0 ||
	    strcmp(arg, "any") == 0 ||
	    strcmp(arg, "all") == 0) {
		if (family == AF_DECnet)
			return -1;
		dst->family = family;
		dst->bytelen = 0;
		dst->bitlen = 0;
		return 0;
	}

	slash = strchr(arg, '/');
	if (slash)
		*slash = 0;
349

osdl.org!shemminger's avatar
osdl.org!shemminger committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363
	err = get_addr_1(dst, arg, family);
	if (err == 0) {
		switch(dst->family) {
			case AF_INET6:
				dst->bitlen = 128;
				break;
			case AF_DECnet:
				dst->bitlen = 16;
				break;
			default:
			case AF_INET:
				dst->bitlen = 32;
		}
		if (slash) {
364 365
			if (get_netmask(&plen, slash+1, 0)
					|| plen > dst->bitlen) {
osdl.org!shemminger's avatar
osdl.org!shemminger committed
366 367 368
				err = -1;
				goto done;
			}
369
			dst->flags |= PREFIXLEN_SPECIFIED;
osdl.org!shemminger's avatar
osdl.org!shemminger committed
370 371 372 373 374 375 376 377 378
			dst->bitlen = plen;
		}
	}
done:
	if (slash)
		*slash = '/';
	return err;
}

379
int get_addr(inet_prefix *dst, const char *arg, int family)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
380 381 382 383 384 385 386 387 388 389 390 391
{
	if (family == AF_PACKET) {
		fprintf(stderr, "Error: \"%s\" may be inet address, but it is not allowed in this context.\n", arg);
		exit(1);
	}
	if (get_addr_1(dst, arg, family)) {
		fprintf(stderr, "Error: an inet address is expected rather than \"%s\".\n", arg);
		exit(1);
	}
	return 0;
}

392
int get_prefix(inet_prefix *dst, char *arg, int family)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
393 394 395 396 397 398 399 400 401 402 403 404
{
	if (family == AF_PACKET) {
		fprintf(stderr, "Error: \"%s\" may be inet prefix, but it is not allowed in this context.\n", arg);
		exit(1);
	}
	if (get_prefix_1(dst, arg, family)) {
		fprintf(stderr, "Error: an inet prefix is expected rather than \"%s\".\n", arg);
		exit(1);
	}
	return 0;
}

405
__u32 get_addr32(const char *name)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
406 407 408 409 410 411 412 413 414
{
	inet_prefix addr;
	if (get_addr_1(&addr, name, AF_INET)) {
		fprintf(stderr, "Error: an IP address is expected rather than \"%s\"\n", name);
		exit(1);
	}
	return addr.data[0];
}

osdl.org!shemminger's avatar
osdl.org!shemminger committed
415
void incomplete_command(void)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
416 417 418 419 420
{
	fprintf(stderr, "Command line is not complete. Try option \"help\"\n");
	exit(-1);
}

421 422 423 424 425 426
void missarg(const char *key)
{
	fprintf(stderr, "Error: argument \"%s\" is required\n", key);
	exit(-1);
}

osdl.org!shemminger's avatar
osdl.org!shemminger committed
427
void invarg(const char *msg, const char *arg)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
428 429 430 431 432
{
	fprintf(stderr, "Error: argument \"%s\" is wrong: %s\n", arg, msg);
	exit(-1);
}

osdl.org!shemminger's avatar
osdl.org!shemminger committed
433
void duparg(const char *key, const char *arg)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
434 435 436 437 438
{
	fprintf(stderr, "Error: duplicate \"%s\": \"%s\" is the second value.\n", key, arg);
	exit(-1);
}

osdl.org!shemminger's avatar
osdl.org!shemminger committed
439
void duparg2(const char *key, const char *arg)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
440 441 442 443 444
{
	fprintf(stderr, "Error: either \"%s\" is duplicate, or \"%s\" is a garbage.\n", key, arg);
	exit(-1);
}

osdl.org!shemminger's avatar
osdl.org!shemminger committed
445
int matches(const char *cmd, const char *pattern)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
446 447 448 449 450 451 452
{
	int len = strlen(cmd);
	if (len > strlen(pattern))
		return -1;
	return memcmp(pattern, cmd, len);
}

osdl.org!shemminger's avatar
osdl.org!shemminger committed
453
int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
454
{
Stephen Hemminger's avatar
Stephen Hemminger committed
455 456
	const __u32 *a1 = a->data;
	const __u32 *a2 = b->data;
osdl.org!shemminger's avatar
osdl.org!shemminger committed
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
	int words = bits >> 0x05;

	bits &= 0x1f;

	if (words)
		if (memcmp(a1, a2, words << 2))
			return -1;

	if (bits) {
		__u32 w1, w2;
		__u32 mask;

		w1 = a1[words];
		w2 = a2[words];

		mask = htonl((0xffffffff) << (0x20 - bits));

		if ((w1 ^ w2) & mask)
			return 1;
	}

	return 0;
}

int __iproute2_hz_internal;

int __get_hz(void)
{
	char name[1024];
	int hz = 0;
	FILE *fp;

	if (getenv("HZ"))
		return atoi(getenv("HZ")) ? : HZ;

	if (getenv("PROC_NET_PSCHED")) {
		snprintf(name, sizeof(name)-1, "%s", getenv("PROC_NET_PSCHED"));
494
	} else if (getenv("PROC_ROOT")) {
osdl.org!shemminger's avatar
osdl.org!shemminger committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
		snprintf(name, sizeof(name)-1, "%s/net/psched", getenv("PROC_ROOT"));
	} else {
		strcpy(name, "/proc/net/psched");
	}
	fp = fopen(name, "r");

	if (fp) {
		unsigned nom, denom;
		if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
			if (nom == 1000000)
				hz = denom;
		fclose(fp);
	}
	if (hz)
		return hz;
	return HZ;
}

513 514 515 516 517 518 519
int __iproute2_user_hz_internal;

int __get_user_hz(void)
{
	return sysconf(_SC_CLK_TCK);
}

osdl.org!shemminger's avatar
osdl.org!shemminger committed
520
const char *rt_addr_n2a(int af, int len, const void *addr, char *buf, int buflen)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
{
	switch (af) {
	case AF_INET:
	case AF_INET6:
		return inet_ntop(af, addr, buf, buflen);
	case AF_IPX:
		return ipx_ntop(af, addr, buf, buflen);
	case AF_DECnet:
	{
		struct dn_naddr dna = { 2, { 0, 0, }};
		memcpy(dna.a_addr, addr, 2);
		return dnet_ntop(af, &dna, buf, buflen);
	}
	default:
		return "???";
	}
}

#ifdef RESOLVE_HOSTNAMES
struct namerec
{
	struct namerec *next;
543
	const char *name;
osdl.org!shemminger's avatar
osdl.org!shemminger committed
544 545 546
	inet_prefix addr;
};

547 548
#define NHASH 257
static struct namerec *nht[NHASH];
osdl.org!shemminger's avatar
osdl.org!shemminger committed
549

550
static const char *resolve_address(const void *addr, int len, int af)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
551 552 553 554 555 556 557 558 559 560 561 562 563 564
{
	struct namerec *n;
	struct hostent *h_ent;
	unsigned hash;
	static int notfirst;


	if (af == AF_INET6 && ((__u32*)addr)[0] == 0 &&
	    ((__u32*)addr)[1] == 0 && ((__u32*)addr)[2] == htonl(0xffff)) {
		af = AF_INET;
		addr += 12;
		len = 4;
	}

565
	hash = *(__u32 *)(addr + len - 4) % NHASH;
osdl.org!shemminger's avatar
osdl.org!shemminger committed
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584

	for (n = nht[hash]; n; n = n->next) {
		if (n->addr.family == af &&
		    n->addr.bytelen == len &&
		    memcmp(n->addr.data, addr, len) == 0)
			return n->name;
	}
	if ((n = malloc(sizeof(*n))) == NULL)
		return NULL;
	n->addr.family = af;
	n->addr.bytelen = len;
	n->name = NULL;
	memcpy(n->addr.data, addr, len);
	n->next = nht[hash];
	nht[hash] = n;
	if (++notfirst == 1)
		sethostent(1);
	fflush(stdout);

585
	if ((h_ent = gethostbyaddr(addr, len, af)) != NULL)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
586 587 588 589 590 591 592 593
		n->name = strdup(h_ent->h_name);

	/* Even if we fail, "negative" entry is remembered. */
	return n->name;
}
#endif


594
const char *format_host(int af, int len, const void *addr,
osdl.org!shemminger's avatar
osdl.org!shemminger committed
595
			char *buf, int buflen)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
596 597 598
{
#ifdef RESOLVE_HOSTNAMES
	if (resolve_hosts) {
599 600
		const char *n;

osdl.org!shemminger's avatar
osdl.org!shemminger committed
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
		if (len <= 0) {
			switch (af) {
			case AF_INET:
				len = 4;
				break;
			case AF_INET6:
				len = 16;
				break;
			case AF_IPX:
				len = 10;
				break;
#ifdef AF_DECnet
			/* I see no reasons why gethostbyname
			   may not work for DECnet */
			case AF_DECnet:
				len = 2;
				break;
#endif
			default: ;
			}
		}
		if (len > 0 &&
		    (n = resolve_address(addr, len, af)) != NULL)
			return n;
	}
#endif
	return rt_addr_n2a(af, len, addr, buf, buflen);
}


631
char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
632
{
633
	char *ptr = buf;
osdl.org!shemminger's avatar
osdl.org!shemminger committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
	int i;

	for (i=0; i<len; i++) {
		if (blen < 3)
			break;
		sprintf(ptr, "%02x", str[i]);
		ptr += 2;
		blen -= 2;
		if (i != len-1 && blen > 1) {
			*ptr++ = ':';
			blen--;
		}
	}
	return buf;
}

650
__u8* hexstring_a2n(const char *str, __u8 *buf, int blen)
osdl.org!shemminger's avatar
osdl.org!shemminger committed
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
{
	int cnt = 0;

	for (;;) {
		unsigned acc;
		char ch;

		acc = 0;

		while ((ch = *str) != ':' && ch != 0) {
			if (ch >= '0' && ch <= '9')
				ch -= '0';
			else if (ch >= 'a' && ch <= 'f')
				ch -= 'a'-10;
			else if (ch >= 'A' && ch <= 'F')
				ch -= 'A'-10;
			else
				return NULL;
			acc = (acc<<4) + ch;
			str++;
		}

		if (acc > 255)
			return NULL;
		if (cnt < blen) {
			buf[cnt] = acc;
			cnt++;
		}
		if (ch == 0)
			break;
		++str;
	}
	if (cnt < blen)
		memset(buf+cnt, 0, blen-cnt);
	return buf;
}
687 688 689 690 691 692 693 694 695 696 697 698 699 700

int print_timestamp(FILE *fp)
{
	struct timeval tv;
	char *tstr;

	memset(&tv, 0, sizeof(tv));
	gettimeofday(&tv, NULL);

	tstr = asctime(localtime(&tv.tv_sec));
	tstr[strlen(tstr)-1] = 0;
	fprintf(fp, "Timestamp: %s %lu usec\n", tstr, tv.tv_usec);
	return 0;
}
shemminger's avatar
shemminger committed
701 702 703 704

int cmdlineno;

/* Like glibc getline but handle continuation lines and comments */
705
ssize_t getcmdline(char **linep, size_t *lenp, FILE *in)
shemminger's avatar
shemminger committed
706
{
707
	ssize_t cc;
shemminger's avatar
shemminger committed
708
	char *cp;
Stephen Hemminger's avatar
Stephen Hemminger committed
709

shemminger's avatar
shemminger committed
710 711 712 713 714
	if ((cc = getline(linep, lenp, in)) < 0)
		return cc;	/* eof or error */
	++cmdlineno;

	cp = strchr(*linep, '#');
Stephen Hemminger's avatar
Stephen Hemminger committed
715
	if (cp)
shemminger's avatar
shemminger committed
716
		*cp = '\0';
Stephen Hemminger's avatar
Stephen Hemminger committed
717

shemminger's avatar
shemminger committed
718 719 720 721 722 723 724 725 726 727 728 729 730 731
	while ((cp = strstr(*linep, "\\\n")) != NULL) {
		char *line1 = NULL;
		size_t len1 = 0;
		size_t cc1;

		if ((cc1 = getline(&line1, &len1, in)) < 0) {
			fprintf(stderr, "Missing continuation line\n");
			return cc1;
		}

		++cmdlineno;
		*cp = 0;

		cp = strchr(line1, '#');
Stephen Hemminger's avatar
Stephen Hemminger committed
732
		if (cp)
shemminger's avatar
shemminger committed
733 734
			*cp = '\0';

735 736
		*lenp = strlen(*linep) + strlen(line1) + 1;
		*linep = realloc(*linep, *lenp);
shemminger's avatar
shemminger committed
737 738
		if (!*linep) {
			fprintf(stderr, "Out of memory\n");
739
			*lenp = 0;
shemminger's avatar
shemminger committed
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
			return -1;
		}
		cc += cc1 - 2;
		strcat(*linep, line1);
		free(line1);
	}
	return cc;
}

/* split command line into argument vector */
int makeargs(char *line, char *argv[], int maxargs)
{
	static const char ws[] = " \t\r\n";
	char *cp;
	int argc = 0;

	for (cp = strtok(line, ws); cp; cp = strtok(NULL, ws)) {
		if (argc >= (maxargs - 1)) {
			fprintf(stderr, "Too many arguments to command\n");
			exit(1);
		}
		argv[argc++] = cp;
	}
	argv[argc] = NULL;

	return argc;
}