/*-
 * Copyright (c) 2001-2005 Christian S.J. Peron
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/* XXX include tcpdump here too */
#include <ipex_includes.h>

struct multparams {
	char		*baseptr;
	char		*newptr;
	int		mult;
};

#define BYTESPERKILO	1024
#define BYTESPERMEG	1048576
#define BYTESPERGIG	1073741824

static int
apply_multiplier(struct multparams *mp)
{
	char *rem;
	u_long val;

	if (mp->mult > 1)
		*mp->newptr = 0;
	val = strtoul(mp->baseptr, &rem, 0);
	if (*rem || rem == mp->baseptr || !val)
		errx(1, "invalid count specification");
	val *= mp->mult;
	return (val);
}

static void
reverse(char *s)
{
	int i, j, c;

	for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
		c = s[i];
		s[i] = s[j];
		s[j] = c;
	}
}

static void
swebitoa(unsigned int n, char *s)
{
	unsigned int i;

	i = 0;
	do {
		s[i++] = n % 10 + '0';
	} while ((n /= 10) > 0);
	s[i] = '\0';
	reverse(s);
}

void
dump_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
{
	struct dump_info *info;
	extern pcap_t *pd;
	char name[255];
	static int cnt;

	info = (struct dump_info *)user;
	if (opts.Cflag && ftell((FILE *)info->p) > opts.Cflag) {
		pcap_dump_close(info->p);
		strcpy(name, info->WFileName);
		swebitoa(cnt, name + strlen(info->WFileName));
		cnt++;
		info->p = pcap_dump_open(info->pd, name);
		if (info->p == NULL)
			err(1, "%s", pcap_geterr(pd));
        }
	pcap_dump((caddr_t)info->p, h, sp);
}

u_long
parseCflag(char *Cflagstr)
{
	struct multparams mp;
	u_long s;

	mp.baseptr = Cflagstr;
	while (*Cflagstr)
		Cflagstr++;
	s = Cflagstr - mp.baseptr;
	mp.newptr = Cflagstr;
	switch (*--mp.newptr) {
	case 'k': case 'K':
		mp.mult = BYTESPERKILO;
		break;
	case 'm': case 'M':
		mp.mult = BYTESPERMEG;
		break;
	case 'g': case 'G':
		mp.mult = BYTESPERGIG;
		break;
	default:
		mp.mult = 1;
	}
        return (apply_multiplier(&mp));
}


syntax highlighted by Code2HTML, v. 0.9.1