New function xmemdup0().
[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 "util.h"
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 const char *program_name;
41
42 static void
43 out_of_memory(void) 
44 {
45     fatal(0, "virtual memory exhausted");
46 }
47
48 void *
49 xcalloc(size_t count, size_t size) 
50 {
51     void *p = count && size ? calloc(count, size) : malloc(1);
52     if (p == NULL) {
53         out_of_memory();
54     }
55     return p;
56 }
57
58 void *
59 xmalloc(size_t size) 
60 {
61     void *p = malloc(size ? size : 1);
62     if (p == NULL) {
63         out_of_memory();
64     }
65     return p;
66 }
67
68 void *
69 xrealloc(void *p, size_t size) 
70 {
71     p = realloc(p, size ? size : 1);
72     if (p == NULL) {
73         out_of_memory();
74     }
75     return p;
76 }
77
78 void *
79 xmemdup(const void *p_, size_t size)
80 {
81     void *p = xmalloc(size);
82     memcpy(p, p_, size);
83     return p;
84 }
85
86 char *
87 xmemdup0(const char *p_, size_t length)
88 {
89     char *p = xmalloc(length + 1);
90     memcpy(p, p_, length);
91     p[length] = '\0';
92     return p;
93 }
94
95 char *
96 xstrdup(const char *s) 
97 {
98     return xmemdup0(s, strlen(s));
99 }
100
101 char *
102 xasprintf(const char *format, ...)
103 {
104     va_list args;
105     size_t needed;
106     char *s;
107
108     va_start(args, format);
109     needed = vsnprintf(NULL, 0, format, args);
110     va_end(args);
111
112     s = xmalloc(needed + 1);
113
114     va_start(args, format);
115     vsnprintf(s, needed + 1, format, args);
116     va_end(args);
117
118     return s;
119 }
120
121 void fatal(int err_no, const char *format, ...)
122 {
123     va_list args;
124
125     fprintf(stderr, "%s: ", program_name);
126     va_start(args, format);
127     vfprintf(stderr, format, args);
128     va_end(args);
129     if (err_no != 0)
130         fprintf(stderr, " (%s)", strerror(err_no));
131     putc('\n', stderr);
132
133     exit(EXIT_FAILURE);
134 }
135
136 void error(int err_no, const char *format, ...)
137 {
138     va_list args;
139
140     fprintf(stderr, "%s: ", program_name);
141     va_start(args, format);
142     vfprintf(stderr, format, args);
143     va_end(args);
144     if (err_no != 0)
145         fprintf(stderr, " (%s)", strerror(err_no));
146     putc('\n', stderr);
147 }
148
149 void debug(int err_no, const char *format, ...)
150 {
151     va_list args;
152
153     fprintf(stderr, "%s: ", program_name);
154     va_start(args, format);
155     vfprintf(stderr, format, args);
156     va_end(args);
157     if (err_no != 0)
158         fprintf(stderr, " (%s)", strerror(err_no));
159     putc('\n', stderr);
160 }
161
162 /* Sets program_name based on 'argv0'.  Should be called at the beginning of
163  * main(), as "set_program_name(argv[0]);".  */
164 void set_program_name(const char *argv0)
165 {
166     const char *slash = strrchr(argv0, '/');
167     program_name = slash ? slash + 1 : argv0;
168 }
169
170 /* Writes the 'size' bytes in 'buf' to 'stream' as hex bytes arranged 16 per
171  * line.  Numeric offsets are also included, starting at 'ofs' for the first
172  * byte in 'buf'.  If 'ascii' is true then the corresponding ASCII characters
173  * are also rendered alongside. */
174 void
175 hex_dump(FILE *stream, const void *buf_, size_t size,
176          uintptr_t ofs, bool ascii)
177 {
178   const uint8_t *buf = buf_;
179   const size_t per_line = 16; /* Maximum bytes per line. */
180
181   while (size > 0)
182     {
183       size_t start, end, n;
184       size_t i;
185
186       /* Number of bytes on this line. */
187       start = ofs % per_line;
188       end = per_line;
189       if (end - start > size)
190         end = start + size;
191       n = end - start;
192
193       /* Print line. */
194       fprintf(stream, "%08jx  ", (uintmax_t) ROUND_DOWN(ofs, per_line));
195       for (i = 0; i < start; i++)
196         fprintf(stream, "   ");
197       for (; i < end; i++)
198         fprintf(stream, "%02hhx%c",
199                 buf[i - start], i == per_line / 2 - 1? '-' : ' ');
200       if (ascii)
201         {
202           for (; i < per_line; i++)
203             fprintf(stream, "   ");
204           fprintf(stream, "|");
205           for (i = 0; i < start; i++)
206             fprintf(stream, " ");
207           for (; i < end; i++) {
208               int c = buf[i - start];
209               putc(c >= 32 && c < 127 ? c : '.', stream);
210           }
211           for (; i < per_line; i++)
212             fprintf(stream, " ");
213           fprintf(stream, "|");
214         }
215       fprintf(stream, "\n");
216
217       ofs += n;
218       buf += n;
219       size -= n;
220     }
221 }