2 * Copyright (c) 2000-2001
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 /* Panic with a message */
42 static void vfail(const char *fmt, va_list args) __attribute__((__noreturn__));
44 static void vfail(const char *fmt, va_list args)
46 vfprintf(stderr, fmt, args);
55 while (1); /* Work around stupid gcc-2.96-85 bug */
58 /* Panic with a nice message */
59 void __fail(const char *file, unsigned int line,
60 const char *func __attribute__((unused)),
65 fprintf(stderr, "\n%s:%u ", file, line);
69 #ifndef HAVE_VARIADIC_MACROS
70 /* Panic with a not-quite-as-nice message */
71 void fail(const char *fmt, ...)
79 void failure(const char *message)
81 fprintf(stderr,message);
85 /* Concatenate 2 strings, allocating space in r for the result */
86 char *rstrcat(region r, const char *s1, const char *s2)
88 char *result = rarrayalloc(r, strlen(s1)+strlen(s2)+1, char);
95 /* Concatenate n strings, allocating space in r for the result. The
96 last argument should be a null pointer. */
97 char *rstrscat(region r, ...)
105 while ((s = va_arg(args, const char *)))
107 result = rarrayalloc(r, len+1, char);
111 while ((s = va_arg(args, const char *)))
117 /* Convert an integer to a string, storing the result in r */
118 const char *inttostr(region r, int i)
126 width = (int) (floor(log10(abs((double) i))) + 1);
129 printf("i=%d, width=%d\n", i, width);
132 result = rarrayalloc(r, width + 1, char);
133 if (snprintf(result, width + 1, "%d", i) == -1) {
134 printf("i=%d, width=%d\n", i, width);
135 fail ("inttostr width wrong\n");
141 /* sprintf a string, allocating space in r for the result */
142 char *rsprintf(region r, const char *fmt, ...)
147 return rvsprintf(r, fmt, args);
150 char *rvsprintf(region r, const char *fmt, va_list args)
152 growbuf buf = growbuf_new(r, 100);
153 gvprintf(buf, fmt, args);
154 return growbuf_contents(buf);
157 /* Space for the ASCII representation of a pointer -- 2 hex chars per
158 byte, plus 3 chars for 0x prefix and trailing \0 */
159 #define PTR_ASCII_SIZE ((int) (3 + sizeof(void *)*2))
161 /* Convert a pointer to an ascii string with leading 0x. Re-uses
163 char *ptr_to_ascii(void *ptr) {
164 static char addr[PTR_ASCII_SIZE];
167 nchars = snprintf(addr, PTR_ASCII_SIZE, "%p", ptr);
168 if (nchars == -1 || nchars >= PTR_ASCII_SIZE)
169 fail("Unable to convert ptr to ascii (need %d bytes, have %d)\n",
170 nchars, PTR_ASCII_SIZE);
174 /* Convert a pointer to an integer */
175 long ptr_hash(void *ptr)
180 /* Return TRUE iff ptr1 == ptr2 */
181 bool ptr_eq(void *ptr1, void *ptr2)
186 /* Return TRUE iff s1 == s2 */
187 bool str_eq(const char *s1, const char *s2)
189 return (strcmp(s1, s2) == 0);
192 /* A total ordering on pointers. Returns 0 if ptr1 = ptr2, a value <0
193 if ptr1 < ptr2, or a value >0 if ptr1 > ptr2. */
194 int ptr_cmp(const void *ptr1, const void *ptr2)
196 return (char *) ptr1 - (char *) ptr2;
199 /* int abs(int a) { if (a < 0) return -a; else return a; } */