No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / ipsec-tools / src / racoon / misc.c
blobc32c45e530bb13179a06d91c6b7faeb19373461b
1 /* $NetBSD: misc.c,v 1.5 2008/06/18 07:04:23 mgrooms Exp $ */
3 /* $KAME: misc.c,v 1.23 2001/08/16 14:37:29 itojun Exp $ */
5 /*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "config.h"
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/stat.h>
39 #include <sys/time.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <errno.h>
45 #include <syslog.h>
46 #include <ctype.h>
47 #include <fcntl.h>
49 #include "var.h"
50 #include "misc.h"
51 #include "debug.h"
53 #if 0
54 static int bindump __P((void *, size_t));
56 static int
57 bindump(buf0, len)
58 void *buf0;
59 size_t len;
61 unsigned char *buf = (unsigned char *)buf0;
62 size_t i;
64 for (i = 0; i < len; i++) {
65 if ((buf[i] & 0x80) || !isprint(buf[i]))
66 printf("\\x%x", buf[i]);
67 else
68 printf("%c", buf[i]);
70 printf("\n");
72 return 0;
74 #endif
76 int
77 racoon_hexdump(buf0, len)
78 void *buf0;
79 size_t len;
81 caddr_t buf = (caddr_t)buf0;
82 size_t i;
84 for (i = 0; i < len; i++) {
85 if (i != 0 && i % 32 == 0)
86 printf("\n");
87 if (i % 4 == 0)
88 printf(" ");
89 printf("%02x", (unsigned char)buf[i]);
91 printf("\n");
93 return 0;
96 char *
97 bit2str(n, bl)
98 int n, bl;
100 #define MAXBITLEN 128
101 static char b[MAXBITLEN + 1];
102 int i;
104 if (bl > MAXBITLEN)
105 return "Failed to convert."; /* NG */
106 memset(b, '0', bl);
107 b[bl] = '\0';
109 for (i = 0; i < bl; i++) {
110 if (n & (1 << i))
111 b[bl - 1 - i] = '1';
114 return b;
117 const char *
118 debug_location(file, line, func)
119 const char *file;
120 int line;
121 const char *func;
123 static char buf[1024];
124 const char *p;
126 /* truncate pathname */
127 p = strrchr(file, '/');
128 if (p)
129 p++;
130 else
131 p = file;
133 if (func)
134 snprintf(buf, sizeof(buf), "%s:%d:%s()", p, line, func);
135 else
136 snprintf(buf, sizeof(buf), "%s:%d", p, line);
138 return buf;
142 * get file size.
143 * -1: error occured.
146 getfsize(path)
147 char *path;
149 struct stat st;
151 if (stat(path, &st) != 0)
152 return -1;
153 else
154 return st.st_size;
158 * set the close-on-exec flag for file descriptor fd.
160 void
161 close_on_exec(fd)
162 int fd;
164 fcntl(fd, F_SETFD, FD_CLOEXEC);
168 * calculate the difference between two times.
169 * t1: start
170 * t2: end
172 double
173 timedelta(t1, t2)
174 struct timeval *t1, *t2;
176 if (t2->tv_usec >= t1->tv_usec)
177 return t2->tv_sec - t1->tv_sec +
178 (double)(t2->tv_usec - t1->tv_usec) / 1000000;
180 return t2->tv_sec - t1->tv_sec - 1 +
181 (double)(1000000 + t2->tv_usec - t1->tv_usec) / 1000000;