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 void *p = malloc(size ? size : 1);
49 COVERAGE_INC(util_xalloc);
57 xrealloc(void *p, size_t size)
59 p = realloc(p, size ? size : 1);
60 COVERAGE_INC(util_xalloc);
68 xmemdup(const void *p_, size_t size)
70 void *p = xmalloc(size);
76 xmemdup0(const char *p_, size_t length)
78 char *p = xmalloc(length + 1);
79 memcpy(p, p_, length);
85 xstrdup(const char *s)
87 return xmemdup0(s, strlen(s));
91 xvasprintf(const char *format, va_list args)
98 needed = vsnprintf(NULL, 0, format, args);
100 s = xmalloc(needed + 1);
102 vsnprintf(s, needed + 1, format, args2);
109 x2nrealloc(void *p, size_t *n, size_t s)
111 *n = *n == 0 ? 1 : 2 * *n;
112 return xrealloc(p, *n * s);
116 xasprintf(const char *format, ...)
121 va_start(args, format);
122 s = xvasprintf(format, args);
129 ovs_strlcpy(char *dst, const char *src, size_t size)
132 size_t n = strlen(src);
133 size_t n_copy = MIN(n, size - 1);
134 memcpy(dst, src, n_copy);
140 ovs_fatal(int err_no, const char *format, ...)
144 fprintf(stderr, "%s: ", program_name);
145 va_start(args, format);
146 vfprintf(stderr, format, args);
149 fprintf(stderr, " (%s)", strerror(err_no));
156 ovs_error(int err_no, const char *format, ...)
158 int save_errno = errno;
161 fprintf(stderr, "%s: ", program_name);
162 va_start(args, format);
163 vfprintf(stderr, format, args);
166 fprintf(stderr, " (%s)", strerror(err_no));
172 /* Sets program_name based on 'argv0'. Should be called at the beginning of
173 * main(), as "set_program_name(argv[0]);". */
174 void set_program_name(const char *argv0)
176 const char *slash = strrchr(argv0, '/');
177 program_name = slash ? slash + 1 : argv0;
180 /* Print the version information for the program. */
182 ovs_print_version(char *date, char *time,
183 uint8_t min_ofp, uint8_t max_ofp)
185 printf("%s (Open vSwitch) "VERSION BUILDNR"\n", program_name);
186 printf("Compiled %s %s\n", date, time);
187 if (min_ofp || max_ofp) {
188 printf("OpenFlow versions %#x:%#x\n", min_ofp, max_ofp);
192 /* Writes the 'size' bytes in 'buf' to 'stream' as hex bytes arranged 16 per
193 * line. Numeric offsets are also included, starting at 'ofs' for the first
194 * byte in 'buf'. If 'ascii' is true then the corresponding ASCII characters
195 * are also rendered alongside. */
197 ovs_hex_dump(FILE *stream, const void *buf_, size_t size,
198 uintptr_t ofs, bool ascii)
200 const uint8_t *buf = buf_;
201 const size_t per_line = 16; /* Maximum bytes per line. */
205 size_t start, end, n;
208 /* Number of bytes on this line. */
209 start = ofs % per_line;
211 if (end - start > size)
216 fprintf(stream, "%08jx ", (uintmax_t) ROUND_DOWN(ofs, per_line));
217 for (i = 0; i < start; i++)
218 fprintf(stream, " ");
220 fprintf(stream, "%02hhx%c",
221 buf[i - start], i == per_line / 2 - 1? '-' : ' ');
224 for (; i < per_line; i++)
225 fprintf(stream, " ");
226 fprintf(stream, "|");
227 for (i = 0; i < start; i++)
228 fprintf(stream, " ");
229 for (; i < end; i++) {
230 int c = buf[i - start];
231 putc(c >= 32 && c < 127 ? c : '.', stream);
233 for (; i < per_line; i++)
234 fprintf(stream, " ");
235 fprintf(stream, "|");
237 fprintf(stream, "\n");
246 str_to_int(const char *s, int base, int *i)
249 bool ok = str_to_llong(s, base, &ll);
255 str_to_long(const char *s, int base, long *li)
258 bool ok = str_to_llong(s, base, &ll);
264 str_to_llong(const char *s, int base, long long *x)
266 int save_errno = errno;
269 *x = strtoll(s, &tail, base);
270 if (errno == EINVAL || errno == ERANGE || tail == s || *tail != '\0') {
281 str_to_uint(const char *s, int base, unsigned int *u)
283 return str_to_int(s, base, (int *) u);
287 str_to_ulong(const char *s, int base, unsigned long *ul)
289 return str_to_long(s, base, (long *) ul);
293 str_to_ullong(const char *s, int base, unsigned long long *ull)
295 return str_to_llong(s, base, (long long *) ull);