2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
26 const char *program_name;
31 ovs_fatal(0, "virtual memory exhausted");
35 xcalloc(size_t count, size_t size)
37 void *p = count && size ? calloc(count, size) : malloc(1);
38 COVERAGE_INC(util_xalloc);
48 return xcalloc(1, size);
54 void *p = malloc(size ? size : 1);
55 COVERAGE_INC(util_xalloc);
63 xrealloc(void *p, size_t size)
65 p = realloc(p, size ? size : 1);
66 COVERAGE_INC(util_xalloc);
74 xmemdup(const void *p_, size_t size)
76 void *p = xmalloc(size);
82 xmemdup0(const char *p_, size_t length)
84 char *p = xmalloc(length + 1);
85 memcpy(p, p_, length);
91 xstrdup(const char *s)
93 return xmemdup0(s, strlen(s));
97 xvasprintf(const char *format, va_list args)
103 va_copy(args2, args);
104 needed = vsnprintf(NULL, 0, format, args);
106 s = xmalloc(needed + 1);
108 vsnprintf(s, needed + 1, format, args2);
115 x2nrealloc(void *p, size_t *n, size_t s)
117 *n = *n == 0 ? 1 : 2 * *n;
118 return xrealloc(p, *n * s);
122 xasprintf(const char *format, ...)
127 va_start(args, format);
128 s = xvasprintf(format, args);
135 ovs_strlcpy(char *dst, const char *src, size_t size)
138 size_t n = strlen(src);
139 size_t n_copy = MIN(n, size - 1);
140 memcpy(dst, src, n_copy);
146 ovs_fatal(int err_no, const char *format, ...)
150 fprintf(stderr, "%s: ", program_name);
151 va_start(args, format);
152 vfprintf(stderr, format, args);
155 fprintf(stderr, " (%s)", strerror(err_no));
162 ovs_error(int err_no, const char *format, ...)
164 int save_errno = errno;
167 fprintf(stderr, "%s: ", program_name);
168 va_start(args, format);
169 vfprintf(stderr, format, args);
172 fprintf(stderr, " (%s)", strerror(err_no));
178 /* Sets program_name based on 'argv0'. Should be called at the beginning of
179 * main(), as "set_program_name(argv[0]);". */
180 void set_program_name(const char *argv0)
182 const char *slash = strrchr(argv0, '/');
183 program_name = slash ? slash + 1 : argv0;
186 /* Print the version information for the program. */
188 ovs_print_version(char *date, char *time,
189 uint8_t min_ofp, uint8_t max_ofp)
191 printf("%s (Open vSwitch) "VERSION BUILDNR"\n", program_name);
192 printf("Compiled %s %s\n", date, time);
193 if (min_ofp || max_ofp) {
194 printf("OpenFlow versions %#x:%#x\n", min_ofp, max_ofp);
198 /* Writes the 'size' bytes in 'buf' to 'stream' as hex bytes arranged 16 per
199 * line. Numeric offsets are also included, starting at 'ofs' for the first
200 * byte in 'buf'. If 'ascii' is true then the corresponding ASCII characters
201 * are also rendered alongside. */
203 ovs_hex_dump(FILE *stream, const void *buf_, size_t size,
204 uintptr_t ofs, bool ascii)
206 const uint8_t *buf = buf_;
207 const size_t per_line = 16; /* Maximum bytes per line. */
211 size_t start, end, n;
214 /* Number of bytes on this line. */
215 start = ofs % per_line;
217 if (end - start > size)
222 fprintf(stream, "%08jx ", (uintmax_t) ROUND_DOWN(ofs, per_line));
223 for (i = 0; i < start; i++)
224 fprintf(stream, " ");
226 fprintf(stream, "%02hhx%c",
227 buf[i - start], i == per_line / 2 - 1? '-' : ' ');
230 for (; i < per_line; i++)
231 fprintf(stream, " ");
232 fprintf(stream, "|");
233 for (i = 0; i < start; i++)
234 fprintf(stream, " ");
235 for (; i < end; i++) {
236 int c = buf[i - start];
237 putc(c >= 32 && c < 127 ? c : '.', stream);
239 for (; i < per_line; i++)
240 fprintf(stream, " ");
241 fprintf(stream, "|");
243 fprintf(stream, "\n");
252 str_to_int(const char *s, int base, int *i)
255 bool ok = str_to_llong(s, base, &ll);
261 str_to_long(const char *s, int base, long *li)
264 bool ok = str_to_llong(s, base, &ll);
270 str_to_llong(const char *s, int base, long long *x)
272 int save_errno = errno;
275 *x = strtoll(s, &tail, base);
276 if (errno == EINVAL || errno == ERANGE || tail == s || *tail != '\0') {
287 str_to_uint(const char *s, int base, unsigned int *u)
289 return str_to_int(s, base, (int *) u);
293 str_to_ulong(const char *s, int base, unsigned long *ul)
295 return str_to_long(s, base, (long *) ul);
299 str_to_ullong(const char *s, int base, unsigned long long *ull)
301 return str_to_llong(s, base, (long long *) ull);