Move Autoconf's macro definitions into config.h.
[sliver-openvswitch.git] / lib / util.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 #include <config.h>
35 #include "util.h"
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 const char *program_name;
42
43 static void
44 out_of_memory(void) 
45 {
46     fatal(0, "virtual memory exhausted");
47 }
48
49 void *
50 xcalloc(size_t count, size_t size) 
51 {
52     void *p = count && size ? calloc(count, size) : malloc(1);
53     if (p == NULL) {
54         out_of_memory();
55     }
56     return p;
57 }
58
59 void *
60 xmalloc(size_t size) 
61 {
62     void *p = malloc(size ? size : 1);
63     if (p == NULL) {
64         out_of_memory();
65     }
66     return p;
67 }
68
69 void *
70 xrealloc(void *p, size_t size) 
71 {
72     p = realloc(p, size ? size : 1);
73     if (p == NULL) {
74         out_of_memory();
75     }
76     return p;
77 }
78
79 void *
80 xmemdup(const void *p_, size_t size)
81 {
82     void *p = xmalloc(size);
83     memcpy(p, p_, size);
84     return p;
85 }
86
87 char *
88 xmemdup0(const char *p_, size_t length)
89 {
90     char *p = xmalloc(length + 1);
91     memcpy(p, p_, length);
92     p[length] = '\0';
93     return p;
94 }
95
96 char *
97 xstrdup(const char *s) 
98 {
99     return xmemdup0(s, strlen(s));
100 }
101
102 char *
103 xasprintf(const char *format, ...)
104 {
105     va_list args;
106     size_t needed;
107     char *s;
108
109     va_start(args, format);
110     needed = vsnprintf(NULL, 0, format, args);
111     va_end(args);
112
113     s = xmalloc(needed + 1);
114
115     va_start(args, format);
116     vsnprintf(s, needed + 1, format, args);
117     va_end(args);
118
119     return s;
120 }
121
122 void fatal(int err_no, const char *format, ...)
123 {
124     va_list args;
125
126     fprintf(stderr, "%s: ", program_name);
127     va_start(args, format);
128     vfprintf(stderr, format, args);
129     va_end(args);
130     if (err_no != 0)
131         fprintf(stderr, " (%s)", strerror(err_no));
132     putc('\n', stderr);
133
134     exit(EXIT_FAILURE);
135 }
136
137 void error(int err_no, const char *format, ...)
138 {
139     va_list args;
140
141     fprintf(stderr, "%s: ", program_name);
142     va_start(args, format);
143     vfprintf(stderr, format, args);
144     va_end(args);
145     if (err_no != 0)
146         fprintf(stderr, " (%s)", strerror(err_no));
147     putc('\n', stderr);
148 }
149
150 void debug(int err_no, const char *format, ...)
151 {
152     va_list args;
153
154     fprintf(stderr, "%s: ", program_name);
155     va_start(args, format);
156     vfprintf(stderr, format, args);
157     va_end(args);
158     if (err_no != 0)
159         fprintf(stderr, " (%s)", strerror(err_no));
160     putc('\n', stderr);
161 }
162
163 /* Sets program_name based on 'argv0'.  Should be called at the beginning of
164  * main(), as "set_program_name(argv[0]);".  */
165 void set_program_name(const char *argv0)
166 {
167     const char *slash = strrchr(argv0, '/');
168     program_name = slash ? slash + 1 : argv0;
169 }
170
171 /* Writes the 'size' bytes in 'buf' to 'stream' as hex bytes arranged 16 per
172  * line.  Numeric offsets are also included, starting at 'ofs' for the first
173  * byte in 'buf'.  If 'ascii' is true then the corresponding ASCII characters
174  * are also rendered alongside. */
175 void
176 hex_dump(FILE *stream, const void *buf_, size_t size,
177          uintptr_t ofs, bool ascii)
178 {
179   const uint8_t *buf = buf_;
180   const size_t per_line = 16; /* Maximum bytes per line. */
181
182   while (size > 0)
183     {
184       size_t start, end, n;
185       size_t i;
186
187       /* Number of bytes on this line. */
188       start = ofs % per_line;
189       end = per_line;
190       if (end - start > size)
191         end = start + size;
192       n = end - start;
193
194       /* Print line. */
195       fprintf(stream, "%08jx  ", (uintmax_t) ROUND_DOWN(ofs, per_line));
196       for (i = 0; i < start; i++)
197         fprintf(stream, "   ");
198       for (; i < end; i++)
199         fprintf(stream, "%02hhx%c",
200                 buf[i - start], i == per_line / 2 - 1? '-' : ' ');
201       if (ascii)
202         {
203           for (; i < per_line; i++)
204             fprintf(stream, "   ");
205           fprintf(stream, "|");
206           for (i = 0; i < start; i++)
207             fprintf(stream, " ");
208           for (; i < end; i++) {
209               int c = buf[i - start];
210               putc(c >= 32 && c < 127 ? c : '.', stream);
211           }
212           for (; i < per_line; i++)
213             fprintf(stream, " ");
214           fprintf(stream, "|");
215         }
216       fprintf(stream, "\n");
217
218       ofs += n;
219       buf += n;
220       size -= n;
221     }
222 }