1/*	$KAME: misc.c,v 1.23 2001/08/16 14:37:29 itojun Exp $	*/
2
3/*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
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
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
29 * SUCH DAMAGE.
30 */
31
32#include <sys/types.h>
33#include <sys/param.h>
34#include <sys/stat.h>
35#include <sys/time.h>
36
37#include <stdlib.h>
38#include <stdio.h>
39#include <string.h>
40#include <errno.h>
41#include <syslog.h>
42#include <ctype.h>
43
44#include "var.h"
45#include "misc.h"
46#include "debug.h"
47
48
49int
50hexdump(buf0, len)
51	void *buf0;
52	size_t len;
53{
54	caddr_t buf = (caddr_t)buf0;
55	size_t i;
56
57	for (i = 0; i < len; i++) {
58		if (i != 0 && i % 32 == 0)
59			printf("\n");
60		if (i % 4 == 0)
61			printf(" ");
62		printf("%02x", (unsigned char)buf[i]);
63	}
64	printf("\n");
65
66	return 0;
67}
68
69char *
70bit2str(n, bl)
71	int n, bl;
72{
73#define MAXBITLEN 128
74	static char b[MAXBITLEN + 1];
75	int i;
76
77	if (bl > MAXBITLEN)
78		return "Failed to convert.";	/* NG */
79	memset(b, '0', bl);
80	b[bl] = '\0';
81
82	for (i = 0; i < bl; i++) {
83		if (n & (1 << i))
84			b[bl - 1 - i] = '1';
85	}
86
87	return b;
88}
89
90const char *
91debug_location(file, line, func)
92	const char *file;
93	int line;
94	const char *func;
95{
96	static char buf[1024];
97	const char *p;
98
99	/* truncate pathname */
100	p = strrchr(file, '/');
101	if (p)
102		p++;
103	else
104		p = file;
105
106	if (func)
107		snprintf(buf, sizeof(buf), "%s:%d:%s()", p, line, func);
108	else
109		snprintf(buf, sizeof(buf), "%s:%d", p, line);
110
111	return buf;
112}
113
114/*
115 * get file size.
116 * -1: error occured.
117 */
118int
119getfsize(path)
120	char *path;
121{
122        struct stat st;
123
124        if (stat(path, &st) != 0)
125                return -1;
126        else
127                return st.st_size;
128}
129
130/*
131 * calculate the difference between two times.
132 * t1: start
133 * t2: end
134 */
135double
136timedelta(t1, t2)
137	struct timeval *t1, *t2;
138{
139	if (t2->tv_usec >= t1->tv_usec)
140		return t2->tv_sec - t1->tv_sec +
141			(double)(t2->tv_usec - t1->tv_usec) / 1000000;
142
143	return t2->tv_sec - t1->tv_sec - 1 +
144		(double)(1000000 + t2->tv_usec - t1->tv_usec) / 1000000;
145}
146